Resizing LVM Logical Volumes-lvextend
1. fdisk命令/dev/sdc再分出一个sdc2分区
[root@rhel7 ~]# fdisk /dev/sdc 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): 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 (206848-10485759, default 206848): Using default value 206848 Last sector, +sectors or +size{K,M,G} (206848-10485759, default 10485759): +200M Partition 2 of type Linux and of size 200 MiB is set Command (m for help): t Partition number (1,2, default 2): Hex code (type L to list all codes): 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 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' 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): 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. --reboot一下生效 [root@rhel7 ~]# fdisk -l /dev/sdc --查看/dev/sdc分区情况 Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0x0c16d904 Device Boot Start End Blocks Id System /dev/sdc1 2048 206847 102400 8e Linux LVM /dev/sdc2 206848 616447 204800 8e Linux LVM [root@rhel7 ~]#
2.创建物理卷pv
[root@rhel7 ~]# pvcreate /dev/sdc2 #没有pvextend的说法 Physical volume "/dev/sdc2" successfully created [root@rhel7 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 rhel lvm2 a-- 19.51g 40.00m /dev/sdc1 vgdate lvm2 a-- 96.00m 4.00m /dev/sdc2 lvm2 --- 200.00m 200.00m [root@rhel7 ~]#
3.增加VG大小:(把上面创建的pv添加到vg里就行)
[root@rhel7 ~]# vgs VG #PV #LV #SN Attr VSize VFree rhel 1 2 0 wz--n- 19.51g 40.00m vgdate 1 1 0 wz--n- 96.00m 4.00m [root@rhel7 ~]# vgdisplay vgdate --- Volume group --- VG Name vgdate System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size 96.00 MiB PE Size 4.00 MiB Total PE 24 Alloc PE / Size 23 / 92.00 MiB Free PE / Size 1 / 4.00 MiB VG UUID hNTqaY-c0Hi-v5wk-p7lQ-TIbc-oSpm-TIOcHs [root@rhel7 ~]# vgextend vgdate /dev/sdc2 ---向卷组vgdata中添加pv。没有加多少M或多少G进来的说法,只能把整个PV添加到卷组中来。 Volume group "vgdate" successfully extended [root@rhel7 ~]# vgs VG #PV #LV #SN Attr VSize VFree rhel 1 2 0 wz--n- 19.51g 40.00m vgdate 2 1 0 wz--n- 292.00m 200.00m [root@rhel7 ~]# vgdisplay vgdate --- Volume group --- VG Name vgdate System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 292.00 MiB PE Size 4.00 MiB Total PE 73 Alloc PE / Size 23 / 92.00 MiB Free PE / Size 50 / 200.00 MiB VG UUID hNTqaY-c0Hi-v5wk-p7lQ-TIbc-oSpm-TIOcHs [root@rhel7 ~]#
4.增加LV的大小
[root@rhel7 ~]# lvdisplay vgdate --- Logical volume --- LV Path /dev/vgdate/lvvol1 LV Name lvvol1 VG Name vgdate LV UUID dt10du-dTAJ-0gUV-LqrO-wRUY-rb5h-171DCr LV Write Access read/write LV Creation host, time rhel7.com, 2016-08-05 04:51:17 -0400 LV Status available # open 1 LV Size 92.00 MiB Current LE 23 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 [root@rhel7 ~]# lvextend -L+200MB /dev/vgdate/lvvol1 --给逻辑卷lvvol1增加200MB的空间。如果写成-L 200M,则表示把逻辑卷扩大至200M。
或者lvextend /dev/vgdate/lvvol1 /dev/sdc2 将把/dev/sdc2这个pv剩余的所有空间都分配给逻辑卷lvvol1。 Size of logical volume vgdate/lvvol1 changed from 92.00 MiB (23 extents) to 292.00 MiB (73 extents). Logical volume lvvol1 successfully resized. [root@rhel7 ~]# lvdisplay vgdate --- Logical volume --- LV Path /dev/vgdate/lvvol1 LV Name lvvol1 VG Name vgdate LV UUID dt10du-dTAJ-0gUV-LqrO-wRUY-rb5h-171DCr LV Write Access read/write LV Creation host, time rhel7.com, 2016-08-05 04:51:17 -0400 LV Status available # open 1 LV Size 292.00 MiB ---LV size已增加至292MB Current LE 73 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 [root@rhel7 ~]#
也可以使用lvresize命令来增加lv的大小:
■lvresize -r -l 75%VG /dev/vgdata/lvdata This resizes the logical volume so that it will take 75% of the total disk space in the volume group.
■ lvresize -r -l +75%VG /dev/vgdata/lvdata This tries to add 75% of the total size of the volume group to the logical volume. (Notice the difference with the previous command.)
■ lvresize -r -l +75%FREE /dev/vgdata/lvdata This adds 75% of all free disk space to the logical volume.
■ lvresize -r -l 75%FREE /dev/vgdata/lvdata This resizes the logical volume to a total size that equals 75% of the amount of free disk space. (Notice the difference with the previous command.)
5. 增加了逻辑卷的容量以后,就需要修改文件系统大小以实现利用扩充的空间
从上面可以看到,虽然LV大小已增加至292MB,但是使用df -hT命令查看到的文件系统/dev/mapper/vgdate-lvvol1的大小没变,还是89M,所以还需要修改文件系统的大小才可以使用扩充的空间。
[root@rhel7 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/rhel-root xfs 18G 4.5G 13G 26% / devtmpfs devtmpfs 911M 0 911M 0% /dev tmpfs tmpfs 921M 0 921M 0% /dev/shm tmpfs tmpfs 921M 8.4M 912M 1% /run tmpfs tmpfs 921M 0 921M 0% /sys/fs/cgroup /dev/mapper/vgdate-lvvol1 xfs 89M 4.8M 84M 6% /lvmFiles /dev/sdd1 xfs 497M 124M 374M 25% /boot tmpfs tmpfs 185M 0 185M 0% /run/user/0
使用xfs_growfs命令扩展文件系统。注意这个命令只能用来扩展xfs格式的文件系统,xfs格式是RHEL7版本默认的文件系统,所以用得比较少。
DESCRIPTION xfs_growfs expands an existing XFS filesystem (see xfs(5)). The mount-point argument is the pathname of the directory where the filesystem is mounted. The filesystem must be mounted to be grown (see mount(8)). The existing contents of the filesystem are undis‐ turbed, and the added space becomes available for additional file storage.
还有另外一个命令resize2fs也是用来扩展文件系统,但是只能用来扩展ext2,ext3,ext4格式的文件系统大小,在RHEL7版本以下的系统中用得比较多。
[root@rhel7 ~]# man resize2fs RESIZE2FS(8) System Manager's Manual RESIZE2FS(8) NAME resize2fs - ext2/ext3/ext4 file system resizer SYNOPSIS resize2fs [ -fFpPM ] [ -d debug-flags ] [ -S RAID-stride ] device [ size ] DESCRIPTION The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel sup‐ ports on-line resizing.
如果在xfs格式的文件系统中使用resize2fs命令,则会报如下错误:
[root@rhel7 ~]# resize2fs /dev/mapper/vgdate-lvvol1 resize2fs 1.42.9 (28-Dec-2013) resize2fs: Bad magic number in super-block while trying to open /dev/mapper/vgdate-lvvol1 Couldn't find valid filesystem superblock. [root@rhel7 ~]# resize2fs -f /dev/mapper/vgdate-lvvol1 resize2fs 1.42.9 (28-Dec-2013) resize2fs: Bad magic number in super-block while trying to open /dev/mapper/vgdate-lvvol1 Couldn't find valid filesystem superblock.
使用xfs_growfs命令扩展文件系统大小:
[root@rhel7 ~]# xfs_growfs /dev/mapper/vgdate-lvvol1 meta-data=/dev/mapper/vgdate-lvvol1 isize=256 agcount=4, agsize=5888 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=23552, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 23552 to 74752 [root@rhel7 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/rhel-root xfs 18G 4.5G 13G 26% / devtmpfs devtmpfs 911M 0 911M 0% /dev tmpfs tmpfs 921M 0 921M 0% /dev/shm tmpfs tmpfs 921M 8.4M 912M 1% /run tmpfs tmpfs 921M 0 921M 0% /sys/fs/cgroup /dev/mapper/vgdate-lvvol1 xfs 289M 5.1M 284M 2% /lvmFiles ----------已扩展完成 /dev/sdd1 xfs 497M 124M 374M 25% /boot tmpfs tmpfs 185M 0 185M 0% /run/user/0