Porting Linux to a different drive

This problem arises infrequently. Usually, it is easier to reinstall the system from scratch than to transfer already installed version on another hard disk or another partition. But if you have there important programs that are undesirable to remove, or you have changed so many settings in the system that installing it again will take much more time than the transfer, then the transfer would be preferable.

In this article we will discuss how to move Linux to another disk using a utility cp or tar archive. The second method is interesting because you can create a backup of the whole system and then just restore it if problems occur.

How to move Linux to another disk

Since all data, settings and objects for the Linux operating system are files that you can transfer your operating system where you want by just copying all the files you need. In Windows just simply will not work, as there are more complex file system with complex dependencies.

1. Preparing to transfer

First, consider how to use cp to transfer files of the operating system. In the folder /mnt mount the partition that will contain the new Linux. For example, /dev/sdb1:

sudo mount /dev/sdb1 /mnt

Now you need to recursively copy all files from the current root in our /mnt directory. This is best done by booting with the LiveCD disk, then all necessary data will be saved. But this is not mandatory, you can do the porting and running of the system, but before that stop all the running databases and services to the maximum, to preserve your settings and you haven’t lost anything in the new version of the system. For example, if you are running database MariaDB or MySQL, then it must stop:

sudo systemctl stop mariadb

Similarly do with all the other not important to the operating system services. Also empty the trash cache of the package Manager and other unnecessary files, so they did not take place in the archive or the new system.

2. The Linux cp utility

Then you can start porting Linux to a new disk. To do this, run the cp utility with the options -a, -r and –x. The first option involves keeping the source and rights metadata of the file, the second is recursively walking file system, and the third restricts the recursion only the current filesystem:

sudo cp-rxa / /mnt/

Because it will only copy the files from the current file system, if your directories /boot and /home are on other partitions, they must be copied separately:

sudo mkdir /mnt/{boot,home}

sudo cp-rxa /boot /mnt/boot/

sudo cp-rxa /home /mnt/home/

If you don’t need a home folder, you may not copy.

3. The Linux tar utility

This is an alternative migration option, if you don’t want to use cp, you can use tar. To immediately migrate the files to another location, you need to create a tunnel, one end of which data will be papakovacsi and another to unpack:

sudo tar-cpv --one-file-system / | sudo tar-x-C /mnt

Option -p causes the utility to save the metadata file during migration. Option –one-file-system specifies that the utility will take files only from the root file system, so all remontirovanniy file system, as in the previous embodiment will be omitted. Therefore, the directories /boot and /home you will have to copy the same team. Or is it possible not to use this option and pass all but useless

sudo tar-cpv --exclude /mnt --exclude /dev --exclude /sys --exclude /proc --exclude /tmp --exclude /run / | sudo tar-x-C /mnt/

You can also create the archive, and then it somewhere to copy to have a backup system:

sudo tar-cvpzf system.tar.gz --exclude system.tar.gz --one-file-system /

Options instead of the –one-file-system, you can use the –exclude option to exclude unwanted directories, as in the previous command. And to unpack use the command:

sudo tar xvzf system.tar.gz -C /mnt

Here, /mnt is the directory in which you want to extract the files.

4. Transfer using rsync

Utility rsync many do not want to use it, but it is very convenient, works quickly and displays the copy progress. To transfer using rsync, run:

sudo rsync-aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*"} / /mnt/

This command works similar to the tar command copies everything in the new location. Options-aAX include preserving all file metadata, symbolic links, owners, groups, and so on.

5. Edit /etc/fstab

We will only work with the new system. The first thing you need to fix the /etc/fstab to correct the UUID of the root partition. To know the UUID of the correct partition with the command blkid:

blkid /dev/sdb1

Now replace the UUID is received, the value of this parameter the root partition in /mnt/etc/fstab:

sudo vi /mnt/etc/fstab

6. Boot loader installation

Next you need to install the Grub boot loader in the new Linux. First mount it in folder /sys, /proc and /dev:

sudo mount --bind /sys /mnt/sys

sudo mount --bind /proc /mnt/proc

sudo mount --bind /dev /mnt/dev

Then log into the chroot environment:

sudo chroot /mnt

Then install the boot loader on that drive that you transferred to Linux, in my case it is /dev/sdb:

sudo grub-install /dev/sdb

And you only have to create the configuration file for the bootloader:

sudo update-grub2

The distros not based on Ubuntu, instead of the update-grub2 command can be used:

sudo grub2-mkconfig -o /boot/grub/grub.cfg

7. Reboot

Exit the chroot command:

exit

Then rasmotrenie system catalogs and your section:

sudo umount /mnt/sys

sudo umount /mnt/proc

sudo umount /mnt/dev

sudo umount /mnt

And then restart the computer. In the BIOS of your computer you need to choose the disk on which you transferred Linux, as the first source to load. After downloading you will have in the new operating system and can always go back to the old one.

Insights

In this article we discussed how to move Linux to another hard disk using the utilities tar, cp or rsync. As you can see, it is quite simple and fast. We could use dd, but it copies the whole disk byte-by-byte, so it will run longer and its archives will occupy more disk space. You can still use the Clonezilla tool.

Source: losst.ru

(Visited 41 times, 1 visits today)