欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[CentOS]磁盘扩容

程序员文章站 2022-03-16 17:34:10
...

我准备在自己虚拟机上面安装mysql用于存储数据,但是问题在于我发现以前为了节省资源,将自己虚拟机的磁盘设置的很小,坑爹了。怎么办呢?还好当初装的时候选择的是可扩展的分区,不然真的只能挂个新的分区或者只能重装了。

一、vmvare扩展磁盘大小
[CentOS]磁盘扩容
二、挂载
上一步只是进行了磁盘扩展,但是空间并没有加入到服务器:
[CentOS]磁盘扩容
fdisk -l可以看到磁盘信息。

[aaa@qq.com ~]# fdisk -l

磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003816d

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     4196351     2097152   83  Linux
/dev/sda2         4196352    41936895    18870272   8e  Linux LVM
/dev/sda3        41936896    62914559    10488832   8e  Linux LVM

磁盘 /dev/mapper/centos-root:12.9 GB, 12889096192 字节,25174016 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:3510 MB, 3510632448 字节,6856704 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-app:10.5 GB, 10506731520 字节,20520960 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-var:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-home:1002 MB, 1002438656 字节,1957888 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

执行fdisk /dev/sda

[aaa@qq.com ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 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 获取帮助):p

磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0003816d

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     4196351     2097152   83  Linux
/dev/sda2         4196352    41936895    18870272   8e  Linux LVM

命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分区号 (3,4,默认 3):3
起始 扇区 (41936896-62914559,默认为 41936896):
将使用默认值 41936896
Last 扇区, +扇区 or +size{K,M,G} (41936896-62914559,默认为 62914559):
将使用默认值 62914559
分区 3 已设置为 Linux 类型,大小设为 10 GiB

命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。

上述命令执行完后,要么重启要么可以执行partprobe命令保存,这个命令是通知系统分区表的变化情况。
接下来执行分区的格式化:

[aaa@qq.com ~]# mkfs.ext3 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655776 inodes, 2622208 blocks
131110 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 

添加新的lvm组到已有lvm组:

[aaa@qq.com ~]# pvcreate /dev/sda3 #初始化刚才的分区
WARNING: ext3 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
  Wiping ext3 signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created.
[aaa@qq.com ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               17.99 GiB
  PE Size               4.00 MiB
  Total PE              4606
  Alloc PE / Size       4605 / <17.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               gSd57O-tV2k-0LfS-oPC1-QsEA-vSlT-dMMmH4
[aaa@qq.com ~]# vgextend centos /dev/sda3 #将初始化过的分区加入到虚拟卷组centos
  Volume group "centos" successfully extended

查看分区信息:

[aaa@qq.com ~]# vgdisplay -v
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               27.99 GiB
  PE Size               4.00 MiB
  Total PE              7166
  Alloc PE / Size       4605 / <17.99 GiB
  Free  PE / Size       2561 / 10.00 GiB
  VG UUID               gSd57O-tV2k-0LfS-oPC1-QsEA-vSlT-dMMmH4
   
  --- Logical volume ---
  LV Path                /dev/centos/app
  LV Name                app
  VG Name                centos
  LV UUID                jeLugD-ICKV-AE6d-JR9E-wMqP-nwqb-p6EhSm
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-06-11 18:55:36 +0800
  LV Status              available
  # open                 1
  LV Size                <9.79 GiB
  Current LE             2505
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                LW7jCe-Bsaf-hQJS-TznY-fxbJ-zU1j-Z41dOi
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-06-11 18:55:36 +0800
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/var
  LV Name                var
  VG Name                centos
  LV UUID                MrXSJt-X01F-Y3vJ-u0GJ-ILqz-c3nZ-b4FatN
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-06-11 18:55:37 +0800
  LV Status              available
  # open                 1
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                3DFJtL-tR31-V6Nf-wmm6-SppP-0J9H-NA1Az3
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-06-11 18:55:37 +0800
  LV Status              available
  # open                 1
  LV Size                956.00 MiB
  Current LE             239
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                Xjo4D7-fgrW-MXb6-egRz-cu54-AXC7-s4N3Nn
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-06-11 18:55:38 +0800
  LV Status              available
  # open                 2
  LV Size                <3.27 GiB
  Current LE             837
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               16yCHV-A3RG-luIV-tLeD-cz23-DsY0-VMLwBO
  PV Status             allocatable
  Total PE / Free PE    4606 / 1
   
  PV Name               /dev/sda3     
  PV UUID               4CEhp5-vGPE-XhKK-xo84-jkzj-U31w-qFZktb
  PV Status             allocatable
  Total PE / Free PE    2560 / 2560
[aaa@qq.com ~]# lvextend -l+2561 /dev/mapper/centos-root
  Size of logical volume centos/root changed from 2.00 GiB (512 extents) to 12.00 GiB (3073 extents).
  Logical volume centos/root successfully resized.

扩展到根分区:

[aaa@qq.com ~]# lvextend -l+2561 /dev/mapper/centos-root
  Size of logical volume centos/root changed from 2.00 GiB (512 extents) to 12.00 GiB (3073 extents).
  Logical volume centos/root successfully resized.
[aaa@qq.com ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, 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 524288 to 3146752

扩展成功:

[aaa@qq.com ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   12G  1.7G   11G   14% /
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                2.0G  133M  1.9G    7% /boot
/dev/mapper/centos-app   9.8G  5.0G  4.9G   51% /app
/dev/mapper/centos-var   2.0G  276M  1.8G   14% /var
/dev/mapper/centos-home  953M   33M  921M    4% /home
tmpfs                     98M     0   98M    0% /run/user/0
相关标签: CentOS