Debian Codegraph @

libexec/container/remove

raw symbols SCIP
1 #!/bin/sh
2
3 # Copyright (C) 2014-2026 Daniel Baumann <[email protected]>
4 #
5 # SPDX-License-Identifier: GPL-3.0+
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 set -e
21
22 PROJECT="open-infrastructure"
23 SOFTWARE="compute-tools"
24 PROGRAM="container"
25 COMMAND="$(basename ${0})"
26
27 CONFIG="/etc/${SOFTWARE}/config"
28 HOOKS="/etc/${SOFTWARE}/hooks"
29 MACHINES="/var/lib/machines"
30
31 Parameters ()
32 {
33 OPTIONS_ALL=""
34
35 GETOPT_LONGOPTIONS="name:,allow-stop,force,verbose,"
36 GETOPT_OPTIONS="n:,f,v,"
37
38 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
39
40 if [ "${?}" != "0" ]
41 then
42 echo "'${COMMAND}': getopt exit" >&2
43 exit 1
44 fi
45
46 eval set -- "${PARAMETERS}"
47
48 while true
49 do
50 case "${1}" in
51 -n|--name)
52 NAME="${2}"
53 shift 2
54 ;;
55
56 --allow-stop)
57 ALLOW_STOP="true"
58 shift 1
59
60 OPTIONS_ALL="${OPTIONS_ALL} --allow-stop"
61 ;;
62
63 -f|--force)
64 FORCE="true"
65 shift 1
66
67 OPTIONS_ALL="${OPTIONS_ALL} --force"
68 ;;
69
70 -v|--verbose)
71 VERBOSE="true"
72 shift 1
73
74 OPTIONS_ALL="${OPTIONS_ALL} --verbose"
75 ;;
76
77 --)
78 shift 1
79 break
80 ;;
81
82 *)
83 echo "'${COMMAND}': getopt error" >&2
84 exit 1
85 ;;
86 esac
87 done
88 }
89
90 Usage ()
91 {
92 echo "Usage: ${PROGRAM} ${COMMAND} -n|--name NAME [--allow-stop] [-f|--force] [-v|--verbose]" >&2
93 echo
94 echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
95
96 exit 1
97 }
98
99 Rmdir ()
100 {
101 DIRECTORIES="${@}"
102
103 for DIRECTORY in ${DIRECTORIES}
104 do
105 PARENT_DIRECTORY="/$(echo ${DIRECTORY} | cut -d / -f 2)"
106
107 if [ "${PARENT_DIRECTORY}" != "${DIRECTORY}" ]
108 then
109 # the directory is at least two levels down from /
110 cd "${PARENT_DIRECTORY}"
111
112 CRUFT="$(echo ${DIRECTORY} | cut -d / -f 3-)"
113 rmdir --ignore-fail-on-non-empty --parents "${CRUFT}" > /dev/null 2>&1 || true
114
115 cd "${OLDPWD}"
116 fi
117 done
118 }
119
120 Parameters "${@}"
121
122 if [ -z "${NAME}" ]
123 then
124 Usage
125 fi
126
127 case "${NAME}" in
128 ALL)
129 NAMES="$(${PROGRAM} list --format shell --stopped)"
130
131 for NAME in ${NAMES}
132 do
133 ${PROGRAM} remove --name ${NAME} ${OPTIONS_ALL} || true
134 done
135
136 exit 0
137 ;;
138 esac
139
140 if [ ! -e "${MACHINES}/${NAME}" ] && [ ! -e "${CONFIG}/${NAME}.conf" ]
141 then
142 echo "'${NAME}': no such container" >&2
143 exit 1
144 fi
145
146 STATE="$(machinectl show ${NAME} 2>&1 | awk -FState= '/^State=/ { print $2 }')"
147
148 case "${STATE}" in
149 running)
150 case "${ALLOW_STOP}" in
151 true)
152 echo "'${NAME}': container is started, stopping it now" >&2
153 ${PROGRAM} stop -n ${NAME} -f
154 ;;
155
156 *)
157 echo "'${NAME}': container is started" >&2
158 exit 1
159 ;;
160 esac
161 ;;
162 esac
163
164 case "${FORCE}" in
165 true)
166 ;;
167
168 *)
169 echo -n "'${NAME}': remove container '${NAME}' [y|N]? "
170 read FORCE
171
172 FORCE="$(echo ${FORCE} | tr '[A-Z]' '[a-z]')"
173
174 case "${FORCE}" in
175 y|yes)
176 ;;
177
178 *)
179 exit 1
180 ;;
181 esac
182 ;;
183 esac
184
185 case "${VERBOSE}" in
186 true)
187 RM_OPTIONS="--verbose"
188 ;;
189
190 *)
191 RM_OPTIONS=""
192 ;;
193 esac
194
195 # Pre hooks
196 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}"
197 do
198 if [ -x "${FILE}" ]
199 then
200 "${FILE}"
201 fi
202 done
203
204 # data
205 if [ -e "${CONFIG}/${NAME}.conf" ]
206 then
207 # Removing rw bind mounts
208 BIND="$(awk -Fbind= '/^bind=/ { print $2 }' ${CONFIG}/${NAME}.conf)"
209
210 if [ -n "${BIND}" ]
211 then
212 BINDS="$(echo ${BIND} | sed -e 's|;| |g')"
213
214 for BIND in ${BINDS}
215 do
216 DIRECTORY="$(echo ${BIND} | awk -F: '{ print $1 }')"
217
218 Rmdir "${DIRECTORY}"
219 done
220 fi
221
222 # Removing ro bind mounts
223 BIND_RO="$(awk -Fbind-ro= '/^bind-ro=/ { print $2 }' ${CONFIG}/${NAME}.conf)"
224
225 if [ -n "${BIND_RO}" ]
226 then
227 BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g')"
228
229 for BIND_RO in ${BINDS_RO}
230 do
231 DIRECTORY="$(echo ${BIND_RO} | awk -F: '{ print $1 }')"
232
233 Rmdir "${DIRECTORY}"
234 done
235 fi
236 fi
237
238 # Run
239 case "${VERBOSE}" in
240 true)
241 echo -n "Removing container ${NAME}..."
242 ;;
243 esac
244
245 rm --preserve-root --one-file-system -rf ${RM_OPTIONS} "${MACHINES}/${NAME}"
246 rm -f ${RM_OPTIONS} "${CONFIG}/${NAME}.conf"
247
248 case "${VERBOSE}" in
249 true)
250 echo " done."
251 ;;
252 esac
253
254 # Post hooks
255 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}"
256 do
257 if [ -x "${FILE}" ]
258 then
259 "${FILE}"
260 fi
261 done