livemedia-creator: cleanup after a crash

Make sure we don't leave any device-mapper or loop devices laying around
after a crash.
This commit is contained in:
Brian C. Lane 2012-05-11 17:35:16 -07:00
parent db6b4a2a76
commit 4b368aab87
2 changed files with 31 additions and 1 deletions

View File

@ -72,6 +72,16 @@ def loop_detach(loopdev):
'''Detach the given loop device. Return False on failure.''' '''Detach the given loop device. Return False on failure.'''
return (call(["losetup", "--detach", loopdev]) == 0) return (call(["losetup", "--detach", loopdev]) == 0)
def get_loop_name(path):
'''Return the loop device associated with the path.
Raises RuntimeError if more than one loop is associated'''
buf = check_output(["losetup", "-j", path], stderr=PIPE)
if len(buf.splitlines()) > 1:
# there should never be more than one loop device listed
raise RuntimeError("multiple loops associated with %s" % path)
name = os.path.basename(buf.split(":")[0])
return name
def dm_attach(dev, size, name=None): def dm_attach(dev, size, name=None):
'''Attach a devicemapper device to the given device, with the given size. '''Attach a devicemapper device to the given device, with the given size.
If name is None, a random name will be chosen. Returns the device name. If name is None, a random name will be chosen. Returns the device name.

View File

@ -45,7 +45,8 @@ from pykickstart.version import makeVersion
from pylorax.base import DataHolder from pylorax.base import DataHolder
from pylorax.treebuilder import TreeBuilder, RuntimeBuilder, udev_escape from pylorax.treebuilder import TreeBuilder, RuntimeBuilder, udev_escape
from pylorax.sysutils import joinpaths, remove, linktree from pylorax.sysutils import joinpaths, remove, linktree
from pylorax.imgutils import PartitionMount, mksparse, mkext4img from pylorax.imgutils import PartitionMount, mksparse, mkext4img, loop_detach
from pylorax.imgutils import get_loop_name, dm_detach
from pylorax.executils import execWithRedirect, execWithCapture from pylorax.executils import execWithRedirect, execWithCapture
# no-virt mode doesn't need libvirt, so make it optional # no-virt mode doesn't need libvirt, so make it optional
@ -385,6 +386,17 @@ class VirtualInstall( object ):
subprocess.call(["virsh","destroy",self.virt_name]) subprocess.call(["virsh","destroy",self.virt_name])
subprocess.call(["virsh","undefine",self.virt_name]) subprocess.call(["virsh","undefine",self.virt_name])
def is_image_mounted(disk_img):
"""
Return True if the disk_img is mounted
"""
with open("/proc/mounts") as mounts:
for mount in mounts:
fields = mount.split()
if len(fields) > 2 and fields[1] == disk_img:
return True
return False
def anaconda_install( disk_img, disk_size, kickstart, repo, args ): def anaconda_install( disk_img, disk_size, kickstart, repo, args ):
""" """
@ -704,6 +716,14 @@ if __name__ == '__main__':
if os.path.exists( "/tmp/"+l ): if os.path.exists( "/tmp/"+l ):
shutil.copy2( "/tmp/"+l, log_anaconda ) shutil.copy2( "/tmp/"+l, log_anaconda )
os.unlink( "/tmp/"+l ) os.unlink( "/tmp/"+l )
# If anaconda failed the disk image may still be in use by dm
dm_name = os.path.splitext(os.path.basename(disk_img))[0]
dm_path = "/dev/mapper/"+dm_name
if os.path.exists(dm_path):
dm_detach(dm_path)
loop_detach(get_loop_name(disk_img))
else: else:
iso_mount = IsoMountpoint( opts.iso ) iso_mount = IsoMountpoint( opts.iso )
log_monitor = LogMonitor( install_log ) log_monitor = LogMonitor( install_log )