Gentoo Archives: gentoo-catalyst

From: Matt Turner <mattst88@g.o>
To: gentoo-catalyst@l.g.o
Cc: Matt Turner <mattst88@g.o>
Subject: [gentoo-catalyst] [PATCH 10/12] catalyst: Remove kill_support_pids()
Date: Thu, 29 Oct 2020 16:16:54
Message-Id: 20201029161632.146732-10-mattst88@gentoo.org
In Reply to: [gentoo-catalyst] [PATCH 01/12] catalyst: Replace pathcompare() by Matt Turner
1 mount_namespaces(7) says
2
3 A mount ceases to be a member of a peer group when either the
4 mount is explicitly unmounted, or when the mount is implicitly
5 unmounted because a mount namespace is removed (because it has
6 no more member processes).
7
8 Now that the build sequence is executed in its own mount namespace, the
9 mounts are implicitly unmounted when the last process in the namespace
10 dies, meaning we don't need to try any funny business around cleaning up
11 processes in order to unmount.
12
13 Signed-off-by: Matt Turner <mattst88@g.o>
14 ---
15 catalyst/base/stagebase.py | 30 ++------------
16 targets/support/kill-chroot-pids.sh | 62 -----------------------------
17 2 files changed, 4 insertions(+), 88 deletions(-)
18 delete mode 100755 targets/support/kill-chroot-pids.sh
19
20 diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
21 index ec9a8f06..5fc11eae 100644
22 --- a/catalyst/base/stagebase.py
23 +++ b/catalyst/base/stagebase.py
24 @@ -638,17 +638,6 @@ class StageBase(TargetBase, ClearBase, GenBase):
25 assert self.settings[verify] == "blake2"
26 self.settings.setdefault("gk_mainargs", []).append("--b2sum")
27
28 - def kill_chroot_pids(self):
29 - log.info('Checking for processes running in chroot and killing them.')
30 -
31 - # Force environment variables to be exported so script can see them
32 - self.setup_environment()
33 -
34 - killcmd = normpath(self.settings["sharedir"] +
35 - self.settings["shdir"] + "/support/kill-chroot-pids.sh")
36 - if os.path.exists(killcmd):
37 - cmd([killcmd], env=self.env)
38 -
39 def mount_safety_check(self):
40 """
41 Check and verify that none of our paths in mypath are mounted. We don't
42 @@ -920,18 +909,10 @@ class StageBase(TargetBase, ClearBase, GenBase):
43 try:
44 cxt = libmount.Context(target=str(target))
45 cxt.umount()
46 - except OSError:
47 - log.warning('First attempt to unmount failed: %s', target)
48 - log.warning('Killing any pids still running in the chroot')
49 -
50 - self.kill_chroot_pids()
51 -
52 - try:
53 - cxt.umount()
54 - except OSError as e:
55 - umount_failed = True
56 - log.warning("Couldn't umount: %s, %s", target,
57 - e.strerror)
58 + except OSError as e:
59 + log.warning("Couldn't umount: %s, %s", target,
60 + e.strerror)
61 + umount_failed = True
62
63 if umount_failed:
64 # if any bind mounts really failed, then we need to raise
65 @@ -1382,9 +1363,6 @@ class StageBase(TargetBase, ClearBase, GenBase):
66 def run(self):
67 self.chroot_lock.write_lock()
68
69 - # Kill any pids in the chroot
70 - self.kill_chroot_pids()
71 -
72 # Check for mounts right away and abort if we cannot unmount them
73 self.mount_safety_check()
74
75 diff --git a/targets/support/kill-chroot-pids.sh b/targets/support/kill-chroot-pids.sh
76 deleted file mode 100755
77 index ea8ee402..00000000
78 --- a/targets/support/kill-chroot-pids.sh
79 +++ /dev/null
80 @@ -1,62 +0,0 @@
81 -#!/bin/bash
82 -# Script to kill processes found running in the chroot.
83 -
84 -if [ "${clst_chroot_path}" == "/" ]
85 -then
86 - echo "Aborting .... clst_chroot_path is set to /"
87 - echo "This is very dangerous"
88 - exit 1
89 -fi
90 -
91 -if [ "${clst_chroot_path}" == "" ]
92 -then
93 - echo "Aborting .... clst_chroot_path is NOT set"
94 - echo "This is very dangerous"
95 - exit 1
96 -fi
97 -
98 -j=0
99 -declare -a pids
100 -# Get files and dirs in /proc
101 -for i in `ls /proc`
102 -do
103 - # Test for directories
104 - if [ -d /proc/$i ]
105 - then
106 - # Search for exe containing string inside ${clst_chroot_path}
107 - ls -la --color=never /proc/$i 2>&1 |grep exe|grep ${clst_chroot_path} > /dev/null
108 -
109 - # If found
110 - if [ $? == 0 ]
111 - then
112 - # Assign the pid into the pids array
113 - pids[$j]=$i
114 - j=$(($j+1))
115 - fi
116 - fi
117 -done
118 -
119 -if [ ${j} -gt 0 ]
120 -then
121 - echo
122 - echo "Killing process(es)"
123 - echo "pid: process name"
124 - for pid in ${pids[@]}
125 - do
126 - P_NAME=$(ls -la --color=never /proc/${pid} 2>&1 |grep exe|grep ${clst_chroot_path}|awk '{print $11}')
127 - echo ${pid}: ${P_NAME}
128 - done
129 - echo
130 - echo "Press Ctrl-C within 10 seconds to abort"
131 -
132 - sleep 10
133 -
134 - for pid in ${pids[@]}
135 - do
136 - kill -9 ${pid}
137 - done
138 -
139 - # Small sleep here to give the process(es) a chance to die before running unbind again.
140 - sleep 5
141 -
142 -fi
143 --
144 2.26.2