Sometimes you need to compile a package as superuser (root), or test a package upgrade, or test a complete system upgrade. Doing so is a risky operation and could (and has) cause(d) severe damage. If there are no other options but doing this on production system (for instance the production system is the only remaining system with exactly that combination of hardware, OS, compiler, and so on), following procedure can be used to do it more or less safe.
There are different methods to achieve this, depending on how the system is configured. This instruction will be based on Logical Volume Management in Linux. I relies on that LVM is able to create RW-snapshots of LV. Same thing can probably be accomplished using similar filesystem features on different OS-es and architectures.
The base for this operation is creating a chroot-environment for our process, and a snapshot copy of our filesystems. We will enter a chroot-shell, do our operations on the snapshot-filesystem, extract our result to the ”real” filesystem if applicable, and then destroy the snapshot.
So, let’s create snapshot for our operation:
[root@titania ~]# lvcreate -s -L 1G -n root-mirror -p rw /dev/vgr01/root
Logical volume "root-mirror" created
This will create a rw snapshot LV called ”root-mirror” from ”root” LV in our ”vgr01″ volume group. This snapshot will contain 1Gb of free space for our changes. If you know you will need more, substitute 1G to whatever you might need, given that your volume group has enough unallocated space.
In similar manner, create any additional partitions that might be needed (for example /var, /tmp, /opt, /usr if they are not on same partition as /).
Remember: on Linux system, /boot can’t reside on a LV. In this tutorial, we will not mount it, and in our chroot-environment it will be empty. If you need it for some reason (to test upgrade of kernel or grub), and you happen to have it on a md-mirror, you could split the mirror and use that. But this is beyond the scope of this tutorial and also a very risky operation.
Now we mount our ”snapshot”:
[root@titania ~]# mkdir /mirror
[root@titania ~]# mount /dev/vgr01/root-mirror /mirror
Make sure it’s really mounted. If your system consists of several partitions (/var,/usr and so on), you need to mount them in consecutive order. ”/” needs to be mounted first and following partitions need to be mounted properly relative to the ”/”. Now we ”chroot” to it:
[root@titania /]# chroot /mirror
[root@titania /]# pwd
/
[root@titania /]# ls
bin dev export lib matrix misc net proc sbin software sys usr
boot etc home lost+found media mnt opt root selinux srv tmp var
[root@titania /]#
The output does not reveal whether we succeeded or not, but if we create a file in our ”mirror” and exit the chroot-environment we can see that it’s working:
[root@titania /]# touch test.chroot
[root@titania /]# ls /
bin dev export lib matrix misc net proc sbin software sys tmp var
boot etc home lost+found media mnt opt root selinux srv test.chroot usr
[root@titania /]# exit
exit
[root@titania /]# ls /
bin dev export lib matrix mirror mnt opt root selinux srv tmp var
boot etc home lost+found media misc net proc sbin software sys usr
[root@titania /]# ls /mirror
bin dev export lib matrix misc net proc sbin software sys tmp var
boot etc home lost+found media mnt opt root selinux srv test.chroot usr
[root@titania /]#
Now we can go back to our chroot-environment and start building/testing:
[root@titania /]# chroot /mirror
[root@titania /]#
.... DO OUR BUILDING STUFF HERE....
[root@titania /]#
[root@titania /]# exit
exit
[root@titania /]#
Your build/test is ready. Any changes/results/files are found in /mirror-directory. Make sure you copy it to the real filesystem before you destroy the snapshot LV.
Now it’s safe to unmount and remove our snapshot LV (and of course, WARNING FOR TYPOS HERE!!!!):
[root@titania /]# umount /mirror
[root@titania /]# lvremove /dev/vgr01/root-mirror
Do you really want to remove active logical volume "root-mirror"? [y/n]: y
Logical volume "root-mirror" successfully removed
[root@titania /]#
DISCLAMER:This tutorial is for people who know what they are doing and know LVM. This method works for me on my systems and I believe it will work on most of the systems out there. However I’m not offering any guarantee that it will work on yours, or not make your system unusable. You have been warned!


Senaste kommentarer