Advertisement
TShiva

удаление всей хуйни

Mar 31st, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. import re
  2. import subprocess
  3. import os
  4. '''
  5. Очистка системы от таргетов,лунов,прикрученных к ним устройств, lvm-ов,raid-ов и bcache
  6. '''
  7. def main():
  8.     clear_scst()
  9.     clear_bcache_backing_device()
  10.     clear_lvm()
  11.     clear_raids()
  12.     clear_bcache_caching_device()
  13.  
  14. def clear_raids():
  15.     raids = str(subprocess.check_output('cat /proc/mdstat', shell=True))
  16.     cock = raids.find('md')
  17.     my_raids = raids[cock:]
  18.     new_raids = my_raids.split('[U' or '[_')
  19.     for string in new_raids:
  20.         md = re.findall(r'(md\d+)', string)
  21.         os.system('mdadm -S /dev/' + ''.join(md))
  22.     raid = re.findall(r'(\w+\[\d+\])', my_raids)
  23.     names = re.findall(r'[a-zA-Z]+', ''.join(raid))
  24.     for name in names:
  25.         sname = ''.join(name)
  26.         os.system('mdadm --zero-superblock /dev/' + sname)
  27. def clear_lvm():
  28.  
  29.     lvms = str(subprocess.check_output('pvdisplay', shell=True))
  30.     new_lvms = lvms.split('--- Physical volume ---')
  31.     for lvm in new_lvms:
  32.         #pv_name = re.findall(r'/\w+/\w+',lvm) # заготовка для парсинга лунов
  33.         VG_name=lvm.find('VG Name')
  34.         PV_size = lvm.find('PV Size')
  35.         my_lvm = lvm[VG_name:PV_size]
  36.         lvm_name=re.findall(r'(\w+\\n)', ''.join(my_lvm))
  37.         slvm = ''.join(lvm_name).strip('\\n')
  38.         os.system('lvremove '+slvm)
  39.         os.system('vgremove '+slvm)
  40.  
  41. def clear_scst():
  42.     #targets = str(subprocess.check_output('scstadmin -list_target', shell=True))
  43.     #targets = 'Driver       Target   --------------------------------------  iscsi        iqn.2015-05.iscsi.lan:tgt  copy_manager copy_manager_tgt         '
  44.     #target_name = re.findall(r'iqn\.\d+\-\d+\.(\w+.)*\:(\w+|\d+|\.|\-)+',targets) # заготовка для парсинга имён таргетов
  45.     os.system('service scst restart')
  46.  
  47. def clear_bcache_backing_device():
  48.     all_devices_for_parse = str(subprocess.check_output('lsblk', shell=True))
  49.     backing_devices = set(re.findall(r'bcache\d+', all_devices_for_parse))
  50.     for backing_device in backing_devices:
  51.         os.system('echo 1 > /sys/block/'+backing_device+'/bcache/stop')
  52.     lvms = set(re.findall(r'\w+-lv\d+', all_devices_for_parse))
  53.  
  54.     for lvm in lvms:
  55.         new_lvm = lvm.replace('-', '/')
  56.         os.system('wipefs -a /dev/'+new_lvm)
  57.  
  58.  
  59. def clear_bcache_caching_device():
  60.     all_devices_for_parse = str(subprocess.check_output('lsblk', shell=True))
  61.     block_names=['sda', 'sdb', 'sdc', 'sdd', 'vdd', 'vdc', 'vdb']
  62.     for block_name in block_names:
  63.         print(block_name)
  64.         csetuuid = str(subprocess.check_output('bcache-super-show /dev/'+block_name+' | grep cset.uuid | cut -c 12-48', shell=True))[2:-3]
  65.         print(csetuuid)
  66.         os.system('echo 1 > /sys/fs/bcache/'+csetuuid+'/stop')
  67.         os.system('wipefs -a /dev/'+block_name)
  68. if __name__ == '__main__':
  69.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement