Merge lp:~salgado/linaro-image-tools/bug-673412 into lp:linaro-image-tools/11.11

Proposed by Guilherme Salgado
Status: Merged
Merged at revision: 231
Proposed branch: lp:~salgado/linaro-image-tools/bug-673412
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~salgado/linaro-image-tools/port-install_hwpacks
Diff against target: 161 lines (+62/-13)
2 files modified
media_create/partitions.py (+34/-7)
media_create/tests/test_media_create.py (+28/-6)
To merge this branch: bzr merge lp:~salgado/linaro-image-tools/bug-673412
Reviewer Review Type Date Requested Status
James Westby (community) Needs Information
Review via email: mp+45266@code.launchpad.net

Description of the change

Fix bug 673412 by ensuring boot/root filesystems are umounted before we attempt to mkfs them.

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

29 + if filesystem in open('/proc/mounts').read():

Is that a bit broad? It would cause an error if the filesystem showed
up in there in a strange place wouldn't it?

Thanks,

James

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

On Wed, 2011-01-05 at 17:32 +0000, James Westby wrote:
> Review: Needs Information
> 29 + if filesystem in open('/proc/mounts').read():
>
> Is that a bit broad? It would cause an error if the filesystem showed
> up in there in a strange place wouldn't it?

I find it highly unlikely that a /dev/{sdXX,mmcblkNpN,loopN} string
(which is usually what the function will be given) ends up there unless
the filesystem is mounted, but I can use UDisks to do a more specific
check.

232. By Guilherme Salgado

Use the UDisks dbus interface to check whether or not a given partition is mounted

Revision history for this message
James Westby (james-w) wrote :

On Wed, 05 Jan 2011 18:22:51 -0000, Guilherme Salgado <email address hidden> wrote:
> On Wed, 2011-01-05 at 17:32 +0000, James Westby wrote:
> > Review: Needs Information
> > 29 + if filesystem in open('/proc/mounts').read():
> >
> > Is that a bit broad? It would cause an error if the filesystem showed
> > up in there in a strange place wouldn't it?
>
> I find it highly unlikely that a /dev/{sdXX,mmcblkNpN,loopN} string
> (which is usually what the function will be given) ends up there unless
> the filesystem is mounted, but I can use UDisks to do a more specific
> check.

Yeah, it's rare. I'm happy with this going in as is and we can see if we
get any fallout.

Thanks,

James

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

http://bazaar.launchpad.net/~salgado/linaro-image-tools/bug-673412/revision/232 has the changes to use the UDisks dbus interface instead of /proc/mounts

Revision history for this message
James Westby (james-w) wrote :

On Wed, 05 Jan 2011 19:37:52 -0000, Guilherme Salgado <email address hidden> wrote:
> http://bazaar.launchpad.net/~salgado/linaro-image-tools/bug-673412/revision/232
> has the changes to use the UDisks dbus interface instead of
> /proc/mounts

Oh, great. Looks good, thanks.

