Revision b98bb41e tools/lvmstrap

b/tools/lvmstrap
44 44
import sys
45 45
import optparse
46 46
import time
47
import errno
47 48

  
48 49
from ganeti.utils import RunCmd, ReadFile
49 50
from ganeti import constants
......
425 426

  
426 427
    dev = ReadDev("/sys/block/%s" % name)
427 428
    CheckSysDev(name, dev)
428
    inuse = not CheckReread(name)
429
    inuse = InUse(name)
429 430
    # Enumerate partitions of the block device
430 431
    partitions = []
431 432
    for partname in os.listdir("/sys/block/%s" % name):
......
561 562
    print line
562 563

  
563 564

  
565
def CheckSysfsHolders(name):
566
  """Check to see if a device is 'hold' at sysfs level.
567

  
568
  This is usually the case for Physical Volumes under LVM.
569

  
570
  @rtype: boolean
571
  @return: true if the device is available according to sysfs
572

  
573
  """
574
  try:
575
    contents = os.listdir("/sys/block/%s/holders/" % name)
576
  except OSError, err:
577
    if err.errno == errno.ENOENT:
578
      contents = []
579
    else:
580
      raise
581
  return not bool(contents)
582

  
583

  
564 584
def CheckReread(name):
565 585
  """Check to see if a block device is in use.
566 586

  
......
593 613
    return result.failed
594 614

  
595 615

  
616
def InUse(name):
617
  """Returns if a disk is in use or not.
618

  
619
  """
620
  return not (CheckSysfsHolders(name) and CheckReread(name))
621

  
622

  
596 623
def WipeDisk(name):
597 624
  """Wipes a block device.
598 625

  
......
604 631

  
605 632
  """
606 633

  
607
  if not CheckReread(name):
634
  if InUse(name):
608 635
    raise OperationalError("CRITICAL: disk %s you selected seems to be in"
609 636
                           " use. ABORTING!" % name)
610 637

  
......
624 651
                           " %d. I don't know how to cleanup. Sorry." %
625 652
                           (name, bytes_written))
626 653

  
627
  if not CheckReread(name):
654
  if InUse(name):
655
    # try to restore the data
628 656
    fd = os.open("/dev/%s" % name, os.O_RDWR | os.O_SYNC)
629 657
    os.write(fd, olddata)
630 658
    os.close(fd)

Also available in: Unified diff