2016-07-16 20:16:13 +00:00
|
|
|
# Copyright 2016 Andreas Florath (andreas@florath.net)
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2017-05-02 03:43:43 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2017-05-18 22:42:00 +00:00
|
|
|
from diskimage_builder.block_device.exception import \
|
2017-05-02 05:32:47 +00:00
|
|
|
BlockDeviceSetupException
|
2016-07-16 20:16:13 +00:00
|
|
|
from diskimage_builder.block_device.level1.mbr import MBR
|
2017-05-25 01:28:13 +00:00
|
|
|
from diskimage_builder.block_device.level1.partition import PartitionNode
|
|
|
|
from diskimage_builder.block_device.plugin import PluginBase
|
2017-05-05 09:57:27 +00:00
|
|
|
from diskimage_builder.block_device.utils import exec_sudo
|
2016-07-16 20:16:13 +00:00
|
|
|
from diskimage_builder.block_device.utils import parse_abs_size_spec
|
|
|
|
from diskimage_builder.block_device.utils import parse_rel_size_spec
|
2017-05-02 03:43:43 +00:00
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-05-25 01:28:13 +00:00
|
|
|
class Partitioning(PluginBase):
|
2016-07-16 20:16:13 +00:00
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
def __init__(self, config, default_config, state):
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Creating Partitioning object; config [%s]", config)
|
2017-05-25 01:28:13 +00:00
|
|
|
super(Partitioning, self).__init__()
|
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
# Unlike other PluginBase we are somewhat persistent, as the
|
|
|
|
# partition nodes call back to us (see create() below). We
|
|
|
|
# need to keep this reference.
|
|
|
|
self.state = state
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
# Because using multiple partitions of one base is done
|
|
|
|
# within one object, there is the need to store a flag if the
|
|
|
|
# creation of the partitions was already done.
|
2018-06-20 14:25:59 +00:00
|
|
|
self.number_of_partitions = 0
|
2016-07-16 20:16:13 +00:00
|
|
|
|
|
|
|
# Parameter check
|
|
|
|
if 'base' not in config:
|
2017-05-09 23:59:26 +00:00
|
|
|
raise BlockDeviceSetupException("Partitioning config needs 'base'")
|
2016-07-16 20:16:13 +00:00
|
|
|
self.base = config['base']
|
|
|
|
|
2017-05-09 23:59:26 +00:00
|
|
|
if 'partitions' not in config:
|
|
|
|
raise BlockDeviceSetupException(
|
|
|
|
"Partitioning config needs 'partitions'")
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
if 'label' not in config:
|
2017-05-09 23:59:26 +00:00
|
|
|
raise BlockDeviceSetupException(
|
|
|
|
"Partitioning config needs 'label'")
|
2016-07-16 20:16:13 +00:00
|
|
|
self.label = config['label']
|
2018-01-15 03:44:10 +00:00
|
|
|
if self.label not in ("mbr", "gpt"):
|
|
|
|
raise BlockDeviceSetupException("Label must be 'mbr' or 'gpt'")
|
2016-07-16 20:16:13 +00:00
|
|
|
|
|
|
|
# It is VERY important to get the alignment correct. If this
|
|
|
|
# is not correct, the disk performance might be very poor.
|
|
|
|
# Example: In some tests a 'off by one' leads to a write
|
|
|
|
# performance of 30% compared to a correctly aligned
|
|
|
|
# partition.
|
|
|
|
# The problem for DIB is, that it cannot assume that the host
|
|
|
|
# system uses the same IO sizes as the target system,
|
|
|
|
# therefore here a fixed approach (as used in all modern
|
|
|
|
# systems with large disks) is used. The partitions are
|
|
|
|
# aligned to 1MiB (which are about 2048 times 512 bytes
|
|
|
|
# blocks)
|
|
|
|
self.align = 1024 * 1024 # 1MiB as default
|
|
|
|
if 'align' in config:
|
|
|
|
self.align = parse_abs_size_spec(config['align'])
|
|
|
|
|
2017-05-01 08:52:52 +00:00
|
|
|
self.partitions = []
|
|
|
|
prev_partition = None
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
for part_cfg in config['partitions']:
|
2017-06-01 04:31:49 +00:00
|
|
|
np = PartitionNode(part_cfg, state, self, prev_partition)
|
2017-05-01 08:52:52 +00:00
|
|
|
self.partitions.append(np)
|
|
|
|
prev_partition = np
|
2016-07-16 20:16:13 +00:00
|
|
|
|
2017-05-25 01:28:13 +00:00
|
|
|
def get_nodes(self):
|
|
|
|
# return the list of partitions
|
|
|
|
return self.partitions
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
def _size_of_block_dev(self, dev):
|
|
|
|
with open(dev, "r") as fd:
|
|
|
|
fd.seek(0, 2)
|
|
|
|
return fd.tell()
|
|
|
|
|
2018-01-15 03:44:10 +00:00
|
|
|
def _create_mbr(self):
|
|
|
|
"""Create partitions with MBR"""
|
|
|
|
with MBR(self.image_path, self.disk_size, self.align) as part_impl:
|
2017-05-01 08:52:52 +00:00
|
|
|
for part_cfg in self.partitions:
|
|
|
|
part_name = part_cfg.get_name()
|
2017-05-25 01:28:13 +00:00
|
|
|
part_bootflag = PartitionNode.flag_boot \
|
2016-07-16 20:16:13 +00:00
|
|
|
in part_cfg.get_flags()
|
2017-05-25 01:28:13 +00:00
|
|
|
part_primary = PartitionNode.flag_primary \
|
2016-07-16 20:16:13 +00:00
|
|
|
in part_cfg.get_flags()
|
|
|
|
part_size = part_cfg.get_size()
|
|
|
|
part_free = part_impl.free()
|
|
|
|
part_type = part_cfg.get_type()
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Not partitioned space [%d]", part_free)
|
2016-07-16 20:16:13 +00:00
|
|
|
part_size = parse_rel_size_spec(part_size,
|
|
|
|
part_free)[1]
|
|
|
|
part_no \
|
|
|
|
= part_impl.add_partition(part_primary, part_bootflag,
|
|
|
|
part_size, part_type)
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("Create partition [%s] [%d]",
|
|
|
|
part_name, part_no)
|
2017-06-06 02:09:24 +00:00
|
|
|
|
|
|
|
# We're going to mount all partitions with kpartx
|
|
|
|
# below once we're done. So the device this partition
|
|
|
|
# will be seen at becomes "/dev/mapper/loop0pX"
|
2018-01-15 03:44:10 +00:00
|
|
|
assert self.device_path[:5] == "/dev/"
|
2017-06-06 02:09:24 +00:00
|
|
|
partition_device_name = "/dev/mapper/%sp%d" % \
|
2018-01-15 03:44:10 +00:00
|
|
|
(self.device_path[5:], part_no)
|
2017-06-01 04:31:49 +00:00
|
|
|
self.state['blockdev'][part_name] \
|
2017-05-01 07:19:49 +00:00
|
|
|
= {'device': partition_device_name}
|
2016-07-16 20:16:13 +00:00
|
|
|
|
2018-01-15 03:44:10 +00:00
|
|
|
def _create_gpt(self):
|
|
|
|
"""Create partitions with GPT"""
|
|
|
|
|
|
|
|
cmd = ['sgdisk', self.image_path]
|
|
|
|
|
|
|
|
# This padding gives us a little room for rounding so we don't
|
|
|
|
# go over the end of the disk
|
|
|
|
disk_free = self.disk_size - (2048 * 1024)
|
|
|
|
pnum = 1
|
|
|
|
|
|
|
|
for p in self.partitions:
|
|
|
|
args = {}
|
|
|
|
args['pnum'] = pnum
|
2018-06-27 03:51:04 +00:00
|
|
|
args['name'] = '%s' % p.get_name()
|
2018-01-15 03:44:10 +00:00
|
|
|
args['type'] = '%s' % p.get_type()
|
|
|
|
|
|
|
|
# convert from a relative/string size to bytes
|
|
|
|
size = parse_rel_size_spec(p.get_size(), disk_free)[1]
|
|
|
|
|
|
|
|
# We keep track in bytes, but specify things to sgdisk in
|
|
|
|
# megabytes so it can align on sensible boundaries. And
|
|
|
|
# create partitions right after previous so no need to
|
|
|
|
# calculate start/end - just size.
|
|
|
|
assert size <= disk_free
|
|
|
|
args['size'] = size // (1024 * 1024)
|
|
|
|
|
2018-06-27 03:51:04 +00:00
|
|
|
new_cmd = ("-n", "{pnum}:0:+{size}M".format(**args),
|
|
|
|
"-t", "{pnum}:{type}".format(**args),
|
|
|
|
# Careful with this one, as {name} could have spaces
|
|
|
|
"-c", "{pnum}:{name}".format(**args))
|
|
|
|
cmd.extend(new_cmd)
|
2018-01-15 03:44:10 +00:00
|
|
|
|
|
|
|
# Fill the state; we mount all partitions with kpartx
|
|
|
|
# below once we're done. So the device this partition
|
|
|
|
# will be seen at becomes "/dev/mapper/loop0pX"
|
|
|
|
assert self.device_path[:5] == "/dev/"
|
|
|
|
device_name = "/dev/mapper/%sp%d" % (self.device_path[5:], pnum)
|
|
|
|
self.state['blockdev'][p.get_name()] \
|
|
|
|
= {'device': device_name}
|
|
|
|
|
|
|
|
disk_free = disk_free - size
|
|
|
|
pnum = pnum + 1
|
|
|
|
logger.debug("Partition %s added, %s remaining in disk",
|
|
|
|
pnum, disk_free)
|
|
|
|
|
|
|
|
logger.debug("cmd: %s", ' '.join(cmd))
|
|
|
|
exec_sudo(cmd)
|
|
|
|
|
|
|
|
# not this is NOT a node and this is not called directly! The
|
|
|
|
# create() calls in the partition nodes this plugin has
|
|
|
|
# created are calling back into this.
|
|
|
|
def create(self):
|
|
|
|
# This is a bit of a hack. Each of the partitions is actually
|
|
|
|
# in the graph, so for every partition we get a create() call
|
|
|
|
# as the walk happens. But we only need to create the
|
|
|
|
# partition table once...
|
2018-06-20 14:25:59 +00:00
|
|
|
self.number_of_partitions += 1
|
|
|
|
if self.number_of_partitions > 1:
|
2018-01-15 03:44:10 +00:00
|
|
|
logger.info("Not creating the partitions a second time.")
|
|
|
|
return
|
|
|
|
|
|
|
|
# the raw file on disk
|
|
|
|
self.image_path = self.state['blockdev'][self.base]['image']
|
|
|
|
# the /dev/loopX device of the parent
|
|
|
|
self.device_path = self.state['blockdev'][self.base]['device']
|
|
|
|
# underlying size
|
|
|
|
self.disk_size = self._size_of_block_dev(self.image_path)
|
|
|
|
|
|
|
|
logger.info("Creating partition on [%s] [%s]",
|
|
|
|
self.base, self.image_path)
|
|
|
|
|
|
|
|
assert self.label in ('mbr', 'gpt')
|
|
|
|
|
|
|
|
if self.label == 'mbr':
|
|
|
|
self._create_mbr()
|
|
|
|
elif self.label == 'gpt':
|
|
|
|
self._create_gpt()
|
|
|
|
|
2017-06-19 00:29:53 +00:00
|
|
|
# "saftey sync" to make sure the partitions are written
|
|
|
|
exec_sudo(["sync"])
|
|
|
|
|
2017-06-06 02:09:24 +00:00
|
|
|
# now all the partitions are created, get device-mapper to
|
|
|
|
# mount them
|
|
|
|
if not os.path.exists("/.dockerenv"):
|
Use kpartx option to update partition mappings
Fix cases of 'mkfs' failing because the partitions never showed up. Partition
mappings will now be updated instead of just adding them with 'kpartx'. That
means that 'kpartx' will also remove devmappings for deleted partitions.
Traceback of failing mkfs call:
2020-05-11 22:03:25.523 | INFO diskimage_builder.block_device.utils [-] Calling [sudo sync]
2020-05-11 22:03:25.539 | INFO diskimage_builder.block_device.utils [-] Calling [sudo kpartx -avs /dev/loop0]
2020-05-11 22:03:25.581 | INFO diskimage_builder.block_device.utils [-] Calling [sudo mkfs -t ext4 -i 4096 -J size=64 -L cloudimg-rootfs -U 21c6f9eb-4d52-4e5c-b9b7-796735de8909 -q /dev/mapper/loop0p1]
2020-05-11 22:03:25.700 | ERROR diskimage_builder.block_device.blockdevice [-] Create failed; rollback initiated
2020-05-11 22:03:25.700 | Traceback (most recent call last):
2020-05-11 22:03:25.700 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/blockdevice.py", line 406, in cmd_create
2020-05-11 22:03:25.700 | node.create()
2020-05-11 22:03:25.700 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/level2/mkfs.py", line 133, in create
2020-05-11 22:03:25.700 | exec_sudo(cmd)
2020-05-11 22:03:25.700 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/utils.py", line 143, in exec_sudo
2020-05-11 22:03:25.700 | raise e
2020-05-11 22:03:25.700 | diskimage_builder.block_device.exception.BlockDeviceSetupException: exec_sudo failed
2020-05-11 22:03:25.700 | INFO diskimage_builder.block_device.level0.localloop [-] loopdev detach
2020-05-11 22:03:25.701 | INFO diskimage_builder.block_device.utils [-] Calling [sudo losetup -d /dev/loop0]
2020-05-11 22:03:25.732 | INFO diskimage_builder.block_device.level0.localloop [-] Remove image file [/tmp/dib_image.muyw7t1h/image0.raw]
2020-05-11 22:03:25.734 | ERROR diskimage_builder.block_device.blockdevice [-] Rollback complete, exiting
2020-05-11 22:03:25.740 | Traceback (most recent call last):
2020-05-11 22:03:25.740 | File "/home/zuul/dib/bin/dib-block-device", line 8, in <module>
2020-05-11 22:03:25.740 | sys.exit(main())
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/cmd.py", line 120, in main
2020-05-11 22:03:25.740 | return bdc.main()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/cmd.py", line 115, in main
2020-05-11 22:03:25.740 | self.args.func()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/cmd.py", line 36, in cmd_create
2020-05-11 22:03:25.740 | self.bd.cmd_create()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/blockdevice.py", line 406, in cmd_create
2020-05-11 22:03:25.740 | node.create()
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/level2/mkfs.py", line 133, in create
2020-05-11 22:03:25.740 | exec_sudo(cmd)
2020-05-11 22:03:25.740 | File "/home/zuul/dib/lib/python3.6/site-packages/diskimage_builder/block_device/utils.py", line 143, in exec_sudo
2020-05-11 22:03:25.740 | raise e
2020-05-11 22:03:25.740 | diskimage_builder.block_device.exception.BlockDeviceSetupException: exec_sudo failed
Change-Id: I374f7f22f9e93ef35eb5813712ca59e75f0733e8
Related-Bug: #1698337
2020-05-13 05:56:16 +00:00
|
|
|
exec_sudo(["kpartx", "-uvs", self.device_path])
|
2017-06-06 02:09:24 +00:00
|
|
|
else:
|
|
|
|
# If running inside Docker, make our nodes manually,
|
|
|
|
# because udev will not be working. kpartx cannot run in
|
|
|
|
# sync mode in docker.
|
2018-01-15 03:44:10 +00:00
|
|
|
exec_sudo(["kpartx", "-av", self.device_path])
|
2017-06-06 02:09:24 +00:00
|
|
|
exec_sudo(["dmsetup", "--noudevsync", "mknodes"])
|
|
|
|
|
2016-07-16 20:16:13 +00:00
|
|
|
return
|
2017-06-06 02:09:24 +00:00
|
|
|
|
2018-06-28 05:06:58 +00:00
|
|
|
def umount(self):
|
2018-06-20 14:25:59 +00:00
|
|
|
# Remove the partition mappings made for the parent
|
|
|
|
# block-device by create() above. This is called from the
|
|
|
|
# child PartitionNode umount. Thus every
|
|
|
|
# partition calls it, but we only want to do it once when
|
|
|
|
# we know this is the very last partition
|
|
|
|
self.number_of_partitions -= 1
|
|
|
|
if self.number_of_partitions == 0:
|
2017-06-06 02:09:24 +00:00
|
|
|
exec_sudo(["kpartx", "-d",
|
|
|
|
self.state['blockdev'][self.base]['device']])
|
2018-06-28 05:06:58 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
pass
|