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

Proposed by Guilherme Salgado
Status: Merged
Approved by: James Westby
Approved revision: 385
Merged at revision: 384
Proposed branch: lp:~salgado/linaro-image-tools/bug-814256
Merge into: lp:linaro-image-tools/11.11
Diff against target: 106 lines (+25/-13)
2 files modified
linaro_image_tools/media_create/rootfs.py (+18/-11)
linaro_image_tools/media_create/tests/test_media_create.py (+7/-2)
To merge this branch: bzr merge lp:~salgado/linaro-image-tools/bug-814256
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+68751@code.launchpad.net

Description of the change

Make sure the root partition is umounted if something goes wrong while it's
being populated.

To post a comment you must log in.
385. By Guilherme Salgado

Update the docs of populate_rootfs() to state clearly that it uses an atexit handler and does not umount the partition directly

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

Hi,

I'm guessing the rest of the code doesn't rely on the device
being unmounted, so removing the sequential unmount is ok.

On whether it should ignore errors or not, given that Python
reports atexit errors separately and doesn't override any
exception thrown by the code I think that's fine. You will
see the original error, and be alerted to any cleanup failure.

Thanks,

James

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

On Thu, 2011-07-21 at 22:27 +0000, James Westby wrote:
> Review: Approve
> Hi,
>
> I'm guessing the rest of the code doesn't rely on the device
> being unmounted, so removing the sequential unmount is ok.

There's no code running after this, so I think just changing the
docstring to state clearly that the umount won't happen until l-m-c
exits is fine.

Thanks for the review!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'linaro_image_tools/media_create/rootfs.py'
2--- linaro_image_tools/media_create/rootfs.py 2011-07-18 14:38:39 +0000
3+++ linaro_image_tools/media_create/rootfs.py 2011-07-21 21:34:17 +0000
4@@ -17,19 +17,27 @@
5 # You should have received a copy of the GNU General Public License
6 # along with Linaro Image Tools. If not, see <http://www.gnu.org/licenses/>.
7
8-import glob
9+import atexit
10 import os
11 import subprocess
12 import tempfile
13
14 from linaro_image_tools import cmd_runner
15
16+
17+def umount(device):
18+ # The old code used to ignore failures here, but I don't think that's
19+ # desirable so I'm using cmd_runner.run()'s standard behaviour, which will
20+ # fail on a non-zero return value.
21+ cmd_runner.run(['umount', device], as_root=True).wait()
22+
23+
24 def populate_partition(content_dir, root_disk, partition):
25 os.makedirs(root_disk)
26 cmd_runner.run(['mount', partition, root_disk], as_root=True).wait()
27+ atexit.register(umount, root_disk)
28 move_contents(content_dir, root_disk)
29 cmd_runner.run(['sync']).wait()
30- cmd_runner.run(['umount', root_disk], as_root=True).wait()
31
32
33 def rootfs_mount_options(rootfs_type):
34@@ -49,11 +57,11 @@
35 This consists of:
36 1. Create a directory on the path specified by root_disk
37 2. Mount the given partition onto the created directory.
38- 3. Move the contents of content_dir to that directory.
39- 4. If should_create_swap, then create it with the given size.
40- 5. Add fstab entries for the / filesystem and swap (if created).
41- 6. Create a /etc/flash-kernel.conf containing the target's boot device.
42- 7. Unmount the partition we mounted on step 2.
43+ 3. Setup an atexit handler to unmount the partition mounted above.
44+ 4. Move the contents of content_dir to that directory.
45+ 5. If should_create_swap, then create it with the given size.
46+ 6. Add fstab entries for the / filesystem and swap (if created).
47+ 7. Create a /etc/flash-kernel.conf containing the target's boot device.
48 """
49 print "\nPopulating rootfs partition"
50 print "Be patient, this may take a few minutes\n"
51@@ -61,6 +69,9 @@
52 os.makedirs(root_disk)
53
54 cmd_runner.run(['mount', partition, root_disk], as_root=True).wait()
55+ # We use an atexit handler to umount the partition to make sure it's
56+ # umounted even if something fails when it's being populated.
57+ atexit.register(umount, root_disk)
58
59 move_contents(content_dir, root_disk)
60
61@@ -91,10 +102,6 @@
62 create_flash_kernel_config(root_disk, 1 + partition_offset)
63
64 cmd_runner.run(['sync']).wait()
65- # The old code used to ignore failures here, but I don't think that's
66- # desirable so I'm using cmd_runner.run()'s standard behaviour, which will
67- # fail on a non-zero return value.
68- cmd_runner.run(['umount', root_disk], as_root=True).wait()
69
70
71 def create_flash_kernel_config(root_disk, boot_partition_number):
72
73=== modified file 'linaro_image_tools/media_create/tests/test_media_create.py'
74--- linaro_image_tools/media_create/tests/test_media_create.py 2011-07-19 22:19:43 +0000
75+++ linaro_image_tools/media_create/tests/test_media_create.py 2011-07-21 21:34:17 +0000
76@@ -101,6 +101,7 @@
77 move_contents,
78 populate_rootfs,
79 rootfs_mount_options,
80+ umount,
81 write_data_to_protected_file,
82 )
83 from linaro_image_tools.media_create.tests.fixtures import (
84@@ -2104,6 +2105,8 @@
85 def fake_create_flash_kernel_config(disk, partition_offset):
86 self.create_flash_kernel_config_called = True
87
88+ atexit_fixture = self.useFixture(MockSomethingFixture(
89+ atexit, 'register', AtExitRegister()))
90 # Mock stdout, cmd_runner.Popen(), append_to_fstab and
91 # create_flash_kernel_config.
92 self.useFixture(MockSomethingFixture(
93@@ -2149,9 +2152,11 @@
94 '%s dd if=/dev/zero of=%s bs=1M count=100' % (
95 sudo_args, swap_file),
96 '%s mkswap %s' % (sudo_args, swap_file),
97- 'sync',
98- '%s umount %s' % (sudo_args, root_disk)]
99+ 'sync']
100 self.assertEqual(expected, popen_fixture.mock.commands_executed)
101+ self.assertEqual(1, len(atexit_fixture.mock.funcs))
102+ self.assertEqual(
103+ (umount, (root_disk,), {}), atexit_fixture.mock.funcs[0])
104
105 def test_create_flash_kernel_config(self):
106 fixture = self.useFixture(MockCmdRunnerPopenFixture())

Subscribers

People subscribed via source and target branches