在使用Linux系统过程中,会遇到LVM分区空间不足的时候,这时候可以进行扩容解决问题,主要的扩容方法有两种:
1、通过空余的磁盘进行扩容,这个方法比较简单,不会对原有数据有影响。
2、将其他LVM分区空间取出一部分给需要扩容的LVM分区。
下面就分别具体介绍这两种方法。
方法一、通过空余磁盘扩容
1、查看系统磁盘
当使用df -h 命令,可以看到系统的磁盘使用情况,如下所示:可以看到/根目录有50G
[root@localhost home]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 51G 24G 25G 49% / tmpfs 935M 0 935M 0% /dev/shm /dev/xvda1 477M 93M 360M 21% /boot /dev/mapper/VolGroup-lv_home 199G 60M 189G 1% /home [root@cjgx-db01 ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cjgxdb01-lv_root 50G 27G 20G 58% / tmpfs 3.9G 224K 3.9G 1% /dev/shm /dev/sda1 477M 41M 411M 10% /boot /dev/mapper/vg_cjgxdb01-lv_home 41G 48M 39G 1% /home /dev/sr0 3.7G 3.7G 0 100% /media/CentOS_6.9_Final
(lsblk命令可查看所有磁盘设备)
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 55.6M 1 loop /snap/core18/2566
loop1 7:1 0 62M 1 loop /snap/core20/1611
loop2 7:2 0 79.9M 1 loop /snap/lxd/22923
loop3 7:3 0 63.2M 1 loop /snap/core20/1623
loop4 7:4 0 103M 1 loop /snap/lxd/23541
loop5 7:5 0 47M 1 loop /snap/snapd/16292
loop6 7:6 0 48M 1 loop /snap/snapd/16778
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1.8G 0 part /boot
└─sda3 8:3 0 8.2G 0 part
└─ubuntu–vg-ubuntu–lv 253:0 0 8.2G 0 lvm /
sdb 8:16 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
2、增加磁盘空间
我用的是vSphere Client,如下图:
用命令fdisk -l再次查看磁盘大小,如下:已从100G增至120G
[root@cjgx-db01 ~]# fdisk -l Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e7152 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 13055 104344576 8e Linux LVM Disk /dev/mapper/vg_cjgxdb01-lv_root: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/vg_cjgxdb01-lv_swap: 8405 MB, 8405385216 bytes 255 heads, 63 sectors/track, 1021 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/vg_cjgxdb01-lv_home: 44.8 GB, 44753223680 bytes 255 heads, 63 sectors/track, 5440 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
3.创建新分区
使用fdisk /dev/sda命令创建新分区,注意:不同操作系统的磁盘命名方式不同,有些是/dev/sda。具体使用方式参考第三步中fdisk -l首行显示Disk。如笔者在该系统中首行显示为Disk /dev/sda: 128.8 GB, 128849018880 bytes,故使用命令fdisk /dev/vda),按照下面Linux操作进行分区:
[root@cjgx-db01 ~]# fdisk /dev/sda WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). 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 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 #new 新分区 Command action e extended p primary partition (1-4) p #选择主分区 Partition number (1-4): 3 #3 分区序号 First cylinder (13055-15665, default 13055): #分区开始回车默认 Using default value 13055 Last cylinder, +cylinders or +size{K,M,G} (13055-15665, default 15665): Using default value 15665 Command (m for help): t #修改分区格式 Partition number (1-4): 3 Hex code (type L to list codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1 Hex code (type L to list codes): 8e #格式选择8e Linux LVM Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): p #显示分区信息 Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e7152 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 13055 104344576 8e Linux LVM /dev/sda3 13055 15665 20971512+ 8e 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.
运行partprobe <dst_dev_path>
或者partx -u <dst_dev_path>
,以通知内核数据盘的分区表已经修改,需要同步更新。
创建分区后如下:增加了/dev/sda3分区
[root@cjgx-db01 ~]# fdisk -l Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e7152 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 13055 104344576 8e Linux LVM /dev/sda3 13055 15665 20971512+ 83 Linux
4、创建逻辑卷pv
注意:这里可能提示创建不成功,如下提示:(需要重启系统reboot)
[root@cjgx-db01 ~]# pvcreate /dev/sda3 Device /dev/sda3 not found (or ignored by filtering).
通过pvcreate命令将磁盘/dev/sda3创建为一个系统pv
[root@cjgx-db01 ~]# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created
5、查看新建物理卷和大小,使用pvdisplay命令
[root@cjgx-db01 ~]# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created [root@cjgx-db01 ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name vg_cjgxdb01 PV Size 99.51 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 25474 Free PE 0 Allocated PE 25474 PV UUID mikfIQ-GtGY-OQPc-zaIX-UM78-0PbG-WEO8GE "/dev/sda3" is a new physical volume of "20.00 GiB" --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 0YdYdL-MXkW-M15f-CPFf-5HTi-IV2B-ldNZmz
您暂时无权查看此隐藏内容!
9、验证扩展结果
使用df -lh命令,如下:可以看到/dev/mapper/vg_cjgxdb01-lv_root已从50G增加至69G
[root@cjgx-db01 ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_cjgxdb01-lv_root 69G 27G 39G 42% / tmpfs 3.9G 72K 3.9G 1% /dev/shm /dev/sda1 477M 41M 411M 10% /boot /dev/mapper/vg_cjgxdb01-lv_home 41G 48M 39G 1% /home
至此,系统盘扩容已成功!
总结:
1.添加磁盘
2.创建PV
3.将创建的PV添加之VG
4.根目录扩容
5.重新加载逻辑卷
温馨提示:
查看Linux新增磁盘方法教程请参考琼杰笔记文档:Linux添加新硬盘,设置分区和开机自动挂载之图文教程!
方法二、利用其他LVM分区空余空间进行扩容
1. 使用df -h查看每个分区的使用情况。如下,发现/dev/mapper/VolGroup-lv_home容量很充裕,本次扩容通过减少/dev/mapper/VolGroup-lv_home的空间给/dev/mapper/VolGroup-lv_root。
[root@localhost home]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 60G 24G 34G 41% / tmpfs 935M 0 935M 0% /dev/shm /dev/xvda1 477M 93M 360M 21% /boot /dev/mapper/VolGroup-lv_home 199G 60M 189G 1% /home
2. 卸载/home
[root@localhost home]# umount /home umount: /home: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))
umount /home 如果提示无法卸载,因为有进程占用/home,使用如下命令来终止占用进程:
[root@localhost home]# fuser -m /home /home: 1463c
如果依然无法卸载,使用以下命令:
[root@localhost home]# umount -l /home
3. 调整/dev/mapper/VolGroup-lv_home分区大小
需要先进行磁盘检测 ,输入命令e2fsck -f /dev/mapper/VolGroup-lv_home。
注意:遇到Abort< y >? 这边输入的是n,才能继续进行。
[root@localhost ~]# e2fsck -f /dev/mapper/VolGroup-lv_home e2fsck 1.41.12 (17-May-2010) The filesystem size (according to the superblock) is 52848640 blocks The physical size of the device is 52592640 blocks Either the superblock or the partition table is likely to be corrupt! Abort<y>? no Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/mapper/VolGroup-lv_home: 26/13213696 files (0.0% non-contiguous), 877250/52848640 blocks
然后输入命令resize2fs -p /dev/mapper/VolGroup-lv_home 100G,进行磁盘重订大小。
[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_home 100G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 26214400 (4k) blocks. Begin pass 2 (max = 32768) Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Begin pass 3 (max = 1613) Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The filesystem on /dev/mapper/VolGroup-lv_home is now 26214400 blocks long.
4. 重新挂载/home
重新挂载后,输入df -h,发现/dev/mapper/VolGroup-lv_home已经改变。
[root@localhost ~]# mount /home [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 60G 24G 34G 41% / tmpfs 935M 0 935M 0% /dev/shm /dev/xvda1 477M 93M 360M 21% /boot /dev/mapper/VolGroup-lv_home 99G 60M 94G 1% /home
5. 设置空闲空间
使用命令vgdisplay,可以看到Free PE/Size 25760 / 100.62 GiB,有了100G的空余空间。 或者使用 vgs 命令
[root@localhost ~]# lvreduce -L 100G /dev/mapper/VolGroup-lv_home WARNING: Reducing active and open logical volume to 100.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv_home? [y/n]: y Size of logical volume VolGroup/lv_home changed from 200.62 GiB (51360 extents) to 100.00 GiB (25600 extents). Logical volume lv_home successfully resized [root@localhost ~]# vgdisplay --- Volume group --- VG Name VolGroup System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 11 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 3 Max PV 0 Cur PV 2 Act PV 2 VG Size 265.50 GiB PE Size 4.00 MiB Total PE 67969 Alloc PE / Size 42209 / 164.88 GiB Free PE / Size 25760 / 100.62 GiB VG UUID hQoxGl-H4T9-Lu9W-sITN-hO2B-aos7-T5TmKC
6. 把闲置空间挂在到根目录下
[root@localhost ~]# lvextend -L +100.62G /dev/mapper/VolGroup-lv_root Rounding size to boundary between physical extents: 100.62 GiB Size of logical volume VolGroup/lv_root changed from 60.97 GiB (15609 extents) to 161.59 GiB (41368 extents). Logical volume lv_root successfully resized
使用命令resize2fs -p /dev/mapper/VolGroup-lv_root, 可以不用重启,就显示最新的磁盘空间。
[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required old desc_blocks = 4, new_desc_blocks = 11 Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 42360832 (4k) blocks. The filesystem on /dev/mapper/VolGroup-lv_root is now 42360832 blocks long.
7. 最后结果
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 159G 24G 128G 16% / tmpfs 935M 0 935M 0% /dev/shm /dev/xvda1 477M 93M 360M 21% /boot /dev/mapper/VolGroup-lv_home 99G 60M 94G 1% /home
评论前必须登录!
注册