clean-initrd-boot.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os
  2. import time
  3. KEEP = 2 # initrd's
  4. f_boot_id = "/etc/kernel/proxmox-boot-uuids"
  5. f = open(f_boot_id)
  6. _boot_ids = f.readlines()
  7. f.close()
  8. boot_ids = []
  9. for line in _boot_ids:
  10. line = line.strip()
  11. if line:
  12. boot_ids.append(line)
  13. print()
  14. print("BOOT-UUID")
  15. for line in boot_ids:
  16. print(" ",line)
  17. def lsblk():
  18. cmd ="lsblk -r -o +UUID"
  19. r=os.popen(cmd)
  20. lines = r.readlines()
  21. parts= []
  22. for line in lines:
  23. line=line.strip()
  24. if "zd" in line:
  25. continue
  26. ok=0
  27. for uuid in boot_ids:
  28. if uuid in line:
  29. ok=1
  30. line = line.split()
  31. parts.append(line[0])
  32. print(" ",line)
  33. return parts
  34. parts=lsblk()
  35. for i,p in enumerate(parts):
  36. print("")
  37. cmd = "mkdir -p /mnt/boot_{}".format(i)
  38. print("cmd:",cmd)
  39. os.system(cmd)
  40. print("")
  41. cmd = "mount /dev/{} /mnt/boot_{}".format(p,i)
  42. print("cmd:",cmd)
  43. os.system(cmd)
  44. print("")
  45. print("LS MOUNT")
  46. cmd = "df -h | grep mnt/boot_"
  47. #print("cmd:",cmd)
  48. r=os.popen(cmd)
  49. for l in r.readlines():
  50. l=l.strip()
  51. print(" ",l)
  52. print("")
  53. print("LS kernel")
  54. def clean():
  55. initrd=[]
  56. for i,p in enumerate(parts):
  57. ls = os.listdir("/mnt/boot_{}".format(i))
  58. ls.sort()
  59. j=0
  60. for l in ls:
  61. if "initrd.img-" not in l:
  62. continue
  63. print(" ",l)
  64. initrd.append(l)
  65. j+=1
  66. print()
  67. print("DELETE OLD INITRD")
  68. if len(initrd) > KEEP:
  69. l=initrd[0] # oldest
  70. cmd="mkdir -p /root/old_boot/"
  71. os.system(cmd)
  72. cmd="mv -v /mnt/boot_{}/{} /root/old_boot/".format(i,l)
  73. print(" ",cmd)
  74. os.system(cmd)
  75. z=1
  76. while 1:
  77. print("count:",z,"KEEP count=",KEEP)
  78. clean()
  79. time.sleep(1)
  80. z+=1