开始虚拟机安装时我们就选择了lvm 进行磁盘分区的划分。后期磁盘不够时再加也很方便,于是就有了这个lvm的扩容。这台内部虚拟机主要做共享和数据备份,需要的磁盘空间比较大。下面记录lvm的扩容

扩容操作

首先增加一块新的磁盘,Centos 新增硬盘以后,系统不能自动进行识别

[root@samba]# fdisk -l磁盘 /dev/sda:536.9 GB, 536870912000 字节,1048576000 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0x0007efbe   设备 Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048      501759      249856   83  Linux/dev/sda2          501760  1048575999   524037120   8e  Linux LVM磁盘 /dev/mapper/vg-0:536.6 GB, 536610865152 字节,1048068096 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节[root@samba]# lsblkNAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsda        8:0    0   500G  0 disk ├─sda1     8:1    0   244M  0 part /boot└─sda2     8:2    0 499.8G  0 part   └─vg-0 253:0    0 499.8G  0 lvm  /sr0       11:0    1  1024M  0 rom  

lsblk命令的英文是“list block”,即用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系(个人很喜欢这个命令,树状列出)

由于不知道新增硬盘挂载的位置,可以先查看现有硬盘挂载的适配器。

[root@samba]# ls -l /sys/block/sdalrwxrwxrwx 1 root root 0 5月  29 10:26 /sys/block/sda -> ../devices/pci0000:00/0000:00:10.0/host16/target16:0:0/16:0:0:0/block/sda

发现磁盘挂载到host16,可以尝试使用下边命令,重新扫描host16信息

[root@samba]# echo "- - -" > /sys/class/scsi_host/host16/scan[root@samba]# lsblkNAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsda        8:0    0   500G  0 disk ├─sda1     8:1    0   244M  0 part /boot└─sda2     8:2    0 499.8G  0 part   └─vg-0 253:0    0 499.8G  0 lvm  /sdb        8:16   0   500G  0 disk sr0       11:0    1  1024M  0 rom  

磁盘分区以及格式化

[root@samba]# fdisk /dev/sdb欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。使用写入命令前请三思。Device does not contain a recognized partition table使用磁盘标识符 0x7bfce339 创建新的 DOS 磁盘标签。命令(输入 m 获取帮助):m命令操作   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)命令(输入 m 获取帮助):nPartition type:   p   primary (0 primary, 0 extended, 4 free)   e   extendedSelect (default p): p分区号 (1-4,默认 1):1起始 扇区 (2048-1048575999,默认为 2048):将使用默认值 2048Last 扇区, +扇区 or +size{K,M,G} (2048-1048575999,默认为 1048575999):将使用默认值 1048575999分区 1 已设置为 Linux 类型,大小设为 500 GiB命令(输入 m 获取帮助):wThe partition table has been altered!Calling ioctl() to re-read partition table.正在同步磁盘。[root@samba]# fdisk -l磁盘 /dev/sda:536.9 GB, 536870912000 字节,1048576000 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0x0007efbe   设备 Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048      501759      249856   83  Linux/dev/sda2          501760  1048575999   524037120   8e  Linux LVM磁盘 /dev/mapper/vg-0:536.6 GB, 536610865152 字节,1048068096 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘 /dev/sdb:536.9 GB, 536870912000 字节,1048576000 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0x7bfce339   设备 Boot      Start         End      Blocks   Id  System/dev/sdb1            2048  1048575999   524286976   83  Linux[root@samba]# mkfs -t xfs /dev/sdbmkfs.xfs: /dev/sdb appears to contain a partition table (dos).mkfs.xfs: Use the -f option to force overwrite.[root@samba]# mkfs.xfs /dev/sdbmkfs.xfs: /dev/sdb appears to contain a partition table (dos).mkfs.xfs: Use the -f option to force overwrite.[root@samba]# mkfs.xfs -f /dev/sdbmeta-data=/dev/sdb               isize=512    agcount=4, agsize=32768000 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=1        finobt=0, sparse=0data     =                       bsize=4096   blocks=131072000, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=1log      =internal log           bsize=4096   blocks=64000, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0

查看vg逻辑卷组

[root@samba]# vgdisplay   --- Volume group ---  VG Name               vg  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               <499.76 GiB  PE Size               4.00 MiB  Total PE              127938  Alloc PE / Size       127938 / <499.76 GiB  Free  PE / Size       0 / 0     VG UUID               kNi93D-daLZ-qYmt-hhzS-Kdm3-ZUHv-0eUYL3

