Changeset 500 for trunk


Ignore:
Timestamp:
May 4, 2008, 8:45:44 PM (17 years ago)
Author:
andersk
Message:

losetup -f finds an unused loop device.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-dom0/files/usr/sbin/sipb-xen-losetup

    r449 r500  
    33import sys
    44import os
    5 from subprocess import call
     5from subprocess import call, Popen, PIPE
    66
    77def losetup(source, offset=0):
    8   # XXX we avoid colliding with other instances of ourself,
    9   #     but when it comes to other loop-device users we just
    10   #     pick a range things don't seem to use and hope...
    118  lockfilename = '/tmp/losetup.lock'
    129  os.close(os.open(lockfilename, os.O_CREAT+os.O_EXCL)) #lock
    1310  try:
    14     loopdevice = None
    15     for i in xrange(32,60): # totally arbitrary, just looks to be unused on black-mesa
    16       filename = '/dev/loop%d'%i
    17       if 0 == len(file(filename).read(1)):
    18         loopdevice = filename # it's empty
    19         break
    20     if loopdevice is not None:
    21       call(['losetup', '-o', str(offset), loopdevice, source])
    22     else:
     11    loopdevice = Popen(['losetup', '-f'], stdout=PIPE).communicate()[0].rstrip()
     12    if loopdevice == '':
    2313      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')
    2416  finally:
    2517    os.unlink(lockfilename) #unlock
Note: See TracChangeset for help on using the changeset viewer.