⬆️ ⬇️

Translation and merging of several virtual machine partitions on LVM

Given: VM with CentOS 5.8 with standard disk partitioning (Not LVM)

Objective: To enlarge one of the sections with archival information.



IMPORTANT! Part of the problems could be avoided if you add VM disk space not just by increasing the size of the virtual disk, but by adding a new HDD. Since I didn’t think of it right away, I’d not pick up the place back, I’m describing the version with the usual resizing.



IMPORTANT! Before starting work, do not forget to make a backup on another / external media: information and / etc / fstab



So, let's begin (all commands are executed with root rights):

')

We will conduct an inventory of the available VM:



[root@localhost ~]# df -h      %   /dev/sda2 3,4G 1,3G 1,9G 40% / /dev/sda6 236M 6,1M 218M 3% /very_important /dev/sda5 244M 6,1M 225M 3% /home /dev/sda1 99M 12M 83M 13% /boot tmpfs 06M 0 506M 0% /dev/shm 




 [root@localhost ~]# fdisk -l  /dev/sda: 6442 , 6442450944  255 heads, 63 sectors/track, 783 cylinders  =   16065 * 512 = 8225280  -     Id  /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 459 3582495 83 Linux /dev/sda3 460 720 2096482+ 82 Linux  / Solaris /dev/sda4 721 783 506047+ 5  /dev/sda5 721 752 257008+ 83 Linux /dev/sda6 753 783 248976 83 Linux 




This shows that the important partition is on / dev / sda6, the HDD is 6442 MB in size and / dev / sda6 is not LVM at all



(It was this unfortunate misunderstanding that caused the writing of the article)



Increase HDD size through VM manager and compare results:



 [root@localhost ~]# fdisk -l  /dev/sda: 8589 , 8589934592  255 heads, 63 sectors/track, 1044 cylinders  =   16065 * 512 = 8225280  -     Id  /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 459 3582495 83 Linux /dev/sda3 460 720 2096482+ 82 Linux  / Solaris /dev/sda4 721 783 506047+ 5  /dev/sda5 721 752 257008+ 83 Linux /dev/sda6 753 783 248976 83 Linux 




We got that the size of / dev / sda grew to 8589 MB, but there are no changes anywhere else, so you need to create a partition in this free area. Immediately the question arises: And, actually, how to do it? After thinking and joking about the Internet, I came to the use of a GParted Live CD (there is USB, PXE and HDD). We download the image, slip its VM (in my vmware, when I restarted the machine, I had to manage to enter the virtual F2 BIOS and put boot from cd in the first place). When loading Gparted in graphics mode, a window with your HDD and partitions on it immediately opens, select the sda4 partition (it is auxiliary for accessing the remaining partitions), press the resize button and stretch all the way. There is a separate button “apply changes”, click it, waiting for a successful completion and exit with a reboot.



So, now we have a place to create a section, let's start:

 [root@localhost ~]# fdisk /dev/sda        1044.     ,   ,  1024,        : 1) ,    (.,   LILO) 2)        (., DOS FDISK, OS/2 FDISK)  (m  ): n # “ ”   (784-1045,   784): #  ,        784    +size  +sizeM  +sizeK (784-1045,   1045): #  ,      ,    ,  enter     1045  (m  ):t #        (1-7): 7 #  /dev/sda7 (  )   ( L    ):8e # LVM     7   8e (Linux LVM)  (m  ): w #       !  ioctl()    . :        16:    .      .       . 




Next, reboot.



So, we are ready to start creating the LVM partition, check again:



 [root@localhost ~]# df -h      %   /dev/sda2 3,4G 1,3G 1,9G 41% / /dev/sda6 236M 6,1M 218M 3% /very_important /dev/sda5 244M 6,1M 225M 3% /home /dev/sda1 99M 12M 83M 13% /boot tmpfs 506M 0 506M 0% /dev/shm 




 [root@localhost ~]# fdisk -l  /dev/sda: 8589 , 8589934592  255 heads, 63 sectors/track, 1044 cylinders  =   16065 * 512 = 8225280  -     Id  /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 459 3582495 83 Linux /dev/sda3 460 720 2096482+ 82 Linux  / Solaris /dev/sda4 721 1045 2605208 5  /dev/sda5 721 752 257008+ 83 Linux /dev/sda6 753 783 248976 83 Linux /dev/sda7 784 1045 2099129 8e Linux LVM 




Make sure that / dev / sda7 is a Linux LVM partition, convert the / dev / sda7 partition to a physical volume so that LVM can use it:



 [root@localhost ~]# pvcreate /dev/sda7 Writing physical volume data to disk “/dev/sda7″ Physical volume “/dev/sda7″ successfully created 




Create a logical volume group with the name important_vg (you can use any name) and add one physical volume to it:



 [root@localhost ~]# vgcreate important_vg /dev/sda7 Volume group “important_vg” successfully created 




Now we create a logical partition with the name important from the total free space in the group:



 [root@localhost ~]# lvcreate -n important -l +100%FREE important_vg Logical volume “important” created 




we can see everything that we got with the commands: pvdisplay, vgdisplay and lvdisplay.

Now we have a logical partition with a convenient name (not a faceless sda7), therefore we need to find out how we can access it now:



 [root@localhost ~]# ls -l /dev/important_vg/  0 lrwxrwxrwx 1 root root 34  11 15:03 important -> /dev/mapper/important_vg-important 




