from https://unix.stackexchange.com/questions/14345/how-do-i-tell-im-running-in-a-chroot
## stat - Display file or file system status.
## -c --format=FORMAT use the specified FORMAT instead of the default;
## output a newline after each use of FORMAT
## The valid format sequences for files (without --file-system):
## %d device number in decimal (st_dev)
## %i inode number
## 현재 root의 device, inode가 /sbin/init가 실행한 root와 동일한지 확인
## PS1에 적용하면 normal과 chroot간에 다른 PS1 사용 가능
if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then
echo "We are chrooted!"
else
echo "Business as usual"
fi
Read This
This is a 5 year old thread. But still it is imho not fully answered. There's a little example missing. Here it is:
In this example:
/dev/sda3 = / /dev/sda2 = swap …using a GPT partition. With MBR (dos partition) the PARTUUID's are shorter but the procedure is the same…
get the PARTUUID's with blkid:
blkid -s PARTUUID -o value /dev/sda3 # root
77fd7830-faa2-4e99-a48b-337ad9eded28
blkid -s PARTUUID -o value /dev/sda2 # swap
5b63167a-6fd2-4e72-948c-90832372956c
/boot/grub/grub.cfg:
search --no-floppy --part-uuid --set=root 77fd7830-faa2-4e99-a48b-337ad9eded28
menuentry "GNU/Linux, KERNEL 4.12.7-lfs-8.1" {
linux /boot/vmlinuz-4.12.7-lfs-8.1 root=PARTUUID=77fd7830-faa2-4e99-a48b-337ad9eded28 net.ifnames=0 ipv6.disable=1 ro rootwait rootfstype=ext4
}
/etc/fstab/:
PARTUUID=77fd7830-faa2-4e99-a48b-337ad9eded28 / ext4 noatime,nodiratime,errors=remount-ro 0 1
PARTUUID=5b63167a-6fd2-4e72-948c-90832372956c swap swap pri=1 0 0
This is known to WORK with lfs8.1 (kernel 4.12.7) But i think it should work with most other kernels too (olders and newers…)