Browse Source

add: clean-old system during "apt upgrade"

micha 1 day ago
parent
commit
6348c7cafb
1 changed files with 94 additions and 0 deletions
  1. 94 0
      tool/clean-initrd-boot.py

+ 94 - 0
tool/clean-initrd-boot.py

@@ -0,0 +1,94 @@
+import os
+import time
+
+KEEP = 2 # initrd's
+
+f_boot_id = "/etc/kernel/proxmox-boot-uuids"
+f = open(f_boot_id)
+_boot_ids = f.readlines()
+f.close()
+
+
+boot_ids = []
+for line in _boot_ids:
+    line = line.strip()
+    if line:
+        boot_ids.append(line)
+
+print()
+print("BOOT-UUID")
+for line in boot_ids:
+    print(" ",line)
+
+def lsblk():
+    cmd ="lsblk -r -o +UUID"
+    r=os.popen(cmd)
+    lines = r.readlines()
+    parts= []
+    for line in lines:
+        line=line.strip()
+        if "zd" in line:
+            continue 
+        ok=0
+        for uuid in boot_ids:
+            if uuid in line:
+                ok=1 
+                line = line.split()
+                parts.append(line[0])
+                print(" ",line)
+
+    return parts
+
+parts=lsblk()
+for i,p in enumerate(parts):
+    print("")
+    cmd = "mkdir -p /mnt/boot_{}".format(i)
+    print("cmd:",cmd)
+    os.system(cmd)
+
+    print("")
+    cmd = "mount /dev/{} /mnt/boot_{}".format(p,i)
+    print("cmd:",cmd)
+    os.system(cmd)
+
+print("")
+print("LS MOUNT")
+cmd = "df -h | grep mnt/boot_"
+#print("cmd:",cmd)
+r=os.popen(cmd)
+for l in r.readlines():
+    l=l.strip()
+    print(" ",l)
+    
+print("")
+print("LS kernel")
+
+def clean():
+    initrd=[]
+    for i,p in enumerate(parts): 
+        ls = os.listdir("/mnt/boot_{}".format(i))
+        ls.sort()
+        j=0
+        for l in ls:
+            if "initrd.img-" not in l:
+                continue
+            print(" ",l)
+            initrd.append(l)
+            j+=1
+        
+        print()
+        print("DELETE OLD INITRD")
+        if len(initrd) > KEEP:
+            l=initrd[0] # oldest
+            cmd="mkdir -p /root/old_boot/"
+            os.system(cmd)
+            cmd="mv -v /mnt/boot_{}/{} /root/old_boot/".format(i,l)
+            print("  ",cmd)
+            os.system(cmd)
+
+z=1
+while 1:
+    print("count:",z,"KEEP count=",KEEP)
+    clean()
+    time.sleep(1)
+    z+=1