summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeil Williams <codehelp@debian.org>2016-07-31 18:00:19 +0100
committerNeil Williams <codehelp@debian.org>2016-07-31 18:13:33 +0100
commit5c0f1d11fb1bdeceffc0aa28e19673c82663a9ab (patch)
tree2821afc840cc5313deed3426a635b90d02d05e64
parentea0b38bb901d2e224f829e540dc50ed13b4d204e (diff)
downloadvmdebootstrap-5c0f1d11fb1bdeceffc0aa28e19673c82663a9ab.tar.gz
improve error handling and cleanup
-rwxr-xr-xbin/vmdebootstrap17
-rw-r--r--vmdebootstrap/filesystem.py2
2 files changed, 17 insertions, 2 deletions
diff --git a/bin/vmdebootstrap b/bin/vmdebootstrap
index dc16c61..799fbd3 100755
--- a/bin/vmdebootstrap
+++ b/bin/vmdebootstrap
@@ -384,16 +384,19 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth
def umount(self):
if not self.settings['image']:
return
+ base = self.handlers[Base.name]
# Umount in the reverse mount order
for i in range(len(self.mount_points) - 1, -1, -1):
mount_point = self.mount_points[i]
+ base.message("Umounting %s" % mount_point)
try:
runcmd(['umount', mount_point], ignore_fail=False)
except cliapp.AppException:
logging.debug("umount failed, sleeping and trying again")
time.sleep(5)
runcmd(['umount', mount_point], ignore_fail=False)
- self.mount_points.pop(i)
+ finally:
+ self.mount_points.pop(i)
def partition_image(self):
"""
@@ -556,6 +559,7 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth
def cleanup_system(self):
base = self.handlers[Base.name]
+ filesystem = self.handlers[Filesystem.name]
# Clean up after any errors.
base.message('Cleaning up')
@@ -563,6 +567,17 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth
# Umount in the reverse mount order
if self.settings['image']:
self.umount()
+
+ # tidy up loop mounting issues on failure.
+ out = runcmd(['losetup', '-a'])
+ rootdev = filesystem.devices['rootdev']
+ if rootdev:
+ runcmd(['dmsetup', 'remove', rootdev]], ignore_fail=True)
+ device = [line.decode('utf-8').split()[0][:-1]
+ for line in out.splitlines()
+ if self.settings['image'] in line.decode('utf-8')]
+ if device:
+ runcmd(['losetup', '-d', "%s" % device[0]], ignore_fail=True)
runcmd(['kpartx', '-d', self.settings['image']], ignore_fail=True)
for dirname in self.remove_dirs:
diff --git a/vmdebootstrap/filesystem.py b/vmdebootstrap/filesystem.py
index f3b2a12..d878d73 100644
--- a/vmdebootstrap/filesystem.py
+++ b/vmdebootstrap/filesystem.py
@@ -133,7 +133,7 @@ class Filesystem(Base):
self.devices['bootdev'] = boot
self.devices['swap'] = swap
- def mkfs(self, device, fstype, opt):
+ def mkfs(self, device, fstype, opt=None):
self.message('Creating filesystem %s' % fstype)
if opt:
runcmd(['mkfs', '-t', fstype, '-O', opt, device])