Introduction
We all know that using lv logical volumes can be easily expanded.
But when the original disk is not a logical volume and there is data on it, how to expand it?
Because the expansion can only be done if the disk type is the logical volume lvm.
Then, after directly pvcreating the disk with data into the virtual volume lvm, will it damage the data?
Let's test it today.
add hard drive
- Add a 5G disk to the virtual machine as an experiment
View the current partition
lsblk
- You can see that the newly added 5G disk sdb is ready to use
[root@C8-196 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part
├─cl-root 253:0 0 70G 0 lvm /
├─cl-swap 253:1 0 2G 0 lvm [SWAP]
└─cl-home 253:2 0 127G 0 lvm /home
sdb 8:16 0 5G 0 disk
Partition using the traditional method
fdisk /dev/sdb
- Create a primary partition
[root@C8-196 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x53b270c9.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Last sector, +sectors or +size{
K,M,G,T,P} (2048-10485759, default 10485759):
Created a new partition 1 of type 'Linux' and of size 5 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
- Check the partition situation again, sdb is divided into a primary partition, the TYPE is part, and the size is 5G
[root@C8-196 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part
├─cl-root 253:0 0 70G 0 lvm /
├─cl-swap 253:1 0 2G 0 lvm [SWAP]
└─cl-home 253:2 0 127G 0 lvm /home
sdb 8:16 0 5G 0 disk
└─sdb1 8:17 0 5G 0 part
Create a filesystem on the newly created partition
- format sdb1 using xfs
[root@C8-196 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=327616 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=1310464, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Create a mount point and mount the new partition
- Create a directory data under the root and mount the new partition to this directory
[root@C8-196 ~]# mkdir -v /data&& mount /dev/sdb1 /data&&df -Th
mkdir: created directory '/data'
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 876M 0 876M 0% /dev
tmpfs tmpfs 896M 0 896M 0% /dev/shm
tmpfs tmpfs 896M 17M 879M 2% /run
tmpfs tmpfs 896M 0 896M 0% /sys/fs/cgroup
/dev/mapper/cl-root xfs 70G 2.3G 68G 4% /
/dev/mapper/cl-home xfs 127G 939M 126G 1% /home
/dev/sda1 xfs 1014M 197M 818M 20% /boot
tmpfs tmpfs 180M 0 180M 0% /run/user/0
/dev/sdb1 xfs 5.0G 68M 5.0G 2% /data
Create a test file on the new disk partition
- Write 100 files to disk for data testing
for i in {1..100};do $(time dd if=/dev/zero of=/data/testw$i.db bs=4k count=10000);done&&ll /data&&df -Th
[root@C8-196 ~]# for i in {1..100};do $(time dd if=/dev/zero of=/data/testw$i.db bs=4k count=10000);done&&ll /data&&df -Th
10000+0 records in
10000+0 records out
40960000 bytes (41 MB, 39 MiB) copied, 0.0162844 s, 2.5 GB/s
## 中间的省略……
real 0m0.016s
user 0m0.002s
sys 0m0.014s
10000+0 records in
10000+0 records out
40960000 bytes (41 MB, 39 MiB) copied, 0.0153671 s, 2.7 GB/s
real 0m0.017s
user 0m0.001s
sys 0m0.015s
total 4000000
## 磁盘中已经有写入的文件了
-rw-r--r-- 1 root root 40960000 Mar 2 21:15 testw100.db
-rw-r--r-- 1 root root 40960000 Mar 2 21:15 testw10.db
-rw-r--r-- 1 root root 40960000 Mar 2 21:15 testw11.db
## …… 中间的省略
-rw-r--r-- 1 root root 40960000 Mar 2 21:15 testw99.db
-rw-r--r-- 1 root root 40960000 Mar 2 21:15 testw9.db
## 分区已经占了78%了
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 876M 0 876M 0% /dev
tmpfs tmpfs 896M 0 896M 0% /dev/shm
tmpfs tmpfs 896M 17M 879M 2% /run
tmpfs tmpfs 896M 0 896M 0% /sys/fs/cgroup
/dev/mapper/cl-root xfs 70G 2.3G 68G 4% /
/dev/mapper/cl-home xfs 127G 939M 126G 1% /home
/dev/sda1 xfs 1014M 197M 818M 20% /boot
tmpfs tmpfs 180M 0 180M 0% /run/user/0
/dev/sdb1 xfs 5.0G 3.9G 1.2G 78% /data
Do pvcreate directly on the partition with data
- Obviously, for security reasons, mounted partitions cannot be directly pvcreated
[root@C8-196 ~]#
[root@C8-196 ~]# type pvcreate
pvcreate is /usr/sbin/pvcreate
[root@C8-196 ~]# pvcreate /dev/sdb1
Can't open /dev/sdb1 exclusively. Mounted filesystem?
Can't open /dev/sdb1 exclusively. Mounted filesystem?
pvcreate again after umount
- After the umount is dropped, the pvcreate is successful, although it is prompted that there is an xfs file system on the partition
[root@C8-196 ~]# umount /dev/sdb1
[root@C8-196 ~]# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
- But it feels like pvcreate has destroyed the existing file system
Create logical volumes
[root@C8-196 ~]# vgcreate vg_data /dev/sdb1
Volume group "vg_data" successfully created
[root@C8-196 ~]# lvcreate -l 100%FREE -n data_lv vg_data
Logical volume "data_lv" created.
[root@C8-196 ~]# pvs&&vgs&&lvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <199.00g 0
/dev/sdb1 vg_data lvm2 a-- <5.00g 0
VG #PV #LV #SN Attr VSize VFree
cl 1 3 0 wz--n- <199.00g 0
vg_data 1 1 0 wz--n- <5.00g 0
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home cl -wi-ao---- 126.96g
root cl -wi-ao---- 70.00g
swap cl -wi-ao---- 2.03g
data_lv vg_data -wi-a----- <5.00g
Try to mount /dev/sdb1 again
- Apparently it can't be loaded
[root@C8-196 ~]# mount /dev/sdb1 /data
mount: /data: unknown filesystem type 'LVM2_member'.
Try to mount the newly created lvm logical volume directly
- Obviously, there is no file system that can't be mounted
[root@C8-196 ~]# ll /dev/mapper/vg_data-data_lv
lrwxrwxrwx 1 root root 7 Mar 2 21:28 /dev/mapper/vg_data-data_lv -> ../dm-3
[root@C8-196 ~]# ll /dev/vg_data/
total 0
lrwxrwxrwx 1 root root 7 Mar 2 21:28 data_lv -> ../dm-3
[root@C8-196 ~]# mount /dev/mapper/vg_data-data_lv /data
mount: /data: wrong fs type, bad option, bad superblock on /dev/mapper/vg_data-data_lv, missing codepage or helper program, or other error.
[root@C8-196 ~]# mount /dev/vg_data/data_lv /data
mount: /data: wrong fs type, bad option, bad superblock on /dev/mapper/vg_data-data_lv, missing codepage or helper program, or other error.
Create a file system on the new logical volume
- Use xfs to format the newly created logical volume, format it, you know
[root@C8-196 ~]# mkfs.xfs /dev/mapper/vg_data-data_lv
meta-data=/dev/mapper/vg_data-data_lv isize=512 agcount=4, agsize=327424 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=1309696, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Mount the logical volume to the former data directory
mount /dev/mapper/vg_data-data_lv /data
[root@C8-196 ~]# mount /dev/mapper/vg_data-data_lv /data
[root@C8-196 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 876M 0 876M 0% /dev
tmpfs tmpfs 896M 0 896M 0% /dev/shm
tmpfs tmpfs 896M 17M 879M 2% /run
tmpfs tmpfs 896M 0 896M 0% /sys/fs/cgroup
/dev/mapper/cl-root xfs 70G 2.3G 68G 4% /
/dev/mapper/cl-home xfs 127G 939M 126G 1% /home
/dev/sda1 xfs 1014M 197M 818M 20% /boot
tmpfs tmpfs 180M 0 180M 0% /run/user/0
/dev/mapper/vg_data-data_lv xfs 5.0G 68M 5.0G 2% /data
Check if there are files in the directory
- It's time to witness the miracle!
/dev/mapper/vg_data-data_lv xfs 5.0G 68M 5.0G 2% /data
[root@C8-196 ~]# ll /data
total 0
- Obviously, the wool is gone.
- If there is production data in this directory, you can leave!
- Another new way to delete the library and run away!
in conclusion
- Using pvcreate cannot directly create pv for mounted partitions
- Use pvcreate to directly create pv for a partition that has data and has been unmounted, and it will prompt that the file system exists