-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-init
executable file
·280 lines (250 loc) · 8.32 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
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
# SHELL
trap '[ "$?" -ne 0 ] && printf "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
# Also check we're running inside a container and not on the host
if [ ! -f /run/.containerenv ]; 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_name}" ] && printf >&2 "Invalid arguments, missing username\n" && exit 2
[ -z "${container_user_uid}" ] && printf >&2 "Invalid arguments, missing user uid\n" && exit 2
[ -z "${container_user_gid}" ] && printf >&2 "Invalid arguments, missing user gud\n" && exit 2
[ -z "${container_user_home}" ] && printf >&2 "Invalid argument, missing user home\n" && exit 2
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# 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"
mount_o=""
# 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 >&2 "Cannot create mount target directory: %s\n" "${target_dir}"
return 1
fi
elif [ -f "${source_dir}" ]; then
# exit if not successful
if ! touch "${target_dir}"; then
printf >&2 "Cannot create mount target file: %s\n" "${target_dir}"
return 1
fi
fi
# Add mountflags if needed
[ "${mount_flags}" = "" ] || mount_o="-o ${mount_flags}"
# bind mount source_dir to target_dir, return error if not successful
# shellcheck disable=SC2086
if ! mount --rbind ${mount_o} "${source_dir}" "${target_dir}"; then
printf >&2 "Failed to bind mount %s to %s\n" "${source_dir}" "${target_dir}"
return 1
fi
return 0
)
# Return sudoers groups.
# Arguments:
# None
# Outputs:
# return all sudoers groups, error if not present.
list_sudo_groups() (
group=""
if grep -q "sudo:" /etc/group; then
group="sudo"
elif grep -q "wheel:" /etc/group; then
group="wheel"
else
# No group for root, create one
groupadd wheel
group="wheel"
fi
printf "%s" "${group}"
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
# check which pkg manager we have at disposal
# and make sure we have all the dependencies we need to function.
if command -v apk; then
apk add --no-cache sudo shadow procps util-linux "${shell_pkg}"
elif command -v apt-get; then
apt-get update && apt-get install -y --no-install-recommends sudo apt-utils passwd procps util-linux "${shell_pkg}"
elif command -v dnf; then
dnf install -y --setopt=install_weak_deps=False sudo shadow-utils passwd procps-ng util-linux "${shell_pkg}"
elif command -v pacman; then
pacman -Sy --noconfirm sudo procps-ng shadow util-linux "${shell_pkg}"
elif command -v xbps-install; then
xbps-install -Sy sudo procps-ng shadow util-linux "${shell_pkg}"
elif command -v yum; then
yum install -y --setopt=install_weak_deps=False sudo shadow-utils passwd procps util-linux "${shell_pkg}"
elif command -v zypper; then
zypper install -y --no-recommends sudo shadow procps util-linux "${shell_pkg}"
else
printf >&2 "FAILED TO INSTALL 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 /mnt /run/systemd/journal /run/libvirt /var/mnt /var/lib/libvirt"
for host_mount in ${HOST_MOUNTS}; do
if ! mount_bind /run/host"${host_mount}" "${host_mount}" rw; then
printf >&2 "Warning: Cannot bind mound %s to /run/host%s\n" "${host_mount}" "${host_mount}"
fi
done
# 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_uid}" "${container_user_name}"
fi
# Let's add our user to the container
# if the user already exists, just add it to the sudoers groups
if ! useradd \
--home-dir "${container_user_home}" \
--no-create-home \
--shell /bin/bash \
--uid "${container_user_uid}" \
--gid "${container_user_gid}" \
--groups "$(list_sudo_groups)" \
"${container_user_name}"; then
usermod -aG "$(list_sudo_groups)" "${container_user_name}"
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 homw folders
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 >&2 "Warning: Cannot bind mound /run/host/usr/share/themes to %s/.local/share/themes\n" "${container_user_home}"
printf >&2 "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 >&2 "Warning: Cannot bind mound /run/host/usr/share/icons to %s/.local/share/icons\n" "${container_user_home}"
printf >&2 "Warning: Icons integration with the host is disabled.\n"
fi
printf "container_setup_done\n"
# Keepalive loop
sleep infinity