Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def mdadm_completly_destroy_array(args):
- try:
- logger.info("{} - info: get lsblk info".format(args.name), extra={'evetn_id':'3500'})
- output = subprocess.run(['lsblk {} -J'.format(args.name)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- out = output.stdout.decode('UTF-8').strip()
- json_string = json.loads(out)
- blockdevices = json.dumps(json_string["blockdevices"])
- for blockdevice in json.loads(blockdevices):
- for children in blockdevice["children"]:
- children_name = children["name"]
- children_mountpoint = children["mountpoint"]
- except Exception as exc:
- exception_command = str(exc.args[1]).replace('\n','').strip()
- exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
- logger.error('{} - fail: get lsblk info. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3502'})
- exit()
- ### remove entry from /etc/fstab
- try:
- logger.info("{} - info: try remove from fstab".format(children_mountpoint), extra={'evetn_id':'3510'})
- with open('/etc/fstab', 'r+') as f:
- lines = f.readlines()
- for index, line in enumerate(lines):
- if line.startswith("/dev/{}".format(children_name)):
- lines.remove(line)
- f.seek(0)
- f.write(''.join(lines))
- f.truncate()
- logger.info("{} - info: removed from fstab".format(children_mountpoint), extra={'evetn_id':'3511'})
- except Exception as exc:
- exception_command = str(exc.args[1]).replace('\n','').strip()
- exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
- logger.error('{} - fail: remove from fstab. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3512'})
- exit()
- ### unmount
- try:
- logger.info("{} - info: try unmount".format(children_mountpoint), extra={'evetn_id':'3520'})
- if not children_mountpoint is None:
- output = subprocess.run(['umount {}'.format(children_mountpoint)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- logger.info("{} - info: unmount successfull".format(children_mountpoint), extra={'evetn_id':'3521'})
- except Exception as exc:
- exception_command = str(exc.args[1]).replace('\n','').strip()
- exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
- logger.error('{} - fail: remove from fstab. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3522'})
- exit()
- ### stop md array and clear superblock
- try:
- logger.info("{} - info: try stop MD array".format(args.name), extra={'evetn_id':'3530'})
- mdjson = mdstat.parse()
- output = subprocess.run(['mdadm -S {}'.format(args.name)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- logger.info("{} - info: MD array stopped".format(args.name), extra={'evetn_id':'3531'})
- except Exception as exc:
- exception_command = str(exc.args[1]).replace('\n','').strip()
- exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
- logger.error('{} - fail: remove from fstab. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3532'})
- exit()
- output = subprocess.run(['update-initramfs -k all -u'],
- shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- output = subprocess.run(['mdadm --detail --scan --verbose > /etc/mdadm/mdadm.conf'],
- shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- for md in mdjson['devices']:
- for disk in mdjson['devices'][md]['disks']:
- time.sleep(5)
- try:
- logger.info("{} - info: try zero superblock".format(disk), extra={'evetn_id':'3540'})
- output = subprocess.run(['mdadm --zero-superblock /dev/{}'.format(disk)], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- if output.stderr:
- raise Exception("mdadm: Couldn't open {} for write - not zeroing".format(disk))
- else:
- logger.info("{} - info: superblock zeroing success".format(disk), extra={'evetn_id':'3541'})
- except Exception as exc:
- exception_command = exc
- exception_message = exc.args
- logger.error('{} - fail: superblock zeroing. Error message [{}]. Error command {}'.format(disk, exception_message, exception_command), extra={'evetn_id':'3542'})
- exit()
- try:
- logger.info("{} - info: try delete partition".format(disk), extra={'evetn_id':'3550'})
- output = subprocess.run(['sfdisk --delete /dev/{}'.format(disk.replace("1", ""))], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- logger.info("{} - info: partition deleted".format(disk), extra={'evetn_id':'3551'})
- except Exception as exc:
- exception_command = str(exc.args[1]).replace('\n','').strip()
- exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
- logger.error('{} - fail: delete partition. Error message [{}]. Error command {}'.format(args.name, exception_message, exception_command), extra={'evetn_id':'3552'})
- exit()
- try:
- logger.info("{} - info: try wipe fs".format(disk.replace("1", "")), extra={'evetn_id':'3000'})
- output = subprocess.run(['wipefs -a /dev/{}'.format(disk.replace("1", ""))], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- output.check_returncode()
- logger.info('{} - info: wipe fs successfull'.format(disk.replace("1", "")), extra={'evetn_id':'3001'})
- except Exception as exc:
- exception_command = str(exc.args[1]).replace('\n','').strip()
- exception_message = exc.args[3].decode('UTF-8').replace('\n','').strip()
- logger.error('{} - fail: wipe fs. Error message [{}]. Error command {}'.format(disk.replace("1", ""), exception_message, exception_command), extra={'evetn_id':'3002'})
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement