2014-11-10 02:25:22 +00:00
|
|
|
#!/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
|
|
|
|
|
2015-02-09 06:36:25 +00:00
|
|
|
mount -t overlay -o lowerdir="$chroot_base",upperdir="$target_dir",workdir="${target_dir}.work" "$mount_name" "$target_dir"
|
2014-11-10 02:25:22 +00:00
|
|
|
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 "$@"
|
|
|
|
|