Advertisement
gassanpavel

Untitled

Feb 11th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. def mdadm_completly_destroy_array(args):
  2. try:
  3. logger.info("{} - info: get lsblk info".format(args.name), extra={'evetn_id':'3500'})
  4. output = subprocess.run(['lsblk {} -J'.format(args.name)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  5. output.check_returncode()
  6. out = output.stdout.decode('UTF-8').strip()
  7. json_string = json.loads(out)
  8. blockdevices = json.dumps(json_string["blockdevices"])
  9. for blockdevice in json.loads(blockdevices):
  10. for children in blockdevice["children"]:
  11. children_name = children["name"]
  12. children_mountpoint = children["mountpoint"]
  13. except Exception as exc:
  14. exception_command = str(exc.args[1]).replace('\n','').strip()
  15. exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
  16. logger.error('{} - fail: get lsblk info. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3502'})
  17. exit()
  18.  
  19. ### remove entry from /etc/fstab
  20. try:
  21. logger.info("{} - info: try remove from fstab".format(children_mountpoint), extra={'evetn_id':'3510'})
  22. with open('/etc/fstab', 'r+') as f:
  23. lines = f.readlines()
  24. for index, line in enumerate(lines):
  25. if line.startswith("/dev/{}".format(children_name)):
  26. lines.remove(line)
  27. f.seek(0)
  28. f.write(''.join(lines))
  29. f.truncate()
  30. logger.info("{} - info: removed from fstab".format(children_mountpoint), extra={'evetn_id':'3511'})
  31. except Exception as exc:
  32. exception_command = str(exc.args[1]).replace('\n','').strip()
  33. exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
  34. logger.error('{} - fail: remove from fstab. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3512'})
  35. exit()
  36.  
  37. ### unmount
  38. try:
  39. logger.info("{} - info: try unmount".format(children_mountpoint), extra={'evetn_id':'3520'})
  40. if not children_mountpoint is None:
  41. output = subprocess.run(['umount {}'.format(children_mountpoint)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  42. output.check_returncode()
  43. logger.info("{} - info: unmount successfull".format(children_mountpoint), extra={'evetn_id':'3521'})
  44. except Exception as exc:
  45. exception_command = str(exc.args[1]).replace('\n','').strip()
  46. exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
  47. logger.error('{} - fail: remove from fstab. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3522'})
  48. exit()
  49.  
  50. ### stop md array and clear superblock
  51. try:
  52. logger.info("{} - info: try stop MD array".format(args.name), extra={'evetn_id':'3530'})
  53. mdjson = mdstat.parse()
  54. output = subprocess.run(['mdadm -S {}'.format(args.name)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  55. output.check_returncode()
  56. logger.info("{} - info: MD array stopped".format(args.name), extra={'evetn_id':'3531'})
  57. except Exception as exc:
  58. exception_command = str(exc.args[1]).replace('\n','').strip()
  59. exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
  60. logger.error('{} - fail: remove from fstab. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3532'})
  61. exit()
  62.  
  63. output = subprocess.run(['update-initramfs -k all -u'],
  64. shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  65. output.check_returncode()
  66. output = subprocess.run(['mdadm --detail --scan --verbose > /etc/mdadm/mdadm.conf'],
  67. shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  68. output.check_returncode()
  69.  
  70. for md in mdjson['devices']:
  71. for disk in mdjson['devices'][md]['disks']:
  72. time.sleep(5)
  73. try:
  74. logger.info("{} - info: try zero superblock".format(disk), extra={'evetn_id':'3540'})
  75. output = subprocess.run(['mdadm --zero-superblock /dev/{}'.format(disk)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  76. output.check_returncode()
  77. if output.stderr:
  78. raise Exception("mdadm: Couldn't open {} for write - not zeroing".format(disk))
  79. else:
  80. logger.info("{} - info: superblock zeroing success".format(disk), extra={'evetn_id':'3541'})
  81. except Exception as exc:
  82. exception_command = exc
  83. exception_message = exc.args
  84. logger.error('{} - fail: superblock zeroing. Error message [{}]. Error command {}'.format(disk, exception_message, exception_command), extra={'evetn_id':'3542'})
  85. exit()
  86.  
  87. try:
  88. logger.info("{} - info: try delete partition".format(disk), extra={'evetn_id':'3550'})
  89. output = subprocess.run(['sfdisk --delete /dev/{}'.format(disk.replace("1", ""))], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  90. output.check_returncode()
  91. logger.info("{} - info: partition deleted".format(disk), extra={'evetn_id':'3551'})
  92. except Exception as exc:
  93. exception_command = str(exc.args[1]).replace('\n','').strip()
  94. exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
  95. logger.error('{} - fail: delete partition. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3552'})
  96. exit()
  97.  
  98. try:
  99. logger.info("{} - info: try wipe fs".format(disk.replace("1", "")), extra={'evetn_id':'3000'})
  100. output = subprocess.run(['wipefs -a /dev/{}'.format(disk.replace("1", ""))], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  101. output.check_returncode()
  102. logger.info('{} - info: wipe fs successfull'.format(disk.replace("1", "")), extra={'evetn_id':'3001'})
  103. except Exception as exc:
  104. exception_command = str(exc.args[1]).replace('\n','').strip()
  105. exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
  106. logger.error('{} - fail: wipe fs. Error message [{}]. Error command {}'.format(disk.replace("1", ""), exception_message, exception_command), extra={'evetn_id':'3002'})
  107. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement