[关闭]
@tony-yin 2017-09-29T14:19:09.000000Z 字数 1845 阅读 908

清空分区和创建分区脚本

Magicloud Script


清空分区

脚本名称

clean_disks.py

使用说明

使用范例

  1. python clean_disks.py
  1. python clean_disks.py sdb
  1. python clean_disks.py sdb sdc

脚本代码

  1. import sys
  2. from mcs3.command import do_cmd
  3. def clean_disk(disk_name):
  4. print 'disk: {} clean start ...'.format(disk_name)
  5. do_cmd('sgdisk -Zog /dev/{}'.format(disk_name))
  6. print 'disk: {} clean done ...'.format(disk_name)
  7. mount_info = do_cmd('mount')
  8. sys_disk_name = mount_info[5:8]
  9. if len(sys.argv) > 1:
  10. disks = sys.argv[1:]
  11. for disk_name in disks:
  12. if disk_name == sys_disk_name:
  13. print '{} is system disk, can\'t be clean!'.format(disk_name)
  14. else:
  15. clean_disk(disk_name)
  16. else:
  17. all_disks = do_cmd('lsblk').splitlines()
  18. for disk in all_disks:
  19. if (disk.startswith('sd')):
  20. disk_name = disk.split()[0]
  21. if disk_name != sys_disk_name:
  22. clean_disk(disk_name)

创建分区

脚本名称

create_partitions.py

使用说明

使用范例

  1. python create_partitions.py 100G 2 sdb sdc
  1. python create_partitions.py 10G 3
  1. python create_partitions.py 1024G 4 sdc

脚本代码

  1. import sys
  2. from mcs3.command import do_cmd
  3. size = sys.argv[1]
  4. num = sys.argv[2]
  5. mount_info = do_cmd('mount')
  6. sys_disk_name = mount_info[5:8]
  7. def parted_disks(num, size, disk):
  8. for i in range(int(num)):
  9. do_cmd('sgdisk -n {}:0:+{} /dev/{}'.format(i+1, size, disk))
  10. print 'disk {} partition {} done ...'.format(disk, i+1)
  11. if len(sys.argv) > 3:
  12. disks = sys.argv[3:]
  13. for disk in disks:
  14. if disk == sys_disk_name:
  15. print '{} is system disk, can\'t be parted!'.format(disk)
  16. else:
  17. parted_disks(num, size, disk)
  18. else:
  19. all_disks = do_cmd('lsblk').splitlines()
  20. for disk in all_disks:
  21. if (disk.startswith('sd')):
  22. disk_name = disk.split()[0]
  23. if disk_name != sys_disk_name:
  24. parted_disks(num, size, disk)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注