-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-init
executable file
·331 lines (295 loc) · 8.94 KB
/
distrobox-init
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
# SHELL
trap '[ "$?" -ne 0 ] && printf "Error: An error occurred\n"' EXIT
# Defaults
verbose=0
version="distrobox_version_placeholder"
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
cat <<EOF
distrobox version: ${version}
distrobox-init is the entrypoint of a created distrobox.
Note that this HAS to run from inside a distrobox, will not work if you run it
from your host.
distrobox-init will take care of installing missing dependencies (eg. sudo), set
up the user and groups, mount directories from the host to ensure the tight
integration.
Usage:
distrobox-init --name ${USER} --user $(id -ru) --group $(id -rg) --home ${HOME}
Options:
--name/-n: user name
--user/-u: uid of the user
--group/-g: gid of the user
--home/-d: path/to/home of the user
--help/-h: show this message
--verbose/-v: show more verbosity
--version/-V: show version
EOF
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit 0
;;
-v | --verbose)
shift
verbose=1
;;
-V | --version)
printf "distrobox: %s\n" "${version}"
exit 0
;;
-n | --name)
if [ -n "$2" ]; then
container_user_name="$2"
shift
shift
fi
;;
-d | --home)
if [ -n "$2" ]; then
container_user_home="$2"
shift
shift
fi
;;
-u | --user)
if [ -n "$2" ]; then
container_user_uid="$2"
shift
shift
fi
;;
-g | --group)
if [ -n "$2" ]; then
container_user_gid="$2"
shift
shift
fi
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# Check we're running inside a container and not on the host
if [ ! -f /run/.containerenv ] && [ ! -f /.dockerenv ]; then
printf >&2 "You must run %s inside a container!\n" " $(basename "$0")"
printf >&2 "distrobox-init should only be used as an entrypoint for a distrobox!\n"
exit 126
fi
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
[ -z "${container_user_gid}" ] && printf "Error: Invalid arguments, missing user gud\n" && exit 2
[ -z "${container_user_home}" ] && printf "Error: Invalid argument, missing user home\n" && exit 2
[ -z "${container_user_name}" ] && printf "Error: Invalid arguments, missing username\n" && exit 2
[ -z "${container_user_uid}" ] && printf "Error: Invalid arguments, missing user uid\n" && exit 2
# Bind mount or error.
# Arguments:
# source_dir
# target_dir
# mount_flags -> optional
# Outputs:
# No output if all ok
# Error if not
mount_bind() (
source_dir="$1"
target_dir="$2"
mount_flags="$3"
# if source dir doesn't exist, just exit normally
! [ -d "${source_dir}" ] && ! [ -f "${source_dir}" ] && return 0
# if the source_dir exists, then create the target_dir
if [ -d "${source_dir}" ]; then
# exit if not successful
if ! mkdir -p "${target_dir}"; then
printf "Warning: cannot create mount target directory: %s\n" "${target_dir}"
return 1
fi
# if instead it's a file, create it with touch
elif [ -f "${source_dir}" ]; then
# exit if not successful
if ! touch "${target_dir}"; then
printf "Warning: cannot create mount target file: %s\n" "${target_dir}"
return 1
fi
fi
# Add mountflags if needed, if no are specificed, use rslave as default.
if [ "${mount_flags}" = "" ]; then
mount_flags="rslave"
fi
# bind mount source_dir to target_dir, return error if not successful
if ! mount --rbind -o "${mount_flags}" "${source_dir}" "${target_dir}"; then
printf "Warning: failed to bind mount %s to %s\n" "${source_dir}" "${target_dir}"
return 1
fi
return 0
)
# Extract shell name from the $SHELL environment variable
# If not present as package in the container, we want to install it.
shell_pkg="$(basename "${SHELL:-"bash"}")"
# Check if dependencies are met for the script to run.
if ! command -v mount || ! command -v passwd ||
! command -v sudo || ! command -v useradd ||
! command -v usermod || ! command -v "${shell_pkg}"; then
# Detect the available package manager
# install minimal dependencies needed to bootstrap the container:
# the same shell that's on the host
# sudo, mount
# passwd, groupadd and useradd
if command -v apk; then
apk add \
"${shell_pkg}" \
procps \
shadow \
sudo \
util-linux
elif command -v apt-get; then
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y \
"${shell_pkg}" \
passwd \
procps \
sudo \
util-linux
elif command -v dnf; then
dnf install -y \
"${shell_pkg}" \
passwd \
procps-ng \
shadow-utils \
sudo \
util-linux
elif command -v pacman; then
pacman -Sy --noconfirm \
"${shell_pkg}" \
procps-ng \
shadow \
sudo \
util-linux
elif command -v slackpkg; then
slackpkg update
yes | slackpkg install -default_answer=yes -batch=yes \
"${shell_pkg}" \
procps \
shadow \
sudo \
util-linux
elif command -v xbps-install; then
xbps-install -Sy \
"${shell_pkg}" \
procps-ng \
shadow \
sudo \
util-linux
elif command -v yum; then
yum install -y \
"${shell_pkg}" \
passwd \
procps \
shadow-utils \
sudo \
util-linux
elif command -v zypper; then
zypper install -y \
"${shell_pkg}" \
procps \
shadow \
sudo \
util-linux
else
printf "Error: could not find a supported package manager.\n"
printf "Error: could not set up base dependencies.\n"
# Exit as command not found
exit 127
fi
fi
# We'll also bind mount in READ-ONLY useful directories from the host
HOST_MOUNTS_RO="/etc/machine-id /var/lib/flatpak /var/lib/systemd/coredump /var/log/journal"
for host_mount_ro in ${HOST_MOUNTS_RO}; do
mount_bind /run/host"${host_mount_ro}" "${host_mount_ro}" ro
done
# We'll also bind mount READ-WRITE useful mountpoints to pass external drives and libvirt from
# the host to the container
HOST_MOUNTS="/media /run/media /run/udev/data /mnt /var/mnt /run/systemd/journal /run/libvirt /var/lib/libvirt"
for host_mount in ${HOST_MOUNTS}; do
if ! mount_bind /run/host"${host_mount}" "${host_mount}" rw; then
printf "Warning: Cannot bind mound %s to /run/host%s\n" "${host_mount}" "${host_mount}"
fi
done
# In case of an RPM distro, we can specify that our bind_mount directories
# are in fact net shares. This prevents conflicts during package installations.
if [ -d "/usr/lib/rpm/macros.d/" ]; then
net_mounts=""
for net_mount in ${HOST_MOUNTS_RO} ${HOST_MOUNTS} '/dev' '/proc' '/sys' '/tmp'; do
net_mounts="${net_mount}:${net_mounts}"
done
net_mounts=${net_mounts%?}
cat <<EOF >/usr/lib/rpm/macros.d/macros.distrobox
%_netsharedpath ${net_mounts}
EOF
fi
# Do not check fqdn when doing sudo, it will not work anyways
if ! grep -q 'Defaults !fqdn' /etc/sudoers; then
printf "Defaults !fqdn\n" >>/etc/sudoers
fi
# Ensure passwordless sudo is set up for user
if ! grep -q "${container_user_name} ALL = (root) NOPASSWD:ALL" /etc/sudoers; then
printf "%s ALL = (root) NOPASSWD:ALL\n" "${container_user_name}" >>/etc/sudoers
fi
# If not existing, ensure we have a group for our user.
if ! grep -q "${container_user_name}" /etc/group; then
groupadd --force --gid "${container_user_gid}" "${container_user_name}"
fi
# Let's add our user to the container if the user already exists, just go ahead.
if ! id "${container_user_name}"; then
if ! useradd \
--home-dir "${container_user_home}" \
--no-create-home \
--shell /bin/bash \
--uid "${container_user_uid}" \
--gid "${container_user_gid}" \
"${container_user_name}"; then
printf "Warning: there was a problem setting up the user\n"
fi
fi
# Delete password for root and user
passwd --delete "${container_user_name}"
passwd --delete root
# Themes and icons integration works using a bind mount inside the container
# of the host's themes and icons directory. This ensures that the host's home will
# not be littered with files and directories and broken symlinks.
#
# bind mount distrobox directory for themes and icons
mkdir -p "${container_user_home}/.local/share/themes"
mkdir -p "${container_user_home}/.local/share/icons"
# Fix permissions for home directories
chown "${container_user_uid}":"${container_user_gid}" "${container_user_home}/.local/share/themes"
chown "${container_user_uid}":"${container_user_gid}" "${container_user_home}/.local/share/icons"
if ! mount_bind "/run/host/usr/share/themes" "${container_user_home}/.local/share/themes" rw; then
printf "Warning: Cannot bind mound /run/host/usr/share/themes to %s/.local/share/themes\n" "${container_user_home}"
printf "Warning: Themes integration with the host is disabled.\n"
fi
if ! mount_bind "/run/host/usr/share/icons" "${container_user_home}/.local/share/icons" rw; then
printf "Warning: Cannot bind mound /run/host/usr/share/icons to %s/.local/share/icons\n" "${container_user_home}"
printf "Warning: Icons integration with the host is disabled.\n"
fi
printf "container_setup_done\n"
# Keepalive loop
sleep infinity