Categories
Bash LXC

Chown Shifting SUB*ID like a bash ninja

Finally, a quick and dirty solution to the LXC SubUID and SubGID conversion conundrum

Pretty self explanatory

#!/usr/bin/env bash

if [[ $# < 1 ]] ; then
	cat <<-EOF

		LXC Toolkit by ASBRA AB

		lxc-chown is used to shift SUBUID and SUBGID inside LXC container folders.
		The script uses the current working directory to start from and works recursively.

		if container is priviged root=0 and you'd like to shift to subuid 165536, the command would be:

		    $0 --dry-run 165536

		The other way around, shifting from 165536 back to 0

		    $0 --dry-run -165536

	EOF
	exit 1

elif [[ $# == 1 ]] ; then
	export LXC_SHIFT_BY="$1"
	find -not -type l -exec sh -c 'chown $((${LXC_SHIFT_BY}+$(stat -c "%u" "$1"))):$((${LXC_SHIFT_BY}+$(stat -c "%g" "$1"))) "$1"' _ {} \;

elif [[ $# == 2 && $1 == "--dry-run" ]] ; then
	export LXC_SHIFT_BY="$2"
	find -not -type l -exec sh -c 'echo "chown $((${LXC_SHIFT_BY}+$(stat -c "%u" "$1"))):$((${LXC_SHIFT_BY}+$(stat -c "%g" "$1"))) $1"' _ {} \;
fi

By Nimpen J. Nordström

System Developer and Network Security Enthusiast