From f4b3ff3c40fac32f395b83a6c753ff8f7d2ded8b Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Fri, 22 Apr 2022 14:29:53 +1200 Subject: [PATCH] Add chroot support to tripleo-mount-image This change adds support for immediately starting a chroot when an image has been mounted. This is convenient since many uses of tripleo-mount-image will require running commands inside the image (such as dnf install). Starting a useful chroot is slightly involved since it requires bind-mounting /dev and copying in /etc/resolve.conf. This change handles that, as well as cleanup and automatic unmount on exit. This change also makes the -m command optional, when not specified a temp directory will be created and used as the mount directory. Change-Id: I533ce13833bbfaab0ede3a11b6c22e45fe767939 --- scripts/tripleo-mount-image | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/scripts/tripleo-mount-image b/scripts/tripleo-mount-image index 1128b441d..c4266d410 100755 --- a/scripts/tripleo-mount-image +++ b/scripts/tripleo-mount-image @@ -24,6 +24,7 @@ SCRIPT_NAME=$(basename $0) NBD_DEVICE="" IMAGE_FILE="" MOUNT_DIR="" +CHROOT="" if [ ! -a "/dev/nbd0" ]; then modprobe nbd fi @@ -55,6 +56,8 @@ mount_show_options() { echo " -m -- Directory to mount image to." echo " -n -- NBD device to use (example, /dev/nbd0)." echo " Defaults to first available" + echo " --chroot -- Start a working chroot which is active until exit." + echo " Directory will be unmounted on chroot exit" echo echo "Mount an overcloud image to a directory" echo @@ -96,6 +99,28 @@ remove_device () { fi } +start_chroot () { + EACTION="echo Exited chroot, unmounting $MOUNT_DIR ; unmount_image" + trap "$EACTION" EXIT + + mount -o bind /dev $MOUNT_DIR/dev/ + EACTION="umount $MOUNT_DIR/dev/; $EACTION" + trap "$EACTION" EXIT + + if [ -a $MOUNT_DIR/etc/resolv.conf ]; then + mv $MOUNT_DIR/etc/resolv.conf $MOUNT_DIR/etc/resolv.conf.tmi + EACTION="mv $MOUNT_DIR/etc/resolv.conf.tmi $MOUNT_DIR/etc/resolv.conf; $EACTION" + trap "$EACTION" EXIT + fi + + cp /etc/resolv.conf $MOUNT_DIR/etc/resolv.conf + EACTION="rm $MOUNT_DIR/etc/resolv.conf; $EACTION" + trap "$EACTION" EXIT + + echo "Starting chroot in $MOUNT_DIR, exit will also unmount" + PS1="[\u@chroot \W]\$ " chroot $MOUNT_DIR /bin/bash -l +} + mount_image() { set -x @@ -266,7 +291,7 @@ if [ $SCRIPT_NAME == "tripleo-unmount-image" ]; then fi unmount_image else - TEMP=`getopt -o ha:m:n: -l help -n $SCRIPT_NAME -- "$@"` + TEMP=`getopt -o ha:m:n: -l help,chroot -n $SCRIPT_NAME -- "$@"` if [ $? != 0 ]; then echo "Terminating..." >&2 exit 1 @@ -280,11 +305,15 @@ else -a) IMAGE_FILE=$2 ; shift 2;; -m) MOUNT_DIR=$2 ; shift 2;; -n) NBD_DEVICE=$2 ; shift 2;; + --chroot) CHROOT=yes ; shift;; --) shift ; break;; *) echo "Error: unsupported option $1." ; exit 1;; esac done - if [ -z "${MOUNT_DIR}" ] || [ -z "${IMAGE_FILE}" ]; then + if [ -z "${MOUNT_DIR}" ]; then + MOUNT_DIR=$(mktemp -d) + fi + if [ -z "${IMAGE_FILE}" ]; then mount_show_options 1 fi MOUNT_DIR=$(realpath ${MOUNT_DIR}) @@ -307,4 +336,7 @@ else fi fi mount_image + if [ -n $CHROOT ]; then + start_chroot + fi fi \ No newline at end of file