forked from containers/qm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·257 lines (227 loc) · 6.71 KB
/
setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#! /bin/bash -e
#
# This setup script will install an OS environment by default into
# /usr/lib/qm/rootfs and create a Podman quadlet containerized environment
# running systemd as PID1 inside.
#
# Globals
SYSTEMCTL_SKIP="N"
INSTALLDIR="/usr/share/qm"
ROOTFS="/usr/lib/qm/rootfs"
RWETCFS="/etc/qm"
RWVARFS="/var/qm"
AGENT_HOSTNAME="$(hostname)"
AGENTCONF="/etc/bluechi/agent.conf.d/agent.conf"
QM_CONTAINER_IDS=1000000000:1500000000
CONTAINER_IDS=2500000000:1500000000
PACKAGES_TO_INSTALL="selinux-policy-targeted podman systemd bluechi-agent procps-ng iptables-nft"
# RHEL kernel uses iptables-nft, not iptables-legacy, the tool should
# make sure this package is removed.
PACKAGES_TO_REMOVE="iptables-legacy"
CMDLINE_ARGUMENT_LIST=(
"installdir"
"rootfs"
"hostname"
"rwetcfs"
"rwvarfs"
"skip-systemctl"
)
root_check() {
if [ "$(id -u)" -ne 0 ];then
echo "Please run this script as root"
exit 1
fi
}
usage()
{
echo "The setup script will install an OS environment by default into"
echo "/usr/lib/qm/rootfs and create a Podman quadlet containerized environment"
echo "running systemd as PID1 inside."
echo
echo "Usage: setup [OPTIONS]"
echo
echo "OPTIONS:"
echo
echo "--help Print this message."
echo "--installdir qm install directory (default: /usr/share/qm)"
echo "--rootfs set rootfs (default: /usr/lib/qm/rootfs)"
echo "--rwetcfs set rwetcfs (default: /etc/qm)"
echo "--rwvarfs set rwvarfs (default: /var/qm)"
echo "--skip-systemctl skip systemctl daemon commands (default: false)"
echo "--hostname custom agent hostname to use (default: \$\(hostname\))"
echo
echo "Example:"
echo " $ sudo ./setup --installdir=/usr/share/qm --rootfs=/usr/lib/qm/rootfs"
}
replaceIDs() {
touch "$1"
grep -q "^$2:" "$1" || echo "$2":"$3" >> "$1"
}
bluechiSetup() {
ROOTFS=$1
RWETCFS=$2
RWVARFS=$3
setupRW "${ROOTFS}" "${RWETCFS}" "${RWVARFS}"
if test ! -f "${ROOTFS}${AGENTCONF}"; then
if test -f "${AGENTCONF}"; then
sed -e 's,^NodeName=,NodeName=qm.,g' "${AGENTCONF}" > "${ROOTFS}${AGENTCONF}"
fi
fi
hostname=$AGENT_HOSTNAME
if test -f "${ROOTFS}${AGENTCONF}"; then
sed -e "s,^NodeName=qm.$,NodeName=qm.${hostname},g" \
-e "s,^NodeName=$,NodeName=qm.${hostname},g" \
-i "${ROOTFS}${AGENTCONF}"
else
cat > "${ROOTFS}${AGENTCONF}" <<EOF
[bluechi-agent]
NodeName=qm.${hostname}
EOF
fi
umount "${ROOTFS}/var" "${ROOTFS}/etc"
}
storage() {
ROOTFS=$1
if ! test -f "${ROOTFS}/etc/containers/storage.conf"; then
mkdir -p "${ROOTFS}/var/lib/shared/overlay-images" \
"${ROOTFS}/var/lib/shared/overlay-layers"
touch "${ROOTFS}/var/lib/shared/overlay-images/images.lock" \
"${ROOTFS}/var/lib/shared/overlay-layers/layers.lock"
sed -e '/additionalimage.*/a "/var/lib/shared",' \
-e 's|^#.*transient_store.*|transient_store=true|g' \
"${ROOTFS}/usr/share/containers/storage.conf" \
> "${ROOTFS}/etc/containers/storage.conf"
fi
}
setupRW() {
ROOTFS=$1
RWETCFS=$2
RWVARFS=$3
mkdir -Z -p "${ROOTFS}/etc" "${ROOTFS}/var"
mkdir -Z -p "${RWETCFS}" "${RWVARFS}"
mount --bind "${RWETCFS}" "${ROOTFS}/etc"
mount --bind "${RWVARFS}" "${ROOTFS}/var"
}
create_rootfs_required_dirs() {
# List of directories required for rootfs
directories=(
"${ROOTFS}/etc/containers/"
"${ROOTFS}/usr/share/containers/selinux/"
"${ROOTFS}/etc/selinux/targeted/contexts/files/"
)
for dir in "${directories[@]}"; do
if [ ! -d "$dir" ]; then
mkdir -Z -p "$dir"
echo "Created directory: $dir"
fi
done
}
install() {
ROOTFS=$1
RWETCFS=$2
RWVARFS=$3
# shellcheck source=/dev/null
. /etc/os-release
setupRW "${ROOTFS}" "${RWETCFS}" "${RWVARFS}"
cmd_dnf_install="dnf -y install --releasever=${VERSION_ID} --installroot ${ROOTFS} ${PACKAGES_TO_INSTALL}"
${cmd_dnf_install}
cmd_dnf_remove="dnf --installroot ${ROOTFS} remove ${PACKAGES_TO_REMOVE} -y"
${cmd_dnf_remove}
dnf -y update --installroot "${ROOTFS}"
rm -rf "${ROOTFS}"/etc/selinux/targeted/contexts/files/file_contexts/*
create_rootfs_required_dirs
cp "${INSTALLDIR}/containers.conf" "${ROOTFS}/etc/containers/"
cp "${INSTALLDIR}/contexts" "${ROOTFS}/usr/share/containers/selinux/"
cp "${INSTALLDIR}/file_contexts" "${ROOTFS}/etc/selinux/targeted/contexts/files/file_contexts"
replaceIDs "${ROOTFS}/etc/subuid" containers ${QM_CONTAINER_IDS}
replaceIDs "${ROOTFS}/etc/subgid" containers ${QM_CONTAINER_IDS}
bluechiSetup "${ROOTFS}" "${RWETCFS}" "${RWVARFS}"
if [ "$SYSTEMCTL_SKIP" == "N" ]; then
unshare --mount-proc -R "${ROOTFS}" -m systemctl enable bluechi-agent.service
else
chroot "${ROOTFS}" ln -fs \
/usr/lib/systemd/system/bluechi-agent.service \
/etc/systemd/system/multi-user.target.wants/bluechi-agent.service
fi
storage "${ROOTFS}"
restorecon -R "${ROOTFS}"
umount "${ROOTFS}/var" "${ROOTFS}/etc"
}
# read command line arguments
opts=$(getopt \
--longoptions "$(printf "help,skip-systemctl,%s:," "${CMDLINE_ARGUMENT_LIST[@]}")" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set --"${opts}"
# main()
root_check
while [[ $# -gt 0 ]]; do
case "$1" in
--installdir)
INSTALLDIR="${2}"
shift 2
;;
--rootfs)
ROOTFS="${2}"
shift 2
;;
--rwetcfs)
RWETCFS="${2}"
shift 2
;;
--rwvarfs)
RWVARFS="${2}"
shift 2
;;
--skip-systemctl)
SYSTEMCTL_SKIP="Y"
shift
;;
--hostname)
AGENT_HOSTNAME="${2}"
shift 2
;;
--help)
usage
exit 1
;;
*)
break
;;
esac
done
# Figure out the options, execute setup
echo "setup has started..."
echo " * rootfs: ${ROOTFS}"
echo " * rwetcfs: ${RWETCFS}"
echo " * rwvarfs: ${RWVARFS}"
echo " * install dir: ${INSTALLDIR}"
echo " * skip-systemctl: ${SYSTEMCTL_SKIP}"
echo " * agent hostname: ${AGENT_HOSTNAME}"
echo
case "$1" in
bluechi-agent)
rm -f "${ROOTFS}${AGENTCONF}"
bluechiSetup "${ROOTFS}" "${RWETCFS}" "${RWVARFS}"
;;
*)
if [ "$SYSTEMCTL_SKIP" == "N" ]; then
if systemctl is-active --quiet "qm.service" ; then
systemctl stop qm.service
fi
fi
install "${ROOTFS}" "${RWETCFS}" "${RWVARFS}"
replaceIDs /etc/subuid qmcontainers ${QM_CONTAINER_IDS}
replaceIDs /etc/subgid qmcontainers ${QM_CONTAINER_IDS}
replaceIDs /etc/subuid containers ${CONTAINER_IDS}
replaceIDs /etc/subgid containers ${CONTAINER_IDS}
if [ "$SYSTEMCTL_SKIP" == "N" ]; then
systemctl daemon-reload
systemctl start qm.service
else
/usr/libexec/podman/quadlet /run/systemd/generator/
fi
;;
esac