逻辑卷管理器是linux系统对硬盘区分进行管理的一种机制,为解决硬盘设备在创建后不易修改分区大小的缺陷。并且通过LVM,可以对逻辑卷进行扩展和缩小分区。
查看磁盘
[root@disk1 ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000dfaf7
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 36079615 17833984 83 Linux
#已经添加进来了
Disk /dev/sdb: 6442 MB, 6442450944 bytes, 12582912 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 /dev/mapper/centos_test-root: 16.1 GB, 16106127360 bytes, 31457280 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 /dev/mapper/centos_test-swap: 2147 MB, 2147483648 bytes, 4194304 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
分区
[root@disk1 ~]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe78a1bcc.
Command (m for help): m #查看帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n #添加一个分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p #添加一个主分区,最多能添加四个主分区或3个主分区、多个逻辑分区
Partition number (1-4, default 1):
First sector (2048-12582911, default 2048):
Using default value 2048
Last sector, +sectors or +size{
K,M,G} (2048-12582911, default 12582911): +1G #设置分区为1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): p #查看分区信息
Disk /dev/sdb: 6442 MB, 6442450944 bytes, 12582912 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: 0xe78a1bcc
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
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):
First sector (2099200-12582911, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{
K,M,G} (2099200-12582911, default 12582911): +1G
Partition 2 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 6442 MB, 6442450944 bytes, 12582912 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: 0xe78a1bcc
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 83 Linux
Command (m for help): w #保存修改信息,退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
查看分区信息
[root@disk1 ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000dfaf7
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 36079615 17833984 83 Linux
Disk /dev/sdb: 6442 MB, 6442450944 bytes, 12582912 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: 0x4a5a2424
#已经分区
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 83 Linux
Disk /dev/mapper/centos_test-root: 16.1 GB, 16106127360 bytes, 31457280 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 /dev/mapper/centos_test-swap: 2147 MB, 2147483648 bytes, 4194304 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
通知系统分区变化
[root@disk1 ~]# partprobe
对分区进行格式化为xfs文件系统
[root@disk1 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, 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
[root@disk1 ~]# mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, 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
创建目录进行挂载
[root@disk1 ~]# mkdir /data{1,2}
[root@disk1 ~]# mount /dev/sdb1 /data1
[root@disk1 ~]# mount /dev/sdb2 /data2
[root@disk1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_test-root 15G 1.9G 14G 13% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 197M 111M 87M 57% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 1014M 33M 982M 4% /data1 #挂载成功
/dev/sdb2 1014M 33M 982M 4% /data2
想要使挂载永久生效,需要修改/etc/fstab
[root@disk1 ~]# vim /etc/fstab
/dev/sdb1 /data1 xfs defaults 0 0
/dev/sdb2 /data2 xfs defaults 0 0
重载/etc/fstab文件信息,使用修改生效,无误,则修改正确
[root@disk1 ~]# mount -a
删除操作的话,
[root@disk1 ~]# fdisk /dev/sdb
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.
Command (m for help): d #删除
Partition number (1,2, default 2): 2
Partition 2 is deleted
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.
创建逻辑卷时,分区不能挂载,需要umount /data1 /data2
修改分区类型的ID为8e
[root@disk1 ~]# fdisk /dev/sdb
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.
Command (m for help): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
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 LVM' to 'Linux LVM'
Command (m for help): t
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 6442 MB, 6442450944 bytes, 12582912 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: 0x5e00bc37
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 4196351 1048576 8e Linux LVM
创建物理卷
[root@disk1 ~]# 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.
[root@disk1 ~]# pvcreate /dev/sdb2
WARNING: xfs signature detected on /dev/sdb2 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb2.
Physical volume "/dev/sdb2" successfully created.
查看物理卷
[root@disk1 ~]# pvscan
PV /dev/sda2 VG centos_test lvm2 [17.00 GiB / 4.00 MiB free]
PV /dev/sdb1 lvm2 [1.00 GiB]
PV /dev/sdb2 lvm2 [1.00 GiB]
Total: 3 [19.00 GiB] / in use: 1 [17.00 GiB] / in no VG: 2 [2.00 GiB]
创建卷组
[root@disk1 ~]# vgcreate vg_test /dev/sdb1 创建卷组,格式:vgcreate vg_name pv_name
Volume group "vg_test" successfully created
[root@disk1 ~]# vgextend vg_test /dev/sdb2 扩展卷组,格式:vgextend vg_name pv_name
Volume group "vg_test" successfully extended
查看卷组
[root@disk1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos_test 1 2 0 wz--n- 17.00g 4.00m
vg_test 2 0 0 wz--n- 1.99g 1.99g
创建逻辑卷
#lvcreate -L +创建lv大小(M/G) -n 逻辑卷名字 卷组名字
[root@disk1 ~]# lvcreate -L +1G -n lv_test vg_test
Logical volume "lv_test" created.
[root@disk1 ~]# mkfs.xfs /dev/vg_test/lv_test
meta-data=/dev/vg_test/lv_test isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, 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
可以在线扩展
[root@disk1 ~]# lvextend -L +500M /dev/vg_test/lv_test
Size of logical volume vg_test/lv_test changed from 1.00 GiB (256 extents) to <1.49 GiB (381 extents).
Logical volume vg_test/lv_test successfully resized.
查看发现大小不变,还需要让文件系统知道,使扩容生效
[root@disk1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_test-root 15G 1.9G 14G 13% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 197M 111M 87M 57% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg_test-lv_test 1014M 33M 982M 4% /data1
使扩容生效
[root@disk1 ~]# xfs_growfs /dev/vg_test/lv_test
meta-data=/dev/mapper/vg_test-lv_test isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 262144 to 390144
查看分区信息
[root@disk1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_test-root 15G 1.9G 14G 13% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 197M 111M 87M 57% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg_test-lv_test 1.5G 33M 1.5G 3% /data1
ps:
xfs文件系统:需要用xfs_growfs
ext2 ext3 ext4文件系统:需要用resize2fs /dev/sdb3 2G ,2G是扩展到2G
不能在线缩减
下边是由1G缩减为500M
xfs系统
缩减,需要重新创建lv,无法直接缩减
#umount /dev/vg_test/lv_test
#lvresize -L 500M /dev/vg_test/lv_test
#lvs 发现lv变为500M
#mkfs.xfs /dev/vg_test/lv_test
#mount /dev/vg_test/lv_test /data1/
ext系统
#umount /dev/vg_test/lv_test
#e2fsck -f /dev/vg_test/lv_test 磁盘检查
#resize2fs /dev/vg_test/lv_test 500M 调整文件系统大小为500M
#mount /dev/vg_test/lv_test /data1/
#df -h 查看文件系统大小已变成500M
#lvs 查看逻辑卷大小依然是1G,没有改变,需要调整LV大小
#lvresize -L 500M /dev/vg_test/lv_test 调增lv大小为500M
[root@disk1 ~]# umount /dev/vg_test/lv_test
[root@disk1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos_test -wi-ao---- 15.00g
swap centos_test -wi-ao---- 2.00g
lv_test vg_test -wi-a----- <1.49g
[root@disk1 ~]# lvremove lv_test
Volume group "lv_test" not found
Cannot process volume group lv_test