How to transfer a Linux system to an LVM volume

Summary: 1. Preliminary knowledge Know what is dd and what is dump? The dd command is used to copy a device/partition and output it as-is to another file/device/partition. The dump command is used to back up ext2 or ext3 filesystems. A directory or an entire file system can be backed up to a specified device, or backed up as one large file.

1. Preliminary knowledge
Know what is dd and what is dump?
The dd command is used to copy a device/partition and output it as-is to another file/device/partition.

The dump command is used to back up ext2 or ext3 filesystems. A directory or an entire file system can be backed up to a specified device, or backed up as one large file.

What to restore from backup?
The restore command is the reverse process of the dump command and is used to restore the backup files generated by the dump command.

Second, the environment simulation
First of all, it is required to have a partition that can be started normally, and then there must be remaining space! (There are two cases here: one is a partition and the other is a new disk. The two are similar. For the convenience of this article, the method of the new disk is used.)

The virtual machine is used to simulate the real situation of the server: /dev/sda1 is the normal system The boot partition, /dev/sdb1, is the new disk partition. (It must be noted that the capacity of /dev/sdb1 is greater than that of /dev/sda1)

Virtual machine settings and partitions:



3. Practical part

1. First, back up the MBR master boot record
dd if=/dev/sda1 of=/mnt/lvm/mbr. bin bs=512 count=1

2. Create an LVM volume
First unmount /dev/sdb1, then use pvcreate to create a physical volume and vgcreate to create a volume group vgtest on it:

umount /dev/sdb1
pvcreate /dev/sdb1
vgcreate vgtest /dev/sdb1

After the volume group is created, you can use the vgs command to view the volume group.

Use the lvs command to view the logical volume. At this time, we do not have any logical volume. Start creating logical volumes and filesystems:

lvcreate -L 25G -n lvtest vgtest
mkfs.ext4 /dev/mapper/vgtest-lvtest

3. Backup the / directory and /boot directory
First backup the / directory. The date command entered before and after the dump command is to see how long the backup took.

date && dump -0f /mnt/lvm/rootfs-20160824.bak / && date

We also back up the /boot directory to be safe:

date && dump -0f /mnt/lvm/boot-20160824.bak /boot && date

4. Try Restore
restore -rf rootfs-20160828.bak boot-20160824.bak

5. Modify the boot file
Note : The root partition (boot partition) needs to be changed to the actual disk to be booted. Otherwise, if it is not changed, the system will use /dev/sdb1 LVM, but the original /dev/sda1 is used!

First view the UUID of the partition through blkid:

blkid
Then modify the root partition in the /mnt/lvm/etc/fstab file (it will become /etc/fstab after switching the follower partition) to point to:

vim /mnt/lvm/etc/fstab

chroot to the new partition, and then check the corresponding partition settings.

chroot /mnt/lvm
Note: To chroot into the directory to see if it is correct! And determine the firewall SELinux and other settings, the most important thing is to open OpenSSH! 

6. Restore the MBR master boot record
dd if=./mbr.bin of=/dev/mapper/vgtest-lvtest

7. Revise the GRUB boot
This allows the new disk to be booted from the source disk.

chroot into the new disk. And rebuild the /boot directory:

chroot /mnt/lvm
ls /boot/
rm -rf /boot/*
yum install grub -y

then type:

root (hd1,0)
setup (hd1)
quit

This will point the GRUB boot to the new one disk.

Restart to see if there is a problem.

The original release time is: 2016-08-26

This article is from the Yunqi community partner "Linux China"


using the Yunqi community APP, comfortable~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326219538&siteId=291194637