From 99b187fe2ef26316eb08c3545d11781c3ea03f62 Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Thu, 2 Dec 2021 19:54:01 +0100 Subject: [PATCH] Fix mount routine (#5) * create: check that a folder exists before adding volume in podman * install: fix basedir chmod * create: let distrobox-export path to be optional * create: improve error message for missing distrobox-init * podman: when we use verbose, also podman should be verbose * arguments: use also full word for verbose * README: add brief explanation of all the tools --- README.md | 9 +++++++-- distrobox-create | 39 +++++++++++++++++++++++++++------------ distrobox-enter | 8 ++++++-- distrobox-export | 2 +- distrobox-init | 2 +- install | 4 ++-- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7a9ad3f001..ed694ea403 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,13 @@ All the props goes to them as they had the great idea to implement this stuff. Simply put it's a fancy `podman` wrapper to create and start containers highly integrated with the hosts. +It is divided in 4 parts: + +- `distrobox-create` - creates the container +- `distrobox-enter` - to enter the container +- `distrobox-init` - it's the entrypoint of the container (not meant to be used manually) +- `distrobox-export` - it is meant to be used inside the container, useful to export apps and services from the container to the host + ## Why? The intention is to provide a mutable environment on a host where the file-system is immutable (like Suse's MicroOS, Fedora Silverblue, Endless OS or SteamOS3) @@ -64,8 +71,6 @@ user 0m0,116s sys 0m0,063s ``` -It is included also a `distrobox-export` functionality to export applications and services from the container onto the host. - # Compatibility This project does **not need** a dedicated image but can use normal images in example from docker hub. diff --git a/distrobox-create b/distrobox-create index e0d98f5f1c..9fa5c8ea12 100755 --- a/distrobox-create +++ b/distrobox-create @@ -48,7 +48,7 @@ while :; do show_help exit ;; - -v) + -v | --verbose) verbose=1 shift ;; @@ -88,10 +88,16 @@ fi # Outputs: # prints the podman command to create the distrobox container generate_command() { + + host_folders="/ /etc /media /mnt /run /tmp /usr /var" + # Set the container hostname the same as the container name. # use the host's namespace for ipc, network, pid, ulimit - echo "podman create - --dns none + echo "podman create" + if [ "${verbose}" -ne 0 ]; then + echo "--log-level debug" + fi + echo "--dns none --env XDG_RUNTIME_DIR=/run/user/${container_user_uid} --hostname ${container_name} --ipc host" @@ -114,27 +120,31 @@ generate_command() { --user root:root --userns keep-id --volume ${distrobox_entrypoint_path}:/usr/bin/entrypoint:ro" + # let's check if we can include distrobox-export or not if [ -n "${distrobox_export_path}" ]; then echo "--volume ${distrobox_export_path}:/usr/bin/distrobox-export:ro" fi + echo "--volume ${container_user_home}:${container_user_home}:rslave - --volume /:/run/host:rslave - --volume /dev:/dev:rslave - --volume /etc:/run/host/etc - --volume /media:/media:rslave - --volume /mnt:/mnt:rslave - --volume /run:/run/host/run:rslave - --volume /tmp:/run/host/tmp:rslave - --volume /usr:/run/host/usr:rw,rslave - --volume /var:/run/host/var:rslave" + --volume /dev:/dev:rslave" + + # Check if host folder exists before mounting it + for host_folder in ${host_folders}; do + if [ -d "${host_folder}" ]; then + echo "--volume "${host_folder}":/run/host"${host_folder}":rslave" + fi + done + if [ -d /run/media ]; then echo "--volume /run/media:/run/media:rslave" fi + # mount also the XDG_RUNTIME_DIR to ensure functionality of the apps if [ -d /run/user/"${container_user_uid}" ]; then echo "--volume /run/user/${container_user_uid}:/run/user/${container_user_uid}" fi + # find all the user's socket and mount them inside the container # this will allow for continuity of functionality between host and container # for example using `podman --remote` to control the host's podman from inside @@ -142,6 +152,7 @@ generate_command() { for socket in $(find /run -iname "*socket" ! -path "/run/user/*" 2>/dev/null); do echo "--volume ${socket}:${socket}" done + # now execute the entrypoint, refer to `distrobox-init -h` for instructions echo "${container_image} /usr/bin/entrypoint -v --name ${container_user_name} @@ -149,6 +160,10 @@ generate_command() { --home ${container_user_home}" } +# check that we have a complete distrobox installation or +# entrypoint and export will not work. +[ -z "${distrobox_entrypoint_path}" ] && echo "Error: no distrobox-init found in $PATH" && exit 1 + # First, check if the image exists in the host if ! podman image exists "${container_image}"; then echo "Image not found, run this command first:" diff --git a/distrobox-enter b/distrobox-enter index fdbd980bdc..9830bafc9d 100755 --- a/distrobox-enter +++ b/distrobox-enter @@ -43,7 +43,7 @@ while :; do show_help exit ;; - -v) + -v | --verbose) shift verbose=1 ;; @@ -103,7 +103,11 @@ generate_command() { fi # entering container using our user and workdir - echo "podman exec --interactive --tty --user=${USER} --workdir=${HOME}" + echo "podman exec" + if [ "${verbose}" -ne 0 ]; then + echo "--log-level debug" + fi + echo "--interactive --tty --user=${USER} --workdir=${HOME}" echo "--env=DISTROBOX_ENTER_PATH=$(command -v distrobox-enter)" # exporting current environment to container for i in $(printenv | grep '=' | head -n -2); do diff --git a/distrobox-export b/distrobox-export index f7c6d0aa1d..34741b80e3 100755 --- a/distrobox-export +++ b/distrobox-export @@ -50,7 +50,7 @@ while :; do show_help exit ;; - -v) + -v | --verbose) shift verbose=1 ;; diff --git a/distrobox-init b/distrobox-init index 6930242a63..07d5616848 100755 --- a/distrobox-init +++ b/distrobox-init @@ -36,7 +36,7 @@ while :; do show_help exit ;; - -v) + -v | --verbose) shift verbose=1 ;; diff --git a/install b/install index 22dfaa0bab..fd4ed64c3c 100755 --- a/install +++ b/install @@ -28,7 +28,7 @@ while :; do show_help exit ;; - -v) + -v | --verbose) shift verbose=1 ;; @@ -87,7 +87,7 @@ else # deploy our files for file in "distrobox-$(echo "${release_name}" | sed 's/.tar.gz//g')"/distrobox-*; do cp "${file}" "${dest_path}" - chmod 0755 "${dest_path}/${file}" + chmod 0755 "${dest_path}/$(basename ${file})" done # securely delete unneeded files