From patchwork Fri Jul 22 13:17:39 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guilherme Salgado X-Patchwork-Id: 3049 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 32AE023E52 for ; Fri, 22 Jul 2011 13:17:43 +0000 (UTC) Received: from mail-qw0-f52.google.com (mail-qw0-f52.google.com [209.85.216.52]) by fiordland.canonical.com (Postfix) with ESMTP id D8229A18857 for ; Fri, 22 Jul 2011 13:17:42 +0000 (UTC) Received: by qwb8 with SMTP id 8so1598949qwb.11 for ; Fri, 22 Jul 2011 06:17:42 -0700 (PDT) Received: by 10.229.217.3 with SMTP id hk3mr1369296qcb.38.1311340662350; Fri, 22 Jul 2011 06:17:42 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.229.217.78 with SMTP id hl14cs14240qcb; Fri, 22 Jul 2011 06:17:41 -0700 (PDT) Received: by 10.216.229.200 with SMTP id h50mr1295557weq.13.1311340660226; Fri, 22 Jul 2011 06:17:40 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id x48si4493013wec.110.2011.07.22.06.17.39; Fri, 22 Jul 2011 06:17:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) client-ip=91.189.90.139; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) smtp.mail=bounces@canonical.com Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QkFbn-0007Lb-J1 for ; Fri, 22 Jul 2011 13:17:39 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 8EBF02E8029 for ; Fri, 22 Jul 2011 13:17:39 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: linaro-image-tools X-Launchpad-Branch: ~linaro-image-tools/linaro-image-tools/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 384 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~linaro-image-tools/linaro-image-tools/trunk] Rev 384: Make sure the root partition is umounted if something goes wrong while it's being populated. Message-Id: <20110722131739.11624.47001.launchpad@loganberry.canonical.com> Date: Fri, 22 Jul 2011 13:17:39 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13483"; Instance="initZopeless config overlay" X-Launchpad-Hash: f7bb1b3298193a9b10cb21e132315ca2db30475b Merge authors: Guilherme Salgado (salgado) Related merge proposals: https://code.launchpad.net/~salgado/linaro-image-tools/bug-814256/+merge/68751 proposed by: Guilherme Salgado (salgado) review: Approve - James Westby (james-w) ------------------------------------------------------------ revno: 384 [merge] committer: Guilherme Salgado branch nick: trunk timestamp: Fri 2011-07-22 10:15:37 -0300 message: Make sure the root partition is umounted if something goes wrong while it's being populated. modified: linaro_image_tools/media_create/rootfs.py linaro_image_tools/media_create/tests/test_media_create.py --- lp:linaro-image-tools https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk You are subscribed to branch lp:linaro-image-tools. To unsubscribe from this branch go to https://code.launchpad.net/~linaro-image-tools/linaro-image-tools/trunk/+edit-subscription === modified file 'linaro_image_tools/media_create/rootfs.py' --- linaro_image_tools/media_create/rootfs.py 2011-07-18 14:38:39 +0000 +++ linaro_image_tools/media_create/rootfs.py 2011-07-21 21:31:15 +0000 @@ -17,19 +17,27 @@ # You should have received a copy of the GNU General Public License # along with Linaro Image Tools. If not, see . -import glob +import atexit import os import subprocess import tempfile from linaro_image_tools import cmd_runner + +def umount(device): + # The old code used to ignore failures here, but I don't think that's + # desirable so I'm using cmd_runner.run()'s standard behaviour, which will + # fail on a non-zero return value. + cmd_runner.run(['umount', device], as_root=True).wait() + + def populate_partition(content_dir, root_disk, partition): os.makedirs(root_disk) cmd_runner.run(['mount', partition, root_disk], as_root=True).wait() + atexit.register(umount, root_disk) move_contents(content_dir, root_disk) cmd_runner.run(['sync']).wait() - cmd_runner.run(['umount', root_disk], as_root=True).wait() def rootfs_mount_options(rootfs_type): @@ -49,11 +57,11 @@ This consists of: 1. Create a directory on the path specified by root_disk 2. Mount the given partition onto the created directory. - 3. Move the contents of content_dir to that directory. - 4. If should_create_swap, then create it with the given size. - 5. Add fstab entries for the / filesystem and swap (if created). - 6. Create a /etc/flash-kernel.conf containing the target's boot device. - 7. Unmount the partition we mounted on step 2. + 3. Setup an atexit handler to unmount the partition mounted above. + 4. Move the contents of content_dir to that directory. + 5. If should_create_swap, then create it with the given size. + 6. Add fstab entries for the / filesystem and swap (if created). + 7. Create a /etc/flash-kernel.conf containing the target's boot device. """ print "\nPopulating rootfs partition" print "Be patient, this may take a few minutes\n" @@ -61,6 +69,9 @@ os.makedirs(root_disk) cmd_runner.run(['mount', partition, root_disk], as_root=True).wait() + # We use an atexit handler to umount the partition to make sure it's + # umounted even if something fails when it's being populated. + atexit.register(umount, root_disk) move_contents(content_dir, root_disk) @@ -91,10 +102,6 @@ create_flash_kernel_config(root_disk, 1 + partition_offset) cmd_runner.run(['sync']).wait() - # The old code used to ignore failures here, but I don't think that's - # desirable so I'm using cmd_runner.run()'s standard behaviour, which will - # fail on a non-zero return value. - cmd_runner.run(['umount', root_disk], as_root=True).wait() def create_flash_kernel_config(root_disk, boot_partition_number): === modified file 'linaro_image_tools/media_create/tests/test_media_create.py' --- linaro_image_tools/media_create/tests/test_media_create.py 2011-07-19 22:19:43 +0000 +++ linaro_image_tools/media_create/tests/test_media_create.py 2011-07-21 21:24:13 +0000 @@ -101,6 +101,7 @@ move_contents, populate_rootfs, rootfs_mount_options, + umount, write_data_to_protected_file, ) from linaro_image_tools.media_create.tests.fixtures import ( @@ -2104,6 +2105,8 @@ def fake_create_flash_kernel_config(disk, partition_offset): self.create_flash_kernel_config_called = True + atexit_fixture = self.useFixture(MockSomethingFixture( + atexit, 'register', AtExitRegister())) # Mock stdout, cmd_runner.Popen(), append_to_fstab and # create_flash_kernel_config. self.useFixture(MockSomethingFixture( @@ -2149,9 +2152,11 @@ '%s dd if=/dev/zero of=%s bs=1M count=100' % ( sudo_args, swap_file), '%s mkswap %s' % (sudo_args, swap_file), - 'sync', - '%s umount %s' % (sudo_args, root_disk)] + 'sync'] self.assertEqual(expected, popen_fixture.mock.commands_executed) + self.assertEqual(1, len(atexit_fixture.mock.funcs)) + self.assertEqual( + (umount, (root_disk,), {}), atexit_fixture.mock.funcs[0]) def test_create_flash_kernel_config(self): fixture = self.useFixture(MockCmdRunnerPopenFixture())