-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-create
executable file
·225 lines (203 loc) · 6.66 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
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
trap '[ "$?" -ne 0 ] && printf "An error occurred\n"' EXIT
# We depend on podman let's be sure we have it
if ! command -v podman >/dev/null; then
printf >&2 "Missing dependency: podman\n"
exit 127
fi
# 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
# Generate Podman command to execute.
# Arguments:
# None
# Outputs:
# prints the podman 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="podman create"
# add podman verbose if -v is specified
if [ "${verbose}" -ne 0 ]; then
result_command="${result_command} --log-level debug"
fi
result_command="${result_command}
--env=\"XDG_RUNTIME_DIR=/run/user/${container_user_uid}\"
--hostname ${container_name}"
# 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
#
# also mount the distrobox-init utility as the container entrypoint
result_command="${result_command} --name ${container_name}
--env=\"SHELL=${SHELL}\"
--ipc host
--network host
--pid host
--privileged
--security-opt label=disable
--user root:root
--userns keep-id
--ulimit host
--volume ${distrobox_entrypoint_path}:/usr/bin/entrypoint:ro"
# 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 user home
result_command="${result_command} --volume ${container_user_home}:${container_user_home}:rslave"
# mount dev inside container. This grants access to external devices
# like usb webcams, disks and so on.
result_command="${result_command} --volume /dev:/dev:rslave"
result_command="${result_command} --volume /:/run/host:rslave"
result_command="${result_command} --mount type=devpts,destination=/dev/pts"
# those are dynamic configs needed by the container to function properly
# and integrate with 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}" ]; then
# Use realpath to not have multi symlink mess
result_command="${result_command} --volume $(realpath "${host_link}"):${host_link}:ro"
fi
done
# 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
host_sockets="$(find /run -iname "*socket" ! -path "/run/user/*" 2>/dev/null || :)"
for host_socket in ${host_sockets}; do
result_command="${result_command} --volume ${host_socket}:${host_socket}"
done
# 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}"
printf "%s" "${result_command}"
}
# check that we have a complete distrobox installation or
# entrypoint and export will not work.
[ -z "${distrobox_entrypoint_path}" ] && printf >&2 "Error: no distrobox-init found in %s\n" "${PATH}" && exit 127
# First, check if the image exists in the host
if [ -z "$(podman images -q "${container_image}")" ]; then
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
podman pull "${container_image}"
;;
n | N | no | No)
printf >&2 "next time, run this command first:\n"
printf >&2 " podman pull %s\n" "${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
# Check if the container already exists.
if podman inspect "${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
# Generate the command and execute
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