This is an old revision of the document!


How to clone your full Linux installation

One of the main advantages of Linux operating system is that you can move or clone your system to other disks and machines. Linux kernel normally includes all the open-source available drivers for all supported hardware. So there is no problem moving disk/installation from one machine to another. Of course there ale also no software licensing issues like with other most used operation systems. Cloning ready to use installation is as easy as copping files. You can even safely clone a running system, then chroot into your new copy to reinstall grub boot loader to boot from your new disk. More difficult is only if you need to do BIOS/EUFI transition. To clone your Linux install, there are just tree simple steps.

  1. copy the partitions (cp -a is all you need)
  2. edit the /etc/fstab on new location to allow new disk partitions to be mounted when booting
  3. install the grub boot loader

There is also another way how to clone or backup your hard rive. You can also use Clonezilla wizard which works with a whole partitions or disk. Clonezilla typically creates an image from disk or partition, which you can later on restore or clone. However you can be using the disk/partition you cloning/restoring so you need to boot into a different system and the files and data stored in the resulting “image” are difficult to access. I personally prefer using basic commands like cp (or rsync or tar) and mount. I consider it to be more straight forward as it lets me better understand the background.

Remember that all disk described commands must be done as root.

Clone running system to a local disk or partition

This is the easiest and fast way how to clone or backup running system. The disadvantage is that you need to connect your new disk to be able to copy locally.

Prepare and mount

Boot the Linux system you want to clone/backup. To see which partition is mounted as root or other important directory see:

df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda2                112G   18G   89G  17% /

To see connected disks, partition types and sizes:

fdisk -l

See the partitions sizes and used filesystem. If needed you can do changes using gparted. If your new disk should be using the UEFI method for booting, make sure the new disk is using gpt type of partition table. To change Partition table to gpt use:

sgdisk -g /dev/sdX

Lets say your main disk is /dev/sda, and /dev/sdb is temporally connected disk you want clone your system to.

If you want to use uEFI on you new disk, you will need a small FAT32 efi partition too. If there is no suitable small partition on your new drive, create it using gparted.

Mount the root partition you want to copy the system from and mount the partition you want to copy the system to. The partition of running system is already mounted as root filesystem. Mount this partion second time. This is easy way how to handle special files like /dev/ and /proc/.

mount /dev/sda2 /mnt/from   # this partition is already mounted as /
mount /dev/sdb3 /mnt/to
mount /dev/sdb2 /mnt/new_uefi_part # only if you use uEFI booting on your new location

Copy

Copy the root (and possibly others) partition to new disk/partition

