How to transfer Linux system to LVM volume


A friend suddenly came to me today and said that he wanted to expand the root partition disk of his Linux system. When I received this question, I immediately started to check the system. It is a CentOS 6.6 system with two disks: /dev/vda1 is a 15G root volume, /dev/vda2 is a 1G swap partition, and the total size of the entire /dev/vda disk is 21G (with 5G /dev/vda5 free space). It is now required to increase the /dev/vda1 partition.



There are three solutions:



the first is to mount the free partition (/dev/vda5) on /dev/vda as /home, /var, etc.;

the second is to make a symbolic link to /dev/vda1, 5G plus 15G can barely expand; the

last one is the most troublesome, make an LVM, migrate data to LVM, add /dev/vda1 to LVM, and finally realize system expansion.

My friend chose the last one. Later, I used many methods for testing data backup. I recommended dd and tar on the Internet. Finally, I also tried cpio, backup, and dump, and found that the performance of the last dump is much higher than that of dd.



This tutorial is also to try again from scratch, and record it for your own convenience.



Brothers Linux training

Friendly reminder ~ The ideas in this article are for reference only! It should be adjusted according to your actual situation. If doing it on a production server, be sure to make a full backup of the entire system. Careless operation may lead to the loss of the entire system data! The author of this article and this site do not assume any responsibility for any possible risks.



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)





3. Actual combat part





1. First backup the MBR master boot record



dd if=/dev/sda1 of=/mnt/lvm/mbr.bin bs=512count=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:



umount /dev/sdb1

pvcreate /dev/sdb1

vgcreate vgtest /dev/sdb1



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



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. Back up the / directory and /boot directory



First back up 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.bakboot-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 check the UUID of the partition through blkid:



blkid

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



vim /mnt/lvm/etc/fstab



chroot Go to the new partition and 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.binof=/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.



PS participate in the activity to get a linux CD-ROM tutorial with free shipping: http://www.hdb.com/party/8f84u.htmll


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326817810&siteId=291194637