James

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'media_create/partitions.py'
--- media_create/partitions.py 2011-01-04 14:27:26 +0000
+++ media_create/partitions.py 2011-01-05 19:35:20 +0000
@@ -15,6 +15,8 @@
15SECTORS = 6315SECTORS = 63
16SECTOR_SIZE = 512 # bytes16SECTOR_SIZE = 512 # bytes
17CYLINDER_SIZE = HEADS * SECTORS * SECTOR_SIZE17CYLINDER_SIZE = HEADS * SECTORS * SECTOR_SIZE
18DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
19UDISKS = "org.freedesktop.UDisks"
1820
1921
20# I wonder if it'd make sense to convert this into a small shim which calls22# I wonder if it'd make sense to convert this into a small shim which calls
@@ -56,6 +58,10 @@
5658
57 if should_format_bootfs:59 if should_format_bootfs:
58 print "\nFormating boot partition\n"60 print "\nFormating boot partition\n"
61 # It looks like KDE somehow automounts the partitions after you
62 # repartition a disk so we need to unmount them here to create the
63 # filesystem.
64 ensure_partition_is_not_mounted(bootfs)
59 proc = cmd_runner.run(65 proc = cmd_runner.run(
60 ['mkfs.vfat', '-F', str(fat_size), bootfs, '-n', bootfs_label],66 ['mkfs.vfat', '-F', str(fat_size), bootfs, '-n', bootfs_label],
61 as_root=True)67 as_root=True)
@@ -64,6 +70,7 @@
64 if should_format_rootfs:70 if should_format_rootfs:
65 print "\nFormating root partition\n"71 print "\nFormating root partition\n"
66 mkfs = 'mkfs.%s' % rootfs_type72 mkfs = 'mkfs.%s' % rootfs_type
73 ensure_partition_is_not_mounted(rootfs)
67 proc = cmd_runner.run(74 proc = cmd_runner.run(
68 [mkfs, '-U', rootfs_uuid, rootfs, '-L', rootfs_label],75 [mkfs, '-U', rootfs_uuid, rootfs, '-L', rootfs_label],
69 as_root=True)76 as_root=True)
@@ -72,6 +79,23 @@
72 return bootfs, rootfs79 return bootfs, rootfs
7380
7481
82def ensure_partition_is_not_mounted(partition):
83 """Ensure the given partition is not mounted, umounting if necessary."""
84 if is_partition_mounted(partition):
85 cmd_runner.run(['umount', partition], as_root=True).wait()
86
87
88def is_partition_mounted(partition):
89 """Is the given partition mounted?"""
90 device_path = _get_udisks_device_path(partition)
91 device = dbus.SystemBus().get_object(UDISKS, device_path)
92 is_partition = device.Get(
93 device_path, 'DeviceIsPartition', dbus_interface=DBUS_PROPERTIES)
94 assert is_partition, "%s is not a partition" % partition
95 return device.Get(
96 device_path, 'DeviceIsMounted', dbus_interface=DBUS_PROPERTIES)
97
98
75def get_boot_and_root_loopback_devices(image_file):99def get_boot_and_root_loopback_devices(image_file):
76 """Return the boot and root loopback devices for the given image file.100 """Return the boot and root loopback devices for the given image file.
77101
@@ -156,15 +180,18 @@
156 """Return the number of partitions on the given media."""180 """Return the number of partitions on the given media."""
157 # We could do the same easily using python-parted but it requires root181 # We could do the same easily using python-parted but it requires root
158 # rights to read block devices, so we use UDisks here.182 # rights to read block devices, so we use UDisks here.
183 device_path = _get_udisks_device_path(media.path)
184 device = dbus.SystemBus().get_object(UDISKS, device_path)
185 return device.Get(
186 device_path, 'PartitionTableCount', dbus_interface=DBUS_PROPERTIES)
187
188
189def _get_udisks_device_path(device):
190 """Return the UDisks path for the given device."""
159 bus = dbus.SystemBus()191 bus = dbus.SystemBus()
160 udisks = dbus.Interface(192 udisks = dbus.Interface(
161 bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks"),193 bus.get_object(UDISKS, "/org/freedesktop/UDisks"), UDISKS)
162 'org.freedesktop.UDisks')194 return udisks.get_dbus_method('FindDeviceByDeviceFile')(device)
163 device_path = udisks.get_dbus_method('FindDeviceByDeviceFile')(media.path)
164 device = bus.get_object("org.freedesktop.UDisks", device_path)
165 return device.Get(
166 device_path, 'partition-table-count',
167 dbus_interface='org.freedesktop.DBus.Properties')
168195
169196
170def convert_size_to_bytes(size):197def convert_size_to_bytes(size):
171198
=== modified file 'media_create/tests/test_media_create.py'
--- media_create/tests/test_media_create.py 2011-01-05 14:36:26 +0000
+++ media_create/tests/test_media_create.py 2011-01-05 19:35:20 +0000
@@ -29,6 +29,7 @@
29 calculate_partition_size_and_offset,29 calculate_partition_size_and_offset,
30 convert_size_to_bytes,30 convert_size_to_bytes,
31 create_partitions,31 create_partitions,
32 ensure_partition_is_not_mounted,
32 get_boot_and_root_loopback_devices,33 get_boot_and_root_loopback_devices,
33 get_boot_and_root_partitions_for_media,34 get_boot_and_root_partitions_for_media,
34 Media,35 Media,
@@ -423,6 +424,21 @@
423 self.assertIn('Successfully wrote the new partition table', stdout)424 self.assertIn('Successfully wrote the new partition table', stdout)
424 return tempfile425 return tempfile
425426
427 def test_ensure_partition_is_not_mounted_for_mounted_partition(self):
428 self.useFixture(MockSomethingFixture(
429 partitions, 'is_partition_mounted', lambda part: True))
430 popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())
431 ensure_partition_is_not_mounted('/dev/whatever')
432 self.assertEqual(
433 [['sudo', 'umount', '/dev/whatever']], popen_fixture.mock.calls)
434
435 def test_ensure_partition_is_not_mounted_for_umounted_partition(self):
436 self.useFixture(MockSomethingFixture(
437 partitions, 'is_partition_mounted', lambda part: False))
438 popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())
439 ensure_partition_is_not_mounted('/dev/whatever')
440 self.assertEqual(None, popen_fixture.mock.calls)
441
426 def test_get_boot_and_root_loopback_devices(self):442 def test_get_boot_and_root_loopback_devices(self):
427 tempfile = self._create_qemu_img_with_partitions(',1,0x0C,*\n,,,-')443 tempfile = self._create_qemu_img_with_partitions(',1,0x0C,*\n,,,-')
428 popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())444 popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())
@@ -446,6 +462,11 @@
446 popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())462 popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())
447 self.useFixture(MockSomethingFixture(463 self.useFixture(MockSomethingFixture(
448 sys, 'stdout', open('/dev/null', 'w')))464 sys, 'stdout', open('/dev/null', 'w')))
465 self.useFixture(MockSomethingFixture(
466 partitions, 'is_partition_mounted', lambda part: False))
467 self.useFixture(MockSomethingFixture(
468 partitions, 'get_boot_and_root_loopback_devices',
469 lambda image: ('/dev/loop99', '/dev/loop98')))
449 uuid = '2e82008e-1af3-4699-8521-3bf5bac1e67a'470 uuid = '2e82008e-1af3-4699-8521-3bf5bac1e67a'
450 bootfs, rootfs = setup_partitions(471 bootfs, rootfs = setup_partitions(
451 'beagle', Media(tempfile), 32, '2G', 'boot', 'root', 'ext3',472 'beagle', Media(tempfile), 32, '2G', 'boot', 'root', 'ext3',
@@ -458,12 +479,6 @@
458 tempfile],479 tempfile],
459 # Make sure changes are written to disk.480 # Make sure changes are written to disk.
460 ['sync'],481 ['sync'],
461 # Register boot/root loopback devices so that we can just copy
462 # their contents over and have it written to the image file.
463 ['sudo', 'losetup', '-f', '--show', tempfile, '--offset',
464 '32256', '--sizelimit', '129024'],
465 ['sudo', 'losetup', '-f', '--show', tempfile, '--offset',
466 '161280', '--sizelimit', '10321920'],
467 ['sudo', 'mkfs.vfat', '-F', '32', bootfs, '-n', 'boot'],482 ['sudo', 'mkfs.vfat', '-F', '32', bootfs, '-n', 'boot'],
468 ['sudo', 'mkfs.ext3', '-U', uuid, rootfs, '-L', 'root']],483 ['sudo', 'mkfs.ext3', '-U', uuid, rootfs, '-L', 'root']],
469 popen_fixture.mock.calls)484 popen_fixture.mock.calls)
@@ -473,6 +488,9 @@
473 sys, 'stdout', open('/dev/null', 'w')))488 sys, 'stdout', open('/dev/null', 'w')))
474 self.useFixture(MockSomethingFixture(489 self.useFixture(MockSomethingFixture(
475 partitions, '_get_partition_count', lambda media: 2))490 partitions, '_get_partition_count', lambda media: 2))
491 # Pretend the partitions are mounted.
492 self.useFixture(MockSomethingFixture(
493 partitions, 'is_partition_mounted', lambda part: True))
476 tempfile = self._create_qemu_img_with_partitions(',1,0x0C,*\n,,,-')494 tempfile = self._create_qemu_img_with_partitions(',1,0x0C,*\n,,,-')
477 media = Media(tempfile)495 media = Media(tempfile)
478 # Pretend our tempfile is a block device.496 # Pretend our tempfile is a block device.
@@ -486,7 +504,11 @@
486 [['sudo', 'parted', '-s', tempfile, 'mklabel', 'msdos'],504 [['sudo', 'parted', '-s', tempfile, 'mklabel', 'msdos'],
487 ['sudo', 'sfdisk', '-D', '-H', '255', '-S', '63', tempfile],505 ['sudo', 'sfdisk', '-D', '-H', '255', '-S', '63', tempfile],
488 ['sync'],506 ['sync'],
507 # Since the partitions are mounted, setup_partitions will umount
508 # them before running mkfs.
509 ['sudo', 'umount', bootfs],
489 ['sudo', 'mkfs.vfat', '-F', '32', bootfs, '-n', 'boot'],510 ['sudo', 'mkfs.vfat', '-F', '32', bootfs, '-n', 'boot'],
511 ['sudo', 'umount', rootfs],
490 ['sudo', 'mkfs.ext3', '-U', uuid, rootfs, '-L', 'root']],512 ['sudo', 'mkfs.ext3', '-U', uuid, rootfs, '-L', 'root']],
491 popen_fixture.mock.calls)513 popen_fixture.mock.calls)
492514

Subscribers

People subscribed via source and target branches