Hi, let’s see how to remove a disk from a logical volume without losing data.
This is the initial schema:
Server:
CentosLab01 (Centos 6.5)Disk to use:
Disk /dev/sdb: 1073 MB, 1073741824 bytes (DISK A)
Disk /dev/sdc: 1073 MB, 1073741824 bytes (DISK B)
Disk /dev/sdd: 1073 MB, 1073741824 bytes (DISK C)
Creating lv_test logical volume composed by A and B disk (sdb and sdc)
1. Phycal volumes
[root@CentosLab01 ~]# pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created
[root@CentosLab01 ~]# pvcreate /dev/sdc Physical volume "/dev/sdc" successfully created
2. Volume Group
[root@CentosLab01 ~]# vgcreate vg01
/dev/sdb /dev/sdc Volume group "vg01" successfully created
3. Logical Volume
[root@CentosLab01 ~]# lvcreate --extents 100%FREE --name lv_test vg01
Logical volume “lv_test” created
4. Formatting lv_test
[root@CentosLab01 ~]# mkfs.ext4 /dev/mapper/vg01-lv_test
5. Mounting disk in /media/test_disk
[root@CentosLab01 ~]# mkdir /media/my_disk
[root@CentosLab01 ~]# mount /dev/mapper/vg01-lv_test /media/my_disk/
[root@CentosLab01 ~]# df -h | grep my_disk
\/dev/mapper/vg01-lv_test 2.0G 35M 1.9G 2% /media/my_disk
Once we created LVM of 2gb let’s write data of 1,5 GB. We will use dd
1. Convert 1.5 GB to Bytes ( 1572864000 byte )
2. Creating files..
[root@CentosLab01 ~]# dd if=/dev/zero of=/media/my_disk/noodles.log bs=1572864000 count=1
1+0 records in 1+0 records out 1572864000 bytes (1.6 GB) copied, 8.09875 s, 194 MB/s
Now our LVM has 81% of used space
[root@CentosLab01 ~]# df -h | grep my_disk /dev/mapper/vg01-lv_test
2.0G 1.5G 371M 81% /media/my_disk
Keep MD5sum of noodles.log that we will compare after the disk replacing.
[root@CentosLab01 my_disk]# md5sum noodles.log
eeba7b4eee6de684b86e346a3c11d4d5 /media/my_disk/noodles.log
Create also an additional file just to be sure..
[root@CentosLab01 ~]# echo "my file " >> /media/my_disk/myfile.txt
[root@CentosLab01 ~]# cat /media/my_disk/myfile.txt
my file
Now let’s assume that DISK B (/dev/sdc) is a slow disk, instead DISK A and DISK B are fast and we have a not homogeneus situation and we want replace DISK B with DISK C (/dev/sdd).
Below the required steps:
1. make physical volume of /dev/sdd
[root@CentosLab01 ~]# pvcreate /dev/sdd
Physical volume "/dev/sdd" successfully created
2. Add sdd to the volume group VG01
[root@CentosLab01 ~]# vgextend vg01 /dev/sdd
Volume group "vg01" successfully extended
Now VG01 contains 3 disks
[root@CentosLab01 ~]# vgdisplay -v vg01
Using volume group(s) on command line
Finding volume group "vg01"
--- Volume group ---
VG Name vg01
--- Physical volumes ---
PV Name /dev/sdb
PV UUID uvRKib-g4Ki-iKeD-bR1C-zOWI-OD9j-CFHpAY
PV Status allocatable
Total PE / Free PE 255 / 0
PV Name /dev/sdc
PV UUID IlrLvj-VqeH-Nvmt-S6ut-je0V-1oue-jG567x
PV Status allocatable
Total PE / Free PE 255 / 0
PV Name /dev/sdd
PV UUID feeRxG-BmRR-VRcp-sAao-xTQR-blXv-bSO1qu
PV Status allocatable
Now the volume group has 3 disks, the LVM has been not extended and neither the filesystems, then we still have a /media/my_disk of 2.0 GB
3. Let’s see the real usage of disks
[root@CentosLab01 ~]# pvs -o+pv_used
PV VG Fmt Attr PSize PFree Used
/dev/sda2 vg_centoslab01 lvm2 a-- 7.51g 0 7.51g
/dev/sdb vg01 lvm2 a-- 1020.00m 0 1020.00m
/dev/sdc vg01 lvm2 a-- 1020.00m 0 1020.00m
/dev/sdd vg01 lvm2 a-- 1020.00m 1020.00m 0
4. Move EXTENTS from DISK B (/dev/sdc) in order to have an “empty” not used disk
[root@CentosLab01 ~]# pvmove /dev/sdc
/dev/sdc: Moved: 3.9%
/dev/sdc: Moved: 100.0%
Now DISK B has 0 used extents:
[root@CentosLab01 ~]# pvs -o+pv_used
PV VG Fmt Attr PSize PFree Used
/dev/sda2 vg_centoslab01 lvm2 a-- 7.51g 0 7.51g
/dev/sdb vg01 lvm2 a-- 1020.00m 0 1020.00m
/dev/sdc vg01 lvm2 a-- 1020.00m 1020.00m 0
/dev/sdd vg01 lvm2 a-- 1020.00m 0 1020.00m
5. We are ready to reduce the VOLUME GROUP
[root@CentosLab01 ~]# vgreduce vg01 /dev/sdc
Removed "/dev/sdc" from volume group "vg01"
The partition is still of 2.0gb
[root@CentosLab01 ~]# df -h | grep my_disk
/dev/mapper/vg01-lv_test 2.0G 1.5G 371M 81% /media/my_disk
and the data is still there 🙂
[root@CentosLab01 ~]# cat /media/my_disk/myfile.txt
my file
[root@CentosLab01 ~]# md5sum /media/my_disk/noodles.log
eeba7b4eee6de684b86e346a3c11d4d5 /media/my_disk/noodles.log
launch vgdisplay -vv vg01
for see which physical volumes are members of VG01.