For example : we have a VM with docker , but we need to move directory /var/lib/docker to separate partition or HDD:
lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- 20.00g
root centos -wi-ao---- 25.80g
swap centos -wi-ao---- 2.00g
tmp centos -wi-ao---- 2.00g
var centos -wi-ao---- 25.00g
var_log centos -wi-ao---- 20.00g
var_log_audit centos -wi-ao---- 2.00g
var_tmp centos -wi-ao---- 2.00g
df -h
File system Size Used Available Used% Mounted
/dev/mapper/centos-root 26G 5.7G 19G 24% /
devtmpfs 858M 0 858M 0% /dev
tmpfs 870M 0 870M 0% /dev/shm
tmpfs 870M 9.4M 860M 2% /run
tmpfs 870M 0 870M 0% /sys/fs/cgroup
/dev/sda2 976M 407M 503M 45% /boot
/dev/sda1 200M 19M 182M 10% /boot/efi
/dev/mapper/centos-var 25G 3.2G 21G 14% /var
/dev/mapper/centos-tmp 2.0G 33M 1.8G 2% /tmp
/dev/mapper/centos-var_log 20G 397M 19G 3% /var/log
/dev/mapper/centos-home 20G 115M 19G 1% /home
/dev/mapper/centos-var_log_audit 2.0G 35M 1.8G 2% /var/log/audit
/dev/mapper/centos-var_tmp 2.0G 160M 1.7G 9% /var/tmp
tmpfs 174M 0 174M 0% /run/user/1000
So add new HDD and check in OS:
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 98.8G 0 part
├─centos-root 253:0 0 25.8G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
├─centos-tmp 253:2 0 2G 0 lvm /tmp
├─centos-var 253:3 0 25G 0 lvm /var
├─centos-var_tmp 253:4 0 2G 0 lvm /var/tmp
├─centos-var_log 253:5 0 20G 0 lvm /var/log
├─centos-var_log_audit 253:6 0 2G 0 lvm /var/log/audit
└─centos-home 253:7 0 20G 0 lvm /home
sdb 8:16 0 40G 0 disk
sr0 11:0 1 1024M 0 rom
fdisk -l /dev/sdb
Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Create new LVM partition on new HDD:
parted -s /dev/sdb mklabel gpt; parted -s /dev/sdb unit mib mkpart primary 1 100% set 1 lvm on
fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: 47E3AF39-FFC1-470E-A40A-334150007411
# Start End Size Type Name
1 2048 83884031 40G Linux LVM primary
lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- 20.00g
root centos -wi-ao---- 25.80g
swap centos -wi-ao---- 2.00g
tmp centos -wi-ao---- 2.00g
var centos -wi-ao---- 25.00g
var_log centos -wi-ao---- 20.00g
var_log_audit centos -wi-ao---- 2.00g
var_tmp centos -wi-ao---- 2.00g
df -h
File system Size Used Available Used% Mounted
/dev/mapper/centos-root 26G 5.7G 19G 24% /
devtmpfs 858M 0 858M 0% /dev
tmpfs 870M 0 870M 0% /dev/shm
tmpfs 870M 9.4M 860M 2% /run
tmpfs 870M 0 870M 0% /sys/fs/cgroup
/dev/sda2 976M 407M 503M 45% /boot
/dev/sda1 200M 19M 182M 10% /boot/efi
/dev/mapper/centos-var 25G 3.2G 21G 14% /var
/dev/mapper/centos-tmp 2.0G 33M 1.8G 2% /tmp
/dev/mapper/centos-var_log 20G 397M 19G 3% /var/log
/dev/mapper/centos-home 20G 115M 19G 1% /home
/dev/mapper/centos-var_log_audit 2.0G 35M 1.8G 2% /var/log/audit
/dev/mapper/centos-var_tmp 2.0G 160M 1.7G 9% /var/tmp
tmpfs 174M 0 174M 0% /run/user/1000
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 98.8G 0 part
├─centos-root 253:0 0 25.8G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
├─centos-tmp 253:2 0 2G 0 lvm /tmp
├─centos-var 253:3 0 25G 0 lvm /var
├─centos-var_tmp 253:4 0 2G 0 lvm /var/tmp
├─centos-var_log 253:5 0 20G 0 lvm /var/log
├─centos-var_log_audit 253:6 0 2G 0 lvm /var/log/audit
└─centos-home 253:7 0 20G 0 lvm /home
sdb 8:16 0 40G 0 disk
sr0 11:0 1 1024M 0 rom
fdisk -l /dev/sdb
Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
parted -s /dev/sdb mklabel gpt; parted -s /dev/sdb unit mib mkpart primary 1 100% set 1 lvm on
fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: gpt
Disk identifier: 47E3AF39-FFC1-470E-A40A-334150007411
# Start End Size Type Name
1 2048 83884031 40G Linux LVM primary
pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 centos lvm2 a-- 98.80g 0
Create new physical group:
pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 centos lvm2 a-- 98.80g 0
/dev/sdb1 lvm2 --- <40.00g <40.00g
New volume group:
vgcreate docker /dev/sdb1
Volume group "docker" successfully created
pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 centos lvm2 a-- 98.80g 0
/dev/sdb1 docker lvm2 a-- <40.00g <40.00g
New logical volume:
lvcreate -l100%FREE docker -n var_lib_docker
Logical volume "var_lib_docker" created.
lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- 20.00g
root centos -wi-ao---- 25.80g
swap centos -wi-ao---- 2.00g
tmp centos -wi-ao---- 2.00g
var centos -wi-ao---- 25.00g
var_log centos -wi-ao---- 20.00g
var_log_audit centos -wi-ao---- 2.00g
var_tmp centos -wi-ao---- 2.00g
var_lib_docker docker -wi-a----- <40.00g
And make ext4 file system on the new volume:
mkfs.ext4 /dev/mapper/docker-var_lib_docker
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10484736 blocks
524236 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
320 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
Mount new volume to empty directory:
mount /dev/mapper/docker-var_lib_docker /mnt/
Check it:
mount | column -t | grep mnt
/dev/mapper/docker-var_lib_docker on /mnt type ext4 (rw,relatime,data=ordered)
Stop docker service:
systemctl stop docker
and check open files in /var/lib/docker:
lsof /var/lib/docker
Now rsync data from /var/lib/docker to /mnt and remove files and dirs on source:
rsync -avr --remove-source-files --prune-empty-dirs /var/lib/docker/* /mnt/ && find /var/lib/docker -depth=1 -type d -empty -exec rmdir "{}" \; && mkdir /var/lib/docker
Then umount /mnt :
umount /mnt
Add new mount point to /etc/fstab:
echo "/dev/mapper/docker-var_lib_docker /var/lib/docker ext4 defaults 1 2" >> /etc/fstab
and mount new logical volume :
mount -a
check block devices and mounts:
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 98.8G 0 part
├─centos-root 253:0 0 25.8G 0 lvm /
├─centos-swap 253:1 0 2G 0 lvm [SWAP]
├─centos-tmp 253:3 0 2G 0 lvm /tmp
├─centos-var 253:4 0 25G 0 lvm /var
├─centos-var_tmp 253:5 0 2G 0 lvm /var/tmp
├─centos-var_log 253:6 0 20G 0 lvm /var/log
├─centos-var_log_audit 253:7 0 2G 0 lvm /var/log/audit
└─centos-home 253:8 0 20G 0 lvm /home
sdb 8:16 0 40G 0 disk
└─sdb1 8:17 0 40G 0 part
└─docker-var_lib_docker 253:2 0 40G 0 lvm /var/lib/docker
and start docker service;
systemctl start docker