feat(disk_util): Replace Resize() with Update()

The new Update() performs the same tasks as the old Resize()
in addition to formatting previously-unformatted partitions. This
allows children disk-layouts to repartition the base layout in
addition to resizing.
This commit is contained in:
Alex Crawford 2014-05-29 13:52:00 -07:00
parent 7372f9f6e6
commit ed50f5fe0b
2 changed files with 40 additions and 18 deletions

View File

@ -229,6 +229,16 @@ def GetPartitionTableFromImage(options, config, partitions):
else: else:
part['image_compat'] = False part['image_compat'] = False
for part in partitions.itervalues():
if not part.get('fs_type', None):
continue
with PartitionLoop(options, part) as loop_dev:
try:
part['image_fs_type'] = subprocess.check_output(
['sudo', 'blkid', '-o', 'value', '-s', 'TYPE', loop_dev]).strip()
except subprocess.CalledProcessError:
part['image_fs_type'] = None
# Set compat flags for any partition not in the image # Set compat flags for any partition not in the image
for part in partitions.itervalues(): for part in partitions.itervalues():
part.setdefault('image_exists', False) part.setdefault('image_exists', False)
@ -334,7 +344,7 @@ def BtrfsSubvolId(path):
def FormatBtrfs(part, device): def FormatBtrfs(part, device):
"""Format an ext2 or ext4 filesystem. """Format a btrfs filesystem.
Args: Args:
part: dict defining the partition part: dict defining the partition
@ -432,6 +442,21 @@ def PartitionLoop(options, partition):
Sudo(['losetup', '--detach', loop_dev]) Sudo(['losetup', '--detach', loop_dev])
def FormatPartition(options, part):
print "Formatting partition %s (%s) as %s" % (
part['num'], part['label'], part['fs_type'])
with PartitionLoop(options, part) as loop_dev:
if part['fs_type'] in ('ext2', 'ext4'):
FormatExt(part, loop_dev)
elif part['fs_type'] == 'btrfs':
FormatBtrfs(part, loop_dev)
elif part['fs_type'] == 'vfat':
FormatFat(part, loop_dev)
else:
raise Exception("Unhandled fs type %s" % part['fs_type'])
def Format(options): def Format(options):
"""Writes the given partition table and initialize fresh filesystems. """Writes the given partition table and initialize fresh filesystems.
@ -450,18 +475,7 @@ def Format(options):
if part['type'] == 'blank' or 'fs_type' not in part: if part['type'] == 'blank' or 'fs_type' not in part:
continue continue
print "Formatting partition %s (%s) as %s" % ( FormatPartition(options, part)
part['num'], part['label'], part['fs_type'])
with PartitionLoop(options, part) as loop_dev:
if part['fs_type'] in ('ext2', 'ext4'):
FormatExt(part, loop_dev)
elif part['fs_type'] == 'btrfs':
FormatBtrfs(part, loop_dev)
elif part['fs_type'] == 'vfat':
FormatFat(part, loop_dev)
else:
raise Exception("Unhandled fs type %s" % part['fs_type'])
def ResizeExt(part, device): def ResizeExt(part, device):
@ -491,8 +505,9 @@ def ResizeBtrfs(part, device):
os.rmdir(btrfs_mount) os.rmdir(btrfs_mount)
def Resize(options): def Update(options):
"""Writes the given partition table and resize ext[234] filesystems. """Writes the given partition table, resize filesystems, and
format free partitions.
Args: Args:
options: Flags passed to the script options: Flags passed to the script
@ -501,6 +516,12 @@ def Resize(options):
config, partitions = LoadPartitionConfig(options) config, partitions = LoadPartitionConfig(options)
WritePartitionTable(options, config, partitions) WritePartitionTable(options, config, partitions)
for part in partitions.itervalues():
if not part.get('fs_type', None):
continue
elif not part['image_fs_type']:
FormatPartition(options, part)
for part in partitions.itervalues(): for part in partitions.itervalues():
resize_func = None resize_func = None
if not part.get('fs_type', None): if not part.get('fs_type', None):
@ -915,11 +936,12 @@ def main(argv):
a.add_argument('disk_image', help='path to disk image file') a.add_argument('disk_image', help='path to disk image file')
a.set_defaults(func=Format, create=True) a.set_defaults(func=Format, create=True)
a = actions.add_parser('resize', help='write gpt and resize filesystems') a = actions.add_parser('update',
help='write gpt, resize filesystems, and format free partitions')
a.add_argument('--mbr_boot_code', default=DEFAULT_MBR_BOOT_CODE, a.add_argument('--mbr_boot_code', default=DEFAULT_MBR_BOOT_CODE,
help='path to mbr boot block, such as syslinux/gptmbr.bin') help='path to mbr boot block, such as syslinux/gptmbr.bin')
a.add_argument('disk_image', help='path to disk image file') a.add_argument('disk_image', help='path to disk image file')
a.set_defaults(func=Resize, create=False) a.set_defaults(func=Update, create=False)
a = actions.add_parser('mount', help='mount filesystems in image') a = actions.add_parser('mount', help='mount filesystems in image')
a.add_argument('--read_only', '-r', action='store_true', a.add_argument('--read_only', '-r', action='store_true',

View File

@ -249,7 +249,7 @@ setup_disk_image() {
if [[ $(_get_vm_opt PARTITIONED_IMG) -eq 1 ]]; then if [[ $(_get_vm_opt PARTITIONED_IMG) -eq 1 ]]; then
"${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \ "${BUILD_LIBRARY_DIR}/disk_util" --disk_layout="${disk_layout}" \
resize "${VM_TMP_IMG}" update "${VM_TMP_IMG}"
fi fi
info "Mounting image to $(relpath "${VM_TMP_ROOT}")" info "Mounting image to $(relpath "${VM_TMP_ROOT}")"