This output of the ls command says the following: in the / dev folder we created a directory with the name of our group important_vg, inside which the important file was created, which in turn is a link to / dev / mapper / important_vg-important, so you can refer to the section as / dev / important_vg / important, and / dev / mapper / important_vg-important (hereafter, option 2 is used)



Next, create a file system in our fresh section called very_important:



 [root@localhost ~]# mkfs.ext3 -L very_important /dev/mapper/important_vg-important mke2fs 1.39 (29-May-2006) Filesystem label=very_important OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 262144 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 16384 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 28 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. 




Now you need to replace the physical / very_important partition with the / dev / mapper / important_vg-important partition in / etc / fstab (let's not forget about the backup #cat / etc / fstab> /etc/fstab.copy):



 [root@localhost ~]# cat /etc/fstab LABEL=/ / ext3 defaults 1 1 #LABEL=/very_important /very_important ext3 defaults 1 2 /dev/mapper/important_vg-important /very_important ext3 defaults 1 2 LABEL=/home /home ext3 defaults 1 2 LABEL=/boot /boot ext3 defaults 1 2 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 LABEL=SWAP-sda3 swap swap defaults 0 0 




Unmount / very_important:



 [root@localhost ~]#umount /very_important 




and on the new we will mount everything that is written in fstab:



 [root@localhost ~]#mount -a 




In fact, on a real machine, errors occurred that the partition was busy and could not be unmounted, I was too lazy to look for blocking descriptors, so I crossed myself:



 [root@localhost ~]#reboot 




Who did not have problems / reboot was successfully continued:



 [root@localhost ~]# df -h      %   /dev/sda2 3,4G 1,3G 1,9G 41% / /dev/sda5 244M 6,1M 225M 3% /home /dev/sda1 99M 12M 83M 13% /boot tmpfs 506M 0 506M 0% /dev/shm /dev/mapper/important_vg-important 2,0G 68M 1,9G 4% /very_important 




Now the partition is ready, and we can move all the data there. To do this, create a new folder where we will mount the old section with Important Information:



 [root@localhost ~]# mkdir /mnt/mpoint [root@localhost ~]# mount /dev/sda6 /mnt/mpoint/ 




Transfer information:



 [root@localhost ~]# cp -a /mnt/mpoint/ /very_important/ 




Say goodbye to the / dev / sda6 partition, the more we will not see it and unmount it:



 [root@localhost ~]# umount /mnt/mpoint/ 




Now the task is to create a physical volume LVM, add it to the group and expand the logical partition.

Convert the volume to LVM type as well as / dev / sda7:



 [root@localhost ~]# fdisk /dev/sda        1044.     ,   ,  1024,        : 1) ,    (.,   LILO) 2)        (., DOS FDISK, OS/2 FDISK)  (m  ):t   (1-7):6   ( L    ):8e    6   8e (Linux LVM)  (m  ):w    !  ioctl()    . :        16:    .      .       . [root@localhost ~]# reboot 




Create a physical volume:



 [root@localhost ~]# pvcreate /dev/sda6 Writing physical volume data to disk “/dev/sda6″ Physical volume “/dev/sda6″ successfully created 




Add to group:



 [root@localhost ~]# vgextend important_vg /dev/sda6 Volume group “important_vg” successfully extended 




Now it's time to talk about the vgdisplay command:



 [root@localhost ~]# vgdisplay — Volume group — VG Name important_vg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 2,23 GB PE Size 4,00 MB Total PE 572 Alloc PE / Size 512 / 2,00 GB Free PE / Size 60 / 240,00 MB VG UUID 4rRD2Y-383d-0igK-BLqR-5Bd9-9ure-U9uk8G 




Cur PV reports that this group consists of 2 physical volumes, VG Size reports the size of the group, and Free PE / Size says that there is free space in the group that is not occupied by any partition. It should be added to the / dev / mapper / important_vg-important section:



[root @ localhost ~] # lvextend -l + 100% FREE / dev / mapper / important_vg-important

Extending logical volume important to 2.23 GB

Logical volume important successfully resized



And finally, we will fix the ext3 file system after increasing the size of the partition:



 [root@localhost ~]# resize2fs /dev/mapper/important_vg-important resize2fs 1.39 (29-May-2006) Filesystem at /dev/mapper/important_vg-important is mounted on /very_important; on-line resizing required Performing an on-line resize of /dev/mapper/important_vg-important to 585728 (4k) blocks. The filesystem on /dev/mapper/important_vg-important is now 585728 blocks long. 




Voila, the system is ready:



 [root@localhost ~]# df -h      %   /dev/sda2 3,4G 1,3G 1,9G 41% / /dev/mapper/important_vg-important 2,2G 68M 2,1G 4% /very_important /dev/sda5 244M 6,1M 225M 3% /home /dev/sda1 99M 12M 83M 13% /boot tmpfs 506M 0 506M 0% /dev/shm [root@localhost ~]# fdisk -l  /dev/sda: 8589 , 8589934592  255 heads, 63 sectors/track, 1044 cylinders  =   16065 * 512 = 8225280  -     Id  /dev/sda1 * 1 13 104391 83 Linux /dev/sda2 14 459 3582495 83 Linux /dev/sda3 460 720 2096482+ 82 Linux  / Solaris /dev/sda4 721 1045 2605208 5  /dev/sda5 721 752 257008+ 83 Linux /dev/sda6 753 783 248976 8e Linux LVM /dev/sda7 784 1045 2099129 8e Linux LVM 

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



All Articles