For mount devices et chroot in it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.9 KiB

#!/usr/bin/env bash
target=$1
ask_for_execution() {
USER_ANSWER="?";
while [ "${USER_ANSWER}" != "n" ] && [ "${USER_ANSWER}" != "y" ] && [ "${USER_ANSWER}" != "q" ]
do
echo -n "Is this the good target ? [y/n/q]"
read -r -n 1 USER_ANSWER
echo "";
done
if [ "$USER_ANSWER" == "y" ]
then
return 0
elif [ "$USER_ANSWER" == "n" ]
then
return 1
elif [ "$USER_ANSWER" == "q" ]
then
exit 0
else
exit 0
fi
}
check_virtual_kernel_filesystems()
{
echo "call check_virtual_kernel_filesystems"
if mount |grep -q "udev on ${target}/dev type devtmpfs" && \
mount |grep -q "devpts on ${target}/dev/pts type devpts" && \
mount |grep -q "proc on ${target}/proc type proc" && \
mount |grep -q "sysfs on ${target}/sys type sysfs" && \
mount |grep -q "tmpfs on ${target}/run type tmpfs" && \
mount |grep -q "tmpfs on ${target}/dev/shm type tmpfs"
then
echo "Ok, Filesystems are ready for chroot"
return 0
else
echo "Warning : Not ready for chroot !!!"
return 1
fi
}
mount_virtual_kernel_filesystem()
{
echo "call mount_virtual_kernel_filesystem"
echo "sudo operation : mount dev sys proc sys run /dev/shm on ${target}"
sudo /usr/bin/mount -v --bind /dev "${target}/dev" || return 1
sudo /usr/bin/mount -vt devpts devpts -o gid=5,mode=0620 "${target}/dev/pts" || return 1
sudo /usr/bin/mount -vt proc proc "${target}/proc" || return 1
sudo /usr/bin/mount -vt sysfs sysfs "${target}/sys" || return 1
sudo /usr/bin/mount -vt tmpfs tmpfs "${target}/run" || return 1
echo "Content of ${target}/dev :"
ls -l "${target}/dev/"
echo "Content of ${target}/dev/shm :"
ls -l "${target}/dev/shm"
if [ -h "${target}/dev/shm" ]
then
echo /usr/bin/install -v -d -m 1777 "${target}$(realpath /dev/shm)" || return 1
sudo /usr/bin/install -v -d -m 1777 "${target}$(realpath /dev/shm)" || return 1
else
echo /usr/bin/mount -vt tmpfs -o nosuid,nodev tmpfs "${target}/dev/shm" || return 1
sudo /usr/bin/mount -vt tmpfs -o nosuid,nodev tmpfs "${target}/dev/shm" || return 1
fi
return 0
}
cat > "${HOME}"/target_chroot.sh <<EOSF
if ! mount |grep -q "proc on ${target}/proc type proc"
then
echo "chroot is not available at this step"
exit 1
fi
sudo /usr/sbin/chroot ${target} /usr/bin/env -i \
HOME=/root \
TERM="${TERM}" \
debian_chroot=" ${target} " \
PS1='(chroot ${target}) \u:\w \$ ' \
PATH=/usr/bin:/usr/sbin \
/bin/bash --login
EOSF
cat > "/tmp/$(whoami)_sudo" << EOF
$(whoami) ALL = NOPASSWD: /usr/bin/mount * ${target}*
$(whoami) ALL = NOPASSWD: /usr/sbin/chroot ${target}*
EOF
target_chroot()
{
echo "call target_chroot $1"
CHROOTFILE=$1
sed "1iif [[ ! \"\${UID\}\" -eq 0 ]] && [ ! -e ${CHROOTFILE} ]" -i "${CHROOTFILE}"
sed "2ithen" -i "${CHROOTFILE}"
sed "3iecho \"bad chroot\"" -i "${CHROOTFILE}"
sed "4iexit 1" -i "${CHROOTFILE}"
sed "5ielse" -i "${CHROOTFILE}"
sed "\$afi" -i "${CHROOTFILE}"
if cat "${CHROOTFILE}" | sudo /usr/sbin/chroot "${target}" /usr/bin/env -i \
HOME=/root \
TERM="$TERM" \
debian_chroot=" ${target} " \
PS1='(${target} chroot) \u:\w\$ ' \
PATH=/usr/bin:/usr/sbin \
/bin/bash
then
echo "command succeed"
return 0
else
return 1
fi
}
usage()
{
echo "Prepare an valid chroot environment"
echo "$ mount_chroot.sh target [filename]"
echo "Examples:"
echo "$ bash mount_chroot.sh /mnt/sid"
echo "$ bash mount_chroot.sh /mnt/sid myscript.sh"
exit 0
}
main()
{
nbargs=$#
if [[ ${nbargs} -ge 1 ]]
then
target=$1
else
echo "--- Give at least target as argument"
usage
fi
echo "--- chroot target is ${target}"
if [ ! -d "${target}" ]
then
echo "--- Error: not a directory"
exit 1
fi
if [[ ! "${target}" =~ ^/mnt/ ]]
then
echo "--- Error: not mounted under /mnt"
exit 1
fi
if ! ask_for_execution
then
echo "--- Give the good target"
exit 1
fi
if ! check_virtual_kernel_filesystems
then
if ! mount_virtual_kernel_filesystem
then
echo "Mount failure. Please fix it!"
exit 1
fi
if ! check_virtual_kernel_filesystems
then
echo "Mount failure. Please fix it!"
exit 1
fi
fi
if [[ ${nbargs} -eq 1 ]]
then
bash ~/target_chroot.sh
elif [[ ${nbargs} -ge 2 ]]
then
shift
script="$*"
echo "--- script is ${script}"
target_chroot "${script}"
fi
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]
then
main "$@"
fi