1 #!/usr/bin/env python2.5
5 from subprocess import call, Popen, PIPE
7 def losetup(source, offset=0):
8 lockfilename = '/tmp/losetup.lock'
9 os.close(os.open(lockfilename, os.O_CREAT+os.O_EXCL)) #lock
11 loopdevice = Popen(['losetup', '-f'], stdout=PIPE).communicate()[0].rstrip()
13 raise RuntimeError('out of loop devices for copying VM image: too many at once?')
14 if call(['losetup', '-o', str(offset), loopdevice, source]) != 0:
15 raise RuntimeError('losetup failed')
17 os.unlink(lockfilename) #unlock
22 os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
23 if not (1 <= len(args) <= 2):
24 print >>sys.stderr, 'usage: %s sourcedevice [offset]' % argv[0]
25 print >>sys.stderr, 'prints resulting loopback device; don\'t forget to losetup -d'
30 if __name__ == '__main__':
31 sys.exit(main(*sys.argv))