Merge lp:~salgado/linaro-image-tools/port-more-create_partitions into lp:linaro-image-tools/11.11

Proposed by Guilherme Salgado
Status: Merged
Merged at revision: 189
Proposed branch: lp:~salgado/linaro-image-tools/port-more-create_partitions
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~salgado/linaro-image-tools/port-create_partitions
Diff against target: 110 lines (+68/-12)
3 files modified
linaro-media-create (+8/-12)
media_create/partition_size.py (+42/-0)
media_create/tests/test_media_create.py (+18/-0)
To merge this branch: bzr merge lp:~salgado/linaro-image-tools/port-more-create_partitions
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+43127@code.launchpad.net

Description of the change

Port a few more bits of create_partitions to python.

The remaining bits depend on many other things being ported as well, as they set variables used elsewhere and register loopback devices (using yet another shell variable) to make sure they are cleaned up.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) wrote :

Hi,

eval isn't pretty, but it is simple, not a security problem here and won't
be around for long anyway.

Thanks,

James

review: Approve
Revision history for this message
Guilherme Salgado (salgado) wrote :

Yeah, eval is just a hack while we need to pass data back from python to the shell script. It will go away soon

Thanks for the review!

182. By Guilherme Salgado

merge parent branch

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'linaro-media-create'
--- linaro-media-create 2010-12-09 20:03:10 +0000
+++ linaro-media-create 2010-12-09 20:03:10 +0000
@@ -442,18 +442,14 @@
442 "${DEVICE-$IMAGE_FILE}" "$FAT_SIZE" "$HEADS" "$SECTORS" "$CYLINDER_ARG"442 "${DEVICE-$IMAGE_FILE}" "$FAT_SIZE" "$HEADS" "$SECTORS" "$CYLINDER_ARG"
443443
444 if [ "${IMAGE_FILE}" ]; then444 if [ "${IMAGE_FILE}" ]; then
445 VFATOFFSET=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep FAT | awk '{print $3}')*512))445 sizes_and_offsets=$(python -m media_create.partition_size "${IMAGE_FILE}")
446 VFATSIZE2=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep FAT | awk '{print $4}')))446 eval $sizes_and_offsets
447 VFATSIZE1=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep FAT | awk '{print $3}')))447 BOOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $VFATOFFSET \
448 VFATSIZE=$((((VFATSIZE2+1-VFATSIZE1)/2)*1024))448 --sizelimit $VFATSIZE)
449 ROOTOFFSET=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep Linux | awk '{print $2}')*512))449 register_loopback "$BOOTFS"
450 ROOTSIZE2=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep Linux | awk '{print $3}')))450 ROOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $ROOTOFFSET \
451 ROOTSIZE1=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep Linux | awk '{print $2}')))451 --sizelimit $ROOTSIZE)
452 ROOTSIZE=$((((ROOTSIZE2+1-ROOTSIZE1)/2)*1024))452 register_loopback "$ROOTFS"
453 BOOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $VFATOFFSET --sizelimit $VFATSIZE)
454 register_loopback "$BOOTFS"
455 ROOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $ROOTOFFSET --sizelimit $ROOTSIZE)
456 register_loopback "$ROOTFS"
457 fi453 fi
458}454}
459455
460456
=== added file 'media_create/partition_size.py'
--- media_create/partition_size.py 1970-01-01 00:00:00 +0000
+++ media_create/partition_size.py 2010-12-09 20:03:10 +0000
@@ -0,0 +1,42 @@
1import sys
2
3from parted import (
4 Device,
5 Disk,
6 )
7
8
9def calculate_partition_size_and_offset(device):
10 """Return the size and offset of the boot and root partitions.
11
12 Both the size and offset are in sectors.
13
14 :param device: A string containing the path to the device.
15 :return: A 4-tuple containing the offset and size of the boot partition
16 followed by the offset and size of the root partition.
17 """
18 disk = Disk(Device(device))
19 vfat_partition = None
20 for partition in disk.partitions:
21 if 'boot' in partition.getFlagsAsString():
22 geometry = partition.geometry
23 vfat_offset = geometry.start * 512
24 vfat_size = geometry.length * 512
25 vfat_partition = partition
26
27 assert vfat_partition is not None, (
28 "Couldn't find boot partition on %s" % device)
29 linux_partition = vfat_partition.nextPartition()
30 geometry = linux_partition.geometry
31 linux_offset = geometry.start * 512
32 linux_size = geometry.length * 512
33 return vfat_size, vfat_offset, linux_size, linux_offset
34
35
36if __name__ == "__main__":
37 vsize, voffset, rsize, roffset = calculate_partition_size_and_offset(
38 sys.argv[1])
39 # These values need to be assigned to shell variables in our script, so we
40 # make its job easier by printing something it can pass on to eval.
41 print "VFATSIZE=%s VFATOFFSET=%s ROOTSIZE=%s ROOTOFFSET=%s" % (
42 vsize, voffset, rsize, roffset)
043
=== modified file 'media_create/tests/test_media_create.py'
--- media_create/tests/test_media_create.py 2010-12-09 20:03:10 +0000
+++ media_create/tests/test_media_create.py 2010-12-09 20:03:10 +0000
@@ -17,6 +17,7 @@
17 create_partitions,17 create_partitions,
18 run_sfdisk_commands,18 run_sfdisk_commands,
19 )19 )
20from media_create.partition_size import calculate_partition_size_and_offset
20from media_create.populate_boot import (21from media_create.populate_boot import (
21 make_boot_script,22 make_boot_script,
22 make_uImage,23 make_uImage,
@@ -301,3 +302,20 @@
301 cmd_runner.SubcommandNonZeroReturnValue,302 cmd_runner.SubcommandNonZeroReturnValue,
302 run_sfdisk_commands,303 run_sfdisk_commands,
303 ',1,0xDA', 5, 63, '', tempfile, as_root=False)304 ',1,0xDA', 5, 63, '', tempfile, as_root=False)
305
306
307class TestCalculatePartitionSizeAndOffset(TestCaseWithFixtures):
308
309 def test_foo(self):
310 tempfile = self.createTempFileAsFixture()
311 cmd_runner.run(['qemu-img', 'create', '-f', 'raw', tempfile, '10M'],
312 stdout=subprocess.PIPE)
313 stdout, stderr = run_sfdisk_commands(
314 ',1,0x0C,*\n,,,-', 5, 63, '', tempfile, as_root=False)
315 self.assertIn('Successfully wrote the new partition table', stdout)
316
317 vfat_size, vfat_offset, linux_size, linux_offset = (
318 calculate_partition_size_and_offset(tempfile))
319 self.assertEqual(
320 [129024L, 32256L, 10321920L, 161280L],
321 [vfat_size, vfat_offset, linux_size, linux_offset])

Subscribers

People subscribed via source and target branches