source: trunk/packages/sipb-xen-dom0/files/usr/sbin/sipb-xen-losetup @ 771

Last change on this file since 771 was 500, checked in by andersk, 17 years ago

losetup -f finds an unused loop device.

  • Property svn:executable set to *
File size: 982 bytes
Line 
1#!/usr/bin/env python2.5
2
3import sys
4import os
5from subprocess import call, Popen, PIPE
6
7def losetup(source, offset=0):
8  lockfilename = '/tmp/losetup.lock'
9  os.close(os.open(lockfilename, os.O_CREAT+os.O_EXCL)) #lock
10  try:
11    loopdevice = Popen(['losetup', '-f'], stdout=PIPE).communicate()[0].rstrip()
12    if loopdevice == '':
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')
16  finally:
17    os.unlink(lockfilename) #unlock
18  return loopdevice
19
20def main(*argv):
21  args = argv[1:]
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'
26    return 2
27  print losetup(*args)
28  return 0
29
30if __name__ == '__main__':
31  sys.exit(main(*sys.argv))
Note: See TracBrowser for help on using the repository browser.