Debian Codegraph @

toolbox

raw
1 #!/bin/sh
2 #
3 # Copyright © 2018 – 2022 Red Hat, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 # THIS IMPLEMENTATION OF TOOLBOX IS NO LONGER MAINTAINED.
19 # Please, use the Go implementation instead:
20 # https://containertoolbx.org/install/
21
22 exec 3>/dev/null
23
24 arguments=""
25 assume_yes=false
26 base_toolbox_command=$(basename "$0" 2>&3)
27 base_toolbox_image=""
28 cgroups_version=""
29
30 # Based on the nameRegex value in:
31 # https://github.com/containers/libpod/blob/master/libpod/options.go
32 container_name_regexp="[a-zA-Z0-9][a-zA-Z0-9_.-]*"
33
34 environment=$(set)
35 environment_variables="COLORTERM \
36 COLUMNS \
37 DBUS_SESSION_BUS_ADDRESS \
38 DBUS_SYSTEM_BUS_ADDRESS \
39 DESKTOP_SESSION \
40 DISPLAY \
41 LANG \
42 LINES \
43 SHELL \
44 SSH_AUTH_SOCK \
45 TERM \
46 TOOLBOX_PATH \
47 USER \
48 VTE_VERSION \
49 WAYLAND_DISPLAY \
50 XAUTHORITY \
51 XDG_CURRENT_DESKTOP \
52 XDG_DATA_DIRS \
53 XDG_MENU_PREFIX \
54 XDG_RUNTIME_DIR \
55 XDG_SEAT \
56 XDG_SESSION_DESKTOP \
57 XDG_SESSION_ID \
58 XDG_SESSION_TYPE \
59 XDG_VTNR"
60 fgc=""
61
62 podman_command="podman"
63 registry="registry.fedoraproject.org"
64 registry_candidate="candidate-registry.fedoraproject.org"
65 release=""
66 release_default=""
67 spinner_animation="[>----] [=>---] [==>--] [===>-] [====>] [----<] [---<=] [--<==] [-<===] [<====]"
68 spinner_template="toolbox-spinner-XXXXXXXXXX"
69 tab="$(printf '\t')"
70 toolbox_command_path=""
71 toolbox_container=""
72 toolbox_container_default=""
73 toolbox_container_old_v1=""
74 toolbox_container_old_v2=""
75 toolbox_container_prefix_default=""
76 toolbox_image=""
77 toolbox_runtime_directory="$XDG_RUNTIME_DIR"/toolbox
78 user_id_real=$(id -ru 2>&3)
79 verbose=false
80
81
82 LGC='\033[1;32m' # Light Green Color
83 NC='\033[0m' # No Color
84
85
86 has_prefix()
87 (
88 str="$1"
89 prefix="$2"
90
91 ret_val=1
92
93 case "$str" in
94 "$prefix"* )
95 ret_val=0
96 ;;
97 * )
98 ret_val=1
99 ;;
100 esac
101
102 return "$ret_val"
103 )
104
105
106 has_substring()
107 (
108 haystack="$1"
109 needle="$2"
110
111 ret_val=1
112
113 case "$haystack" in
114 *"$needle"* )
115 ret_val=0
116 ;;
117 * )
118 ret_val=1
119 ;;
120 esac
121
122 return "$ret_val"
123 )
124
125
126 is_integer()
127 {
128 [ "$1" != "" ] && [ "$1" -eq "$1" ] 2>&3
129 return "$?"
130 }
131
132
133 save_positional_parameters()
134 {
135 for i; do
136 printf "%s\\n" "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" 2>&3
137 done
138 echo " "
139 }
140
141
142 spinner_start()
143 (
144 directory="$1"
145 message="$2"
146
147 if $verbose; then
148 rm --force --recursive "$directory" 2>&3
149 return 0
150 fi
151
152 if ! touch "$directory/spinner-start" 2>&3; then
153 echo "$base_toolbox_command: unable to start spinner: spinner start file couldn't be created" >&2
154 return 1
155 fi
156
157 printf "%s" "$message"
158 tput civis 2>&3
159
160 exec 4>"$directory/spinner-start"
161 if ! flock 4 2>&3; then
162 echo "$base_toolbox_command: unable to start spinner: spinner lock couldn't be acquired" >&2
163 return 1
164 fi
165
166 (
167 while [ -f "$directory/spinner-start" ]; do
168 echo "$spinner_animation" | sed "s/ /\n/g" 2>&3 | while read -r frame; do
169 if ! [ -f "$directory/spinner-start" ] 2>&3; then
170 break
171 fi
172
173 printf "%s" "$frame"
174
175 frame_len=${#frame}
176 i=0
177 while [ "$i" -lt "$frame_len" ]; do
178 printf "\b"
179 i=$((i + 1))
180 done
181
182 sleep 1
183 done
184 done
185
186 printf "\033[2K" # delete entire line regardless of cursor position
187 printf "\r"
188 tput cnorm 2>&3
189 ) &
190
191 return 0
192 )
193
194
195 spinner_stop()
196 (
197 $verbose && return
198 directory="$1"
199
200 exec 4>"$directory/spinner-start"
201
202 if ! rm "$directory/spinner-start" 2>&3; then
203 echo "$base_toolbox_command: unable to stop spinner: spinner start file couldn't be removed" >&2
204 return
205 fi
206
207 if ! flock 4 2>&3; then
208 echo "$base_toolbox_command: unable to stop spinner: spinner lock couldn't be acquired" >&2
209 return
210 fi
211
212 rm --force --recursive "$directory" 2>&3
213 )
214
215
216 ask_for_confirmation()
217 (
218 default_response="$1"
219 prompt="$2"
220 ret_val=0
221
222 while :; do
223 printf "%s " "$prompt"
224 read -r user_response
225
226 if [ "$user_response" = "" ] 2>&3; then
227 user_response="$default_response"
228 else
229 user_response=$(echo "$user_response" | tr "[:upper:]" "[:lower:]" 2>&3)
230 fi
231
232 if [ "$user_response" = "no" ] 2>&3 || [ "$user_response" = "n" ] 2>&3; then
233 ret_val=1
234 break
235 elif [ "$user_response" = "yes" ] 2>&3 || [ "$user_response" = "y" ] 2>&3; then
236 ret_val=0
237 break
238 fi
239 done
240
241 return "$ret_val"
242 )
243
244
245 container_name_is_valid()
246 (
247 name="$1"
248
249 echo "$name" | grep "^$container_name_regexp$" >/dev/null 2>&3
250 return "$?"
251 )
252
253
254 container_start()
255 (
256 container="$1"
257
258 error_message=$( ($podman_command start "$container" >/dev/null) 2>&1)
259 ret_val="$?"
260 [ "$error_message" != "" ] 2>&3 && echo "$error_message" >&3
261
262 if [ "$ret_val" -ne 0 ] 2>&3; then
263 if echo "$error_message" | grep "use system migrate to mitigate" >/dev/null 2>&3; then
264 echo "$base_toolbox_command: checking if 'podman system migrate' supports --new-runtime" >&3
265
266 if ! ($podman_command system migrate --help 2>&3 | grep "new-runtime" >/dev/null 2>&3); then
267 echo "$base_toolbox_command: container $container doesn't support cgroups v$cgroups_version" >&2
268 echo "Update Podman to version 1.6.2 or newer." >&2
269 return 1
270 else
271 echo "$base_toolbox_command: 'podman system migrate' supports --new-runtime" >&3
272
273 oci_runtime_required="runc"
274 [ "$cgroups_version" -eq 2 ] 2>&3 && oci_runtime_required="crun"
275
276 echo "$base_toolbox_command: migrating containers to OCI runtime $oci_runtime_required" >&3
277
278 if ! $podman_command system migrate --new-runtime "$oci_runtime_required" >/dev/null 2>&3; then
279 echo "$base_toolbox_command: failed to migrate containers to OCI runtime $oci_runtime_required" >&2
280 echo "Factory reset with: toolbox reset" >&2
281 echo "Try '$base_toolbox_command --help' for more information." >&2
282 return 1
283 fi
284
285 if ! $podman_command start "$container" >/dev/null 2>&3; then
286 echo "$base_toolbox_command: container $container doesn't support cgroups v$cgroups_version" >&2
287 echo "Factory reset with: toolbox reset" >&2
288 echo "Try '$base_toolbox_command --help' for more information." >&2
289 return 1
290 fi
291 fi
292 else
293 echo "$base_toolbox_command: failed to start container $container" >&2
294 return 1
295 fi
296 fi
297
298 return 0
299 )
300
301
302 copy_etc_profile_d_toolbox_to_container()
303 (
304 container="$1"
305
306 profile_d_lock="$toolbox_runtime_directory"/profile.d-toolbox.lock
307
308 # shellcheck disable=SC2174
309 if ! mkdir --mode 700 --parents "$toolbox_runtime_directory" 2>&3; then
310 echo "$base_toolbox_command: unable to copy $toolbox_runtime_directory/toolbox.sh: runtime directory not created" >&2
311 return 1
312 fi
313
314 exec 5>"$profile_d_lock"
315 if ! flock 5 2>&3; then
316 echo "$base_toolbox_command: unable to copy $toolbox_runtime_directory/toolbox.sh: copy lock not acquired" >&2
317 return 1
318 fi
319
320 if ! [ -f "$toolbox_runtime_directory"/toolbox.sh ] 2>&3; then
321 echo "$base_toolbox_command: $toolbox_runtime_directory/toolbox.sh not found" >&2
322 return 0
323 fi
324
325 echo "$base_toolbox_command: copying $toolbox_runtime_directory/toolbox.sh to container $container" >&3
326
327 if ! $podman_command exec \
328 --user root:root \
329 "$container" \
330 sh -c "cp $toolbox_runtime_directory/toolbox.sh /etc/profile.d" sh 2>&3; then
331 echo "$base_toolbox_command: unable to copy $toolbox_runtime_directory/toolbox.sh to container $container" >&2
332 return 1
333 fi
334
335 return 0
336 )
337
338
339 copy_etc_profile_d_toolbox_to_runtime_directory()
340 (
341 profile_d_lock="$toolbox_runtime_directory"/profile.d-toolbox.lock
342
343 if ! [ -f /etc/profile.d/toolbox.sh ] 2>&3; then
344 echo "$base_toolbox_command: /etc/profile.d/toolbox.sh not found" >&2
345 return 0
346 fi
347
348 # shellcheck disable=SC2174
349 if ! mkdir --mode 700 --parents "$toolbox_runtime_directory" 2>&3; then
350 echo "$base_toolbox_command: unable to copy /etc/profile.d/toolbox.sh: runtime directory not created" >&2
351 return 1
352 fi
353
354 exec 5>"$profile_d_lock"
355 if ! flock 5 2>&3; then
356 echo "$base_toolbox_command: unable to copy /etc/profile.d/toolbox.sh: copy lock not acquired" >&2
357 return 1
358 fi
359
360 echo "$base_toolbox_command: copying /etc/profile.d/toolbox.sh to $toolbox_runtime_directory" >&3
361
362 if ! cp /etc/profile.d/toolbox.sh "$toolbox_runtime_directory" 2>&3; then
363 echo "$base_toolbox_command: unable to copy /etc/profile.d/toolbox.sh to $toolbox_runtime_directory" >&2
364 return 1
365 fi
366
367 return 0
368 )
369
370
371 create_enter_command()
372 (
373 container="$1"
374
375 if [ "$container" = "$toolbox_container_default" ] 2>&3; then
376 echo "$base_toolbox_command enter"
377 elif [ "$container" = "$toolbox_container_prefix_default-$release" ] 2>&3; then
378 echo "$base_toolbox_command enter --release $release"
379 else
380 echo "$base_toolbox_command enter --container $container"
381 fi
382 )
383
384
385 create_environment_options()
386 (
387 columns=""
388 lines=""
389
390 if terminal_size=$(stty size 2>&3); then
391 columns=$(echo "$terminal_size" | cut --delimiter " " --fields 2 2>&3)
392 if ! is_integer "$columns"; then
393 echo "$base_toolbox_command: failed to parse the number of columns from the terminal size" >&3
394 columns=""
395 fi
396
397 lines=$(echo "$terminal_size" | cut --delimiter " " --fields 1 2>&3)
398 if ! is_integer "$lines"; then
399 echo "$base_toolbox_command: failed to parse the number of lines from the terminal size" >&3
400 lines=""
401 fi
402 else
403 echo "$base_toolbox_command: failed to read terminal size" >&3
404 fi
405
406 echo "$environment_variables" \
407 | sed "s/ \+/\n/g" 2>&3 \
408 | {
409 environment_options=""
410 echo "$base_toolbox_command: creating list of environment variables to forward" >&3
411 value=""
412 while read -r variable; do
413 if echo "$environment" | grep "^$variable" >/dev/null 2>&3; then
414 eval value="$""$variable"
415 echo "$base_toolbox_command: $variable=$value" >&3
416 environment_options="$environment_options --env=$variable=$value"
417 else
418 echo "$base_toolbox_command: $variable is unset" >&3
419 fi
420 done
421
422 if ! (echo "$environment_options" | grep COLUMNS >/dev/null 2>&3) && [ "$columns" != "" ] 2>&3; then
423 environment_options="$environment_options --env=COLUMNS=$columns"
424 fi
425
426 if ! (echo "$environment_options" | grep LINES >/dev/null 2>&3) && [ "$lines" != "" ] 2>&3; then
427 environment_options="$environment_options --env=LINES=$lines"
428 fi
429
430 environment_options=${environment_options#" "}
431 echo "$base_toolbox_command: created options for environment variables to forward" >&3
432 echo "$environment_options" >&3
433 echo "$environment_options"
434 }
435 )
436
437
438 create_toolbox_container_name()
439 (
440 image="$1"
441
442 basename=$(image_reference_get_basename "$image")
443 if [ "$basename" = "" ] 2>&3; then
444 return 100
445 fi
446
447 tag=$(image_reference_get_tag "$image")
448 if [ "$tag" = "" ] 2>&3; then
449 return 101
450 fi
451
452 echo "$basename-$tag"
453 return 0
454 )
455
456
457 create_toolbox_image_name()
458 (
459 # Based on the ResolveName function implemented in:
460 # https://github.com/containers/buildah/blob/master/util/util.go
461
462 if image_reference_can_be_id "$base_toolbox_image"; then
463 if base_toolbox_image_id=$($podman_command inspect \
464 --format "{{.Id}}" \
465 --type image \
466 "$base_toolbox_image" 2>&3); then
467 if has_prefix "$base_toolbox_image_id" "$base_toolbox_image"; then
468 echo "$base_toolbox_image-$USER:latest"
469 return 0
470 fi
471 fi
472 fi
473
474 basename=$(image_reference_get_basename "$base_toolbox_image")
475 if [ "$basename" = "" ] 2>&3; then
476 return 100
477 fi
478
479 tag=$(image_reference_get_tag "$base_toolbox_image")
480 if [ "$tag" = "" ] 2>&3; then
481 echo "$basename-$USER:latest"
482 else
483 echo "$basename-$USER:$tag"
484 fi
485
486 return 0
487 )
488
489
490 enter_print_container_not_found()
491 (
492 container="$1"
493
494 echo "$base_toolbox_command: container $container not found" >&2
495 echo "Use the 'create' command to create a toolbox." >&2
496 echo "Try '$base_toolbox_command --help' for more information." >&2
497 )
498
499
500 get_cgroups_version()
501 (
502 version=1
503
504 if ! mounts=$(mount 2>&3); then
505 echo "$base_toolbox_command: failed to detect cgroups version: couldn't list mount points" >&2
506 return 1
507 fi
508
509 if ! (echo "$mounts" | grep "^cgroup " >/dev/null 2>&3) && (echo "$mounts" | grep "^cgroup2 " >/dev/null 2>&3); then
510 version=2
511 fi
512
513 echo "$version"
514 return 0
515 )
516
517
518 get_group_for_sudo()
519 (
520 group=""
521 if getent group sudo >/dev/null 2>&3; then
522 group="sudo"
523 elif getent group wheel >/dev/null 2>&3; then
524 group="wheel"
525 else
526 return 1
527 fi
528
529 echo "$group"
530 return 0
531 )
532
533
534 get_host_id()
535 (
536 # shellcheck disable=SC1091
537 . /usr/lib/os-release
538 echo "$ID"
539 )
540
541
542 get_host_variant_id()
543 (
544 # shellcheck disable=SC1091
545 . /usr/lib/os-release
546 echo "$VARIANT_ID"
547 )
548
549
550 get_host_version_id()
551 (
552 # shellcheck disable=SC1091
553 . /usr/lib/os-release
554 echo "$VERSION_ID"
555 )
556
557
558 image_reference_can_be_id()
559 (
560 image="$1"
561
562 echo "$image" | grep "^[a-f0-9]\{6,64\}$" >/dev/null 2>&3
563 return "$?"
564 )
565
566
567 image_reference_get_basename()
568 (
569 image="$1"
570
571 domain=$(image_reference_get_domain "$image")
572 remainder=${image#"$domain"}
573 path=${remainder%:*}
574 basename=${path##*/}
575 echo "$basename"
576 )
577
578
579 image_reference_get_domain()
580 (
581 image="$1"
582
583 image_reference_has_domain "$image" && domain=${image%%/*}
584 echo "$domain"
585 )
586
587
588 image_reference_get_tag()
589 (
590 image="$1"
591
592 domain=$(image_reference_get_domain "$image")
593 remainder=${image#"$domain"}
594
595 tag=""
596 if (echo "$remainder" | grep ":" >/dev/null 2>&3); then
597 tag=${remainder#*:}
598 fi
599
600 echo "$tag"
601 )
602
603
604 image_reference_has_domain()
605 (
606 # Based on the splitDockerDomain function implemented in:
607 # https://github.com/docker/distribution/blob/master/reference/normalize.go
608
609 image="$1"
610
611 if ! (echo "$image" | grep "/" >/dev/null 2>&3); then
612 return 1
613 fi
614
615 prefix=${image%%/*}
616 if ! (echo "$prefix" | grep "[.:]" >/dev/null 2>&3) && [ "$prefix" != "localhost" ] 2>&3; then
617 return 1
618 fi
619
620 return 0
621 )
622
623
624 images_get_details()
625 (
626 images="$1"
627
628 if ! echo "$images" | while read -r image; do
629 [ "$image" = "" ] 2>&3 && continue
630
631 if ! $podman_command images \
632 --format "{{.ID}} {{.Repository}}:{{.Tag}} {{.Created}}" \
633 --noheading \
634 "$image" 2>&3; then
635 echo "$base_toolbox_command: failed to get details for image $image" >&2
636 return 1
637 fi
638 echo
639 done; then
640 return 1
641 fi
642
643 return 0
644 )
645
646
647 is_etc_profile_d_toolbox_a_bind_mount()
648 {
649 container="$1"
650
651 $podman_command inspect --format "[{{range .Mounts}}{{.Dst}} {{end}}]" --type container "$container" 2>&3 \
652 | grep /etc/profile.d/toolbox.sh >/dev/null 2>/dev/null 2>&3
653
654 return "$?"
655 }
656
657
658 list_container_names()
659 (
660 if ! containers_old=$($podman_command ps \
661 --all \
662 --filter "label=com.redhat.component=fedora-toolbox" \
663 --format "{{.Names}}" 2>&3); then
664 echo "$base_toolbox_command: failed to list containers with com.redhat.component=fedora-toolbox" >&2
665 return 1
666 fi
667
668 if ! containers=$($podman_command ps \
669 --all \
670 --filter "label=com.github.debarshiray.toolbox=true" \
671 --format "{{.Names}}" 2>&3); then
672 echo "$base_toolbox_command: failed to list containers with com.github.debarshiray.toolbox=true" >&2
673 return 1
674 fi
675
676 printf "%s\n%s\n" "$containers_old" "$containers" | sort 2>&3 | uniq 2>&3
677 return 0
678 )
679
680
681 mount_bind()
682 (
683 source="$1"
684 target="$2"
685 mount_flags="$3"
686
687 mount_o=""
688
689 ! [ -d "$source" ] 2>&3 && ! [ -f "$source" ] 2>&3 && return 0
690
691 if [ -d "$source" ] 2>&3; then
692 echo "$base_toolbox_command: creating $target" >&3
693
694 if ! mkdir --parents "$target" 2>&3; then
695 echo "$base_toolbox_command: failed to create $target" >&2
696 return 1
697 fi
698 fi
699
700 echo "$base_toolbox_command: binding $target to $source" >&3
701
702 [ "$mount_flags" = "" ] 2>&3 || mount_o="-o $mount_flags"
703
704 # shellcheck disable=SC2086
705 if ! mount --rbind $mount_o "$source" "$target" 2>&3; then
706 echo "$base_toolbox_command: failed to bind $target to $source" >&2
707 return 1
708 fi
709
710 return 0
711 )
712
713
714 pull_base_toolbox_image()
715 (
716 domain=""
717 has_domain=false
718 prompt_for_download=true
719 pull_image=false
720
721 if image_reference_can_be_id "$base_toolbox_image"; then
722 echo "$base_toolbox_command: looking for image $base_toolbox_image" >&3
723
724 if $podman_command image exists "$base_toolbox_image" >/dev/null 2>&3; then
725 return 0
726 fi
727 fi
728
729 image_reference_has_domain "$base_toolbox_image" && has_domain=true
730
731 if ! $has_domain; then
732 echo "$base_toolbox_command: looking for image localhost/$base_toolbox_image" >&3
733
734 if $podman_command image exists "localhost/$base_toolbox_image" >/dev/null 2>&3; then
735 return 0
736 fi
737 fi
738
739 if $has_domain; then
740 base_toolbox_image_full="$base_toolbox_image"
741 else
742 base_toolbox_image_full="$registry/$fgc/$base_toolbox_image"
743 fi
744
745 echo "$base_toolbox_command: looking for image $base_toolbox_image_full" >&3
746
747 if $podman_command image exists "$base_toolbox_image_full" >/dev/null 2>&3; then
748 return 0
749 fi
750
751 domain=$(image_reference_get_domain "$base_toolbox_image_full")
752 if $assume_yes || [ "$domain" = "localhost" ] 2>&3; then
753 prompt_for_download=false
754 pull_image=true
755 fi
756
757 if $prompt_for_download; then
758 echo "Image required to create toolbox container."
759
760 prompt=$(printf "Download %s (500MB)? [y/N]:" "$base_toolbox_image_full")
761 if ask_for_confirmation "n" "$prompt"; then
762 pull_image=true
763 else
764 pull_image=false
765 fi
766 fi
767
768 if ! $pull_image; then
769 return 1
770 fi
771
772 echo "$base_toolbox_command: pulling image $base_toolbox_image_full" >&3
773
774 if spinner_directory=$(mktemp --directory --tmpdir $spinner_template 2>&3); then
775 spinner_message="Pulling $base_toolbox_image_full: "
776 if ! spinner_start "$spinner_directory" "$spinner_message"; then
777 spinner_directory=""
778 fi
779 else
780 echo "$base_toolbox_command: unable to start spinner: spinner directory not created" >&2
781 spinner_directory=""
782 fi
783
784 $podman_command pull "$base_toolbox_image_full" >/dev/null 2>&3
785 ret_val=$?
786
787 if [ "$spinner_directory" != "" ]; then
788 spinner_stop "$spinner_directory"
789 fi
790
791 if [ "$ret_val" -ne 0 ] 2>&3; then
792 echo "$base_toolbox_command: failed to pull base image $base_toolbox_image" >&2
793 fi
794
795 return $ret_val
796 )
797
798
799 unshare_userns_rm()
800 (
801 path="$1"
802
803 if ! unshare_directory=$(mktemp --directory --tmpdir "toolbox-unshare-userns-rm-XXXXXXXXXX" 2>&3); then
804 echo "$base_toolbox_command: failed to enter user namespace: directory couldn't be created" >&2
805 return 1
806 fi
807
808 if ! touch "$unshare_directory/map" 2>&3; then
809 echo "$base_toolbox_command: failed to enter user namespace: file couldn't be created" >&2
810 return 1
811 fi
812
813 exec 4>"$unshare_directory/map"
814 if ! flock 4 2>&3; then
815 echo "$base_toolbox_command: failed to enter user namespace: lock couldn't be acquired" >&2
816 return 1
817 fi
818
819 echo "$base_toolbox_command: parsing /etc/subgid" >&3
820
821 if ! subgid_entry=$(grep "^$USER:" /etc/subgid 2>&3); then
822 echo "$base_toolbox_command: failed to enter user namespace: no entry in /etc/subgid" >&2
823 return 1
824 fi
825
826 userns_gid_start=$(echo "$subgid_entry" | cut --delimiter ":" --fields 2 2>&3)
827 if ! is_integer "$userns_gid_start"; then
828 echo "$base_toolbox_command: failed to enter user namespace: cannot parse the first sub-GID" >&2
829 return 1
830 fi
831
832 userns_gid_len=$(echo "$subgid_entry" | cut --delimiter ":" --fields 3 2>&3)
833 if ! is_integer "$userns_gid_len"; then
834 echo "$base_toolbox_command: failed to enter user namespace: cannot parse the sub-GID count" >&2
835 return 1
836 fi
837
838 echo "$base_toolbox_command: parsing /etc/subuid" >&3
839
840 if ! subuid_entry=$(grep "^$USER:" /etc/subuid 2>&3); then
841 echo "$base_toolbox_command: failed to enter user namespace: no entry in /etc/subuid" >&2
842 return 1
843 fi
844
845 userns_uid_start=$(echo "$subuid_entry" | cut --delimiter ":" --fields 2 2>&3)
846 if ! is_integer "$userns_uid_start"; then
847 echo "$base_toolbox_command: failed to enter user namespace: cannot parse the first sub-UID" >&2
848 return 1
849 fi
850
851 userns_uid_len=$(echo "$subuid_entry" | cut --delimiter ":" --fields 3 2>&3)
852 if ! is_integer "$userns_uid_len"; then
853 echo "$base_toolbox_command: failed to enter user namespace: cannot parse the sub-UID count" >&2
854 return 1
855 fi
856
857 echo "$base_toolbox_command: unsharing user namespace" >&3
858
859 unshare --user sh -c "flock $unshare_directory/map rm --force --recursive $path" 2>&3 &
860 unshare_pid="$!"
861
862 echo "$base_toolbox_command: setting GID and UID map of user namespace" >&3
863
864 if ! newgidmap "$unshare_pid" 0 "$user_id_real" 1 1 "$userns_gid_start" "$userns_gid_len" 2>&3; then
865 echo "$base_toolbox_command: failed to set GID mapping of user namespace" >&2
866 kill -9 "$unshare_pid" 2>&3
867 return 1
868 fi
869
870 if ! newuidmap "$unshare_pid" 0 "$user_id_real" 1 1 "$userns_uid_start" "$userns_uid_len" 2>&3; then
871 echo "$base_toolbox_command: failed to set UID mapping of user namespace" >&2
872 kill -9 "$unshare_pid" 2>&3
873 return 1
874 fi
875
876 echo "$base_toolbox_command: GID map of user namespace:" >&3
877 cat /proc/"$unshare_pid"/gid_map 1>&3 2>&3
878
879 echo "$base_toolbox_command: UID map of user namespace:" >&3
880 cat /proc/"$unshare_pid"/uid_map 1>&3 2>&3
881
882 if ! flock --unlock 4 2>&3; then
883 echo "$base_toolbox_command: failed to remove $path: lock couldn't be unlocked" >&2
884 kill -9 "$unshare_pid" 2>&3
885 return 1
886 fi
887
888 if ! wait "$unshare_pid" 2>&3; then
889 echo "$base_toolbox_command: failed to remove $path" >&2
890 return 1
891 fi
892
893 rm --force --recursive "$unshare_directory" 2>&3
894
895 return 0
896 )
897
898
899 create()
900 (
901 enter_command_skip="$1"
902
903 dbus_system_bus_address="unix:path=/var/run/dbus/system_bus_socket"
904 home_link=""
905 kcm_socket=""
906 kcm_socket_bind=""
907 media_link=""
908 media_path_bind=""
909 mnt_link=""
910 mnt_path_bind=""
911 run_media_path_bind=""
912 toolbox_profile_bind=""
913 ulimit_host=""
914 usr_mount_destination_flags="ro"
915
916 # shellcheck disable=SC2153
917 if [ "$DBUS_SYSTEM_BUS_ADDRESS" != "" ]; then
918 dbus_system_bus_address=$DBUS_SYSTEM_BUS_ADDRESS
919 fi
920 dbus_system_bus_path=$(echo "$dbus_system_bus_address" | cut --delimiter = --fields 2 2>&3)
921 dbus_system_bus_path=$(readlink --canonicalize "$dbus_system_bus_path" 2>&3)
922
923 # Note that 'systemctl show ...' doesn't terminate with a non-zero exit
924 # code when used with an unknown unit. eg.:
925 # $ systemctl show --value --property Listen foo
926 # $ echo $?
927 # 0
928 if ! kcm_socket_listen=$(systemctl show --value --property Listen sssd-kcm.socket 2>&3); then
929 echo "$base_toolbox_command: failed to use 'systemctl show'" >&3
930 kcm_socket_listen=""
931 elif [ "$kcm_socket_listen" = "" ] 2>&3; then
932 echo "$base_toolbox_command: failed to read property Listen from sssd-kcm.socket" >&3
933 else
934 echo "$base_toolbox_command: checking value $kcm_socket_listen of property Listen in sssd-kcm.socket" >&3
935
936 if ! (echo "$kcm_socket_listen" | grep " (Stream)$" >/dev/null 2>&3); then
937 echo "$base_toolbox_command: unknown socket in sssd-kcm.socket" >&2
938 echo "$base_toolbox_command: expected SOCK_STREAM" >&2
939 kcm_socket_listen=""
940 elif ! (echo "$kcm_socket_listen" | grep "^/" >/dev/null 2>&3); then
941 echo "$base_toolbox_command: unknown socket in sssd-kcm.socket" >&2
942 echo "$base_toolbox_command: expected file system socket in the AF_UNIX family" >&2
943 kcm_socket_listen=""
944 fi
945 fi
946
947 echo "$base_toolbox_command: parsing value $kcm_socket_listen of property Listen in sssd-kcm.socket" >&3
948
949 if [ "$kcm_socket_listen" != "" ] 2>&3; then
950 kcm_socket=${kcm_socket_listen%" (Stream)"}
951 kcm_socket_bind="--volume $kcm_socket:$kcm_socket"
952 fi
953
954 echo "$base_toolbox_command: checking if 'podman create' supports --ulimit host" >&3
955
956 if man podman-create 2>&3 | grep "You can pass host" >/dev/null 2>&3; then
957 echo "$base_toolbox_command: 'podman create' supports --ulimit host" >&3
958
959 ulimit_host="--ulimit host"
960 fi
961
962 if ! pull_base_toolbox_image; then
963 return 1
964 fi
965
966 if image_reference_has_domain "$base_toolbox_image"; then
967 base_toolbox_image_full="$base_toolbox_image"
968 else
969 if ! base_toolbox_image_full=$($podman_command inspect \
970 --format "{{index .RepoTags 0}}" \
971 --type image \
972 "$base_toolbox_image" 2>&3); then
973 echo "$base_toolbox_command: failed to get RepoTag for base image $base_toolbox_image" >&2
974 return 1
975 fi
976
977 echo "$base_toolbox_command: base image $base_toolbox_image resolved to $base_toolbox_image_full" >&3
978 fi
979
980 echo "$base_toolbox_command: checking if container $toolbox_container already exists" >&3
981
982 enter_command=$(create_enter_command "$toolbox_container")
983 if $podman_command container exists "$toolbox_container" >/dev/null 2>&3; then
984 echo "$base_toolbox_command: container $toolbox_container already exists" >&2
985 echo "Enter with: $enter_command" >&2
986 echo "Try '$base_toolbox_command --help' for more information." >&2
987 return 1
988 fi
989
990 if [ -f /etc/profile.d/toolbox.sh ] 2>&3; then
991 toolbox_profile_bind="--volume /etc/profile.d/toolbox.sh:/etc/profile.d/toolbox.sh:ro"
992 elif [ -f /usr/share/profile.d/toolbox.sh ] 2>&3; then
993 toolbox_profile_bind="--volume /usr/share/profile.d/toolbox.sh:/etc/profile.d/toolbox.sh:ro"
994 else
995 echo "$base_toolbox_command: failed to bind mount toolbox.sh" >&3
996 fi
997
998 if [ -d /media ] 2>&3; then
999 echo "$base_toolbox_command: checking if /media is a symbolic link to /run/media" >&3
1000
1001 if [ "$(readlink /media)" = run/media ] 2>&3; then
1002 echo "$base_toolbox_command: /media is a symbolic link to /run/media" >&3
1003 media_link="--media-link"
1004 else
1005 media_path_bind="--volume /media:/media:rslave"
1006 fi
1007 fi
1008
1009 echo "$base_toolbox_command: checking if /mnt is a symbolic link to /var/mnt" >&3
1010
1011 if [ "$(readlink /mnt)" = var/mnt ] 2>&3; then
1012 echo "$base_toolbox_command: /mnt is a symbolic link to /var/mnt" >&3
1013 mnt_link="--mnt-link"
1014 else
1015 mnt_path_bind="--volume /mnt:/mnt:rslave"
1016 fi
1017
1018 if [ -d /run/media ] 2>&3; then
1019 run_media_path_bind="--volume /run/media:/run/media:rslave"
1020 fi
1021
1022 echo "$base_toolbox_command: checking if /usr is mounted read-only or read-write" >&3
1023
1024 if ! usr_mount_point=$(df --output=target /usr | tail --lines 1 2>&3); then
1025 echo "$base_toolbox_command: failed to get the mount-point of /usr" >&2
1026 else
1027 echo "$base_toolbox_command: mount-point of /usr is $usr_mount_point" >&3
1028
1029 if ! usr_mount_source_flags=$(findmnt --noheadings --output OPTIONS "$usr_mount_point" 2>&3); then
1030 echo "$base_toolbox_command: failed to get the mount options of $usr_mount_point" >&2
1031 else
1032 echo "$base_toolbox_command: mount flags of /usr on the host are $usr_mount_source_flags" >&3
1033
1034 if echo "$usr_mount_source_flags" | grep --invert-match "ro" >/dev/null 2>&3; then
1035 usr_mount_destination_flags="rw"
1036 fi
1037 fi
1038 fi
1039
1040 if ! home_canonical=$(readlink --canonicalize "$HOME" 2>&3); then
1041 echo "$base_toolbox_command: failed to canonicalize $HOME" >&2
1042 return 1
1043 fi
1044
1045 echo "$base_toolbox_command: $HOME canonicalized to $home_canonical" >&3
1046
1047 echo "$base_toolbox_command: checking if /home is a symbolic link to /var/home" >&3
1048
1049 if [ "$(readlink /home)" = var/home ] 2>&3; then
1050 echo "$base_toolbox_command: /home is a symbolic link to /var/home" >&3
1051 home_link="--home-link"
1052 fi
1053
1054 echo "$base_toolbox_command: calling org.freedesktop.Flatpak.SessionHelper.RequestSession" >&3
1055
1056 if ! gdbus call \
1057 --session \
1058 --dest org.freedesktop.Flatpak \
1059 --object-path /org/freedesktop/Flatpak/SessionHelper \
1060 --method org.freedesktop.Flatpak.SessionHelper.RequestSession >/dev/null 2>&3; then
1061 echo "$base_toolbox_command: failed to call org.freedesktop.Flatpak.SessionHelper.RequestSession" >&2
1062 exit 1
1063 fi
1064
1065 echo "$base_toolbox_command: creating container $toolbox_container" >&3
1066
1067 if spinner_directory=$(mktemp --directory --tmpdir $spinner_template 2>&3); then
1068 spinner_message="Creating container $toolbox_container: "
1069 if ! spinner_start "$spinner_directory" "$spinner_message"; then
1070 spinner_directory=""
1071 fi
1072 else
1073 echo "$base_toolbox_command: unable to start spinner: spinner directory not created" >&2
1074 spinner_directory=""
1075 fi
1076
1077 # shellcheck disable=SC2086
1078 $podman_command create \
1079 --dns none \
1080 --env TOOLBOX_PATH="$TOOLBOX_PATH" \
1081 --hostname toolbox \
1082 --ipc host \
1083 --label "com.github.containers.toolbox=true" \
1084 --label "com.github.debarshiray.toolbox=true" \
1085 --name $toolbox_container \
1086 --network host \
1087 --no-hosts \
1088 --pid host \
1089 --privileged \
1090 --security-opt label=disable \
1091 $ulimit_host \
1092 --userns=keep-id \
1093 --user root:root \
1094 $kcm_socket_bind \
1095 $media_path_bind \
1096 $mnt_path_bind \
1097 $run_media_path_bind \
1098 $toolbox_profile_bind \
1099 --volume "$TOOLBOX_PATH":/usr/bin/toolbox:ro \
1100 --volume "$XDG_RUNTIME_DIR":"$XDG_RUNTIME_DIR" \
1101 --volume "$XDG_RUNTIME_DIR"/.flatpak-helper/monitor:/run/host/monitor \
1102 --volume "$dbus_system_bus_path":"$dbus_system_bus_path" \
1103 --volume "$home_canonical":"$home_canonical":rslave \
1104 --volume /etc:/run/host/etc \
1105 --volume /dev:/dev:rslave \
1106 --volume /run:/run/host/run:rslave \
1107 --volume /tmp:/run/host/tmp:rslave \
1108 --volume /usr:/run/host/usr:"$usr_mount_destination_flags",rslave \
1109 --volume /var:/run/host/var:rslave \
1110 "$base_toolbox_image_full" \
1111 toolbox --verbose init-container \
1112 --home "$HOME" \
1113 $home_link \
1114 $media_link \
1115 $mnt_link \
1116 --monitor-host \
1117 --shell "$SHELL" \
1118 --uid "$user_id_real" \
1119 --user "$USER" >/dev/null 2>&3
1120 ret_val=$?
1121
1122 if [ "$spinner_directory" != "" ]; then
1123 spinner_stop "$spinner_directory"
1124 fi
1125
1126 if [ $ret_val -ne 0 ]; then
1127 echo "$base_toolbox_command: failed to create container $toolbox_container" >&2
1128 return 1
1129 fi
1130
1131 if ! $enter_command_skip; then
1132 echo "Created container: $toolbox_container"
1133 echo "Enter with: $enter_command"
1134 fi
1135
1136 return 0
1137 )
1138
1139
1140 enter()
1141 (
1142 emit_escape_sequence=false
1143 host_id=$(get_host_id)
1144 host_variant_id=$(get_host_variant_id)
1145
1146 if [ "$host_id" = "fedora" ] 2>&3 \
1147 && { [ "$host_variant_id" = "silverblue" ] 2>&3 || [ "$host_variant_id" = "workstation" ] 2>&3; }; then
1148 emit_escape_sequence=true
1149 fi
1150
1151 run "$emit_escape_sequence" true false "$SHELL" -l
1152 )
1153
1154
1155 init_container()
1156 {
1157 init_container_home="$1"
1158 init_container_home_link="$2"
1159 init_container_media_link="$3"
1160 init_container_mnt_link="$4"
1161 init_container_monitor_host="$5"
1162 init_container_shell="$6"
1163 init_container_uid="$7"
1164 init_container_user="$8"
1165
1166 if [ "$XDG_RUNTIME_DIR" = "" ] 2>&3; then
1167 echo "$base_toolbox_command: XDG_RUNTIME_DIR is unset" >&3
1168
1169 XDG_RUNTIME_DIR=/run/user/"$init_container_uid"
1170 echo "$base_toolbox_command: XDG_RUNTIME_DIR set to $XDG_RUNTIME_DIR" >&3
1171
1172 toolbox_runtime_directory="$XDG_RUNTIME_DIR"/toolbox
1173 fi
1174
1175 init_container_initialized_stamp="$toolbox_runtime_directory"/container-initialized-"$$"
1176
1177 echo "$base_toolbox_command: creating /run/.toolboxenv" >&3
1178
1179 if ! touch /run/.toolboxenv 2>&3; then
1180 echo "$base_toolbox_command: failed to create /run/.toolboxenv" >&2
1181 return 1
1182 fi
1183
1184 if $init_container_monitor_host; then
1185 working_directory="$PWD"
1186
1187 if [ -d /run/host/etc ] 2>&3; then
1188 if ! readlink /etc/host.conf >/dev/null 2>&3; then
1189 echo "$base_toolbox_command: redirecting /etc/host.conf to /run/host/etc/host.conf" >&3
1190
1191 if ! (cd /etc 2>&3 \
1192 && rm --force host.conf 2>&3 \
1193 && ln --symbolic /run/host/etc/host.conf host.conf 2>&3); then
1194 echo "$base_toolbox_command: failed to redirect /etc/host.conf to /run/host/etc/host.conf" >&2
1195 return 1
1196 fi
1197 fi
1198
1199 if ! readlink /etc/hosts >/dev/null 2>&3; then
1200 echo "$base_toolbox_command: redirecting /etc/hosts to /run/host/etc/hosts" >&3
1201
1202 if ! (cd /etc 2>&3 \
1203 && rm --force hosts 2>&3 \
1204 && ln --symbolic /run/host/etc/hosts hosts 2>&3); then
1205 echo "$base_toolbox_command: failed to redirect /etc/hosts to /run/host/etc/hosts" >&2
1206 return 1
1207 fi
1208 fi
1209
1210 if ! readlink /etc/resolv.conf >/dev/null 2>&3; then
1211 echo "$base_toolbox_command: redirecting /etc/resolv.conf to /run/host/etc/resolv.conf" >&3
1212
1213 if ! (cd /etc 2>&3 \
1214 && rm --force resolv.conf 2>&3 \
1215 && ln --symbolic /run/host/etc/resolv.conf resolv.conf 2>&3); then
1216 echo "$base_toolbox_command: failed to redirect /etc/resolv.conf to /run/host/etc/resolv.conf" \
1217 >&2
1218 return 1
1219 fi
1220 fi
1221
1222
1223 if ! mount_bind /run/host/etc/machine-id /etc/machine-id ro; then
1224 return 1
1225 fi
1226
1227 if ! mount_bind /run/host/run/libvirt /run/libvirt; then
1228 return 1
1229 fi
1230
1231 if ! mount_bind /run/host/run/systemd/journal /run/systemd/journal; then
1232 return 1
1233 fi
1234
1235 if [ -d /sys/fs/selinux ] 2>&3; then
1236 if ! mount_bind /usr/share/empty /sys/fs/selinux; then
1237 return 1
1238 fi
1239 fi
1240
1241 if ! mount_bind /run/host/var/lib/flatpak /var/lib/flatpak ro; then
1242 return 1
1243 fi
1244
1245 if ! mount_bind /run/host/var/log/journal /var/log/journal ro; then
1246 return 1
1247 fi
1248
1249 if ! mount_bind /run/host/var/mnt /var/mnt rslave; then
1250 return 1
1251 fi
1252 fi
1253
1254 if [ -d /run/host/monitor ] 2>&3; then
1255 if ! localtime_target=$(readlink /etc/localtime >/dev/null 2>&3) \
1256 || [ "$localtime_target" != "/run/host/monitor/localtime" ] 2>&3; then
1257 echo "$base_toolbox_command: redirecting /etc/localtime to /run/host/monitor/localtime" >&3
1258
1259 if ! (cd /etc 2>&3 \
1260 && rm --force localtime 2>&3 \
1261 && ln --symbolic /run/host/monitor/localtime localtime 2>&3); then
1262 echo "$base_toolbox_command: failed to redirect /etc/localtime to /run/host/monitor/localtime" \
1263 >&2
1264 return 1
1265 fi
1266 fi
1267
1268 if ! readlink /etc/timezone >/dev/null 2>&3; then
1269 echo "$base_toolbox_command: redirecting /etc/timezone to /run/host/monitor/timezone" >&3
1270
1271 if ! (cd /etc 2>&3 \
1272 && rm --force timezone 2>&3 \
1273 && ln --symbolic /run/host/monitor/timezone timezone 2>&3); then
1274 echo "$base_toolbox_command: failed to redirect /etc/timezone to /run/host/monitor/timezone" >&2
1275 return 1
1276 fi
1277 fi
1278 fi
1279
1280 if ! cd "$working_directory" 2>&3; then
1281 echo "$base_toolbox_command: failed to restore working directory" >&2
1282 fi
1283 fi
1284
1285 if $init_container_media_link && ! readlink /media >/dev/null 2>&3; then
1286 echo "$base_toolbox_command: making /media a symbolic link to /run/media" >&3
1287
1288 # shellcheck disable=SC2174
1289 if ! (rmdir /media 2>&3 \
1290 && mkdir --mode 0755 --parents /run/media 2>&3 \
1291 && ln --symbolic run/media /media 2>&3); then
1292 echo "$base_toolbox_command: failed to make /media a symbolic link" >&2
1293 return 1
1294 fi
1295 fi
1296
1297 if $init_container_mnt_link && ! readlink /mnt >/dev/null 2>&3; then
1298 echo "$base_toolbox_command: making /mnt a symbolic link to /var/mnt" >&3
1299
1300 # shellcheck disable=SC2174
1301 if ! (rmdir /mnt 2>&3 \
1302 && mkdir --mode 0755 --parents /var/mnt 2>&3 \
1303 && ln --symbolic var/mnt /mnt 2>&3); then
1304 echo "$base_toolbox_command: failed to make /mnt a symbolic link" >&2
1305 return 1
1306 fi
1307 fi
1308
1309 if ! id -u "$init_container_user" >/dev/null 2>&3; then
1310 if $init_container_home_link ; then
1311 echo "$base_toolbox_command: making /home a symlink" >&3
1312
1313 # shellcheck disable=SC2174
1314 if ! (rmdir /home 2>&3 \
1315 && mkdir --mode 0755 --parents /var/home 2>&3 \
1316 && ln --symbolic var/home /home 2>&3); then
1317 echo "$base_toolbox_command: failed to make /home a symlink" >&2
1318 return 1
1319 fi
1320 fi
1321
1322 if ! groups=$(get_group_for_sudo); then
1323 echo "$base_toolbox_command: failed to add user $init_container_user: group for sudo not found" >&2
1324 return 1
1325 fi
1326
1327 echo "$base_toolbox_command: adding user $init_container_user with UID $init_container_uid" >&3
1328
1329 if ! useradd \
1330 --home-dir "$init_container_home" \
1331 --no-create-home \
1332 --shell "$init_container_shell" \
1333 --uid "$init_container_uid" \
1334 --groups "$groups" \
1335 "$init_container_user" >/dev/null 2>&3; then
1336 echo "$base_toolbox_command: failed to add user $init_container_user with UID $init_container_uid" >&2
1337 return 1
1338 fi
1339
1340 echo "$base_toolbox_command: removing password for user $init_container_user" >&3
1341
1342 if ! passwd --delete "$init_container_user" >/dev/null 2>&3; then
1343 echo "$base_toolbox_command: failed to remove password for user $init_container_user" >&2
1344 return 1
1345 fi
1346
1347 echo "$base_toolbox_command: removing password for user root" >&3
1348
1349 if ! passwd --delete root >/dev/null 2>&3; then
1350 echo "$base_toolbox_command: failed to remove password for user root" >&2
1351 return 1
1352 fi
1353
1354 fi
1355
1356 if [ -d /etc/krb5.conf.d ] 2>&3 && ! [ -f /etc/krb5.conf.d/kcm_default_ccache ] 2>&3; then
1357 echo "$base_toolbox_command: setting KCM as the default Kerberos credential cache" >&3
1358
1359 cat <<EOF >/etc/krb5.conf.d/kcm_default_ccache 2>&3
1360 # Written by Toolbox
1361 # https://github.com/containers/toolbox
1362 #
1363 # # To disable the KCM credential cache, comment out the following lines.
1364
1365 [libdefaults]
1366 default_ccache_name = KCM:
1367 EOF
1368 ret_val=$?
1369
1370 if [ "$ret_val" -ne 0 ] 2>&3; then
1371 echo "$base_toolbox_command: failed to set KCM as the default Kerberos credential cache" >&2
1372 return 1
1373 fi
1374 fi
1375
1376 echo "$base_toolbox_command: finished initializing container" >&3
1377
1378 if ! touch "$init_container_initialized_stamp" 2>&3; then
1379 echo "$base_toolbox_command: failed to create initialization stamp" >&2
1380 return 1
1381 fi
1382
1383 echo "$base_toolbox_command: going to sleep" >&3
1384
1385 exec sleep +Inf
1386 }
1387
1388
1389 run()
1390 (
1391 emit_escape_sequence="$1"
1392 fallback_to_bash="$2"
1393 pedantic="$3"
1394 program="$4"
1395 shift 4
1396
1397 create_toolbox_container=false
1398 prompt_for_create=true
1399
1400 echo "$base_toolbox_command: checking if container $toolbox_container exists" >&3
1401
1402 if ! $podman_command container exists "$toolbox_container" 2>&3; then
1403 echo "$base_toolbox_command: container $toolbox_container not found" >&3
1404
1405 if $podman_command container exists "$toolbox_container_old_v1" 2>&3; then
1406 echo "$base_toolbox_command: container $toolbox_container_old_v1 found" >&3
1407
1408 # shellcheck disable=SC2030
1409 toolbox_container="$toolbox_container_old_v1"
1410 elif $podman_command container exists "$toolbox_container_old_v2" 2>&3; then
1411 echo "$base_toolbox_command: container $toolbox_container_old_v2 found" >&3
1412
1413 # shellcheck disable=SC2030
1414 toolbox_container="$toolbox_container_old_v2"
1415 else
1416 if $pedantic; then
1417 enter_print_container_not_found "$toolbox_container"
1418 exit 1
1419 fi
1420
1421 if ! containers=$(list_container_names); then
1422 enter_print_container_not_found "$toolbox_container"
1423 exit 1
1424 fi
1425
1426 containers_count=$(echo "$containers" | grep --count . 2>&3)
1427 if ! is_integer "$containers_count"; then
1428 enter_print_container_not_found "$toolbox_container"
1429 exit 1
1430 fi
1431
1432 echo "$base_toolbox_command: found $containers_count containers" >&3
1433
1434 if [ "$containers_count" -eq 0 ] 2>&3; then
1435 if $assume_yes; then
1436 create_toolbox_container=true
1437 prompt_for_create=false
1438 fi
1439
1440 if $prompt_for_create; then
1441 prompt="No toolbox containers found. Create now? [y/N]"
1442 if ask_for_confirmation "n" "$prompt"; then
1443 create_toolbox_container=true
1444 else
1445 create_toolbox_container=false
1446 fi
1447 fi
1448
1449 if ! $create_toolbox_container; then
1450 echo "A container can be created later with the 'create' command." >&2
1451 echo "Try '$base_toolbox_command --help' for more information." >&2
1452 exit 1
1453 fi
1454
1455 if ! update_container_and_image_names; then
1456 exit 1
1457 fi
1458
1459 if ! create true; then
1460 exit 1
1461 fi
1462 elif [ "$containers_count" -eq 1 ] 2>&3 \
1463 && [ "$toolbox_container" = "$toolbox_container_default" ] 2>&3; then
1464 echo "$base_toolbox_command: container $toolbox_container not found" >&2
1465
1466 toolbox_container=$(echo "$containers" | grep . 2>&3 | head --lines 1 2>&3)
1467 echo "Entering container $toolbox_container instead." >&2
1468 echo "Use the 'create' command to create a different toolbox." >&2
1469 echo "Try '$base_toolbox_command --help' for more information." >&2
1470 else
1471 echo "$base_toolbox_command: container $toolbox_container not found" >&2
1472 echo "Use the '--container' option to select a toolbox." >&2
1473 echo "Try '$base_toolbox_command --help' for more information." >&2
1474 exit 1
1475 fi
1476 fi
1477 fi
1478
1479 echo "$base_toolbox_command: calling org.freedesktop.Flatpak.SessionHelper.RequestSession" >&3
1480
1481 if ! gdbus call \
1482 --session \
1483 --dest org.freedesktop.Flatpak \
1484 --object-path /org/freedesktop/Flatpak/SessionHelper \
1485 --method org.freedesktop.Flatpak.SessionHelper.RequestSession >/dev/null 2>&3; then
1486 echo "$base_toolbox_command: failed to call org.freedesktop.Flatpak.SessionHelper.RequestSession" >&2
1487 exit 1
1488 fi
1489
1490 echo "$base_toolbox_command: starting container $toolbox_container" >&3
1491
1492 if is_etc_profile_d_toolbox_a_bind_mount "$toolbox_container"; then
1493 echo "$base_toolbox_command: /etc/profile.d/toolbox.sh already mounted in container $toolbox_container" >&3
1494
1495 if ! container_start "$toolbox_container"; then
1496 exit 1
1497 fi
1498 else
1499 echo "$base_toolbox_command: /etc/profile.d/toolbox.sh not mounted in container $toolbox_container" >&3
1500
1501 if ! copy_etc_profile_d_toolbox_to_runtime_directory; then
1502 exit 1
1503 fi
1504
1505 if ! container_start "$toolbox_container"; then
1506 exit 1
1507 fi
1508
1509 if ! copy_etc_profile_d_toolbox_to_container "$toolbox_container"; then
1510 exit 1
1511 fi
1512 fi
1513
1514 echo "$base_toolbox_command: inspecting entry point of container $toolbox_container" >&3
1515
1516 if ! entry_point=$($podman_command inspect --format "{{index .Config.Cmd 0}}" --type container "$toolbox_container" 2>&3); then
1517 echo "$base_toolbox_command: failed to inspect entry point of container $toolbox_container" >&2
1518 exit 1
1519 fi
1520
1521 echo "$base_toolbox_command: entry point of container $toolbox_container is $entry_point" >&3
1522
1523 if [ "$entry_point" = "toolbox" ] 2>&3; then
1524 echo "$base_toolbox_command: waiting for container $toolbox_container to finish initializing" >&3
1525
1526 if ! entry_point_pid=$($podman_command inspect --format "{{.State.Pid}}" --type container "$toolbox_container" 2>&3); then
1527 echo "$base_toolbox_command: failed to inspect entry point PID of container $toolbox_container" >&2
1528 exit 1
1529 fi
1530
1531 if ! is_integer "$entry_point_pid"; then
1532 echo "$base_toolbox_command: failed to parse entry point PID of container $toolbox_container" >&2
1533 exit 1
1534 fi
1535
1536 if [ "$entry_point_pid" -le 0 ] 2>&3; then
1537 echo "$base_toolbox_command: invalid entry point PID of container $toolbox_container" >&2
1538 exit 1
1539 fi
1540
1541 container_initialized_stamp="$toolbox_runtime_directory/container-initialized-$entry_point_pid"
1542 container_initialized_timeout=25 #s
1543
1544 i=0
1545 while ! [ -f "$container_initialized_stamp" ] 2>&3; do
1546 sleep 1 2>&3
1547
1548 i=$((i + 1))
1549 if [ "$i" -eq "$container_initialized_timeout" ] 2>&3; then
1550 echo "$base_toolbox_command: failed to initialize container $toolbox_container" >&2
1551 exit 1
1552 fi
1553 done
1554 else
1555 echo "$base_toolbox_command: container $toolbox_container uses deprecated features" >&2
1556 echo "Consider recreating it with Toolbox version 0.0.17 or newer." >&2
1557 fi
1558
1559 if ! $podman_command exec --user root:root "$toolbox_container" touch /run/.toolboxenv 2>&3; then
1560 echo "$base_toolbox_command: failed to create /run/.toolboxenv in container $toolbox_container" >&2
1561 exit 1
1562 fi
1563
1564 set_environment=$(create_environment_options)
1565
1566 echo "$base_toolbox_command: looking for $program in container $toolbox_container" >&3
1567
1568 # shellcheck disable=SC2016
1569 if ! $podman_command exec \
1570 --user "$USER" \
1571 "$toolbox_container" \
1572 sh -c 'command -v "$1"' sh "$program" >/dev/null 2>&3; then
1573 if $fallback_to_bash; then
1574 echo "$base_toolbox_command: $program not found in $toolbox_container; using /bin/bash instead" >&3
1575 program=/bin/bash
1576 else
1577 echo "$base_toolbox_command: command '$program' not found in container $toolbox_container" >&2
1578 exit 127
1579 fi
1580 fi
1581
1582 echo "$base_toolbox_command: running in container $toolbox_container:" >&3
1583 echo "$base_toolbox_command: $program" >&3
1584 for i in "$@"; do
1585 echo "$base_toolbox_command: $i" >&3
1586 done
1587
1588 $emit_escape_sequence && printf "\033]777;container;push;%s;toolbox\033\\" "$toolbox_container"
1589
1590 # shellcheck disable=SC2016
1591 # for the command passed to capsh
1592 # shellcheck disable=SC2086
1593 $podman_command exec \
1594 --interactive \
1595 --tty \
1596 --user "$USER" \
1597 --workdir "$PWD" \
1598 $set_environment \
1599 "$toolbox_container" \
1600 capsh --caps="" -- -c 'exec "$@"' /bin/sh "$program" "$@" 2>&3
1601 ret_val="$?"
1602
1603 $emit_escape_sequence && printf "\033]777;container;pop;;\033\\"
1604
1605 if [ "$ret_val" -eq 127 ] 2>&3; then
1606 # shellcheck disable=SC2016
1607 if ! $podman_command exec \
1608 --user "$USER" \
1609 "$toolbox_container" \
1610 sh -c 'test -d "$1"' sh "$PWD" >/dev/null 2>&3; then
1611 echo "$base_toolbox_command: directory '$PWD' not found in container $toolbox_container" >&2
1612 fi
1613 fi
1614
1615 exit "$ret_val"
1616 )
1617
1618
1619 help()
1620 (
1621 to_help_command="$1"
1622
1623 if [ "$to_help_command" = "" ] 2>&3 || [ "$to_help_command" = "$base_toolbox_command" ] 2>&3; then
1624 exec man toolbox 2>&1
1625 fi
1626
1627 exec man toolbox-"$to_help_command" 2>&1
1628 )
1629
1630
1631 list_images()
1632 (
1633 output=""
1634
1635 if ! images_old=$($podman_command images \
1636 --filter "label=com.redhat.component=fedora-toolbox" \
1637 --format "{{.Repository}}:{{.Tag}}" 2>&3); then
1638 echo "$base_toolbox_command: failed to list images with com.redhat.component=fedora-toolbox" >&2
1639 return 1
1640 fi
1641
1642 if ! images=$($podman_command images \
1643 --filter "label=com.github.debarshiray.toolbox=true" \
1644 --format "{{.Repository}}:{{.Tag}}" 2>&3); then
1645 echo "$base_toolbox_command: failed to list images with com.github.debarshiray.toolbox=true" >&2
1646 return 1
1647 fi
1648
1649 images=$(printf "%s\n%s\n" "$images_old" "$images" | sort 2>&3 | uniq 2>&3)
1650 if ! details=$(images_get_details "$images"); then
1651 return 1
1652 fi
1653
1654 if [ "$details" != "" ] 2>&3; then
1655 table_data=$(printf "%s\t%s\t%s\n" "IMAGE ID" "IMAGE NAME" "CREATED"; echo "$details")
1656 if ! output=$(echo "$table_data" | sed "s/ \{2,\}/\t/g" 2>&3 | column -s "$tab" -t 2>&3); then
1657 echo "$base_toolbox_command: failed to parse list of images" >&2
1658 return 1
1659 fi
1660 fi
1661
1662 if [ "$output" != "" ]; then
1663 echo "$output"
1664 fi
1665
1666 return 0
1667 )
1668
1669
1670 containers_get_details()
1671 (
1672 containers="$1"
1673
1674 if ! echo "$containers" | while read -r container; do
1675 [ "$container" = "" ] 2>&3 && continue
1676
1677 if ! $podman_command ps --all \
1678 --filter "name=$container" \
1679 --format "{{.ID}} {{.Names}} {{.Created}} {{.Status}} {{.Image}}" 2>&3; then
1680 echo "$base_toolbox_command: failed to get details for container $container" >&2
1681 return 1
1682 fi
1683 done; then
1684 return 1
1685 fi
1686
1687 return 0
1688 )
1689
1690
1691 list_containers()
1692 (
1693 output=""
1694
1695 if ! containers=$(list_container_names); then
1696 return 1
1697 fi
1698
1699 if ! details=$(containers_get_details "$containers"); then
1700 return 1
1701 fi
1702
1703 if [ "$details" != "" ] 2>&3; then
1704 table_data=$(printf "%s\t%s\t%s\t%s\t%s\n" "CONTAINER ID" "CONTAINER NAME" "CREATED" "STATUS" "IMAGE NAME"
1705 echo "$details")
1706 if ! output=$(echo "$table_data" | sed "s/ \{2,\}/\t/g" 2>&3 | column -s "$tab" -t 2>&3); then
1707 echo "$base_toolbox_command: failed to parse list of containers" >&2
1708 return 1
1709 fi
1710 fi
1711
1712 if [ "$output" != "" ]; then
1713 echo "$output" | head --lines 1 2>&3
1714
1715 echo "$output" | tail --lines +2 2>&3 \
1716 | (
1717 while read -r container; do
1718 id=$(echo "$container" | cut --delimiter " " --fields 1 2>&3)
1719 is_running=$($podman_command inspect "$id" --format "{{.State.Running}}" 2>&3)
1720 if $is_running; then
1721 # shellcheck disable=SC2059
1722 printf "${LGC}$container${NC}\n"
1723 else
1724 echo "$container"
1725 fi
1726 done
1727 )
1728 fi
1729
1730 return 0
1731 )
1732
1733
1734 migrate()
1735 (
1736 configuration_directory="$HOME/.config/toolbox"
1737 migrate_stamp="$configuration_directory/podman-system-migrate"
1738
1739 migrate_lock="$toolbox_runtime_directory"/migrate.lock
1740
1741 if ! version=$($podman_command version --format "{{.Version}}" 2>&3); then
1742 echo "$base_toolbox_command: unable to migrate containers: Podman version couldn't be read" >&2
1743 return 1
1744 fi
1745
1746 echo "$base_toolbox_command: current Podman version is $version" >&3
1747
1748 if ! mkdir --parents "$configuration_directory" 2>&3; then
1749 echo "$base_toolbox_command: unable to migrate containers: configuration directory not created" >&2
1750 return 1
1751 fi
1752
1753 # shellcheck disable=SC2174
1754 if ! mkdir --mode 700 --parents "$toolbox_runtime_directory" 2>&3; then
1755 echo "$base_toolbox_command: unable to migrate containers: runtime directory not created" >&2
1756 return 1
1757 fi
1758
1759 exec 5>"$migrate_lock"
1760 if ! flock 5 2>&3; then
1761 echo "$base_toolbox_command: unable to migrate containers: migration lock not acquired" >&3
1762 return 1
1763 fi
1764
1765 if [ -f "$migrate_stamp" ] 2>&3; then
1766 if grep "$version" "$migrate_stamp" >/dev/null 2>&3; then
1767 echo "$base_toolbox_command: migration not needed: Podman version $version is unchanged" >&3
1768 return 0
1769 fi
1770
1771 if ! version_old=$(printf "%s\n" "$version" \
1772 | cat "$migrate_stamp" - 2>&3 \
1773 | sort --version-sort 2>&3 \
1774 | head --lines 1 2>&3); then
1775 echo "$base_toolbox_command: unable to migrate containers: Podman versions couldn't be sorted" >&2
1776 return 1
1777 fi
1778
1779 if [ "$version" = "$version_old" ] 2>&3; then
1780 echo "$base_toolbox_command: migration not needed: Podman version $version is old" >&3
1781 return 0
1782 fi
1783 fi
1784
1785 if ! $podman_command system migrate >/dev/null 2>&3; then
1786 echo "$base_toolbox_command: unable to migrate containers" >&2
1787 return 1
1788 fi
1789
1790 echo "$version" >"$migrate_stamp"
1791 return 0
1792 )
1793
1794
1795 remove_containers()
1796 (
1797 ids=$1
1798 all=$2
1799 force=$3
1800
1801 ret_val=0
1802
1803 $force && force_option="--force"
1804
1805 if $all; then
1806 if ! ids_old=$($podman_command ps \
1807 --all \
1808 --filter "label=com.redhat.component=fedora-toolbox" \
1809 --format "{{.ID}}" 2>&3); then
1810 echo "$base_toolbox_command: failed to list containers with com.redhat.component=fedora-toolbox" >&2
1811 return 1
1812 fi
1813
1814 if ! ids=$($podman_command ps \
1815 --all \
1816 --filter "label=com.github.debarshiray.toolbox=true" \
1817 --format "{{.ID}}" 2>&3); then
1818 echo "$base_toolbox_command: failed to list containers with com.github.debarshiray.toolbox=true" >&2
1819 return 1
1820 fi
1821
1822 ids=$(printf "%s\n%s\n" "$ids_old" "$ids" | sort 2>&3 | uniq 2>&3)
1823 if [ "$ids" != "" ]; then
1824 ret_val=$(echo "$ids" \
1825 | (
1826 while read -r id; do
1827 if ! $podman_command rm "$force_option" "$id" >/dev/null 2>&3; then
1828 echo "$base_toolbox_command: failed to remove container $id" >&2
1829 ret_val=1
1830 fi
1831 done
1832
1833 echo "$ret_val"
1834 )
1835 )
1836 fi
1837 else
1838 ret_val=$(echo "$ids" \
1839 | sed "s/ \+/\n/g" 2>&3 \
1840 | (
1841 while read -r id; do
1842 if ! labels=$($podman_command inspect \
1843 --format "{{.Config.Labels}}" \
1844 --type container \
1845 "$id" 2>&3); then
1846 echo "$base_toolbox_command: failed to inspect $id" >&2
1847 ret_val=1
1848 continue
1849 fi
1850
1851 if ! has_substring "$labels" "com.github.debarshiray.toolbox" \
1852 && ! has_substring "$labels" "com.redhat.component:fedora-toolbox"; then
1853 echo "$base_toolbox_command: $id is not a toolbox container" >&2
1854 ret_val=1
1855 continue
1856 fi
1857
1858 if ! $podman_command rm "$force_option" "$id" >/dev/null 2>&3; then
1859 echo "$base_toolbox_command: failed to remove container $id" >&2
1860 ret_val=1
1861 fi
1862 done
1863
1864 echo "$ret_val"
1865 )
1866 )
1867 fi
1868
1869 return "$ret_val"
1870 )
1871
1872
1873 remove_images()
1874 (
1875 ids=$1
1876 all=$2
1877 force=$3
1878
1879 ret_val=0
1880
1881 $force && force_option="--force"
1882
1883 if $all; then
1884 if ! ids_old=$($podman_command images \
1885 --filter "label=com.redhat.component=fedora-toolbox" \
1886 --format "{{.ID}}" 2>&3); then
1887 echo "$0: failed to list images with com.redhat.component=fedora-toolbox" >&2
1888 return 1
1889 fi
1890
1891 if ! ids=$($podman_command images \
1892 --all \
1893 --filter "label=com.github.debarshiray.toolbox=true" \
1894 --format "{{.ID}}" 2>&3); then
1895 echo "$0: failed to list images with com.github.debarshiray.toolbox=true" >&2
1896 return 1
1897 fi
1898
1899 ids=$(printf "%s\n%s\n" "$ids_old" "$ids" | sort 2>&3 | uniq 2>&3)
1900 if [ "$ids" != "" ]; then
1901 ret_val=$(echo "$ids" \
1902 | (
1903 while read -r id; do
1904 if ! $podman_command rmi "$force_option" "$id" >/dev/null 2>&3; then
1905 echo "$base_toolbox_command: failed to remove image $id" >&2
1906 ret_val=1
1907 fi
1908 done
1909
1910 echo "$ret_val"
1911 )
1912 )
1913 fi
1914 else
1915 ret_val=$(echo "$ids" \
1916 | sed "s/ \+/\n/g" 2>&3 \
1917 | (
1918 while read -r id; do
1919 if ! labels=$($podman_command inspect \
1920 --format "{{.Labels}}" \
1921 --type image \
1922 "$id" 2>&3); then
1923 echo "$base_toolbox_command: failed to inspect $id" >&2
1924 ret_val=1
1925 continue
1926 fi
1927
1928 if ! has_substring "$labels" "com.github.debarshiray.toolbox" \
1929 && ! has_substring "$labels" "com.redhat.component:fedora-toolbox"; then
1930 echo "$base_toolbox_command: $id is not a toolbox image" >&2
1931 ret_val=1
1932 continue
1933 fi
1934
1935 if ! $podman_command rmi "$force_option" "$id" >/dev/null 2>&3; then
1936 echo "$base_toolbox_command: failed to remove image $id" >&2
1937 ret_val=1
1938 fi
1939 done
1940
1941 echo "$ret_val"
1942 )
1943 )
1944 fi
1945
1946 return "$ret_val"
1947 )
1948
1949
1950 reset()
1951 (
1952 do_reset=false
1953 prompt_for_reset=true
1954 ret_val=0
1955
1956 if [ "$user_id_real" -eq 0 ] 2>&3; then
1957 if [ -d /run/containers ] 2>&3; then
1958 echo "$base_toolbox_command: The 'reset' command cannot be used after other commands" >&2
1959 echo "Reboot the system before using it again." >&2
1960 echo "Try '$base_toolbox_command --help' for more information." >&2
1961 return 1
1962 fi
1963 else
1964 if [ -d "$XDG_RUNTIME_DIR"/overlay-containers ] 2>&3 \
1965 || [ -d "$XDG_RUNTIME_DIR"/overlay-layers ] 2>&3 \
1966 || [ -d "$XDG_RUNTIME_DIR"/overlay-locks ] 2>&3; then
1967 echo "$base_toolbox_command: The 'reset' command cannot be used after other commands" >&2
1968 echo "Reboot the system before using it again." >&2
1969 echo "Try '$base_toolbox_command --help' for more information." >&2
1970 return 1
1971 fi
1972 fi
1973
1974 if $assume_yes; then
1975 do_reset=true
1976 prompt_for_reset=false
1977 fi
1978
1979 if $prompt_for_reset; then
1980 echo "All existing podman (and toolbox) containers and images will be removed."
1981
1982 prompt=$(printf "Continue? [y/N]:")
1983 if ask_for_confirmation "n" "$prompt"; then
1984 do_reset=true
1985 else
1986 do_reset=false
1987 fi
1988 fi
1989
1990 if ! $do_reset; then
1991 return 1
1992 fi
1993
1994 echo "$base_toolbox_command: resetting local state" >&3
1995
1996 if [ "$user_id_real" -eq 0 ] 2>&3; then
1997 if ! rm --force --recursive /var/lib/containers/cache >/dev/null 2>&3; then
1998 echo "$base_toolbox_command: failed to remove /var/lib/containers/cache" >&2
1999 ret_val=1
2000 fi
2001
2002 if ! rm --force --recursive /var/lib/containers/sigstore/* >/dev/null 2>&3; then
2003 echo "$base_toolbox_command: failed to remove the contents of /var/lib/containers/sigstore" >&2
2004 ret_val=1
2005 fi
2006
2007 if ! rm --force --recursive /var/lib/containers/storage >/dev/null 2>&3; then
2008 echo "$base_toolbox_command: failed to remove /var/lib/containers/storage" >&2
2009 ret_val=1
2010 fi
2011 else
2012 if ! unshare_userns_rm "$HOME/.local/share/containers"; then
2013 ret_val=1
2014 fi
2015
2016 if ! rm --force --recursive "$HOME/.config/containers" >/dev/null 2>&3; then
2017 echo "$base_toolbox_command: failed to remove $HOME/.config/containers" >&2
2018 ret_val=1
2019 fi
2020 fi
2021
2022 if ! rm --force --recursive "$HOME/.config/toolbox" >/dev/null 2>&3; then
2023 echo "$base_toolbox_command: failed to remove $HOME/.config/toolbox" >&2
2024 ret_val=1
2025 fi
2026
2027 return "$ret_val"
2028 )
2029
2030
2031 exit_if_extra_operand()
2032 {
2033 if [ "$1" != "" ]; then
2034 echo "$base_toolbox_command: extra operand '$1'" >&2
2035 echo "Try '$base_toolbox_command --help' for more information." >&2
2036 exit 1
2037 fi
2038 }
2039
2040
2041 exit_if_missing_argument()
2042 {
2043 if [ "$2" = "" ]; then
2044 echo "$base_toolbox_command: missing argument for '$1'" >&2
2045 echo "Try '$base_toolbox_command --help' for more information." >&2
2046 exit 1
2047 fi
2048 }
2049
2050
2051 exit_if_non_positive_argument()
2052 {
2053 if ! is_integer "$2"; then
2054 echo "$base_toolbox_command: invalid argument for '$1'" >&2
2055 echo "Try '$base_toolbox_command --help' for more information." >&2
2056 exit 1
2057 fi
2058 if [ "$2" -le 0 ] 2>&3; then
2059 echo "$base_toolbox_command: invalid argument for '$1'" >&2
2060 echo "Try '$base_toolbox_command --help' for more information." >&2
2061 exit 1
2062 fi
2063 }
2064
2065
2066 exit_if_unrecognized_option()
2067 {
2068 echo "$base_toolbox_command: unrecognized option '$1'" >&2
2069 echo "Try '$base_toolbox_command --help' for more information." >&2
2070 exit 1
2071 }
2072
2073
2074 # shellcheck disable=SC2120
2075 forward_to_host()
2076 (
2077 if ! command -v flatpak-spawn >/dev/null 2>&3; then
2078 echo "$base_toolbox_command: flatpak-spawn not found" >&2
2079 return 1
2080 fi
2081
2082 eval "set -- $arguments"
2083
2084 set_environment=$(create_environment_options)
2085
2086 echo "$base_toolbox_command: forwarding to host:" >&3
2087 echo "$base_toolbox_command: $TOOLBOX_PATH" >&3
2088 for i in "$@"; do
2089 echo "$base_toolbox_command: $i" >&3
2090 done
2091
2092 # shellcheck disable=SC2086
2093 flatpak-spawn $set_environment --host "$TOOLBOX_PATH" "$@" 2>&3
2094 ret_val="$?"
2095
2096 return "$ret_val"
2097 )
2098
2099
2100 update_container_and_image_names()
2101 {
2102 [ "$release" = "" ] 2>&3 && release="$release_default"
2103
2104 if [ "$base_toolbox_image" = "" ] 2>&3; then
2105 base_toolbox_image="fedora-toolbox:$release"
2106 else
2107 release=$(image_reference_get_tag "$base_toolbox_image")
2108 [ "$release" = "" ] 2>&3 && release="$release_default"
2109 fi
2110
2111 fgc="f$release"
2112 echo "$base_toolbox_command: Fedora generational core is $fgc" >&3
2113
2114 echo "$base_toolbox_command: base image is $base_toolbox_image" >&3
2115
2116 toolbox_image=$(create_toolbox_image_name)
2117 if ! (
2118 ret_val=$?
2119 if [ "$ret_val" -ne 0 ] 2>&3; then
2120 if [ "$ret_val" -eq 100 ] 2>&3; then
2121 echo "$base_toolbox_command: failed to get the basename of base image $base_toolbox_image" >&2
2122 else
2123 echo "$base_toolbox_command: failed to create an ID for the customized user-specific image" >&2
2124 fi
2125
2126 exit 1
2127 fi
2128
2129 exit 0
2130 ); then
2131 return 1
2132 fi
2133
2134 # shellcheck disable=SC2031
2135 if [ "$toolbox_container" = "" ]; then
2136 toolbox_container=$(create_toolbox_container_name "$base_toolbox_image")
2137 if ! (
2138 ret_val=$?
2139 if [ "$ret_val" -ne 0 ] 2>&3; then
2140 if [ "$ret_val" -eq 100 ] 2>&3; then
2141 echo "$base_toolbox_command: failed to get the basename of image $base_toolbox_image" >&2
2142 elif [ "$ret_val" -eq 101 ] 2>&3; then
2143 echo "$base_toolbox_command: failed to get the tag of image $base_toolbox_image" >&2
2144 else
2145 echo "$base_toolbox_command: failed to create a name for the toolbox container" >&2
2146 fi
2147
2148 exit 1
2149 fi
2150
2151 exit 0
2152 ); then
2153 return 1
2154 fi
2155
2156 if ! container_name_is_valid "$toolbox_container"; then
2157 echo "$base_toolbox_command: generated container name $toolbox_container is invalid" >&2
2158 echo "Container names must match '$container_name_regexp'." >&2
2159 echo "Try '$base_toolbox_command --help' for more information." >&2
2160 return 1
2161 fi
2162
2163 toolbox_container_old_v1=$(create_toolbox_container_name "$toolbox_image")
2164 if ! (
2165 ret_val=$?
2166 if [ "$ret_val" -ne 0 ] 2>&3; then
2167 if [ "$ret_val" -eq 100 ] 2>&3; then
2168 echo "$base_toolbox_command: failed to get the basename of image $toolbox_image" >&2
2169 elif [ "$ret_val" -eq 101 ] 2>&3; then
2170 echo "$base_toolbox_command: failed to get the tag of image $toolbox_image" >&2
2171 else
2172 echo "$base_toolbox_command: failed to create a name for the toolbox container" >&2
2173 fi
2174
2175 exit 1
2176 fi
2177
2178 exit 0
2179 ); then
2180 return 1
2181 fi
2182
2183 toolbox_container_old_v2="$toolbox_image"
2184 fi
2185
2186 echo "$base_toolbox_command: container is $toolbox_container" >&3
2187 return 0
2188 }
2189
2190
2191 arguments=$(save_positional_parameters "$@")
2192
2193 echo "THIS IMPLEMENTATION OF TOOLBOX IS NO LONGER MAINTAINED."
2194 printf "Please, use the Go implementation instead: "
2195 # shellcheck disable=SC1003
2196 printf '\033]8;;https://containertoolbx.org/install/\033\\containertoolbx.org/install\033]8;;\033\\'
2197 printf "\n"
2198 echo
2199
2200 host_id=$(get_host_id)
2201 if [ "$host_id" = "fedora" ] 2>&3; then
2202 release_default=$(get_host_version_id)
2203 else
2204 release_default="30"
2205 fi
2206 toolbox_container_prefix_default="fedora-toolbox"
2207 toolbox_container_default="$toolbox_container_prefix_default-$release_default"
2208
2209 while has_prefix "$1" -; do
2210 case $1 in
2211 --assumeyes | -y )
2212 assume_yes=true
2213 ;;
2214 -h | --help )
2215 if [ -f /run/.containerenv ] 2>&3; then
2216 if ! [ -f /run/.toolboxenv ] 2>&3; then
2217 echo "$base_toolbox_command: this is not a toolbox container" >&2
2218 exit 1
2219 fi
2220
2221 # shellcheck disable=SC2119
2222 forward_to_host
2223 exit
2224 fi
2225
2226 help "$2"
2227 exit
2228 ;;
2229 -v | --verbose )
2230 exec 3>&2
2231 verbose=true
2232 ;;
2233 -vv | --very-verbose )
2234 exec 3>&2
2235 podman_command="podman --log-level debug"
2236 verbose=true
2237 ;;
2238 * )
2239 exit_if_unrecognized_option "$1"
2240 esac
2241 shift
2242 done
2243
2244 echo "$base_toolbox_command: running as real user ID $user_id_real" >&3
2245
2246 if ! toolbox_command_path=$(realpath "$0" 2>&3); then
2247 echo "$base_toolbox_command: failed to resolve absolute path to $0" >&2
2248 exit 1
2249 fi
2250
2251 echo "$base_toolbox_command: resolved absolute path for $0 to $toolbox_command_path" >&3
2252
2253 if [ -f /run/.containerenv ] 2>&3; then
2254 if [ "$TOOLBOX_PATH" = "" ] 2>&3; then
2255 echo "$base_toolbox_command: TOOLBOX_PATH not set" >&2
2256 exit 1
2257 fi
2258 else
2259 if [ "$user_id_real" -ne 0 ] 2>&3; then
2260 echo "$base_toolbox_command: checking if /etc/subgid and /etc/subuid have entries for user $USER" >&3
2261
2262 if ! grep "^$USER:" /etc/subgid >/dev/null 2>&3 || ! grep "^$USER:" /etc/subuid >/dev/null 2>&3; then
2263 echo "$base_toolbox_command: /etc/subgid and /etc/subuid don't have entries for user $USER" >&2
2264 echo "See the podman(1), subgid(5), subuid(5) and usermod(8) manuals for more" >&2
2265 echo "information." >&2
2266 exit 1
2267 fi
2268 fi
2269
2270 if [ "$TOOLBOX_PATH" = "" ] 2>&3; then
2271 TOOLBOX_PATH="$toolbox_command_path"
2272 fi
2273 fi
2274
2275 echo "$base_toolbox_command: TOOLBOX_PATH is $TOOLBOX_PATH" >&3
2276
2277 if [ "$1" = "" ]; then
2278 echo "$base_toolbox_command: missing command" >&2
2279 echo >&2
2280 echo "These are some common commands:" >&2
2281 echo "create Create a new toolbox container" >&2
2282 echo "enter Enter an existing toolbox container" >&2
2283 echo "list List all existing toolbox containers and images" >&2
2284 echo >&2
2285 echo "Try '$base_toolbox_command --help' for more information." >&2
2286 exit 1
2287 fi
2288
2289 op=$1
2290 shift
2291
2292 if [ -f /run/.containerenv ] 2>&3; then
2293 case $op in
2294 create | enter | list | rm | rmi | run | help )
2295 if ! [ -f /run/.toolboxenv ] 2>&3; then
2296 echo "$base_toolbox_command: this is not a toolbox container" >&2
2297 exit 1
2298 fi
2299
2300 # shellcheck disable=SC2119
2301 forward_to_host
2302 exit "$?"
2303 ;;
2304 init-container )
2305 init_container_home_link=false
2306 init_container_media_link=false
2307 init_container_mnt_link=false
2308 init_container_monitor_host=false
2309 while has_prefix "$1" -; do
2310 case $1 in
2311 -h | --help )
2312 # shellcheck disable=SC2119
2313 forward_to_host
2314 exit
2315 ;;
2316 --home )
2317 shift
2318 exit_if_missing_argument --home "$1"
2319 init_container_home="$1"
2320 ;;
2321 --home-link )
2322 init_container_home_link=true
2323 ;;
2324 --media-link )
2325 init_container_media_link=true
2326 ;;
2327 --mnt-link )
2328 init_container_mnt_link=true
2329 ;;
2330 --monitor-host )
2331 init_container_monitor_host=true
2332 ;;
2333 --shell )
2334 shift
2335 exit_if_missing_argument --shell "$1"
2336 init_container_shell="$1"
2337 ;;
2338 --uid )
2339 shift
2340 exit_if_missing_argument --uid "$1"
2341 init_container_uid="$1"
2342 ;;
2343 --user )
2344 shift
2345 exit_if_missing_argument --user "$1"
2346 init_container_user="$1"
2347 ;;
2348 * )
2349 exit_if_unrecognized_option "$1"
2350 esac
2351 shift
2352 done
2353 init_container \
2354 "$init_container_home" \
2355 "$init_container_home_link" \
2356 "$init_container_media_link" \
2357 "$init_container_mnt_link" \
2358 "$init_container_monitor_host" \
2359 "$init_container_shell" \
2360 "$init_container_uid" \
2361 "$init_container_user"
2362 exit "$?"
2363 ;;
2364 reset )
2365 echo "$base_toolbox_command: The 'reset' command cannot be used inside containers" >&2
2366 echo "Try '$base_toolbox_command --help' for more information." >&2
2367 exit 1
2368 ;;
2369 * )
2370 echo "$base_toolbox_command: unrecognized command '$op'" >&2
2371 echo "Try '$base_toolbox_command --help' for more information." >&2
2372 exit 1
2373 ;;
2374 esac
2375 fi
2376
2377 if ! cgroups_version=$(get_cgroups_version); then
2378 exit 1
2379 fi
2380
2381 echo "$base_toolbox_command: running on a cgroups v$cgroups_version host" >&3
2382
2383 if [ "$op" != "reset" ] 2>&3; then
2384 if ! migrate; then
2385 exit 1
2386 fi
2387 fi
2388
2389 case $op in
2390 create )
2391 while has_prefix "$1" -; do
2392 case $1 in
2393 --candidate-registry )
2394 registry=$registry_candidate
2395 ;;
2396 -c | --container )
2397 shift
2398 exit_if_missing_argument --container "$1"
2399 arg=$1
2400 if ! container_name_is_valid "$arg"; then
2401 echo "$base_toolbox_command: invalid argument for '--container'" >&2
2402 echo "Container names must match '$container_name_regexp'." >&2
2403 echo "Try '$base_toolbox_command --help' for more information." >&2
2404 exit 1
2405 fi
2406 toolbox_container="$arg"
2407 ;;
2408 -h | --help )
2409 help "$op"
2410 exit
2411 ;;
2412 -i | --image )
2413 shift
2414 exit_if_missing_argument --image "$1"
2415 base_toolbox_image=$1
2416 ;;
2417 -r | --release )
2418 shift
2419 exit_if_missing_argument --release "$1"
2420 arg=$(echo "$1" | sed "s/^F\|^f//" 2>&3)
2421 exit_if_non_positive_argument --release "$arg"
2422 release=$arg
2423 ;;
2424 * )
2425 exit_if_unrecognized_option "$1"
2426 esac
2427 shift
2428 done
2429 exit_if_extra_operand "$1"
2430 if ! update_container_and_image_names; then
2431 exit 1
2432 fi
2433 if ! create false; then
2434 exit 1
2435 fi
2436 exit
2437 ;;
2438 enter )
2439 while has_prefix "$1" -; do
2440 case $1 in
2441 -c | --container )
2442 shift
2443 exit_if_missing_argument --container "$1"
2444 toolbox_container=$1
2445 ;;
2446 -h | --help )
2447 help "$op"
2448 exit
2449 ;;
2450 -r | --release )
2451 shift
2452 exit_if_missing_argument --release "$1"
2453 arg=$(echo "$1" | sed "s/^F\|^f//" 2>&3)
2454 exit_if_non_positive_argument --release "$arg"
2455 release=$arg
2456 ;;
2457 * )
2458 exit_if_unrecognized_option "$1"
2459 esac
2460 shift
2461 done
2462 exit_if_extra_operand "$1"
2463 if ! update_container_and_image_names; then
2464 exit 1
2465 fi
2466 enter
2467 exit
2468 ;;
2469 help )
2470 while has_prefix "$1" -; do
2471 case $1 in
2472 -h | --help )
2473 help "$op"
2474 exit
2475 ;;
2476 * )
2477 exit_if_unrecognized_option "$1"
2478 esac
2479 # shellcheck disable=SC2317
2480 shift
2481 done
2482 help "$1"
2483 exit
2484 ;;
2485 init-container )
2486 while has_prefix "$1" -; do
2487 case $1 in
2488 -h | --help )
2489 help "$op"
2490 exit
2491 esac
2492 shift
2493 done
2494 echo "$base_toolbox_command: The 'init-container' command can only be used inside containers" >&2
2495 echo "Try '$base_toolbox_command --help' for more information." >&2
2496 exit 1
2497 ;;
2498 list )
2499 ls_add_empty_line=false
2500 ls_images=false
2501 ls_containers=false
2502 while has_prefix "$1" -; do
2503 case $1 in
2504 -c | --containers )
2505 ls_containers=true
2506 ;;
2507 -h | --help )
2508 help "$op"
2509 exit
2510 ;;
2511 -i | --images )
2512 ls_images=true
2513 ;;
2514 * )
2515 exit_if_unrecognized_option "$1"
2516 esac
2517 shift
2518 done
2519 exit_if_extra_operand "$1"
2520
2521 if ! $ls_containers && ! $ls_images; then
2522 ls_containers=true
2523 ls_images=true
2524 fi
2525
2526 if $ls_images; then
2527 if ! images=$(list_images); then
2528 exit 1
2529 fi
2530 fi
2531
2532 if $ls_containers; then
2533 if ! containers=$(list_containers); then
2534 exit 1
2535 fi
2536 fi
2537
2538 if $ls_images && [ "$images" != "" ] 2>&3; then
2539 echo "$images"
2540 ls_add_empty_line=true
2541 fi
2542
2543 if $ls_containers && [ "$containers" != "" ] 2>&3; then
2544 $ls_add_empty_line && echo ""
2545 echo "$containers"
2546 fi
2547
2548 exit
2549 ;;
2550 reset )
2551 while has_prefix "$1" -; do
2552 case $1 in
2553 -h | --help )
2554 help "$op"
2555 exit
2556 ;;
2557 * )
2558 exit_if_unrecognized_option "$1"
2559 esac
2560 # shellcheck disable=SC2317
2561 shift
2562 done
2563 exit_if_extra_operand "$1"
2564
2565 reset
2566 exit "$?"
2567 ;;
2568 rm | rmi )
2569 rm_all=false
2570 rm_force=false
2571 while has_prefix "$1" -; do
2572 case $1 in
2573 -a | --all )
2574 rm_all=true
2575 ;;
2576 -f | --force )
2577 rm_force=true
2578 ;;
2579 -h | --help )
2580 help "$op"
2581 exit
2582 ;;
2583 * )
2584 exit_if_unrecognized_option "$1"
2585 esac
2586 shift
2587 done
2588
2589 rm_ids=""
2590 if $rm_all; then
2591 exit_if_extra_operand "$1"
2592 else
2593 exit_if_missing_argument "$op" "$1"
2594 while [ "$1" != "" ]; do
2595 rm_ids="$rm_ids $1"
2596 shift
2597 done
2598 fi
2599
2600 rm_ids=$(echo "$rm_ids" | sed "s/^ \+//" 2>&3)
2601
2602 if [ "$op" = "rm" ]; then
2603 remove_containers "$rm_ids" "$rm_all" "$rm_force"
2604 else
2605 remove_images "$rm_ids" "$rm_all" "$rm_force"
2606 fi
2607 exit
2608 ;;
2609 run )
2610 while has_prefix "$1" -; do
2611 case $1 in
2612 -c | --container )
2613 shift
2614 exit_if_missing_argument --container "$1"
2615 toolbox_container=$1
2616 ;;
2617 -h | --help )
2618 help "$op"
2619 exit
2620 ;;
2621 -r | --release )
2622 shift
2623 exit_if_missing_argument --release "$1"
2624 arg=$(echo "$1" | sed "s/^F\|^f//" 2>&3)
2625 exit_if_non_positive_argument --release "$arg"
2626 release=$arg
2627 ;;
2628 * )
2629 exit_if_unrecognized_option "$1"
2630 esac
2631 shift
2632 done
2633 exit_if_missing_argument "$op" "$1"
2634 if ! update_container_and_image_names; then
2635 exit 1
2636 fi
2637 run false false true "$@"
2638 exit
2639 ;;
2640 * )
2641 echo "$base_toolbox_command: unrecognized command '$op'" >&2
2642 echo "Try '$base_toolbox_command --help' for more information." >&2
2643 exit 1
2644 esac