From: Anders Kaseorg Date: Mon, 5 May 2008 00:45:44 +0000 (-0400) Subject: losetup -f finds an unused loop device. X-Git-Tag: sipb-xen-dom0/2.7~7 X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-xen-config.git/commitdiff_plain/1e7fcd0c0b076ab8d0dac4ce97f59f4749233eb0?hp=526abfbb28d335fa9aeee371ccdc3d5b16161844 losetup -f finds an unused loop device. svn path=/trunk/packages/sipb-xen-dom0/; revision=500 --- diff --git a/files/usr/sbin/sipb-xen-losetup b/files/usr/sbin/sipb-xen-losetup index 5776248..b876187 100755 --- a/files/usr/sbin/sipb-xen-losetup +++ b/files/usr/sbin/sipb-xen-losetup @@ -2,25 +2,17 @@ import sys import os -from subprocess import call +from subprocess import call, Popen, PIPE def losetup(source, offset=0): - # XXX we avoid colliding with other instances of ourself, - # but when it comes to other loop-device users we just - # pick a range things don't seem to use and hope... lockfilename = '/tmp/losetup.lock' os.close(os.open(lockfilename, os.O_CREAT+os.O_EXCL)) #lock try: - loopdevice = None - for i in xrange(32,60): # totally arbitrary, just looks to be unused on black-mesa - filename = '/dev/loop%d'%i - if 0 == len(file(filename).read(1)): - loopdevice = filename # it's empty - break - if loopdevice is not None: - call(['losetup', '-o', str(offset), loopdevice, source]) - else: + loopdevice = Popen(['losetup', '-f'], stdout=PIPE).communicate()[0].rstrip() + if loopdevice == '': raise RuntimeError('out of loop devices for copying VM image: too many at once?') + if call(['losetup', '-o', str(offset), loopdevice, source]) != 0: + raise RuntimeError('losetup failed') finally: os.unlink(lockfilename) #unlock return loopdevice