📜 ⬆️ ⬇️

Transferring the LINUX system to another hard drive with partitioning

Once upon a time in a distant galaxy I needed to transfer my linux system from one hard drive to another, and I didn’t want to use horror as proprietary. Googling and searching with the help of other search engines, I found a solution for myself that the Linux administrator may seem ridiculous, but it helped me out great then.

In general - here it is. Beat your legs, cut into pieces. Meet


')
The first thing we need to do is to mark the new hard disk in accordance with our desires and, possibly, by eliminating errors of the previous markup (if something is not pleasant on the source disk).

Let's look at a specific example of my system. I'm going to transfer the system from HDD = 80Gb to HDD = 20Gb .

My 80Gb disk is labeled as follows:

/dev/hda1 — /boot (250 Mb)
/dev/hda2 — swap (1Gb)
/dev/hda3 — extended (20Gb) ( /dev/hda5, /dev/hda6, /dev/hda7, /dev/hda8)
/dev/hda5 — / (5Gb)
/dev/hda6 — /tmp (512Mb)
/dev/hda7 — /usr (10Gb)
/dev/hda8 — /var (5Gb)
/dev/hda4 — /home (54Gb)


/ home is very full of information, so I don’t include it in cloning.

Having set the jumpers as expected, I connect the new HDD to the system. He took the hdb tag
You can mark it with ANY utility that is convenient for you. It was easier for me to use GPARTED - it’s visually understandable, and you can see right away how much space is actually occupied by data on a particular partition, which will help determine whether to allocate as much (or little) space.

I tagged the new disk (/ dev / hdb) as follows:

/dev/hdb1 — /boot (250 Mb)
/dev/hdb2 — swap (1Gb)
/dev/hdb3 — extended (14Gb) ( /dev/hdb5, /dev/hdb6, /dev/hdb7, /dev/hdb8)
/dev/hdb5 — / (1Gb)
/dev/hdb6 — /tmp (512Mb)
/dev/hdb7 — /var (5Gb)
/dev/hdb8 — /usr (7Gb)
/dev/hdb4 — /home (4Gb)


The next step is to mount all created partitions on the new HDD to the existing system.
To do this, I created the / backup directory on the existing system, in which I created the / boot, / root, / var, / usr subdirectories ( / tmp is not needed), according to the sections into which I divided the new HDD.
Next, perform the mount itself:

sudo mount /dev/hdb1 /backup/boot
sudo mount /dev/hdb5 /backup/root
sudo mount /dev/hdb7 /backup/var
sudo mount /dev/hdb8 /backup/usr


So we got to the most interesting, but not the easiest place to copy the data.
Unlike Windows, Linux allows you to copy yourself FULLY. But one important point to remember is that the system has attributes on directories and files, as well as symlinks and hardlinks. So it is necessary to copy the system so that all these connections are not lost. For such a procedure, in the opinion of large specialists, the tar command is best suited.
The point is that we WILL NOT archive files to a disk, but will transfer them through the so-called “pipe” or “stream” to the receiving HDD.

To simplify the transfer procedure, I wrote the backup.sh script:

#!/bin/sh
cd /
tar -cf - dev initrd.img opt srv bin cdrom etc initrd lib sbin sys vmlinuz | (cd /backup/root; tar -xvpf -)
cd /boot
tar -cf - * | (cd /backup/boot; tar -xvpf -)
cd /var
tar -cf - * | (cd /backup/var; tar -xvpf -)
cd /usr
tar -cf - * (cd /backup/usr; tar -xvpf -)


explain a little what's what:

"Cd /"
- transition to root

"Tar -cf - dev initrd.img opt srv bin cdrom etc initrd lib sbin sys | (cd / backup / root; tar -xvpf -)
"- for the tar of the listed directories and files with their transfer to the tar to the receiving HDD.

Find out what directories and files need to be transferred easily. Simply execute the " ls / " command , thereby obtaining a listing of your system starting with / .
In my case it is:

ls /
backup boot dev home initrd.img media opt root srv tmp var
bin cdrom etc initrd lib mnt proc sbin sys usr vmlinuz


From all this "good" we need to select what will be in / .
So, as I selected separate partitions on the disk under / boot, / var, / tmp, / usr and / home , it turns out that they should be skipped at this step. Therefore, we take only dev initrd.img opt srv var
bin cdrom etc initrd lib sbin sys vmlinuz

