Cloning Systems
Motivation
Once you have setup a system you might want to have a copy of that same system without the need to configure it the same. Of course: When you install the same lists of packets you have a similar system. If you maintain a larger number of systems you will also want to use a configuration management system to configure them all alike. But still you might find situations where it is convenient to be able to create a clone of an existing system.
Cloning with dd
When your system is not running you can make a 1:1 copy of your disk. You should not do this when the system is running since you will not get a consistent state of filesystem. One way to do this is by booting your system from a Live-CD of a Live-USB drive. Those live systems run off CD or USB and you can safely access the data on the hard drive.
E.g. If your harddrive is /dev/sda and you have connected an external USB drive and you want to store a .gz compressed image of your complete harddrive you could run:
dd if=/dev/sda bs=1M | gzip > /media/mydrive/img.gz
If you then boot your target system from a live CD you can overwrite the harddrive with
#ATTENTION: this overwrites your harddrive zcat /media/mydrive/img.gz | dd of=/dev/sdX bs=1M
where /dev/sdX should be replaced with the disk you want to overwrite.
E.g. The images for installing a raspberry pi often come in this way and you can use dd to write them to an SD-card.
The new system of course needs a disk of the same size or bigger. If the disk is bigger you can later add the remaining free space at the end to be used by your system.
Cloning with tar
Of course you can also create a backup of your files and then restore it on a target system but it is a bit more complicated. The problems you will be facing are:
- Your system might consist of multiple partitions and you you need to copy all of them.
- Some files on your root partition (/) are hidden because other filsystems are mounted over it. Especially /dev
- You will have to create a boot sector on your target system to be able to boot into your clone.
- You need to be careful to copy all special permissions and attributes of your files. E.g. /bin/ping has special attributes that can be seen with getcap that allows it to open a raw network socket that normally only can done by root.
Let's assume we have a system that consists only of a / and a /boot partition and we use ssh to copy this to a different system:
In order to have good access to our root partition we first do a bind mount:
mkdir /mnt2 mount --bind / /mnt2
Now we see everything that is in our root partition also under /mnt2. E.g.: /mnt2/dev/ contains the files that where initially there before /dev was mounted via the virtual file-system that contains the devices that the kernel created on the fly.
cd /mnt2 tar --acls --xattrs --xattrs-include='security.capability' -c -p -f - | ssh othersystem "cd /mnt3 ; tar -x --acls --xattrs --xattrs-include='security.capability' -f - " cd /boot tar --acls --xattrs --xattrs-include='security.capability' -c -p -f - | ssh othersystem "cd /mnt3/boot ; tar -x --acls --xattrs --xattrs-include='security.capability' -f - "
This assume that you can ssh into othersystem and that we want our cloned system in the filesystem below /mnt3 and also want the /boot filesystem in the main filesystem there.
After that we would go to othersystem and we would have to write a bootsector there. E.g. by:
mount --bind /dev /mnt3/dev mount --bind /proc /mnt3/proc mount --bind /sys /mnt3/sys chroot /mnt3 grub-install /dev/sda update-grub exit umount /mnt3/dev umount /mnt3/proc umount /mnt3/sys
What do do after cloning
Now you usually do not want your cloned system to be totally identical. In order to know that it is a cloned system you should change the /etc/hostname to a new name. Also you will most likely want a differnt ssh key so you should regenerate this. Also not that the MAC address of your network card is different and also maybe other hardware is different. Some systems bind the name of a network card to the MAC address of the card that first found it so you might change that rules. They can be found in /etc/udev/rules.d
If you have a static IP address you need to change that otherwise you will have 2 systems with the same IP which will cause you a lot of headache.
Exercises
- create a bind mount of your root partition
- create a .gz compressed backup of your root partition on an external harddrive using tar.
- verify the backup and look it contains the right device files in /dev/
- use getcap to view the "capabilities" of ping.