-
Notifications
You must be signed in to change notification settings - Fork 419
/
distrobox-init
executable file
·2637 lines (2425 loc) · 77.7 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only
#
# This file is part of the distrobox project:
# https://github.com/89luca89/distrobox
#
# Copyright (C) 2021 distrobox contributors
#
# distrobox is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# distrobox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with distrobox; if not, see <http://www.gnu.org/licenses/>.
# POSIX
# Expected env variables:
# HOME
# USER
# SHELL
trap '[ "$?" -ne 0 ] && printf "Error: An error occurred\n"' EXIT
# Redirect stderr to stdout as podman by default logs stderr as priority 3 journald errors.
# Github issue: https://github.com/containers/podman/issues/20728
exec 2>&1
# We'll also bind mount READ-WRITE useful mountpoints to pass external drives and libvirt from
# the host to the container
HOST_MOUNTS="
/etc/host.conf
/etc/machine-id
/media
/mnt
/run/libvirt
/run/media
/run/netconfig/
/run/systemd/journal
/run/systemd/resolve/
/run/systemd/seats
/run/systemd/sessions
/run/systemd/users
/run/udev
/var/lib/libvirt
/var/mnt"
# We'll also bind mount in READ-ONLY useful directories from the host
HOST_MOUNTS_RO="
/etc/localtime
/var/lib/systemd/coredump
/var/log/journal"
HOST_MOUNTS_RO_INIT="
/etc/localtime
/run/systemd/journal
/run/systemd/resolve
/run/systemd/seats
/run/systemd/sessions
/run/systemd/users
/var/lib/systemd/coredump
/var/log/journal"
# Defaults
container_additional_packages=""
init=0
init_hook=""
nvidia=0
pre_init_hook=""
rootful=0
upgrade=0
verbose=0
version="1.8.0"
# show_help will print usage to stdout.
# Arguments:
# None
# Expected global variables:
# version: distrobox version
# Expected env variables:
# USER
# HOME
# Outputs:
# print usage with examples.
show_help()
{
cat << EOF
distrobox version: ${version}
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
--additional-packages: packages to install in addition
--init/-I: whether to use or not init
--pre-init-hooks: commands to execute prior to init
--nvidia: try to integrate host's nVidia drivers in the guest
--upgrade/-U: run init in upgrade mode
--verbose/-v: show more verbosity
--version/-V: show version
--: end arguments execute the rest as command to execute during init
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
;;
-U | --upgrade)
shift
upgrade=1
;;
-n | --name)
if [ -n "$2" ]; then
container_user_name="$2"
shift
shift
fi
;;
-i | --init)
if [ -n "$2" ]; then
init="$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
;;
--pre-init-hooks)
if [ -n "$2" ]; then
pre_init_hook="$2"
fi
shift
shift
;;
--additional-packages)
if [ -n "$2" ]; then
container_additional_packages="$2"
fi
shift
shift
;;
--nvidia)
if [ -n "$2" ]; then
nvidia="$2"
shift
shift
fi
;;
--)
shift
init_hook=$*
break
;;
-*) # Invalid options.
printf >&2 "Error: Invalid flag '%s'\n\n" "$1"
show_help
exit 1
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
done
# Check we're running inside a container and not on the host
if [ ! -f /run/.containerenv ] && [ ! -f /.dockerenv ] && [ -z "${container:-}" ]; 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\n"
printf >&2 "This is not intended to be used manually, but instead used by distrobox-enter\n"
printf >&2 "to set up the container's entrypoint.\n"
exit 126
fi
# Ensure the foundamental variables are set and not empty, we will not proceed if
# they are not all set.
if [ "${upgrade}" -eq 0 ]; then
[ -z "${container_user_gid}" ] && printf "Error: Invalid arguments, missing user gid\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
fi
set -o errexit
set -o nounset
# set verbosity
if [ "${verbose}" -ne 0 ]; then
set -o xtrace
fi
# Determine if we're in a rootful container, generally if we're able to read
# host's /etc/shadow, it means we're really root!
#
# if /run/.nopasswd is present, let's treat the init as rootless, this is not
# a good thing, users behold!
if stat /run/host/etc/shadow > /dev/null &&
[ "$(stat -c "%u" /run/host/etc/shadow)" = "0" ] &&
[ ! -e /run/.nopasswd ]; then
rootful=1
fi
# Get host $LANG
if [ -f "/run/host/etc/locale.conf" ]; then
HOST_LOCALE=$(grep -e '^LANG=' /run/host/etc/locale.conf | sed 's/LANG=//' | sed 's/"//g')
HOST_LOCALE_ENCODING=$(echo "${HOST_LOCALE}" | sed -n 's/^[^.]*\.\(.*\)$/\1/p')
HOST_LOCALE_LANG=$(echo "${HOST_LOCALE}" | sed -n 's/^\([^.]*\)\..*$/\1/p')
elif [ -f "/run/host/etc/default/locale" ]; then
HOST_LOCALE=$(grep -e '^LANG=' /run/host/etc/default/locale | sed 's/LANG=//' | sed 's/"//g')
HOST_LOCALE_ENCODING=$(echo "${HOST_LOCALE}" | sed -n 's/^[^.]*\.\(.*\)$/\1/p')
HOST_LOCALE_LANG=$(echo "${HOST_LOCALE}" | sed -n 's/^\([^.]*\)\..*$/\1/p')
fi
# Add fallback values in case host's locale is not set correctly
if [ -z "${HOST_LOCALE:-}" ] || [ "${HOST_LOCALE:-}" = "C.UTF-8" ]; then
HOST_LOCALE="en_US.UTF-8"
HOST_LOCALE_ENCODING="UTF-8"
HOST_LOCALE_LANG="en_US"
fi
# get_locked_mount_flags will print mount flags considered "locked".
# Arguments:
# src: path to the file/directory
# Expected env variables:
# None
# Expected global variables:
# None
# Outputs:
# Comma-separated list of locked mount flags
get_locked_mount_flags()
{
src="$1"
prev=""
locked_flags=""
# If findmnt does not exist, exit
if ! command -v findmnt 2> /dev/null > /dev/null; then
return 0
fi
# If we can't read the file/directory, exit
if ! ls "${src}" 2> /dev/null > /dev/null; then
return 0
fi
# Get mount flags of given file/directory, using nearest mountpoint.
# Earlier versions of findmnt did not check parents until it found a mountpoint,
# so we use a workaround with dirname.
while true; do
flags="$(findmnt --noheadings --output OPTIONS --target "${src}" || :)"
# shellcheck disable=SC2181
if [ -n "${flags}" ]; then
break
fi
prev="${src}"
src="$(dirname "${src}")"
[ "${src}" = "${prev}" ] && return 1
done
for flag in nodev noexec nosuid; do
if printf "%s" "${flags}" | grep -q "${flag}"; then
# Locked flag found, append to list while avoiding leading/trailing commas
locked_flags="${locked_flags:+${locked_flags},}${flag}"
fi
done
printf "%s" "${locked_flags}"
}
# init_readlink is a simplistic implementation for
# readlink -fm
# we use this as readlink -fm does not work on
# busybox systems, and we need the path even for broken links.
# Arguments:
# source file
# Expected env variables:
# None
# Expected global variables:
# None
# Outputs:
# original path the link is pointing
init_readlink()
{
# shellcheck disable=SC2010
ls -l "${1}" | grep -Eo '\->.*' | cut -d' ' -f2- | sed 's|\.\./|/|g'
}
# mount_bind will perform a bind mount for inputs or error
# Arguments:
# source_dir: string what to mount
# target_dir: string where to mount
# mount_flags: list of mount flags -> optional
# Expected env variables:
# None
# Expected global variables:
# None
# Outputs:
# No output if all ok
# Error if not
mount_bind()
{
source_dir="$1"
target_dir="$2"
mount_flags=""
if [ "$#" -gt 2 ]; then
mount_flags="$3"
fi
# Adjust source_dir in order to point to /run/host if it's a symlink
if [ -L "${source_dir}" ]; then
source_dir="$(init_readlink "${source_dir}")"
if ! printf "%s" "${source_dir}" | grep -q "/run/host"; then
source_dir="/run/host${source_dir}"
fi
fi
# if source dir doesn't exist, just exit
if [ ! -d "${source_dir}" ] && [ ! -f "${source_dir}" ]; then
return 0
fi
# if target_dir exists, check if it is a mountpoint and umount it.
if [ -e "${target_dir}" ] && findmnt "${target_dir}" > /dev/null; then
umount "${target_dir}"
fi
# if target_dir exists, and is a symlink, remove it
if [ -L "${target_dir}" ]; then
rm -f "${target_dir}"
fi
# if the source_dir exists, then create the target_dir
if [ -d "${source_dir}" ]; then
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
if [ ! -d "$(dirname "${target_dir}")" ]; then
mkdir -p "$(dirname "${target_dir}")"
fi
# if we encounter a broken link, and we touch it
# then remove the broken link, the next touch
# will cover it.
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 specified, use rslave as default.
# bind mount source_dir to target_dir, return error if not successful
if [ "${mount_flags}" = "" ]; then
if ! mount --rbind "${source_dir}" "${target_dir}"; then
printf "Warning: failed to bind mount %s to %s\n" "${source_dir}" "${target_dir}"
return 1
fi
if ! mount --make-rslave "${target_dir}"; then
printf "Warning: failed to make rslave to %s\n" "${target_dir}"
return 1
fi
elif ! mount --rbind -o "${mount_flags}" "${source_dir}" "${target_dir}"; then
printf "Warning: failed to bind mount %s to %s using option %s\n" "${source_dir}" "${target_dir}" "${mount_flags}"
return 1
fi
return 0
}
if [ -n "${pre_init_hook}" ]; then
printf "distrobox: Executing pre-init hooks...\n"
# execute pre-init hooks if specified
# shellcheck disable=SC2086
eval ${pre_init_hook}
fi
###############################################################################
printf "distrobox: Installing basic packages...\n"
# 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"}")"
# Ash shell is an exception, it is not a standalone package, but part of busybox.
# for this reason, use this quirk to adjust the package name to standard bash.
if [ "${shell_pkg}" = "ash" ]; then
shell_pkg="bash"
fi
# setup_pkg_manager_hooks will create umount/remount hooks script for a package
# manager. Mainly used by apt and pacman.
# Arguments:
# None
# Expected global variables:
# init: if this is an initful container
# HOST_MOUNTS_RO_INIT: list of mountpoints of an initful system, to avoid
# Expected env variables:
# None
# Outputs:
# None
setup_pkg_manager_hooks()
{
if {
[ -d "/etc/dpkg/dpkg.cfg.d/" ] || [ -d "/usr/share/libalpm/scripts" ]
} && [ "${init}" -eq 0 ]; then
cat << EOF > /etc/distrobox-pre-hook.sh
#!/bin/sh
mounts="${HOST_MOUNTS_RO_INIT}"
for mount in \$mounts; do
if findmnt \$mount >/dev/null; then
umount -l \$mount
fi
done
EOF
cat << EOF > /etc/distrobox-post-hook.sh
#!/bin/sh
mounts="${HOST_MOUNTS_RO_INIT}"
for mount in \$mounts; do
if [ -e /run/host/\$mount ] || [ -e /run/host/\$(readlink -fm /run/host/\$mount) ]; then
if [ ! -d /run/host/\$mount ]; then
rm -f \$mount && touch \$mount
fi
if ! mount --rbind \$(readlink -fm /run/host/\$mount) \$mount; then
mount --rbind /run/host/\$(readlink -fm /run/host/\$mount) \$mount
fi
fi
done
EOF
chmod +x /etc/distrobox-pre-hook.sh /etc/distrobox-post-hook.sh
fi
}
# setup_deb_exceptions will create path-excludes for host mounts, dpkg/apt only.
# Arguments:
# None
# Expected global variables:
# init: if this is an initful container
# HOST_MOUNTS_RO: list of readonly mountpoints, to avoid
# HOST_MOUNTS: list of readwrite mountpoints, to avoid
# Expected env variables:
# None
# Outputs:
# None
setup_deb_exceptions()
{
setup_pkg_manager_hooks
# In case of an DEB distro, we can specify that our bind_mount directories
# have to be ignored. This prevents conflicts during package installations.
if [ "${init}" -eq 0 ]; then
# Loop through all the environment vars
# and export them to the container.
mkdir -p /etc/dpkg/dpkg.cfg.d/
printf "" > /etc/dpkg/dpkg.cfg.d/00_distrobox
for net_mount in ${HOST_MOUNTS_RO} ${HOST_MOUNTS}; do
printf "path-exclude %s/*\n" "${net_mount}" >> /etc/dpkg/dpkg.cfg.d/00_distrobox
done
# Also we put a hook to clear some critical paths that do not play well
# with read only filesystems, like Systemd.
if [ -d "/etc/apt/apt.conf.d/" ]; then
printf 'DPkg::Pre-Invoke {/etc/distrobox-pre-hook.sh};\n' > /etc/apt/apt.conf.d/00_distrobox
printf 'DPkg::Post-Invoke {/etc/distrobox-post-hook.sh};\n' >> /etc/apt/apt.conf.d/00_distrobox
fi
fi
}
# setup_pacman_exceptions will set pre/post transaction hooks to avoid host's mounts, pacman only.
# Arguments:
# None
# Expected global variables:
# init: if this is an initful container
# Expected env variables:
# None
# Outputs:
# None
setup_pacman_exceptions()
{
# Workarounds for pacman. We need to exclude the paths by using a pre-hook to umount them and a
# post-hook to remount them. Additionally we neutralize the systemd-post-hooks as they do not
# work on a rootless container system.
if [ -d "/usr/share/libalpm/scripts" ] && [ "${init}" -eq 0 ]; then
# in case we're not using an init image, neutralize systemd post installation hooks
# so that we do not encounter problems along the way.
# This will be removed if we're using --init.
cat << EOF > /usr/share/libalpm/scripts/distrobox_post_hook.sh
#!/bin/sh
echo -e '#!/bin/sh\nexit 0' > /usr/share/libalpm/scripts/systemd-hook;
EOF
chmod +x /usr/share/libalpm/scripts/distrobox_post_hook.sh
# create hooks files for them
find /usr/share/libalpm/hooks/*distrobox* -delete || :
for hook in /etc/distrobox-pre-hook.sh /etc/distrobox-post-hook.sh /usr/share/libalpm/scripts/distrobox_post_hook.sh; do
when="PostTransaction"
[ -z "${hook##*pre*}" ] && when="PreTransaction"
cat << EOF > "/usr/share/libalpm/hooks/$(basename "${hook}").hook"
[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = *
[Action]
Description = Distrobox hook ${hook}...
When = ${when}
Exec = ${hook}
EOF
done
fi
}
# setup_rpm_exceptions will create path-excludes for host mounts, rpm only (dnf, yum, zypper, microdnf).
# Arguments:
# None
# Expected global variables:
# init: if this is an initful container
# HOST_MOUNTS_RO: list of readonly mountpoints, to avoid
# HOST_MOUNTS: list of readwrite mountpoints, to avoid
# Expected env variables:
# None
# Outputs:
# None
setup_rpm_exceptions()
{
# 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 [ "${init}" -eq 0 ]; then
mkdir -p /usr/lib/rpm/macros.d/
# Loop through all the environment vars
# and export them to the container.
net_mounts=""
for net_mount in \
${HOST_MOUNTS_RO} ${HOST_MOUNTS} \
'/dev' '/proc' '/sys' '/tmp' \
'/etc/hosts' '/etc/resolv.conf' '/etc/passwd' '/etc/shadow'; 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
}
# setup_xbps_exceptions will create path-excludes for host mounts, xbps only.
# Arguments:
# None
# Expected global variables:
# None
# Expected env variables:
# None
# Outputs:
# None
setup_xbps_exceptions()
{
# We have to lock this paths from xbps extraction, as it's incompatible with distrobox's
# mount process.
cat << EOF > /etc/xbps.d/distrobox-ignore.conf
noextract=/etc/passwd
noextract=/etc/hosts
noextract=/etc/host.conf
noextract=/etc/hostname
noextract=/etc/localtime
noextract=/etc/machine-id
noextract=/etc/resolv.conf
EOF
}
# setup_apk will upgrade or setup all packages for apk based systems.
# Arguments:
# None
# Expected global variables:
# upgrade: if we need to upgrade or not
# container_additional_packages: additional packages to install during this phase
# Expected env variables:
# None
# Outputs:
# None
setup_apk()
{
# If we need to upgrade, do it and exit, no further action required.
if [ "${upgrade}" -ne 0 ]; then
apk update
apk upgrade
exit
fi
# Check if shell_pkg is available in distro's repo. If not we
# fall back to bash, and we set the SHELL variable to bash so
# that it is set up correctly for the user.
if ! apk add "${shell_pkg}"; then
shell_pkg="bash"
fi
if apk add wolfi-base; then
deps="
busybox
gnutar
man-db
mesa
openssh-client
posix-libc-utils
"
elif apk add alpine-base; then
deps="
bash-completion
docs
gcompat
libc-utils
lsof
man-pages
mandoc
musl-utils
openssh-client-default
pinentry
tar
vte3
which
$(apk search -q mesa-dri)
$(apk search -q mesa-vulkan)
"
fi
deps="${deps:-}
${shell_pkg}
bash
bc
bzip2
coreutils
curl
diffutils
findmnt
findutils
gnupg
gpg
iproute2
iputils
keyutils
less
libcap
mount
ncurses
ncurses-terminfo
net-tools
pigz
rsync
shadow
sudo
tcpdump
tree
tzdata
umount
unzip
util-linux
util-linux-login
util-linux-misc
vulkan-loader
wget
xauth
xz
zip
$(apk search -qe procps)
"
# shellcheck disable=SC2086
found_deps="$(apk search -qe ${deps} | tr '\n' ' ')"
install_pkg=""
for dep in ${deps}; do
# shellcheck disable=SC2249
case " ${found_deps} " in
*" ${dep} "*)
install_pkg="${install_pkg} ${dep}"
;;
esac
done
# shellcheck disable=SC2086
apk add --force-overwrite ${install_pkg}
# Ensure we have tzdata installed and populated, sometimes container
# images blank the zoneinfo directory, so we reinstall the package to
# ensure population
if [ ! -e /usr/share/zoneinfo/UTC ]; then
apk del tzdata
apk add tzdata
fi
# Install additional packages passed at distrbox-create time
if [ -n "${container_additional_packages}" ]; then
# shellcheck disable=SC2086
apk add --force-overwrite ${container_additional_packages}
fi
}
# setup_apt will upgrade or setup all packages for apt based systems.
# Arguments:
# None
# Expected global variables:
# upgrade: if we need to upgrade or not
# container_additional_packages: additional packages to install during this phase
# Expected env variables:
# None
# Outputs:
# None
setup_apt()
{
export DEBIAN_FRONTEND=noninteractive
# If we need to upgrade, do it and exit, no further action required.
if [ "${upgrade}" -ne 0 ]; then
apt-get update
apt-get upgrade -o Dpkg::Options::="--force-confold" -y
exit
fi
# In Ubuntu official images, dpkg is configured to ignore locale and docs
# This however, results in a rather poor out-of-the-box experience
# So, let's enable them.
rm -f /etc/dpkg/dpkg.cfg.d/excludes
apt-get update
# Check if shell_pkg is available in distro's repo. If not we
# fall back to bash, and we set the SHELL variable to bash so
# that it is set up correctly for the user.
if ! apt-get install -y "${shell_pkg}"; then
shell_pkg="bash"
fi
deps="
${shell_pkg}
apt-utils
bash-completion
bc
bzip2
curl
dialog
diffutils
findutils
gnupg
gnupg2
gpgsm
hostname
iproute2
iputils-ping
keyutils
language-pack-en
less
libcap2-bin
libkrb5-3
libnss-mdns
libnss-myhostname
libvte-2.9*-common
libvte-common
locales
lsof
man-db
manpages
mtr
ncurses-base
openssh-client
passwd
pigz
pinentry-curses
procps
rsync
sudo
tcpdump
time
traceroute
tree
tzdata
unzip
util-linux
wget
xauth
xz-utils
zip
libgl1
libegl-mesa0
libegl1-mesa
libgl1-mesa-glx
libegl1
libglx-mesa0
libvulkan1
mesa-vulkan-drivers
"
# shellcheck disable=SC2086,2046
apt-get install -y $(apt-cache show ${deps} 2> /dev/null | grep "Package:" | sort -u | cut -d' ' -f2-)
# Setup hooks
setup_deb_exceptions
# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if [ -e /etc/locale.gen ] && {
! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"
}; then
sed -i "s|#.*en_US.UTF-8|en_US.UTF-8|g" /etc/locale.gen
sed -i "s|#.*${HOST_LOCALE}|${HOST_LOCALE}|g" /etc/locale.gen
locale-gen
update-locale LC_ALL="${HOST_LOCALE}" LANG="${HOST_LOCALE}"
dpkg-reconfigure locales
fi
# Ensure we have tzdata installed and populated, sometimes container
# images blank the zoneinfo directory, so we reinstall the package to
# ensure population
if [ ! -e /usr/share/zoneinfo/UTC ]; then
apt-get --reinstall install tzdata
fi
# Install additional packages passed at distrbox-create time
if [ -n "${container_additional_packages}" ]; then
# shellcheck disable=SC2086
apt-get install -y ${container_additional_packages}
fi
}
# setup_dnf will upgrade or setup all packages for dnf/yum based systems.
# Arguments:
# manager: yum or dnf
# Expected global variables:
# upgrade: if we need to upgrade or not
# container_additional_packages: additional packages to install during this phase
# Expected env variables:
# None
# Outputs:
# None
setup_dnf()
{
setup_rpm_exceptions
manager=$1
# If we need to upgrade, do it and exit, no further action required.
if [ "${upgrade}" -ne 0 ]; then
${manager} upgrade -y
exit
fi
# In dnf family official images, dnf is configured to ignore locale and docs
# This however, results in a rather poor out-of-the-box experience
# So, let's enable them.
[ -e /etc/dnf/dnf.conf ] && sed -i '/tsflags=nodocs/d' /etc/dnf/dnf.conf
[ -e /etc/yum.conf ] && sed -i '/tsflags=nodocs/d' /etc/yum.conf
# Check if shell_pkg is available in distro's repo. If not we
# fall back to bash, and we set the SHELL variable to bash so
# that it is set up correctly for the user.
if ! ${manager} install -y "${shell_pkg}" 2> /dev/null; then
shell_pkg="bash"
fi
flags=""
if [ "${manager}" = "dnf" ]; then
flags="--allowerasing"
fi
deps="
${shell_pkg}
bash-completion
bc
bzip2
cracklib-dicts
curl
diffutils
dnf-plugins-core
findutils
glibc-all-langpacks
glibc-common
glibc-locale-source
gnupg2
gnupg2-smime
hostname
iproute
iputils
keyutils
krb5-libs
less
lsof
man-db
man-pages
mtr
ncurses
nss-mdns
openssh-clients
pam
passwd
pigz
pinentry
procps-ng
rsync
shadow-utils
sudo
tcpdump
time
traceroute
tree
tzdata
unzip
util-linux
vte-profile
wget
which
whois
words
xorg-x11-xauth
xz
zip
mesa-dri-drivers
mesa-vulkan-drivers
vulkan
"
# shellcheck disable=SC2086,2046,2248
${manager} install ${flags} -y $(${manager} list -q ${deps} |
grep -v "Packages" |
grep "$(uname -m)" |
cut -d' ' -f1)
# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if [ ! -e /usr/share/i18n/charmaps ]; then
${manager} reinstall -y glibc-common
fi
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
LANG="${HOST_LOCALE}" localedef -i "${HOST_LOCALE_LANG}" -f "${HOST_LOCALE_ENCODING}" "${HOST_LOCALE}"
fi
# Ensure we have tzdata installed and populated, sometimes container
# images blank the zoneinfo directory, so we reinstall the package to
# ensure population
if [ ! -e /usr/share/zoneinfo/UTC ]; then
${manager} reinstall -y tzdata
fi
# Install additional packages passed at distrbox-create time
if [ -n "${container_additional_packages}" ]; then
# shellcheck disable=SC2086
${manager} install -y ${container_additional_packages}
fi
}
# setup_emerge will upgrade or setup all packages for gentoo based systems.
# Arguments:
# None
# Expected global variables:
# upgrade: if we need to upgrade or not
# container_additional_packages: additional packages to install during this phase
# Expected env variables:
# None
# Outputs:
# None
setup_emerge()
{
# Check if the container we are using has a ::gentoo repo defined,
# if it is defined and it is empty, then synchroznize it.
gentoo_repo="$(portageq get_repo_path / gentoo)"
if [ -n "${gentoo_repo}" ] && [ ! -e "${gentoo_repo}" ]; then
emerge-webrsync
fi
# If we need to upgrade, do it and exit, no further action required.
if [ "${upgrade}" -ne 0 ]; then
emerge --sync
exit
fi
# Check if shell_pkg is available in distro's repo. If not we
# fall back to bash, and we set the SHELL variable to bash so
# that it is set up correctly for the user.
if ! emerge --ask=n --autounmask-continue --noreplace --quiet-build "${shell_pkg}"; then
shell_pkg="bash"
fi