And the rest is easier:
cd /boot
- go to the existing / boot

tar -cf - * | (cd /backup/boot; tar -xvpf -)
we transfer all contents of / boot to / backup / boot

etc.

So, run the backup.sh script and go to rest for a while. It took me about 10 minutes.

Now one more important point which I did not pay attention to at first, for which I paid extra time and headache.

It is NOT enough to simply copy the data to / var, / tmp, / usr partitions.
After all the transfer procedures, we need to CREATE these directories on the destination disk, as mount points for those partitions (and all those mount points that we excluded during the transfer are / home, / media, / tmp, / var, / mnt, / proc, / usr ) MANDATORY WITH THE SAME Attributes! I.e:

sudo mkdir /backup/root/home
sudo mkdir /backup/root/media
sudo mkdir /backup/root/tmp
sudo mkdir /backup/root/var
sudo mkdir /backup/root/mnt
sudo mkdir /backup/root/proc
sudo mkdir /backup/root/usr


which attributes to put on the directory can be found by typing the command ls -la / on the source system. But besides / tmp, the root owner and root rights 766 should be everywhere. It is necessary to put the rights 777 on / tmp.

Now you need to fix fstab on the receiving HDD, if necessary (if all of a sudden you ALL OVERDOWNED the disk). And also fix the menu.lst boot loader GRUB.

In UBUNTU that in fstab, that in menu.lst all disks are registered through UUID and not just / dev / hda .
You can find out which UUID of your disk you have with the help of the command: ls -l / dev / disk / by-uuid , the output should be something like:

lrwxrwxrwx 1 root root 10 2008-02-17 17:45 11815c66-5ae7-4497-9039-51de9adef664 -> ../../hda2
lrwxrwxrwx 1 root root 10 2008-02-17 17:45 78711a48-6776-4474-8fa8-87016aad83a2 -> ../../hda6
lrwxrwxrwx 1 root root 10 2008-02-17 17:45 83fded3d-37c4-4d85-a965-a7bbe326178a -> ../../hda7
lrwxrwxrwx 1 root root 10 2008-02-17 17:45 a60e482c-8260-48fb-a19e-f5f906d4d444 -> ../../hda8
lrwxrwxrwx 1 root root 10 2008-02-17 17:45 bc7607fe-3bf2-4bc1-adce-8ab749a271c9 -> ../../hda1
lrwxrwxrwx 1 root root 10 2008-02-17 17:45 cacd40ea-ac88-4143-b5d9-5cb477eeb85d -> ../../hda4
lrwxrwxrwx 1 root root 10 2008-02-17 17:45 d4404ea9-0a8e-4a4c-b72d-10a5edd697be -> ../../hda5

we just need the numbers “ 11815c66-5ae7-4497-9039-51de9adef664 ”, for example. This is the UUID of the swap partition (in my case).

in menu.lst we edit the following lines:

# kopt=root=UUID=d4404ea9-0a8e-4a4c-b72d-10a5edd697be ro
(do not see that the line is commented out; when updating the kernel, this is where the information comes from)

kernel /vmlinuz-2.6.22-14-generic root=UUID=d4404ea9-0a8e-4a4c-b72d-10a5edd697be ro quiet splash locale

and

kernel /vmlinuz-2.6.22-14-generic root=UUID=d4404ea9-0a8e-4a4c-b72d-10a5edd697be ro single


Do not forget the fact that although the system is copied to disk, it still does not know how to boot, because we did not install the bootloader.
This is done simply:
We take the installation disk UBUNTU and boot.
go to console ctrl + f1

sudo su
grub
find /grub/stage1
root (hd0,0)
setup (hd0) .
quit


All - disconnect the disk from the system, connect, check. It all worked for me.

Shl!

Since I did not tolerate / home , the system cursed me as a user, but I don’t have a home folder. I entered the barbaric way - userdel %username% , and then adduser %username% .

I just was unimportant. And too lazy to place the attributes. And so the system did everything for me.

PS Do not scold for much, perhaps an obsolete way, but at that time it worked and worked well! ( That moment can be calculated by the kernel version mentioned in this article).

Source: https://habr.com/ru/post/128095/


All Articles