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
1=== modified file 'linaro-media-create'
2--- linaro-media-create 2010-12-09 20:03:10 +0000
3+++ linaro-media-create 2010-12-09 20:03:10 +0000
4@@ -442,18 +442,14 @@
5 "${DEVICE-$IMAGE_FILE}" "$FAT_SIZE" "$HEADS" "$SECTORS" "$CYLINDER_ARG"
6
7 if [ "${IMAGE_FILE}" ]; then
8- VFATOFFSET=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep FAT | awk '{print $3}')*512))
9- VFATSIZE2=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep FAT | awk '{print $4}')))
10- VFATSIZE1=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep FAT | awk '{print $3}')))
11- VFATSIZE=$((((VFATSIZE2+1-VFATSIZE1)/2)*1024))
12- ROOTOFFSET=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep Linux | awk '{print $2}')*512))
13- ROOTSIZE2=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep Linux | awk '{print $3}')))
14- ROOTSIZE1=$(($(LC_ALL=C fdisk -l -u "$IMAGE_FILE" | grep Linux | awk '{print $2}')))
15- ROOTSIZE=$((((ROOTSIZE2+1-ROOTSIZE1)/2)*1024))
16- BOOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $VFATOFFSET --sizelimit $VFATSIZE)
17- register_loopback "$BOOTFS"
18- ROOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $ROOTOFFSET --sizelimit $ROOTSIZE)
19- register_loopback "$ROOTFS"
20+ sizes_and_offsets=$(python -m media_create.partition_size "${IMAGE_FILE}")
21+ eval $sizes_and_offsets
22+ BOOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $VFATOFFSET \
23+ --sizelimit $VFATSIZE)
24+ register_loopback "$BOOTFS"
25+ ROOTFS=$(sudo losetup -f --show "$IMAGE_FILE" --offset $ROOTOFFSET \
26+ --sizelimit $ROOTSIZE)
27+ register_loopback "$ROOTFS"
28 fi
29 }
30
31
32=== added file 'media_create/partition_size.py'
33--- media_create/partition_size.py 1970-01-01 00:00:00 +0000
34+++ media_create/partition_size.py 2010-12-09 20:03:10 +0000
35@@ -0,0 +1,42 @@
36+import sys
37+
38+from parted import (
39+ Device,
40+ Disk,
41+ )
42+
43+
44+def calculate_partition_size_and_offset(device):
45+ """Return the size and offset of the boot and root partitions.
46+
47+ Both the size and offset are in sectors.
48+
49+ :param device: A string containing the path to the device.
50+ :return: A 4-tuple containing the offset and size of the boot partition
51+ followed by the offset and size of the root partition.
52+ """
53+ disk = Disk(Device(device))
54+ vfat_partition = None
55+ for partition in disk.partitions:
56+ if 'boot' in partition.getFlagsAsString():
57+ geometry = partition.geometry
58+ vfat_offset = geometry.start * 512
59+ vfat_size = geometry.length * 512
60+ vfat_partition = partition
61+
62+ assert vfat_partition is not None, (
63+ "Couldn't find boot partition on %s" % device)
64+ linux_partition = vfat_partition.nextPartition()
65+ geometry = linux_partition.geometry
66+ linux_offset = geometry.start * 512
67+ linux_size = geometry.length * 512
68+ return vfat_size, vfat_offset, linux_size, linux_offset
69+
70+
71+if __name__ == "__main__":
72+ vsize, voffset, rsize, roffset = calculate_partition_size_and_offset(
73+ sys.argv[1])
74+ # These values need to be assigned to shell variables in our script, so we
75+ # make its job easier by printing something it can pass on to eval.
76+ print "VFATSIZE=%s VFATOFFSET=%s ROOTSIZE=%s ROOTOFFSET=%s" % (
77+ vsize, voffset, rsize, roffset)
78
79=== modified file 'media_create/tests/test_media_create.py'
80--- media_create/tests/test_media_create.py 2010-12-09 20:03:10 +0000
81+++ media_create/tests/test_media_create.py 2010-12-09 20:03:10 +0000
82@@ -17,6 +17,7 @@
83 create_partitions,
84 run_sfdisk_commands,
85 )
86+from media_create.partition_size import calculate_partition_size_and_offset
87 from media_create.populate_boot import (
88 make_boot_script,
89 make_uImage,
90@@ -301,3 +302,20 @@
91 cmd_runner.SubcommandNonZeroReturnValue,
92 run_sfdisk_commands,
93 ',1,0xDA', 5, 63, '', tempfile, as_root=False)
94+
95+
96+class TestCalculatePartitionSizeAndOffset(TestCaseWithFixtures):
97+
98+ def test_foo(self):
99+ tempfile = self.createTempFileAsFixture()
100+ cmd_runner.run(['qemu-img', 'create', '-f', 'raw', tempfile, '10M'],
101+ stdout=subprocess.PIPE)
102+ stdout, stderr = run_sfdisk_commands(
103+ ',1,0x0C,*\n,,,-', 5, 63, '', tempfile, as_root=False)
104+ self.assertIn('Successfully wrote the new partition table', stdout)
105+
106+ vfat_size, vfat_offset, linux_size, linux_offset = (
107+ calculate_partition_size_and_offset(tempfile))
108+ self.assertEqual(
109+ [129024L, 32256L, 10321920L, 161280L],
110+ [vfat_size, vfat_offset, linux_size, linux_offset])

Subscribers

People subscribed via source and target branches