-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-create
executable file
·260 lines (235 loc) · 7.61 KB
/
distrobox-create
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
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
trap '[ "$?" -ne 0 ] && printf "\nAn error occurred\n"' EXIT
# Defaults
container_image="registry.fedoraproject.org/fedora-toolbox:35"
container_name="fedora-toolbox-35"
container_user_gid="$(id -rg)"
container_user_home="${HOME:-"/"}"
container_user_name="${USER}"
container_user_uid="$(id -ru)"
distrobox_entrypoint_path="$(command -v distrobox-init)"
distrobox_export_path="$(command -v distrobox-export)"
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-create takes care of creating the container with input name and image.
Created container will be tightly integrated with the host, allowing to share
the HOME directory of the user, external storage, external usb devices and
graphical apps (X11/Wayland) and audio.
Usage:
distrobox-create --image registry.fedoraproject.org/fedora-toolbox:35 --name fedora-toolbox-35
Options:
--image/-i: image to use for the container default: registry.fedoraproject.org/fedora-toolbox:35
--name/-n: name for the distrobox default: fedora-toolbox-35
--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)
verbose=1
shift
;;
-V | --version)
printf "distrobox: %s\n" "${version}"
exit 0
;;
-i | --image)
if [ -n "$2" ]; then
container_image="$2"
shift
shift
fi
;;
-n | --name)
if [ -n "$2" ]; then
container_name="$2"
shift
shift
fi
;;
--) # End of all options.
shift
break
;;
*) # 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
# We depend on a container manager let's be sure we have it
# First we use podman, else docker
container_manager="podman"
if ! command -v podman >/dev/null; then
container_manager="docker"
if ! command -v docker >/dev/null; then
printf >&2 "Missing dependency: we need a container manager\n."
printf >&2 "Please install one of podman or docker.\n"
exit 127
fi
fi
# Generate Podman or Docker command to execute.
# Arguments:
# None
# Outputs:
# prints the podman or docker command to create the distrobox container
generate_command() {
# Set the container hostname the same as the container name.
# use the host's namespace for ipc, network, pid, ulimit
result_command="${container_manager}"
# add verbose if -v is specified
if [ "${verbose}" -ne 0 ]; then
result_command="${result_command} --log-level debug"
fi
result_command="${result_command} create"
result_command="${result_command}
--env=\"SHELL=${SHELL}\"
--env=\"XDG_RUNTIME_DIR=/run/user/${container_user_uid}\"
--hostname ${container_name}
--ipc host
--name ${container_name}
--network host
--pid host
--privileged
--security-opt label=disable
--user root:root"
# let's check if we can include distrobox-export or not
if [ -n "${distrobox_export_path}" ]; then
result_command="${result_command}
--volume ${distrobox_export_path}:/usr/bin/distrobox-export:ro"
fi
# Mount useful stuff inside the container.
# We also mount host's root filesystem to /run/host, to be able to syphon
# dynamic configurations from the host.
#
# Mount user home, dev and host's root inside container.
# This grants access to external devices like usb webcams, disks and so on.
#
# Mount also the distrobox-init utility as the container entrypoint.
result_command="${result_command}
--volume ${container_user_home}:${container_user_home}:rslave
--volume ${distrobox_entrypoint_path}:/usr/bin/entrypoint:ro
--volume /:/run/host:rslave
--volume /dev:/dev:rslave
--volume /sys:/sys:rslave
--volume /tmp:/tmp:rslave"
# Mount also the XDG_RUNTIME_DIR to ensure functionality of the apps.
if [ -d "/run/user/${container_user_uid}" ]; then
result_command="${result_command}
--volume /run/user/${container_user_uid}:/run/user/${container_user_uid}"
fi
# Find all the user's socket and mount them inside the container
# this will allow for continuity of functionality between host and container
#
# for example using `podman --remote` to control the host's podman from inside
# the container or accessing docker and libvirt sockets.
host_sockets="$(find /run -iname "*sock" ! -path "/run/user/*" 2>/dev/null || :)"
for host_socket in ${host_sockets}; do
if [ -r "${host_socket}" ]; then
result_command="${result_command}
--volume $(realpath "${host_socket}"):${host_socket}"
fi
done
# These are dynamic configs needed by the container to function properly
# and integrate with the host
#
# We're doing this now instead of inside the init because some distros will
# have symlinks places for these files that use absolute paths instead of
# relative paths. Those symlinks will result broken inside the container so
# we need to resolve them now on the host.
host_links="/etc/host.conf /etc/hosts /etc/resolv.conf /etc/localtime"
for host_link in ${host_links}; do
# Check if the file exists first
if [ -f "${host_link}" ] && [ -r "${host_link}" ]; then
# Use realpath to not have multi symlink mess
result_command="${result_command}
--volume $(realpath "${host_link}"):${host_link}:ro"
fi
done
if [ "${container_manager}" = "podman" ]; then
result_command="${result_command}
--userns keep-id
--ulimit host
--mount type=devpts,destination=/dev/pts"
fi
# Now execute the entrypoint, refer to `distrobox-init -h` for instructions
result_command="${result_command} ${container_image}
/usr/bin/entrypoint -v --name ${container_user_name}
--user ${container_user_uid}
--group ${container_user_gid}
--home ${container_user_home}"
# Return generated command.
printf "%s" "${result_command}"
}
# Check that we have a complete distrobox installation or
# entrypoint and export will not work.
if [ -z "${distrobox_entrypoint_path}" ]; then
printf >&2 "Error: no distrobox-init found in %s\n" "${PATH}"
exit 127
fi
# Check if the container already exists.
# If it does, notify the user and exit.
if "${container_manager}" inspect --type container "${container_name}" >/dev/null 2>&1; then
printf "Distrobox named '%s' already exists.\n" "${container_name}"
printf "To enter, run:\n"
printf "\tdistrobox-enter --name %s\n" "${container_name}"
exit 0
fi
# First, check if the image exists in the host.
# If not prompt to download it.
if ! ${container_manager} inspect --type image "${container_image}" >/dev/null 2>&1; then
# Prompt to download it.
printf >&2 "Image not found.\n"
printf >&2 "Do you want to pull the image now?[y/n] "
read -r response
# Accept only y,Y,Yes,yes,n,N,No,no.
case "${response}" in
y | Y | yes | Yes)
# Pull the image
"${container_manager}" pull "${container_image}"
;;
n | N | no | No)
printf >&2 "next time, run this command first:\n"
printf >&2 "\t%s pull %s\n" "${container_manager}" "${container_image}"
exit 0
;;
*) # Default case: If no more options then break out of the loop.
printf >&2 "Invalid input.\n"
printf >&2 "The available choices are: y,Y,Yes,yes or n,N,No,no.\nExiting.\n"
exit 1
;;
esac
fi
# Generate the create command and run it
cmd="$(generate_command)"
# Eval the generated command. If successful display an helpful message.
# shellcheck disable=SC2086
if eval ${cmd}; then
printf "Distrobox '%s' successfully created.\n" "${container_name}"
printf "To enter, run:\n"
printf "\tdistrobox-enter --name %s\n" "${container_name}"
fi