[explorer/machine_type] Implement chroot detection using /proc/.../mountinfo

This commit is contained in:
Dennis Camera 2021-08-01 23:09:02 +02:00
parent 5af1317c29
commit 05c2a62191

View file

@ -165,11 +165,21 @@ has_chroot_procfs() {
test -d /proc/ test -d /proc/
} }
check_chroot_procfs() { check_chroot_procfs() (
is_chroot=false # default
if test -e /proc/1/root && ! files_same /proc/1/root / if test -e /proc/1/root && ! files_same /proc/1/root /
then
is_chroot=true
fi
if test -e /proc/1/mountinfo -a -e /proc/self/mountinfo
then
has_mountinfo=true
cmp -s /proc/1/mountinfo /proc/self/mountinfo || is_chroot=true
fi
if ${is_chroot}
then then
# try to determine where the chroot has been mounted # try to determine where the chroot has been mounted
(
rootdev=$(LC_ALL=C df -P / | awk 'NR==2{print $1}') rootdev=$(LC_ALL=C df -P / | awk 'NR==2{print $1}')
if test -e "${rootdev}" if test -e "${rootdev}"
@ -181,14 +191,26 @@ check_chroot_procfs() {
# shellcheck disable=SC2012 # shellcheck disable=SC2012
root_ino=$(ls -1di / | awk '{print $1}') root_ino=$(ls -1di / | awk '{print $1}')
# Get mount point # escape chroot and find mount point by inode
chroot /proc/1/root find "${rootdevmnt}" -xdev -type d -inum "${root_ino}" chroot /proc/1/root find "${rootdevmnt}" -xdev -type d -inum "${root_ino}"
elif ${has_mountinfo}
then
while read -r mntid _ _ _ cmntpnt _
do
read -r _ _ _ _ hmntpnt _ <<-EOF
$(grep -e "^$((mntid)) " /proc/1/mountinfo)
EOF
printf '%s\n' "${hmntpnt%${cmntpnt}}"
done </proc/self/mountinfo \
| sort -u \
| head -n 1 # just take the first...
fi
return 0
else
return 1
fi fi
) )
return 0
fi
return 1
}
# Check for container # Check for container