扩容vg

[root@samba]# vgextend vg /dev/sdbWARNING: xfs signature detected on /dev/sdb at offset 0. Wipe it? [y/n]: y  Wiping xfs signature on /dev/sdb.  Physical volume "/dev/sdb" successfully created.  Volume group "vg" successfully extended[root@samba]# vgdisplay   --- Volume group ---  VG Name               vg  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               999.75 GiB  PE Size               4.00 MiB  Total PE              255937  Alloc PE / Size       127938 / <499.76 GiB  Free  PE / Size       127999 / <500.00 GiB  VG UUID               kNi93D-daLZ-qYmt-hhzS-Kdm3-ZUHv-0eUYL3

查看lv,把lv扩容到vg的剩余容量

[root@samba]# lvlvchange     lvcreate     lvextend     lvmconf      lvmdiskscan  lvmetad      lvmsadc      lvreduce     lvrename     lvs          lvconvert    lvdisplay    lvm          lvmconfig    lvmdump      lvmpolld     lvmsar       lvremove     lvresize     lvscan       [root@samba]# lvdisplay   --- Logical volume ---  LV Path                /dev/vg/0  LV Name                0  VG Name                vg  LV UUID                JQVovf-H4tM-kGNi-Nq9b-QRyE-QDIW-lAg2KP  LV Write Access        read/write  LV Creation host, time samba, 2020-04-09 16:23:09 +0800  LV Status              available  # open                 1  LV Size                <499.76 GiB  Current LE             127938  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:0   [root@samba]# lvextend /dev/vg/0 /dev/sdb  Size of logical volume vg/0 changed from <499.76 GiB (127938 extents) to 999.75 GiB (255937 extents).  Logical volume vg/0 successfully resized.[root@samba]# lvdisplay   --- Logical volume ---  LV Path                /dev/vg/0  LV Name                0  VG Name                vg  LV UUID                JQVovf-H4tM-kGNi-Nq9b-QRyE-QDIW-lAg2KP  LV Write Access        read/write  LV Creation host, time samba, 2020-04-09 16:23:09 +0800  LV Status              available  # open                 1  LV Size                999.75 GiB  Current LE             255937  Segments               2  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:0 [root@samba]# lsblkNAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTsda        8:0    0   500G  0 disk ├─sda1     8:1    0   244M  0 part /boot└─sda2     8:2    0 499.8G  0 part   └─vg-0 253:0    0 999.8G  0 lvm  /sdb        8:16   0   500G  0 disk └─vg-0   253:0    0 999.8G  0 lvm  /sr0       11:0    1  1024M  0 rom  

最后,让新增的空间在线生效.centos7 推荐用xfs文件格式。

[root@samba]# xfs_xfs_admin      xfs_copy       xfs_estimate   xfs_fsr        xfs_info       xfs_logprint   xfs_metadump   xfs_ncheck     xfs_repair     xfs_bmap       xfs_db         xfs_freeze     xfs_growfs     xfs_io         xfs_mdrestore  xfs_mkfile     xfs_quota      xfs_rtcp       [root@samba]# xfs_growfs -hxfs_growfs:无效选项 -- hUsage: xfs_growfs [options] mountpointOptions:    -d          grow data/metadata section    -l          grow log section    -r          grow realtime section    -n          don't change anything, just show geometry    -i          convert log from external to internal format    -t          alternate location for mount table (/etc/mtab)    -x          convert log from internal to external format    -D size     grow data/metadata section to size blks    -L size     grow/shrink log section to size blks    -R size     grow realtime section to size blks    -e size     set realtime extent size to size blks    -m imaxpct  set inode max percent to imaxpct    -V          print version information[root@samba]# xfs_growfs /meta-data=/dev/mapper/vg-0       isize=512    agcount=4, agsize=32752128 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=1        finobt=0 spinodes=0data     =                       bsize=4096   blocks=131008512, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=1log      =internal               bsize=4096   blocks=63969, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0data blocks changed from 131008512 to 262079488

到此,lvm扩容完成,最后查看一下

[root@samba]# df -h文件系统          容量  已用  可用 已用% 挂载点devtmpfs          979M     0  979M    0% /devtmpfs             991M     0  991M    0% /dev/shmtmpfs             991M   18M  973M    2% /runtmpfs             991M     0  991M    0% /sys/fs/cgroup/dev/mapper/vg-0 1000G  267G  734G   27% //dev/sda1         241M  133M  109M   55% /boottmpfs             199M     0  199M    0% /run/user/1000