3 # node manager has a few working assumptions, like
4 # if a domain d does not exist, there is no /vservers/d
6 # this utility tries to detect and assess potentially
7 # conflictual situations, that could prevent nodemanager
8 # from recovering properly
10 # the logic is simply to find zombie-containers, i.e.
11 # VMs that do have a workdir in /vservers/<zombie>
12 # but that are not reported as running by virsh --list
13 # which suggests they have been improperly trashed
16 # then we trash them but for that some subdirs must be
17 # btrfs-subvolume-delete'd and not rm-rf'ed
23 from argparse import ArgumentParser
25 def running_domains():
33 names_string = subprocess.check_output(
35 universal_newlines = True,
36 stdin = subprocess.DEVNULL,
38 names = [ name for name in names_string.strip().split("\n") if name ]
41 def existing_vservers():
42 all_dirs = glob.glob("/vservers/*")
43 dirs = ( dir for dir in all_dirs if os.path.isdir(dir) )
44 dirnames = ( path.replace("/vservers/", "") for path in dirs)
47 def display_or_run_commands(commands, run):
50 print("========== You should run")
51 for command in commands:
52 print(" ".join(command))
54 for command in commands:
55 print("Running {}".format(" ".join(command)))
56 retcod = subprocess.call(command)
58 print("Warning: failed with retcod = {}".format(retcod))
61 parser = ArgumentParser()
62 # the default is to cowardly show commands to run
63 # use --run to actually do it
64 parser.add_argument("-r", "--run", action='store_true', default=False)
65 args = parser.parse_args()
67 running_containers = set(running_domains())
68 existing_containers = set(existing_vservers())
69 zombies_containers = existing_containers - running_containers
71 # the prefix used to locate subvolumes
78 # we need to call 'btrfs subvolume delete' on these remainings
79 # instead of just 'rm'
80 if zombies_containers:
82 zombie_dirs = ["/vservers/"+z for z in zombies_containers]
83 print("-------- Found {} existing, but not running, containers".format(len(zombies_containers)))
84 print("zombie_dirs='{}'".format(" ".join(zombie_dirs)))
86 for z in zombies_containers
87 for prefix in flavour_prefixes
88 for path in glob.glob("/vservers/{z}/{prefix}*".format(z=z, prefix=prefix))]
90 print("zombie_subvolumes='{}'".format(" ".join(subvolumes)))
91 for subvolume in subvolumes:
92 commands.append([ 'btrfs', 'subvolume', 'delete', subvolume])
93 for zombie_dir in zombie_dirs:
94 commands.append([ 'btrfs', 'subvolume', 'delete', zombie_dir ])
95 display_or_run_commands(commands, args.run)
96 # find the containers dirs that might still exist
97 zombie_dirs = [ path for path in zombie_dirs if os.path.isdir(path) ]
98 commands = [ ['rm', '-rf', path] for path in zombie_dirs ]
99 display_or_run_commands(commands, args.run)
101 #### should happen much less frequently
102 weirdos_containers = running_containers - existing_containers
103 if weirdos_containers:
104 print("-------- Found {} running but non existing".format(len(weirdos_containers)))
105 for w in weirdos_containers:
106 print("/vservers/{}".format(w))