lvm-磁盘逻辑卷扩容

10次阅读

共计 6762 个字符,预计需要花费 17 分钟才能阅读完成。

开始虚拟机安装时我们就选择了 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]# lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda        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/sda
lrwxrwxrwx 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]# lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda        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 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-1048575999,默认为 2048):将使用默认值 2048
Last 扇区, + 扇区 or +size{K,M,G} (2048-1048575999,默认为 1048575999):将使用默认值 1048575999
分区 1 已设置为 Linux 类型,大小设为 500 GiB

命令 (输入 m 获取帮助):w
The 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/sdb
mkfs.xfs: /dev/sdb appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[root@samba]# mkfs.xfs /dev/sdb
mkfs.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/sdb
meta-data=/dev/sdb               isize=512    agcount=4, agsize=32768000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=131072000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=64000, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =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/sdb
WARNING: 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]# lv
lvchange     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]# lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda        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 -h
xfs_growfs:无效选项 -- h
Usage: xfs_growfs [options] mountpoint

Options:
    -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=0
data     =                       bsize=4096   blocks=131008512, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=63969, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 131008512 to 262079488

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

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

正文完
 0