|
| 1 | +#!/usr/bin/python -u |
| 2 | + |
| 3 | + |
| 4 | +"""%prog [options] /dev/disk ... |
| 5 | +
|
| 6 | +%prog will partition, format, and mount each of the specified disks. |
| 7 | +
|
| 8 | +Examples: |
| 9 | + %prog /dev/sdb |
| 10 | + This will create a single partition on /dev/sdb, format it using ext4, |
| 11 | + and then mount it as /grid/0. |
| 12 | +
|
| 13 | + %prog /dev/sdb /dev/sdc |
| 14 | + Same as above but will repeat with /dev/sdc and mount it is /grid/1. |
| 15 | +
|
| 16 | + %prog -a -m sdb-c |
| 17 | + Automatically discover the disks that match the pattern /dev/sd[b-z], |
| 18 | + then partition, format, and mount them. |
| 19 | +
|
| 20 | + %prog -a -m sdb-c -t |
| 21 | + Automatically discover the disks that match the pattern /dev/sd[b-z]. |
| 22 | + Prints the list of disks but does not take any action to partition, |
| 23 | + format, or mount the disks. Use the -t option to ensure the list of |
| 24 | + disks is correct before preparing them. |
| 25 | +
|
| 26 | + %prog --help |
| 27 | + View all options. |
| 28 | +
|
| 29 | +WARNING! THIS WILL ERASE THE CONTENTS OF THESE DISKS!""" |
| 30 | + |
| 31 | +import os |
| 32 | +import glob |
| 33 | +import platform |
| 34 | +import multiprocessing |
| 35 | +import subprocess |
| 36 | +import optparse |
| 37 | +from time import sleep |
| 38 | + |
| 39 | +def get_disks_to_prepare(discover_method): |
| 40 | + disks = [] |
| 41 | + if discover_method == 'ecs': |
| 42 | + disk_list_commands = [ |
| 43 | + 'parted -m -s -l | grep /dev/.*:4001GB | cut -d : -f 1 | sort', |
| 44 | + 'parted -m -s -l | grep /dev/.*:6001GB | cut -d : -f 1 | sort', |
| 45 | + 'parted -m -s -l | grep /dev/.*:8002GB | cut -d : -f 1 | sort', |
| 46 | + ] |
| 47 | + disks = [subprocess.check_output(cmd, shell=True) for cmd in disk_list_commands] |
| 48 | + disks = ''.join(disks) |
| 49 | + disks = disks.rstrip('\n').split('\n') |
| 50 | + disks = [d for d in disks if d != ''] |
| 51 | + elif discover_method == 'sdb-z': |
| 52 | + disks = sorted(glob.glob('/dev/sd[b-z]')) |
| 53 | + elif discover_method == 'vdb-z': |
| 54 | + disks = sorted(glob.glob('/dev/vd[b-z]')) |
| 55 | + return disks |
| 56 | + |
| 57 | +def umount_disk(disk_info): |
| 58 | + disk = disk_info['disk'] |
| 59 | + disk_number = disk_info['disk_number'] |
| 60 | + print('%s: Umounting disk %d, %s' % (platform.node(), disk_number, disk)) |
| 61 | + os.system('umount %s1' % disk) |
| 62 | + |
| 63 | +def partition_disk(disk_info): |
| 64 | + disk = disk_info['disk'] |
| 65 | + disk_number = disk_info['disk_number'] |
| 66 | + print('%s: Partitioning disk %d, %s' % (platform.node(), disk_number, disk)) |
| 67 | + if True: |
| 68 | + assert os.system('parted -s %s mklabel GPT' % disk) == 0 |
| 69 | + os.system('parted -s %s rm 1' % disk) |
| 70 | + os.system('parted -s %s rm 2' % disk) |
| 71 | + # Must sleep to avoid errors updating OS partition table. |
| 72 | + sleep(0.2) |
| 73 | + assert os.system('parted -s -a cylinder %s -- mkpart primary ext4 1 -1' % disk) == 0 |
| 74 | + |
| 75 | +def format_disk(disk_info): |
| 76 | + disk = disk_info['disk'] |
| 77 | + disk_number = disk_info['disk_number'] |
| 78 | + mount = disk_info['mount'] |
| 79 | + print('%s: Formatting disk %d, %s, %s' % (platform.node(), disk_number, disk, mount)) |
| 80 | + if True: |
| 81 | + assert os.system('mkfs.ext4 -q -T largefile -m 0 %s1' % disk) == 0 |
| 82 | + |
| 83 | +def mount_disk(disk_info): |
| 84 | + disk = disk_info['disk'] |
| 85 | + disk_number = disk_info['disk_number'] |
| 86 | + mount = disk_info['mount'] |
| 87 | + print('%s: Mounting disk %d, %s, %s' % (platform.node(), disk_number, disk, mount)) |
| 88 | + assert os.system('mkdir -p /grid/%d' % disk_number) == 0 |
| 89 | + assert os.system('echo %s1\t%s\text4\tdefaults,noatime\t0\t0 >> /etc/fstab' % (disk, mount)) == 0 |
| 90 | + assert os.system('mount %s' % mount) == 0 |
| 91 | + |
| 92 | +def main(): |
| 93 | + parser = optparse.OptionParser(usage=__doc__) |
| 94 | + parser.add_option('-a', '--auto', action='store_true', dest='auto', |
| 95 | + help='automatically discover disks to prepare') |
| 96 | + parser.add_option('-m', '--discover-method', action='store', dest='discover_method', default='sdb-z', |
| 97 | + help='method to discover disks to prepare') |
| 98 | + parser.add_option('-x', '--exclude', action='append', dest='exclude', |
| 99 | + help='disk to always exclude (will not be prepared)') |
| 100 | + parser.add_option('-t', '--test', action='store_true', dest='test', |
| 101 | + help='show disks that will be prepared but do not prepare them') |
| 102 | + parser.add_option('-n', '--disk-count', action='store', dest='disk_count', type='int', |
| 103 | + help='ensure there are exactly this many disks to prepare') |
| 104 | + options, args = parser.parse_args() |
| 105 | + disks = args |
| 106 | + |
| 107 | + # Get list of disk partitions to format and mount. |
| 108 | + if options.auto: |
| 109 | + disks += get_disks_to_prepare(discover_method=options.discover_method) |
| 110 | + |
| 111 | + if options.exclude: |
| 112 | + disks = [d for d in disks if d not in options.exclude] |
| 113 | + |
| 114 | + disk_info = [{ |
| 115 | + 'disk_number': disk_number, # First disk will be 0 |
| 116 | + 'disk': disk, |
| 117 | + 'mount': '/grid/%d' % disk_number |
| 118 | + } for disk_number, disk in enumerate(disks)] |
| 119 | + |
| 120 | + print('The following %d disks will be erased:' % len(disks)) |
| 121 | + print(' ' + '\n '.join(disks)) |
| 122 | + |
| 123 | + if options.disk_count: |
| 124 | + assert len(disks) == options.disk_count |
| 125 | + |
| 126 | + if options.test: |
| 127 | + return 0 |
| 128 | + |
| 129 | + assert os.system('mkdir -p /grid') == 0 |
| 130 | + |
| 131 | + # Remove /grid mounts from fstab. |
| 132 | + assert os.system('egrep -v "/grid/" /etc/fstab > /tmp/fstab ; cp /tmp/fstab /etc/fstab') == 0 |
| 133 | + |
| 134 | + map(umount_disk, disk_info) |
| 135 | + map(partition_disk, disk_info) |
| 136 | + map(umount_disk, disk_info) |
| 137 | + |
| 138 | + # Format all disks in parallel. |
| 139 | + pool = multiprocessing.Pool(len(disks)) |
| 140 | + pool.map(format_disk, disk_info) |
| 141 | + print('Format done.') |
| 142 | + |
| 143 | + map(mount_disk, disk_info) |
| 144 | + |
| 145 | + assert os.system('mount | grep /grid/') == 0 |
| 146 | + |
| 147 | + print('%s: prepare_data_disks complete.' % platform.node()) |
| 148 | + |
| 149 | +if __name__ == '__main__': |
| 150 | + main() |
0 commit comments