1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
raspberrypi-build/scripts/common.sh

58 lines
1.2 KiB
Bash
Raw Normal View History

2017-07-02 03:09:19 +00:00
unmount() {
2016-04-11 07:21:07 +01:00
if [ -z "$1" ]; then
DIR=$PWD
else
DIR=$1
fi
while mount | grep -q "$DIR"; do
local LOCS
LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r)
2016-04-11 07:21:07 +01:00
for loc in $LOCS; do
umount "$loc"
2016-04-11 07:21:07 +01:00
done
done
}
2016-05-04 15:51:41 +01:00
2017-07-02 03:09:19 +00:00
unmount_image() {
2016-05-04 15:51:41 +01:00
sync
sleep 1
local LOOP_DEVICES
LOOP_DEVICES=$(losetup -j "${1}" | cut -f1 -d':')
for LOOP_DEV in ${LOOP_DEVICES}; do
if [ -n "${LOOP_DEV}" ]; then
local MOUNTED_DIR
MOUNTED_DIR=$(mount | grep "$(basename "${LOOP_DEV}")" | head -n 1 | cut -f 3 -d ' ')
if [ -n "${MOUNTED_DIR}" ] && [ "${MOUNTED_DIR}" != "/" ]; then
unmount "$(dirname "${MOUNTED_DIR}")"
fi
sleep 1
losetup -d "${LOOP_DEV}"
2016-05-27 11:54:56 +01:00
fi
done
2016-05-04 15:51:41 +01:00
}
2016-04-11 07:21:07 +01:00
on_chroot() {
2017-07-02 20:44:07 +00:00
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/proc)"; then
mount -t proc proc "${ROOTFS_DIR}/proc"
fi
2016-04-11 07:21:07 +01:00
2017-07-02 20:44:07 +00:00
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev)"; then
mount --bind /dev "${ROOTFS_DIR}/dev"
fi
2017-07-02 20:44:07 +00:00
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev/pts)"; then
mount --bind /dev/pts "${ROOTFS_DIR}/dev/pts"
fi
2016-04-11 07:21:07 +01:00
2017-07-02 20:44:07 +00:00
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/sys)"; then
mount --bind /sys "${ROOTFS_DIR}/sys"
fi
2016-04-11 07:21:07 +01:00
2017-07-02 20:44:07 +00:00
capsh --drop=cap_setfcap "--chroot=${ROOTFS_DIR}/" -- "$@"
2016-04-11 07:21:07 +01:00
}
2017-07-02 13:08:18 +00:00
export -f unmount
export -f unmount_image
export -f on_chroot