INT 21h

Select language

LVM: extend root volume and fs, move /var to root volume

№ 11814 В разделе Sysadmin от September 14th, 2022,
В подшивках: ,

We have: virtual machine with small disk and few small volumes.
Task: add more space to disk, remove /var and /home and use this space for root volume, extend pv, lv and fs.

Run df -h to see mounted volume sizes

[root@oshift-test0 ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
devtmpfs                       7.9G     0  7.9G   0% /dev
tmpfs                          7.9G     0  7.9G   0% /dev/shm
tmpfs                          7.9G  8.7M  7.9G   1% /run
tmpfs                          7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/mapper/vg_centos-lv_root   36G  1.6G   33G   5% /
/dev/sda1                      488M  169M  284M  38% /boot
/dev/mapper/vg_centos-lv_home  484M  2.3M  453M   1% /home
/dev/mapper/vg_centos-lv_var   2.9G  311M  2.5G  12% /var
tmpfs                          1.6G     0  1.6G   0% /run/user/0

Stop all services using /var like MySQL or Docker.

Create /var2 directory. Use rsync to copy files from /var to /var2 and remove old directory:

rsync -aqxP /var/* /var2
umount -l /var
rm -rf /var
mv /var2 /var

Do absolutely same for /home.

Remove mount lines in /etc/fstab and reboot.

In VM control panel add some space to your disk and extend LVM to new space:

[root@oshift-test0 ~]# parted /dev/sda
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free                                                       
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  538MB   537MB   primary  ext4         boot
 2      538MB   8590MB  8052MB  primary               lvm
        8590MB  42.9GB  34.4GB           Free Space

(parted) resizepart 2
End?  [8590MB]? 42.9GB                                                    
(parted) quit                                                             
Information: You may need to update /etc/fstab.

Extend PV on sda2:

[root@oshift-test0 ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@oshift-test0 ~]# pvscan 
  PV /dev/sda2   VG vg_centos       lvm2 [<39.45 GiB / 31.95 GiB free]
  Total: 1 [<39.45 GiB] / in use: 1 [<39.45 GiB] / in no VG: 0 [0   ]

Cool! PV extended, but logical volumes still same. Remove unused volumes and extend root LV:

[root@oshift-test0 /]# lvremove /dev/vg_centos/lv_var 
File descriptor 7 (pipe:[19210]) leaked on lvremove invocation. Parent PID 1355: bash
Do you really want to remove active logical volume vg_centos/lv_var? [y/n]: y
  Logical volume "lv_var" successfully removed

[root@oshift-test0 /]# lvremove /dev/vg_centos/lv_home 
File descriptor 7 (pipe:[19210]) leaked on lvremove invocation. Parent PID 1355: bash
Do you really want to remove active logical volume vg_centos/lv_home? [y/n]: y
  Logical volume "lv_home" successfully removed

[root@oshift-test0 /]# lvextend -l +100%FREE /dev/vg_centos/lv_root 
File descriptor 7 (pipe:[19210]) leaked on lvextend invocation. Parent PID 1355: bash
  Size of logical volume vg_centos/lv_root changed from 35.95 GiB (9204 extents) to <39.45 GiB (10099 extents).
  Logical volume vg_centos/lv_root successfully resized.

[root@oshift-test0 /]# resize2fs /dev/vg_centos/lv_root 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg_centos/lv_root is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 5
The filesystem on /dev/vg_centos/lv_root is now 10341376 blocks long.

Bang!

[root@oshift-test0 /]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
devtmpfs                       7.9G     0  7.9G   0% /dev
tmpfs                          7.9G     0  7.9G   0% /dev/shm
tmpfs                          7.9G  8.6M  7.9G   1% /run
tmpfs                          7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/mapper/vg_centos-lv_root   39G  1.9G   36G   6% /
/dev/sda1                      488M  169M  284M  38% /boot
tmpfs                          1.6G     0  1.6G   0% /run/user/0

I did it without VM reinstall and data loss.

Нет комментариев »

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Облачная платформа
Яндекс.Метрика

Fortune cookie: Today's spam: Have you seen every Paris Hilton sex tape made yet? You have to check it out its a free and much more! Check her out here