Please help us improve HowtoForge and take our survey:
Entering A Safe Mirror When Logging In With Unionfs And Chroot
Entering A Safe Mirror When Logging In With Unionfs And Chroot1. IntroductionWhen reading a 'hint' on the website of LinuxFromScratch I discovered the special capabilities of unionfs, specially in combination with chroot. Later I read a HowTo on a wikiwebsite of Gentoo, about entering a chrooted homedirectory when using a special script as shell. Combining these two brings me to using a chrooted environment, which you enter when logging in as a special user. This environment is a exact copy (mirror) of the system you're working on. Because you're in safe copy of the real system, you can do whatever you like, it will never change the system, everything stays inside the cache (the readwrite branch). Links:
2. Basic technique
Do whatever you like, install, change and remove files from the system, and no harm whatsoever. Your real system stays untouched.
This may sound like magic, but is in reality just possible by combining some techniques from all those available for Linux.
2.1 Unionfs
The most important part is the use of unionfs. Unionfs gives you the possibility to create a filesystem, which is the union of at least two others. See www.unionfs.org for more info. Now by letting the new filesystem be the union of our original filesytem (the root) in only read mode, and of a temporary filesystem (the cache) in readwrite mode, you'll have a filesystem which looks exactly like your original filesystem, but in which you can modify, delete and/or add files without doing anything to your original system. This is not possible, because the root is mounted readonly. Every modification is by the unionfs stored in the cache. A special note: today [june 2007] it looks as if unionfs will be included in the kernel. Unionfs is undergoing heavy development at this moment. Look at the website for more info. On the website you will find information howto enable unionfs. For thye latest kernels (later than 2.6.19) there is a patch for the kernelsource, for not so recent kernels there is a external module.
2.2 (Re)Mounting
One extra thing you'll have to do is (re)mounting several crucial directories like /dev, /proc and /sys. This is because the union filesystem does not preserve existing mount points.
2.3 ChrootBy chrooting to this mountpoint, you enter an environment which is absolutely a copy of your system. You can do whatever you like, even remove crucial directories and files. Test it! Look how far you can go before your system gets stuck.
2.4 Logging in to this environment
Like the concept explained in Home_directory_jail it is possible by creating
a special loginshell to enter the environment created with unionfs and chroot.
3. Preparation3.1 The cache partitionTo start a partition with sufficient space to function as cache. This does not have to be a physical partition, it may be a virtual drive. Create this drive with:
dd if=/dev/zero of=/mnt/cache.img bs=1M count=500 chmod 777 /mnt/cache mkdir /mnt/union This creates a virtual partition (or drive) of 500M. (Note: the loopback device has to be supported in your kernel. Kernels of most distributions do.)
3.2 Special loginshellCreate a shellscript chroot-union which will do all the necessary steps: The chroot-union script in /bin: #!/bin/bash
function mount_unionfs {
# mount temporary filesystems
if [ -z "$(mount -t unionfs | grep -w /mnt/union )" ]; then
sudo /bin/mount -t unionfs -o dirs=/mnt/cache:/=ro unionfs /mnt/union
fi
if [ -n "$(mount -t unionfs | grep -w /mnt/union )" ]; then
# basic system mounts
if [ -z "$(mount | grep -w /mnt/union/dev)" ]; then
sudo /bin/mount --bind /dev /mnt/union/dev 2> /dev/null
fi
if [ -z "$(mount -t devpts | grep -w /mnt/union/dev/pts)" ]; then
sudo /bin/mount -t devpts devpts /mnt/union/dev/pts 2> /dev/null
fi
if [ -z "$(mount -t tmpfs | grep -w /mnt/union/dev/shm)" ]; then
sudo /bin/mount -t tmpfs shm /mnt/union/dev/shm 2> /dev/null
fi
if [ -z "$(mount -t sysfs | grep -w /mnt/union/sys)" ]; then
sudo /bin/mount -t sysfs sysfs /mnt/union/sys 2> /dev/null
fi
if [ -z "$(mount -t proc | grep -w /mnt/union/proc)" ]; then
sudo /bin/mount -t proc proc /mnt/union/proc 2> /dev/null
fi
if [ -z "$(mount | grep -w /mnt/union/tmp)" ]; then
sudo /bin/mount --bind /tmp /mnt/union/tmp 2> /dev/null
fi
else
echo "Mount of /mnt/union failed."
exit 2
fi
}
function umount_unionfs {
#
# unmount /tmp
#
if [ -n "$(mount | grep -w /mnt/union/tmp)" ]; then
sudo /bin/umount /mnt/union/tmp 2> /dev/null
fi
#
# unmount /proc
#
if [ -n "$(mount -t proc | grep -w /mnt/union/proc)" ]; then
sudo /bin/umount /mnt/union/proc 2> /dev/null
fi
#
# unmount /sys
#
if [ -n "$(mount -t sysfs | grep -w /mnt/union/sys)" ]; then
sudo /bin/umount /mnt/union/sys 2> /dev/null
fi
#
# unmount /dev/shm
#
if [ -n "$(mount -t tmpfs | grep -w /mnt/union/dev/shm)" ]; then
sudo /bin/umount /mnt/union/dev/shm 2> /dev/null
fi
#
# unmount /dev/pts
#
if [ -n "$(mount -t devpts | grep -w /mnt/union/dev/pts)" ]; then
sudo /bin/umount /mnt/union/dev/pts 2> /dev/null
fi
#
# unmount /dev
#
if [ -n "$(mount | grep -w /mnt/union/dev)" ]; then
sudo /bin/umount /mnt/union/dev 2> /dev/null
fi
if [ -n "$(mount | grep -w /mnt/union )" ]; then
sudo /bin/umount /mnt/union 2> /dev/null
fi
}
mount_unionfs
# enter the chroot
sudo /usr/sbin/chroot /mnt/union /bin/su --shell /bin/bash --login $USER
# umount temporary filesystems
umount_unionfs
EOF
Add the new loginshell to the /etc/shells file. You'll have to do this when PAM will check the shell.
3.3 Create user and group.Create a new group and user with this script as shell: groupadd -g 27 uniongroup
3.4 Give the user enough rightsGive the new user more rights with sudo. Add the following line to the configurationfile of sudo, /etc/sudoers: unionuser ALL=(ALL) ALL Note: there are other ways to give this user the permissions. I'm looking at them at this moment. Note: giving these full permissions is too much for a normal user. But for a user which will install software and modify your system it's necessary.
What is possibleSafe and secure environment for normal users This construction is very suitable for guest users, which you cannot trust. The first thing I'v tried is
starting a graphical session. I did not have any problem. Another possible use is the installation of software as this user. This can be done as follows:
For example the compilation and installation of a small package, audiofile-0.2.6. Assume the source is in /tmp. First login: [root@hostname ]# login Now compile and install the package: [unionuser@hostname ]$ cd /tmp/audiofile-0.2.6 Now exit the session and check the contents of the cache: [unionuser@hostname ]$exit The home directory appears here because the shell Bash changes the file .bash_history; the var directory shows up because of changes in the file /var/run/utmp and directory /var/run/sudo. This proves it works like it should. Now, when looking to the changes in de /usr directory where I've installed the software gives: [root@hostname ]# find usr -type f As you can see everything is in the /mnt/cache/usr directory. You can make a backup of this: [root@hostname ]# find usr | sort -u > /tmp/filelist-audiofile-0.2.6 It's also very possible to make a backup of all the files which will be overwritten: [root@hostname ]# for installfile in $(cat /tmp/filelist-audiofile-0.2.6); do \ Now do the real install by copying all the files to the root: [root@hostname ]# for installfile in $(cat /tmp/filelist-audiofile-0.2.6); do \ Note: the commands above are to illustrate the idea. I've created scripts doing the backing up, checking and installing, and it works very good.
|








Recent comments
1 day 9 hours ago
1 day 11 hours ago
1 day 15 hours ago
3 days 9 hours ago
3 days 9 hours ago
3 days 10 hours ago
5 days 11 hours ago
6 days 19 hours ago
1 week 11 hours ago
1 week 15 hours ago