script-dump/lxc/remap-uid-and-gid-for-lv

50 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Remap all files uid/gid to a new range using fuidshift
#
# This is a wrapper around `fuidshift` from `lxd-tools` Debian package for my use-case of LVM based root disks.
#
usage () {
echo "remap-uid-and-gid <container-name> [<target-lv>]"
printf "\nIf <target-lv> is not given it is computed from a default value with the given container name\n"
exit 1
}
if [ "$(which fuidshift)" = "" ]; then
echo "fuidshift binary not found. Please install lxd-tools package."
exit 1
fi
CONTAINER_NAME="${1}"
if [ "${CONTAINER_NAME}" = "" ] || [ "${CONTAINER_NAME}" = "--help" ] || [ "${CONTAINER_NAME}" = "-h" ]; then
usage
fi
TARGET_LV="${2}"
if [ "${TARGET_LV}" = "" ]; then
GUESSED_LV="/dev/mapper/daffy--vg-lxc--${CONTAINER_NAME}"
if [ -e "${GUESSED_LV}" ]; then
TARGET_LV="${GUESSED_LV}"
fi
fi
if [ "${TARGET_LV}" = "" ]; then
usage
fi
if mount | grep -qF " on /mnt"; then
echo "Mountpoint /mnt is already in use."
exit 1
fi
mount "${TARGET_LV}" /mnt
# we get e.g. lxc.idmap = u 0 1000000 65535 and want u:0:1000000:65535
USER_NAMESPACE=$(get-lxc-idmap-config u $CONTAINER_NAME | sed -r -e 's/.*= (.*)/\1/' -e 's/ /:/g')
GROUP_NAMESPACE=$(get-lxc-idmap-config g $CONTAINER_NAME | sed -r -e 's/.*= (.*)/\1/' -e 's/ /:/g')
printf "fuidshift %s %s %s\n" /mnt $USER_NAMESPACE $GROUP_NAMESPACE
# fuidshift /mnt $USER_NAMESPACE $GROUP_NAMESPACE
cd /
umount /mnt