Revision 42143c5b

b/tools/lvmstrap
54 54

  
55 55
USAGE = ("\tlvmstrap diskinfo\n"
56 56
         "\tlvmstrap [--vg-name=NAME] [--allow-removable]"
57
         " { --alldisks | --disks DISKLIST }"
57
         " { --alldisks | --disks DISKLIST } [--use-sfdisk]"
58 58
         " create")
59 59

  
60 60
verbose_flag = False
......
85 85

  
86 86
#: Minimum partition size to be considered (1 GB)
87 87
PART_MINSIZE = 1024 * 1024 * 1024
88

  
88
MBR_MAX_SIZE = 2 * 10**12
89 89

  
90 90
class Error(Exception):
91 91
  """Generic exception"""
......
188 188
  parser.add_option("-g", "--vg-name", type="string",
189 189
                    dest="vgname", default="xenvg", metavar="NAME",
190 190
                    help="the volume group to be created [default: xenvg]")
191
  parser.add_option("--use-sfdisk", dest="use_sfdisk",
192
                    action="store_true", default=False,
193
                    help="use sfdisk instead of parted")
191 194

  
192 195

  
193 196
  options, args = parser.parse_args()
......
730 733
                           name)
731 734

  
732 735

  
733
def PartitionDisk(name):
736
def PartitionDisk(name, use_sfdisk):
734 737
  """Partitions a disk.
735 738

  
736 739
  This function creates a single partition spanning the entire disk,
......
739 742
  @param name: the device name, e.g. sda
740 743

  
741 744
  """
742
  result = ExecCommand(
743
    'echo ,,8e, | sfdisk /dev/%s' % name)
745

  
746
  # Check that parted exists
747
  result = ExecCommand("parted --help")
744 748
  if result.failed:
745
    raise OperationalError("CRITICAL: disk %s which I have just partitioned"
746
                           " cannot reread its partition table, or there"
747
                           " is some other sfdisk error. Likely, it is in"
748
                           " use. You have to clean this yourself. Error"
749
                           " message from sfdisk: %s" %
750
                           (name, result.output))
749
    use_sfdisk = True
750
    print >> sys.stderr, ("Unable to execute \"parted --help\","
751
                          " falling back to sfdisk.")
752

  
753
  # Check disk size - over 2TB means we need to use GPT
754
  size = ReadSize("/sys/block/%s" % name)
755
  if size > MBR_MAX_SIZE:
756
    label_type = "gpt"
757
    if use_sfdisk:
758
      raise OperationalError("Critical: Disk larger than 2TB detected, but"
759
                             " parted is either not installed or --use-sfdisk"
760
                             " has been specified")
761
  else:
762
    label_type = "msdos"
763

  
764
  if use_sfdisk:
765
    result = ExecCommand(
766
        "echo ,,8e, | sfdisk /dev/%s" % name)
767
    if result.failed:
768
      raise OperationalError("CRITICAL: disk %s which I have just partitioned"
769
                             " cannot reread its partition table, or there"
770
                             " is some other sfdisk error. Likely, it is in"
771
                             " use. You have to clean this yourself. Error"
772
                             " message from sfdisk: %s" %
773
                             (name, result.output))
774

  
775
  else:
776
    result = ExecCommand("parted -s /dev/%s mklabel %s" % (name, label_type))
777
    if result.failed:
778
      raise OperationalError("Critical: failed to create %s label on %s" %
779
                             (label_type,name))
780
    result = ExecCommand("parted -s /dev/%s mkpart pri ext2 1 100%%" % name)
781
    if result.failed:
782
      raise OperationalError("Critical: failed to create partition on %s" %
783
                             name)
784
    result = ExecCommand("parted -s /dev/%s set 1 lvm on" % name)
785
    if result.failed:
786
      raise OperationalError("Critical: failed to set partition on %s to LVM" %
787
                             name)
751 788

  
752 789

  
753 790
def CreatePVOnDisk(name):
......
861 898
  for disk in disklist:
862 899
    WipeDisk(disk)
863 900
    if IsPartitioned(disk):
864
      PartitionDisk(disk)
901
      PartitionDisk(disk, options.use_sfdisk)
865 902
  for disk in disklist:
866 903
    CreatePVOnDisk(disk)
867 904
  CreateVG(vgname, disklist)

Also available in: Unified diff