cp -a /mnt/from/* /mnt/to
cp -a /boot/efi/* /mnt/new_uefi_part # only if you use uEFI booting

Edit fstab

Learn your new UUID (disk partitions ids) and edit /etc/fstab on the new location

ls -l /dev/disk/by-uuid/
blkid # or lsblk -f

if UUID is missing, PARTUUID can be used too, or you can create or change UUID by gparted.

 blkid /dev/sdb5
/dev/sdb5: TYPE="swap" PARTUUID="0008d844-05"

Install grub

Let assume you are still running the Linux from your original location. Now lets do chroot to use the cloned location and install and configure grub for on the new disk or location. Before doing chroot, mount the various dirs as grub will need them to access for the grub-install and update-grub commands.

cd /mnt/sdb3
mount --bind /proc proc
mount --bind /sys sys
mount --bind /dev dev
mount --bind /run run
mount /dev/sdb4 boot/efi  # only needed if you use uefi
chroot .
grub-install /dev/sdb # sdb is your new disk
# if you did not mount those dirs as described above you get an err
# grub-install: error: /usr/lib/grub/i386-pc/modinfo.sh doesn't exist. Please specify --target or --directory.
update-grub

If the booting fails you can try boot-repair-disk as described below or read the troubleshooting section.

Clone running system over a network

Create USB live distro to boot in

I recommend you to use Boot-Repair-Disk. Download the 64 bit version.

It is live ubuntu based distro wich will guide you to fix your booting. It should be able to fix both BIOS and UEFI type of booting and changes between them.

To place the Boot-Repair-Disk on your USB stick, you just can not use dd like dd bs=4M if=boot-repair-disk-64bit.iso of=/dev/sdx && sync This will make unbootable usb stick. Use other recommended tool. I used unetbootin and it booted under the 64 bit booting menu option.

Boot-Repair-Disk contains gparted, ssh client (not server) and rsync, so this live distribution may be used also to do the copy of the system over network.

Boot-Repair-Disk supports Debian, Ubuntu, Mint, Fedora, OpenSuse, ArchLinux … The transition of your booting from BIOS to UEFI should also work. Or have a look at Switch to UEFI boot. The presence/absence of a special Debian package grub-efi-amd64 seems to make the difference.

Boot the live Boot-Repair-Disk distro on the destination computer.

Prepare new disk

First run gparted to create/see/format your new disk partitions. If you are planning to use uEFI booting now or in near feature, don't forget to create a small FAT32 partition and use GTP as partition table type. Don't forget to create swap partition.

Copy/sync your old system over the network

On the target computer run:

mount /dev/sda1 /mnt/new_root
cd /mnt/new_root
rsync -Hazx -v --numeric-ids --delete root@192.168.0.119:/mnt/sda2  .

After you enter the command rsync you will be asked for a root password on the remote computer where is the original operating system to be cloned. You may want temporarily allow root login (edit /etc/ssh/sshd_config and restart sshd) or put your temporary public key to /root/.ssh/authorized_keys to the remote server.

Edit fstab

See instruction above. If you want to use uEFI booting, don't forget to add/update an entry for /boot/efi.

Install Grub

Run the the boot-repair wizard from your usb stick and follow the prompts.

Troubleshooting

Not booting using UEFI?

When you have a new booting option on uEFI system, this option should be registered with the uEFI firmware. This should be done automatically when doing update-grub or when using the boot-repair wizard.(see above) If it fails you can try this:

Copy (a symlink would be relevant here, but that cannot be done on a FAT file system!) the GRUB image to that other path on the EFI System Partition: efi/boot/bootx64.efi (again, relatively to the ESP filesystem's root, which means /boot/efi/efi/boot/bootx64.efi in absolute). This is where the UEFI firmware looks for a boot loader when it has not been configured for a specific path, typically on removable media. Reboot to check that…

If still not booting read this GRUB EFI Reinstall article on debian wiki.

This article says that you should use The rEFInd Boot Manager to boot.

After you manage to boot once with this tool, fix the booting from inside the running system using grub-install and update-grub .

Still not booting ?

If everything fails and your system is still not booting, you may try another approach. Do additional minimal system install on the new disk in order to be able to repair the booting. Allocate or create for this purpose a new small partition. Just make ordinary new install to this partition. The new installation process will detect all already installed operating systems and let you boot all of them. To create the new small partition use the install wizard menu or gparted from a live distribution.

If you have installed small “rescue” install before you have cloned your main installation, boot into the rescue partition and run upgrade-grub. To see which other installed systems where detected, run /etc/grub.d/30_os-prober.

Sources of information

Possible little fixes at the end

Generate new ssh keys for your new mashine

Each machine should have unique ssh keys. If you clone your machine, you should create new ssh keys.

rm /etc/ssh/ssh_host_*
dpkg-reconfigure openssh-server

Rename your network adapters back to old names

When you run a cloned system, the Linux “thinks” that he has found new Ethernet cards and will assigned new names. Therefore your network card will be having different names like eth3, so your network setting in /etc/network/interfaces may be wrong,your booting may take longer or your internet connection may not work.

To give the names you want, just edit this file.

vim /etc/udev/rules.d/70-persistent-net.rules

nvidia driver

If your new system has nvidia graphic card, you be better using proprietary nvidia drivers. https://wiki.debian.org/NvidiaGraphicsDrivers You can see used driver by lsmod command. The name of sometime problematic opensource driver is nouveau. For me it fixed random freezes when running for example iceweasel. After installing the recommended driver (nvidia-detect will tell you) install and run nvidia-xconfig to create /etc/X11/xorg.conf for you.

 
clone_linux_instalation.1439214248.txt.gz · Last modified: 2015/08/10 15:44 (external edit)