-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathdistrobox-create
executable file
·175 lines (155 loc) · 4.49 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
#!/bin/sh
# POSIX
# Expected env variables:
# HOME
# USER
trap '[ "$?" -ne 0 ] && echo An error occurred' EXIT
# We depend on podman let's be sure we have it
if ! command -v podman >/dev/null; then
echo "Missing dependency: podman"
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
# Print usage to stdout.
# Arguments:
# None
# Outputs:
# print usage with examples.
show_help() {
echo "USAGE:
distrobox-create --image registry.fedoraproject.org/fedora-toolbox:35 --name fedora-toolbox-35
Arguments:
--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
-v: show more verbosity
"
}
# Parse arguments
while :; do
case $1 in
-h | --help)
# Call a "show_help" function to display a synopsis, then exit.
show_help
exit
;;
-v | --verbose)
verbose=1
shift
;;
-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() {
host_folders="/ /etc /media /mnt /run /tmp /usr /var"
# Set the container hostname the same as the container name.
# use the host's namespace for ipc, network, pid, ulimit
echo "podman create"
if [ "${verbose}" -ne 0 ]; then
echo "--log-level debug"
fi
echo "--dns none
--env XDG_RUNTIME_DIR=/run/user/${container_user_uid}
--hostname ${container_name}
--ipc host"
if [ -f /dev/pts ]; then
echo "--mount type=devpts,destination=/dev/pts"
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
#
# also mount the distrobox-init utility as the container entrypoint
echo "--name ${container_name}
--env=SHELL=${SHELL}
--network host
--no-hosts
--pid host
--privileged
--security-opt label=disable
--ulimit host
--user root:root
--userns keep-id
--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
echo "--volume ${distrobox_export_path}:/usr/bin/distrobox-export:ro"
fi
echo "--volume ${container_user_home}:${container_user_home}:rslave
--volume /dev:/dev:rslave"
# Check if host folder exists before mounting it
for host_folder in ${host_folders}; do
if [ -d "${host_folder}" ]; then
echo "--volume "${host_folder}":/run/host"${host_folder}":rslave"
fi
done
if [ -d /run/media ]; then
echo "--volume /run/media:/run/media:rslave"
fi
# mount also the XDG_RUNTIME_DIR to ensure functionality of the apps
if [ -d /run/user/"${container_user_uid}" ]; then
echo "--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
for socket in $(find /run -iname "*socket" ! -path "/run/user/*" 2>/dev/null); do
echo "--volume ${socket}:${socket}"
done
# now execute the entrypoint, refer to `distrobox-init -h` for instructions
echo "${container_image}
/usr/bin/entrypoint -v --name ${container_user_name}
--user ${container_user_uid} --group ${container_user_gid}
--home ${container_user_home}"
}
# check that we have a complete distrobox installation or
# entrypoint and export will not work.
[ -z "${distrobox_entrypoint_path}" ] && echo "Error: no distrobox-init found in $PATH" && exit 1
# First, check if the image exists in the host
if ! podman image exists "${container_image}"; then
echo "Image not found, run this command first:"
echo " podman pull ${container_image}"
exit 1
fi
# Generate the command and execute
$(generate_command)