# fdisk /dev/sda Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
# 创建主分区2,空间为1g,磁盘格式为lvm Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): 2 First sector (83886080-167772159, default 83886080): Using default value 83886080 Last sector, +sectors or +size{K,M,G} (83886080-167772159, default 167772159): +1G Partition 2 of type Linux and of size 1 GiB is set
Command (m for help): t Partition number (1,2, default 2): 2 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM'
# 创建主分区3,空间为5g,磁盘格式为lvm Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): 3 First sector (85983232-167772159, default 85983232): Using default value 85983232 Last sector, +sectors or +size{K,M,G} (85983232-167772159, default 167772159): +5G Partition 3 of type Linux and of size 5 GiB is set
Command (m for help): t Partition number (1-3, default 3): 3 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM'
# 保存上述操作 Command (m for help): w The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
当前磁盘空间状态如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
# fdisk -l
Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000a05f8
Device Boot Start End Blocks Id System /dev/sda1 * 2048 83886079 41942016 83 Linux /dev/sda2 83886080 85983231 1048576 8e Linux LVM /dev/sda3 85983232 96468991 5242880 8e Linux LVM
# pvcreate /dev/sda2 /dev/sda3 Physical volume "/dev/sda2" successfully created. Physical volume "/dev/sda3" successfully created. [root@localhost vagrant]# pvdisplay "/dev/sda2" is a new physical volume of "1.00 GiB" --- NEW Physical volume --- PV Name /dev/sda2 VG Name PV Size 1.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID MgarF2-Ka6D-blDi-8ecd-1SEU-y2GD-JtiK2c
"/dev/sda3" is a new physical volume of "5.00 GiB" --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 5.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID ToKp2T-30mS-0P8c-ZrcB-2lO4-ayo9-62fuDx
# vgcreate vg1 /dev/sda1 # vgdisplay -v --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 5.99 GiB PE Size 4.00 MiB Total PE 1534 Alloc PE / Size 0 / 0 Free PE / Size 1534 / 5.99 GiB VG UUID XI8Biv-JtUv-tsur-wuvm-IJQz-HLZu-6a2u5G
--- Physical volumes --- PV Name /dev/sda2 PV UUID MgarF2-Ka6D-blDi-8ecd-1SEU-y2GD-JtiK2c PV Status allocatable Total PE / Free PE 255 / 255
PV Name /dev/sda3 PV UUID ToKp2T-30mS-0P8c-ZrcB-2lO4-ayo9-62fuDx PV Status allocatable Total PE / Free PE 1279 / 1279
# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID EesY4i-lSqY-ef1R-599C-XTrZ-IcVL-P7W46Q LV Write Access read/write LV Creation host, time localhost.localdomain, 2019-06-16 07:29:38 +0000 LV Status available # open 0 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
# mkfs.ext4 /dev/vg1/lv1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 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 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912
Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done
# lvextend -L +1G /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents). Logical volume vg1/lv1 successfully resized.
# resize2fs /dev/vg1/lv1 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg1/lv1 is mounted on /tmp/lvm; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/vg1/lv1 is now 786432 blocks long.
lvremove操作执行的时候经常会出现提示“Logical volume xx contains a filesystem in use.”的情况,该问题一般是由于有其他进程在使用该文件系统导致的。网络上经常看到的是通过fuser或者lsof命令来查找使用方,但偶尔该命令会失效,尤其在本机上有容器的场景下。另外一个可行的办法是通过 grep -nr "/data" /proc/*/mount 命令,可以找到挂载该目录的所有进程,简单有效。
$ pvdisplay "/dev/vdb" is a new physical volume of "100.00 GiB" --- NEW Physical volume --- PV Name /dev/vdb VG Name PV Size 100.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 8NnlYc-4f3f-fkeW-a3l3-LXoC-9UEH-fvpb5V
"/dev/vdc" is a new physical volume of "200.00 GiB" --- NEW Physical volume --- PV Name /dev/vdc VG Name PV Size 200.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID I9ffpN-c1vc-PQOB-yKyd-MdzO-Ngff-6e116t
$ vgcreate vg1 /dev/vdb /dev/vdc Volume group "vg1" successfully created
$ vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 299.99 GiB PE Size 4.00 MiB Total PE 76798 Alloc PE / Size 0 / 0 Free PE / Size 76798 / 299.99 GiB VG UUID Gi0bJx-jqY8-YpSo-kB0l-9wdk-ZfCT-GpgFZY
$ lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID JQZ193-dz6A-I0Ue-rTKC-6XrQ-gb1F-Qy9kDl LV Write Access read/write LV Creation host, time iZt4nd6wiprf8foracovwqZ, 2022-01-08 23:28:45 +0800 LV Status available # open 0 LV Size 299.99 GiB Current LE 76798 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:0
$ lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID FnOmCM-IkuE-3Rvv-fQqk-Cv1Q-lb8S-l5X7O3 LV Write Access read/write LV Creation host, time iZt4nd6wiprf8foracovwqZ, 2022-01-09 00:07:08 +0800 LV Status available # open 0 LV Size 50.00 GiB Current LE 12800 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:0