78 lines
1.3 KiB
Bash
78 lines
1.3 KiB
Bash
|
#!/usr/bin/env bash
|
||
|
|
||
|
err() { echo "$1" >&2; }
|
||
|
|
||
|
usage() {
|
||
|
cat <<- EOF
|
||
|
Usage: "${0##*/}" [options] <chroot>
|
||
|
Options:
|
||
|
-s chroot base dir
|
||
|
-n mount name
|
||
|
-u unmount chroot
|
||
|
EOF
|
||
|
}
|
||
|
|
||
|
chroot.mount() {
|
||
|
for i in "$target_dir" "$chroot_base"; do
|
||
|
[[ "$i" ]] || {
|
||
|
usage
|
||
|
return 1
|
||
|
}
|
||
|
done
|
||
|
|
||
|
[[ -d "$chroot_base" ]] || {
|
||
|
err "Chroot base ($chroot_base) does not exist."
|
||
|
return 1
|
||
|
}
|
||
|
|
||
|
mount_name="${mount_name:-chroot}"
|
||
|
|
||
|
for i in "$target_dir" "${target_dir}.work"; do
|
||
|
[[ -d "$i" ]] || {
|
||
|
mkdir -p "$i" || {
|
||
|
return 3
|
||
|
}
|
||
|
}
|
||
|
done
|
||
|
|
||
|
mount -t overlayfs -o lowerdir="$chroot_base",upperdir="$target_dir",workdir="${target_dir}.work" "$mount_name" "$target_dir"
|
||
|
mount -t proc "${mount_name}_proc" "${target_dir}/proc"
|
||
|
mount --rbind /dev "${target_dir}/dev"
|
||
|
mount --rbind /sys "${target_dir}/sys"
|
||
|
}
|
||
|
|
||
|
chroot.umount() {
|
||
|
umount --recursive "$target_dir"
|
||
|
}
|
||
|
|
||
|
main() {
|
||
|
while [[ "$1" ]]; do
|
||
|
case "$1" in
|
||
|
-s) chroot_base="$2"; shift;;
|
||
|
-n) mount_name="$2"; shift;;
|
||
|
-u) action='umount';;
|
||
|
|
||
|
--) shift; break;;
|
||
|
-*)
|
||
|
err "Unknown key: $1"
|
||
|
usage
|
||
|
return 1
|
||
|
;;
|
||
|
*) break;;
|
||
|
esac
|
||
|
shift
|
||
|
done
|
||
|
|
||
|
target_dir="$1"
|
||
|
action="${action:-mount}"
|
||
|
mount_name="${mount_name:-chroot}"
|
||
|
|
||
|
case "$action" in
|
||
|
mount) chroot.mount;;
|
||
|
umount) chroot.umount;;
|
||
|
esac
|
||
|
}
|
||
|
|
||
|
main "$@"
|
||
|
|