3 # Copyright (c) 2007, Red Hat, Inc.
4 # Copyright (c) 2009, 2010, 2011 Intel, Inc.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the Free
8 # Software Foundation; version 2 of the License
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc., 59
17 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 from __future__ import with_statement
31 from mic.utils import runner
32 from mic.utils.errors import *
35 def find_binary_inchroot(binary, chroot):
43 bin_path = "%s/%s" % (path, binary)
44 if os.path.exists("%s/%s" % (chroot, bin_path)):
48 def find_binary_path(binary):
49 if os.environ.has_key("PATH"):
50 paths = os.environ["PATH"].split(":")
53 if os.environ.has_key("HOME"):
54 paths += [os.environ["HOME"] + "/bin"]
55 paths += ["/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"]
58 bin_path = "%s/%s" % (path, binary)
59 if os.path.exists(bin_path):
61 raise CreatorError("Command '%s' is not available." % binary)
63 def makedirs(dirname):
64 """A version of os.makedirs() that doesn't throw an
65 exception if the leaf directory already exists.
70 if err.errno != errno.EEXIST:
73 def mkvdfs(in_img, out_img, fsoptions):
74 """ This function is incomplete. """
75 fullpathmkvdfs = find_binary_path("mkfs.vdfs")
76 # args = fullpathmkvdfs + " -i -r "+ in_img + " -z 1024M -s " + out_img
77 args = fullpathmkvdfs + " " + fsoptions + " -r " + in_img + " " + out_img
78 msger.verbose("vdfs args: %s" % args)
79 runner.show("%s --help" % fullpathmkvdfs)
80 # if not sys.stdout.isatty():
81 # args.append("-no-progress")
82 # runner.show("%s --help" % fullpathmkvdfs)
83 ret = runner.show(args)
85 runner.show ("vdfs error")
86 raise VdfsError("' %s' exited with error (%d)" % (args, ret))
88 def mksquashfs(in_img, out_img):
89 fullpathmksquashfs = find_binary_path("mksquashfs")
90 args = [fullpathmksquashfs, in_img, out_img]
92 if not sys.stdout.isatty():
93 args.append("-no-progress")
95 ret = runner.show(args)
97 raise SquashfsError("'%s' exited with error (%d)" % (' '.join(args), ret))
99 def resize2fs(fs, size):
100 resize2fs = find_binary_path("resize2fs")
102 # it means to minimalize it
103 return runner.show([resize2fs, '-M', fs])
105 return runner.show([resize2fs, fs, "%sK" % (size / 1024,)])
107 class BindChrootMount:
108 """Represents a bind mount of a directory into a chroot."""
109 def __init__(self, src, chroot, dest = None, option = None):
110 self.root = os.path.abspath(os.path.expanduser(chroot))
111 self.mount_option = option
113 self.orig_src = self.src = src
114 if os.path.islink(src):
115 self.src = os.readlink(src)
116 if not self.src.startswith('/'):
117 self.src = os.path.abspath(os.path.join(os.path.dirname(src),
122 self.dest = os.path.join(self.root, dest.lstrip('/'))
125 self.mountcmd = find_binary_path("mount")
126 self.umountcmd = find_binary_path("umount")
129 with open('/proc/mounts') as f:
131 if line.split()[1] == os.path.abspath(self.dest):
137 if self.mounted or self.ismounted():
143 if err.errno == errno.ENOSPC:
144 msger.warning("No space left on device '%s'" % err.filename)
147 if self.mount_option:
148 cmdline = [self.mountcmd, "-o" ,"bind", "-o", "%s" % \
149 self.mount_option, self.src, self.dest]
151 cmdline = [self.mountcmd, "-o" ,"bind", self.src, self.dest]
152 rc, errout = runner.runtool(cmdline, catch=2)
154 raise MountError("Bind-mounting '%s' to '%s' failed: %s" %
155 (self.src, self.dest, errout))
158 if os.path.islink(self.orig_src):
159 dest = os.path.join(self.root, self.orig_src.lstrip('/'))
160 if not os.path.exists(dest):
161 os.symlink(self.src, dest)
164 if self.mounted or self.ismounted():
165 runner.show([self.umountcmd, "-l", self.dest])
169 """LoopbackMount compatibility layer for old API"""
170 def __init__(self, lofile, mountdir, fstype = None):
171 self.diskmount = DiskMount(LoopbackDisk(lofile,size = 0),mountdir,fstype,rmmountdir = True)
173 self.losetupcmd = find_binary_path("losetup")
176 self.diskmount.cleanup()
179 self.diskmount.unmount()
183 runner.show([self.losetupcmd, "-d", self.loopdev])
191 self.loopdev = get_loop_device(self.losetupcmd, self.lofile)
195 self.diskmount.mount()
197 class SparseLoopbackMount(LoopbackMount):
198 """SparseLoopbackMount compatibility layer for old API"""
199 def __init__(self, lofile, mountdir, size, fstype = None):
200 self.diskmount = DiskMount(SparseLoopbackDisk(lofile,size),mountdir,fstype,rmmountdir = True)
202 def expand(self, create = False, size = None):
203 self.diskmount.disk.expand(create, size)
205 def truncate(self, size = None):
206 self.diskmount.disk.truncate(size)
209 self.diskmount.disk.create()
211 class SparseExtLoopbackMount(SparseLoopbackMount):
212 """SparseExtLoopbackMount compatibility layer for old API"""
213 def __init__(self, lofile, mountdir, size, fstype, blocksize, fslabel):
214 self.diskmount = ExtDiskMount(SparseLoopbackDisk(lofile,size), mountdir, fstype, blocksize, fslabel, rmmountdir = True)
217 def __format_filesystem(self):
218 self.diskmount.__format_filesystem()
221 self.diskmount.disk.create()
223 def resize(self, size = None):
224 return self.diskmount.__resize_filesystem(size)
227 self.diskmount.mount()
230 self.extdiskmount.__fsck()
232 def __get_size_from_filesystem(self):
233 return self.diskmount.__get_size_from_filesystem()
235 def __resize_to_minimal(self):
236 return self.diskmount.__resize_to_minimal()
238 def resparse(self, size = None):
239 return self.diskmount.resparse(size)
242 """Generic base object for a disk
244 The 'create' method must make the disk visible as a block device - eg
245 by calling losetup. For RawDisk, this is obviously a no-op. The 'cleanup'
246 method must undo the 'create' operation.
248 def __init__(self, size, device = None):
249 self._device = device
258 def get_device(self):
260 def set_device(self, path):
262 device = property(get_device, set_device)
266 size = property(get_size)
270 """A Disk backed by a block device.
271 Note that create() is a no-op.
273 def __init__(self, size, device):
274 Disk.__init__(self, size, device)
282 class LoopbackDisk(Disk):
283 """A Disk backed by a file via the loop module."""
284 def __init__(self, lofile, size):
285 Disk.__init__(self, size)
287 self.losetupcmd = find_binary_path("losetup")
293 return os.path.exists(self.lofile)
296 if self.device is not None:
299 self.device = get_loop_device(self.losetupcmd, self.lofile)
302 if self.device is None:
304 msger.debug("Losetup remove %s" % self.device)
305 rc = runner.show([self.losetupcmd, "-d", self.device])
308 def reread_size(self):
309 if self.device is None:
311 msger.debug("Reread size %s" % self.device)
312 rc = runner.show([self.losetupcmd, "-c", self.device])
313 # XXX: (WORKAROUND) re-setup loop device when losetup isn't support '-c' option.
315 msger.debug("Fail to reread size, Try to re-setup loop device to reread the size of the file associated")
316 runner.show([self.losetupcmd, "-d", self.device])
317 runner.show([self.losetupcmd, self.device, self.lofile])
319 class SparseLoopbackDisk(LoopbackDisk):
320 """A Disk backed by a sparse file via the loop module."""
321 def __init__(self, lofile, size):
322 LoopbackDisk.__init__(self, lofile, size)
324 def expand(self, create = False, size = None):
328 if not os.path.exists(self.lofile):
329 makedirs(os.path.dirname(self.lofile))
334 msger.debug("Extending sparse file %s to %d" % (self.lofile, size))
336 fd = os.open(self.lofile, flags, 0644)
338 fd = os.open(self.lofile, flags)
343 os.ftruncate(fd, size)
345 # may be limited by 2G in 32bit env
346 os.ftruncate(fd, 2**31L)
350 def truncate(self, size = None):
354 msger.debug("Truncating sparse file %s to %d" % (self.lofile, size))
355 fd = os.open(self.lofile, os.O_WRONLY)
356 os.ftruncate(fd, size)
360 if self.device is not None:
363 self.expand(create = True)
364 LoopbackDisk.create(self)
367 """A generic base class to deal with mounting things."""
368 def __init__(self, mountdir):
369 self.mountdir = mountdir
374 def mount(self, options = None):
380 class DiskMount(Mount):
381 """A Mount object that handles mounting of a Disk."""
382 def __init__(self, disk, mountdir, fstype = None, rmmountdir = True):
383 Mount.__init__(self, mountdir)
387 self.rmmountdir = rmmountdir
392 self.mkfscmd = find_binary_path("mkfs." + self.fstype)
395 self.mountcmd = find_binary_path("mount")
396 self.umountcmd = find_binary_path("umount")
404 msger.debug("Unmounting directory %s" % self.mountdir)
405 runner.quiet('sync') # sync the data on this mount point
406 rc = runner.show([self.umountcmd, "-l", self.mountdir])
410 raise MountError("Failed to umount %s" % self.mountdir)
411 if self.rmdir and not self.mounted:
413 os.rmdir(self.mountdir)
423 def mount(self, options = None):
427 if not os.path.isdir(self.mountdir):
428 msger.debug("Creating mount point %s" % self.mountdir)
429 os.makedirs(self.mountdir)
430 self.rmdir = self.rmmountdir
434 msger.debug("Mounting %s at %s" % (self.disk.device, self.mountdir))
436 args = [ self.mountcmd, "-o", options, self.disk.device, self.mountdir ]
438 args = [ self.mountcmd, self.disk.device, self.mountdir ]
440 args.extend(["-t", self.fstype])
442 rc = runner.show(args)
444 raise MountError("Failed to mount '%s' to '%s' with command '%s'. Retval: %s" %
445 (self.disk.device, self.mountdir, " ".join(args), rc))
449 class ExtDiskMount(DiskMount):
450 """A DiskMount object that is able to format/resize ext[23] filesystems."""
451 def __init__(self, disk, mountdir, fstype, blocksize, fslabel, rmmountdir=True, skipformat = False, fsopts = None, fsuuid=None):
452 DiskMount.__init__(self, disk, mountdir, fstype, rmmountdir)
453 self.blocksize = blocksize
454 self.fslabel = fslabel.replace("/", "")
455 self.uuid = fsuuid or str(uuid.uuid4())
456 self.skipformat = skipformat
458 self.__extopts = None
459 self.dumpe2fs = find_binary_path("dumpe2fs")
460 self.tune2fs = find_binary_path("tune2fs")
462 def __get_extopts(self):
463 return self.__extopts
465 def __set_extopts(self, val):
467 self.__extopts = None
469 m = re.search(r'-b\s*(?P<blocksize>\d+)', val)
471 self.blocksize = int(m.group('blocksize'))
472 val = val.replace(m.group(), '')
475 extopts = property(__get_extopts, __set_extopts)
477 def __parse_field(self, output, field):
478 for line in output.split("\n"):
479 if line.startswith(field + ":"):
480 return line[len(field) + 1:].strip()
482 raise KeyError("Failed to find field '%s' in output" % field)
484 def __format_filesystem(self):
486 msger.debug("Skip filesystem format.")
489 msger.verbose("Formating %s filesystem on %s" % (self.fstype, self.disk.device))
490 cmdlist = [self.mkfscmd, "-F", "-L", self.fslabel, "-m", "1", "-b",
491 str(self.blocksize), "-U", self.uuid]
493 cmdlist.extend(self.__extopts.split())
494 cmdlist.extend([self.disk.device])
496 rc, errout = runner.runtool(cmdlist, catch=2)
498 raise MountError("Error creating %s filesystem on disk %s:\n%s" %
499 (self.fstype, self.disk.device, errout))
501 if not self.__extopts:
502 msger.debug("Tuning filesystem on %s" % self.disk.device)
503 runner.show([self.tune2fs, "-c0", "-i0", "-Odir_index", "-ouser_xattr,acl", self.disk.device])
505 def __resize_filesystem(self, size = None):
506 msger.info("Resizing filesystem ...")
507 current_size = os.stat(self.disk.lofile)[stat.ST_SIZE]
510 size = self.disk.size
512 if size == current_size:
515 if size > current_size:
516 self.disk.expand(size=size)
520 resize2fs(self.disk.lofile, size)
521 if size and size != os.stat(self.disk.lofile)[stat.ST_SIZE]:
522 raise MountError("Failed to resize filesystem %s to " % (self.disk.lofile, size))
528 if not self.disk.fixed() and self.disk.exists():
534 self.__resize_filesystem()
536 self.__format_filesystem()
538 def mount(self, options = None, init_expand = False):
541 expand_size = long(self.disk.size * 1.5)
542 msger.info("Initial partition size expanded : %ld -> %ld" % (self.disk.size, expand_size))
543 self.__resize_filesystem(expand_size)
544 self.disk.reread_size()
545 DiskMount.mount(self, options)
548 msger.info("Checking filesystem %s" % self.disk.lofile)
549 runner.quiet(["/sbin/e2fsck", "-f", "-y", self.disk.lofile])
551 def __get_size_from_filesystem(self):
552 return int(self.__parse_field(runner.outs([self.dumpe2fs, '-h', self.disk.lofile]),
553 "Block count")) * self.blocksize
555 def __resize_to_minimal(self):
556 msger.info("Resizing filesystem to minimal ...")
558 resize2fs(self.disk.lofile, 0)
559 return self.__get_size_from_filesystem()
561 def resparse(self, size = None):
566 minsize = self.__resize_to_minimal()
567 self.disk.truncate(minsize)
569 self.__resize_filesystem(size)
572 class VfatDiskMount(DiskMount):
573 """A DiskMount object that is able to format vfat/msdos filesystems."""
574 def __init__(self, disk, mountdir, fstype, blocksize, fslabel, rmmountdir=True, skipformat = False, fsopts = None, fsuuid = None):
575 DiskMount.__init__(self, disk, mountdir, fstype, rmmountdir)
576 self.blocksize = blocksize
577 self.fslabel = fslabel.replace("/", "")
578 rand1 = random.randint(0, 2**16 - 1)
579 rand2 = random.randint(0, 2**16 - 1)
580 self.uuid = fsuuid or "%04X-%04X" % (rand1, rand2)
581 self.skipformat = skipformat
583 self.fsckcmd = find_binary_path("fsck." + self.fstype)
585 def __format_filesystem(self):
587 msger.debug("Skip filesystem format.")
590 msger.verbose("Formating %s filesystem on %s" % (self.fstype, self.disk.device))
591 rc = runner.show([self.mkfscmd, "-n", self.fslabel,
592 "-i", self.uuid.replace("-", ""), self.disk.device])
594 raise MountError("Error creating %s filesystem on disk %s" % (self.fstype,self.disk.device))
596 msger.verbose("Tuning filesystem on %s" % self.disk.device)
598 def __resize_filesystem(self, size = None):
599 current_size = os.stat(self.disk.lofile)[stat.ST_SIZE]
602 size = self.disk.size
604 if size == current_size:
607 if size > current_size:
608 self.disk.expand(size=size)
612 #resize2fs(self.disk.lofile, size)
617 if not self.disk.fixed() and self.disk.exists():
623 self.__resize_filesystem()
625 self.__format_filesystem()
627 def mount(self, options = None, init_expand = False):
630 expand_size = long(self.disk.size * 1.5)
631 msger.info("Initial partition size expanded : %ld -> %ld" % (self.disk.size, expand_size))
632 self.__resize_filesystem(expand_size)
633 self.disk.reread_size()
634 DiskMount.mount(self, options)
637 msger.debug("Checking filesystem %s" % self.disk.lofile)
638 runner.show([self.fsckcmd, "-y", self.disk.lofile])
640 def __get_size_from_filesystem(self):
641 return self.disk.size
643 def __resize_to_minimal(self):
647 # Use a binary search to find the minimal size
648 # we can resize the image to
651 top = self.__get_size_from_filesystem()
654 def resparse(self, size = None):
656 minsize = self.__resize_to_minimal()
657 self.disk.truncate(minsize)
658 self.__resize_filesystem(size)
661 class BtrfsDiskMount(DiskMount):
662 """A DiskMount object that is able to format/resize btrfs filesystems."""
663 def __init__(self, disk, mountdir, fstype, blocksize, fslabel, rmmountdir=True, skipformat = False, fsopts = None, fsuuid = None):
665 DiskMount.__init__(self, disk, mountdir, fstype, rmmountdir)
666 self.blocksize = blocksize
667 self.fslabel = fslabel.replace("/", "")
668 self.uuid = fsuuid or None
669 self.skipformat = skipformat
671 self.blkidcmd = find_binary_path("blkid")
672 self.btrfsckcmd = find_binary_path("btrfsck")
674 def __check_btrfs(self):
676 """ Need to load btrfs module to mount it """
678 for line in open("/proc/filesystems").xreadlines():
679 if line.find("btrfs") > -1:
683 raise MountError("Your system can't mount btrfs filesystem, please make sure your kernel has btrfs support and the module btrfs.ko has been loaded.")
685 # disable selinux, selinux will block write
686 if os.path.exists("/usr/sbin/setenforce"):
687 runner.show(["/usr/sbin/setenforce", "0"])
689 def __parse_field(self, output, field):
690 for line in output.split(" "):
691 if line.startswith(field + "="):
692 return line[len(field) + 1:].strip().replace("\"", "")
694 raise KeyError("Failed to find field '%s' in output" % field)
696 def __format_filesystem(self):
698 msger.debug("Skip filesystem format.")
701 msger.verbose("Formating %s filesystem on %s" % (self.fstype, self.disk.device))
702 rc = runner.show([self.mkfscmd, "-L", self.fslabel, self.disk.device])
704 raise MountError("Error creating %s filesystem on disk %s" % (self.fstype,self.disk.device))
706 self.uuid = self.__parse_field(runner.outs([self.blkidcmd, self.disk.device]), "UUID")
708 def __resize_filesystem(self, size = None):
709 current_size = os.stat(self.disk.lofile)[stat.ST_SIZE]
712 size = self.disk.size
714 if size == current_size:
717 if size > current_size:
718 self.disk.expand(size=size)
725 if not self.disk.fixed() and self.disk.exists():
731 self.__resize_filesystem()
733 self.__format_filesystem()
735 def mount(self, options = None, init_expand = False):
738 expand_size = long(self.disk.size * 1.5)
739 msger.info("Initial partition size expanded : %ld -> %ld" % (self.disk.size, expand_size))
740 self.__resize_filesystem(expand_size)
741 self.disk.reread_size()
742 DiskMount.mount(self, options)
745 msger.debug("Checking filesystem %s" % self.disk.lofile)
746 runner.quiet([self.btrfsckcmd, self.disk.lofile])
748 def __get_size_from_filesystem(self):
749 return self.disk.size
751 def __resize_to_minimal(self):
754 return self.__get_size_from_filesystem()
756 def resparse(self, size = None):
758 minsize = self.__resize_to_minimal()
759 self.disk.truncate(minsize)
760 self.__resize_filesystem(size)
763 class DeviceMapperSnapshot(object):
764 def __init__(self, imgloop, cowloop):
765 self.imgloop = imgloop
766 self.cowloop = cowloop
768 self.__created = False
770 self.dmsetupcmd = find_binary_path("dmsetup")
772 """Load dm_snapshot if it isn't loaded"""
773 load_module("dm_snapshot")
776 if self.__name is None:
778 return os.path.join("/dev/mapper", self.__name)
779 path = property(get_path)
785 self.imgloop.create()
786 self.cowloop.create()
788 self.__name = "imgcreate-%d-%d" % (os.getpid(),
789 random.randint(0, 2**16))
791 size = os.stat(self.imgloop.lofile)[stat.ST_SIZE]
793 table = "0 %d snapshot %s %s p 8" % (size / 512,
797 args = [self.dmsetupcmd, "create", self.__name, "--table", table]
798 if runner.show(args) != 0:
799 self.cowloop.cleanup()
800 self.imgloop.cleanup()
801 raise SnapshotError("Could not create snapshot device using: " + ' '.join(args))
803 self.__created = True
805 def remove(self, ignore_errors = False):
806 if not self.__created:
810 rc = runner.show([self.dmsetupcmd, "remove", self.__name])
811 if not ignore_errors and rc != 0:
812 raise SnapshotError("Could not remove snapshot device")
815 self.__created = False
817 self.cowloop.cleanup()
818 self.imgloop.cleanup()
820 def get_cow_used(self):
821 if not self.__created:
825 # dmsetup status on a snapshot returns e.g.
826 # "0 8388608 snapshot 416/1048576"
827 # or, more generally:
829 # where C is the number of 512 byte sectors in use
831 out = runner.outs([self.dmsetupcmd, "status", self.__name])
833 return int((out.split()[3]).split('/')[0]) * 512
835 raise SnapshotError("Failed to parse dmsetup status: " + out)
837 def create_image_minimizer(path, image, minimal_size):
839 Builds a copy-on-write image which can be used to
840 create a device-mapper snapshot of an image where
841 the image's filesystem is as small as possible
844 1) Create a sparse COW
845 2) Loopback mount the image and the COW
846 3) Create a device-mapper snapshot of the image
848 4) Resize the filesystem to the minimal size
849 5) Determine the amount of space used in the COW
850 6) Restroy the device-mapper snapshot
851 7) Truncate the COW, removing unused space
852 8) Create a squashfs of the COW
854 imgloop = LoopbackDisk(image, None) # Passing bogus size - doesn't matter
856 cowloop = SparseLoopbackDisk(os.path.join(os.path.dirname(path), "osmin"),
859 snapshot = DeviceMapperSnapshot(imgloop, cowloop)
864 resize2fs(snapshot.path, minimal_size)
866 cow_used = snapshot.get_cow_used()
868 snapshot.remove(ignore_errors = (not sys.exc_info()[0] is None))
870 cowloop.truncate(cow_used)
872 mksquashfs(cowloop.lofile, path)
874 os.unlink(cowloop.lofile)
876 def load_module(module):
878 for line in open('/proc/modules').xreadlines():
879 if line.startswith("%s " % module):
883 msger.info("Loading %s..." % module)
884 runner.quiet(['modprobe', module])
886 class LoopDevice(object):
887 def __init__(self, loopid=None):
891 self.kpartxcmd = find_binary_path("kpartx")
892 self.losetupcmd = find_binary_path("losetup")
894 def register(self, device):
899 def reg_atexit(self):
901 atexit.register(self.close)
903 def _genloopid(self):
905 if not glob.glob("/dev/loop[0-9]*"):
908 fint = lambda x: x[9:].isdigit() and int(x[9:]) or 0
909 maxid = 1 + max(filter(lambda x: x<100,
910 map(fint, glob.glob("/dev/loop[0-9]*"))))
911 if maxid < 10: maxid = 10
912 if maxid >= 100: raise
915 def _kpseek(self, device):
916 rc, out = runner.runtool([self.kpartxcmd, '-l', '-v', device])
918 raise MountError("Can't query dm snapshot on %s" % device)
919 for line in out.splitlines():
920 if line and line.startswith("loop"):
924 def _loseek(self, device):
926 rc, out = runner.runtool([self.losetupcmd, '-a'])
928 raise MountError("Failed to run 'losetup -a'")
929 for line in out.splitlines():
930 m = re.match("([^:]+): .*", line)
931 if m and m.group(1) == device:
938 self.loopid = self._genloopid()
939 self.device = "/dev/loop%d" % self.loopid
940 if os.path.exists(self.device):
941 if self._loseek(self.device):
942 raise MountError("Device busy: %s" % self.device)
947 mknod = find_binary_path('mknod')
948 rc = runner.show([mknod, '-m664', self.device, 'b', '7', str(self.loopid)])
950 raise MountError("Failed to create device %s" % self.device)
959 except MountError, e:
960 raise CreatorError("%s" % e)
964 if self.device is None:
968 if self._kpseek(self.device):
969 runner.quiet([self.kpartxcmd, "-d", self.device])
970 if self._loseek(self.device):
971 runner.quiet([self.losetupcmd, "-d", self.device])
972 # FIXME: should sleep a while between two loseek
973 if self._loseek(self.device):
974 msger.warning("Can't cleanup loop device %s" % self.device)
976 os.unlink(self.device)
978 DEVICE_PIDFILE_DIR = "/var/tmp/mic/device"
979 DEVICE_LOCKFILE = "/var/lock/__mic_loopdev.lock"
981 def get_loop_device(losetupcmd, lofile):
982 global DEVICE_PIDFILE_DIR
983 global DEVICE_LOCKFILE
986 makedirs(os.path.dirname(DEVICE_LOCKFILE))
987 fp = open(DEVICE_LOCKFILE, 'w')
988 fcntl.flock(fp, fcntl.LOCK_EX)
991 devinst = LoopDevice()
993 # clean up left loop device first
996 # provide an avaible loop device
997 rc, out = runner.runtool([losetupcmd, "--find"])
999 loopdev = out.split()[0]
1000 devinst.register(loopdev)
1001 if not loopdev or not os.path.exists(loopdev):
1003 loopdev = devinst.device
1005 # setup a loop device for image file
1006 rc = runner.show([losetupcmd, loopdev, lofile])
1008 raise MountError("Failed to setup loop device for '%s'" % lofile)
1010 devinst.reg_atexit()
1012 # try to save device and pid
1013 makedirs(DEVICE_PIDFILE_DIR)
1014 pidfile = os.path.join(DEVICE_PIDFILE_DIR, os.path.basename(loopdev))
1015 if os.path.exists(pidfile):
1017 with open(pidfile, 'w') as wf:
1018 wf.write(str(os.getpid()))
1020 except MountError, err:
1021 raise CreatorError("%s" % str(err))
1026 fcntl.flock(fp, fcntl.LOCK_UN)
1033 def clean_loop_devices(piddir=DEVICE_PIDFILE_DIR):
1034 if not os.path.exists(piddir) or not os.path.isdir(piddir):
1037 for loopdev in os.listdir(piddir):
1038 pidfile = os.path.join(piddir, loopdev)
1040 with open(pidfile, 'r') as rf:
1041 devpid = int(rf.read())
1045 # if the process using this device is alive, skip it
1046 if not devpid or os.path.exists(os.path.join('/proc', str(devpid))):
1049 # try to clean it up
1051 devinst = LoopDevice()
1052 devinst.register(os.path.join('/dev', loopdev))