From 18203bf101dc04c28b53a92cd95c8be88959c428 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Tue, 22 Nov 2016 09:58:55 -0500 Subject: disk_setup: Use sectors as unit when formatting MBR disks with sfdisk. The version of sfdisk in wily (and onwards) only accepts sectors as a valid disk size. As such, this refactors the MBR code path in cc_disk_setup to use sectors. - use --unit=S: while newer versions of sfdisk assume --unit=S, older versions do not so we specifically pass it in. Versions of sfdisk found in supported OSes such as centos6 wont assume --unit=S. - add --force: this exists back to centos 6 (2.17.2), so it should be fine, and is what we ultimately want. "do what I say, even if it is stupid" - keep --Linux. Even though this has been deprecated for quite some time, we keep it until versions that want it are unsupported. If necessary at some point we could check for util linux version and if it had --Linux and use it in those cases. Additionally, improve usefulness of some log messages. LP: #1460715 --- cloudinit/config/cc_disk_setup.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/config/cc_disk_setup.py b/cloudinit/config/cc_disk_setup.py index 0c4b794d..15cd110e 100644 --- a/cloudinit/config/cc_disk_setup.py +++ b/cloudinit/config/cc_disk_setup.py @@ -436,14 +436,13 @@ def get_dyn_func(*args): def get_mbr_hdd_size(device): - size_cmd = [SFDISK_CMD, '--show-size', device] - size = None try: - size, _err = util.subp(size_cmd) + size_in_bytes, _ = util.subp([BLKDEV_CMD, '--getsize64', device]) + sector_size, _ = util.subp([BLKDEV_CMD, '--getss', device]) except Exception as e: raise Exception("Failed to get %s size\n%s" % (device, e)) - return int(size.strip()) + return int(size_in_bytes) / int(sector_size) def get_gpt_hdd_size(device): @@ -588,7 +587,7 @@ def get_partition_mbr_layout(size, layout): raise Exception("Partition was incorrectly defined: %s" % part) percent, part_type = part - part_size = int((float(size) * (float(percent) / 100)) / 1024) + part_size = int(float(size) * (float(percent) / 100)) if part_num == last_part_num: part_definition.append(",,%s" % part_type) @@ -692,7 +691,7 @@ def exec_mkpart_mbr(device, layout): types, i.e. gpt """ # Create the partitions - prt_cmd = [SFDISK_CMD, "--Linux", "-uM", device] + prt_cmd = [SFDISK_CMD, "--Linux", "--unit=S", "--force", device] try: util.subp(prt_cmd, data="%s\n" % layout) except Exception as e: @@ -909,7 +908,8 @@ def mkfs(fs_cfg): LOG.debug("Error in device identification handling.") return - LOG.debug("File system %s will be created on %s", label, device) + LOG.debug("File system type '%s' with label '%s' will be created on %s", + fs_type, label, device) # Make sure the device is defined if not device: -- cgit v1.2.3