centos 7磁盘扩容、扩展分区/dev/mapper/centos-root


  • 一、VMware workstation菜单栏👉虚拟机👉设置👉添加👉硬盘👉下一步
  • 二、SCSI👉下一步:
  • 三、创建新虚拟磁盘👉下一步
  • 四、选择自己需要的硬盘大小👉立即分配磁盘取消勾选👉将虚拟磁盘存储为单个文件👉下一步
  • 五、完成
  • 六、重启虚拟机后,使用lsblk命令查看磁盘情况
  • 七、使用fdisk创建新分区
  • 八、使用mkfs.ext4命令进行格式化
  • 九、挂载分区(扩容)
  • 十、扩展/dev/mapper/centos-root


一、VMware workstation菜单栏👉虚拟机👉设置👉添加👉硬盘👉下一步

centos7分区扩容 centos7扩展分区_虚拟磁盘


centos7分区扩容 centos7扩展分区_centos_02

二、SCSI👉下一步:

centos7分区扩容 centos7扩展分区_centos7分区扩容_03

三、创建新虚拟磁盘👉下一步

centos7分区扩容 centos7扩展分区_centos_04

四、选择自己需要的硬盘大小👉立即分配磁盘取消勾选👉将虚拟磁盘存储为单个文件👉下一步

centos7分区扩容 centos7扩展分区_centos7分区扩容_05

五、完成

centos7分区扩容 centos7扩展分区_centos7分区扩容_06

六、重启虚拟机后,使用lsblk命令查看磁盘情况

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    5G  0 disk 
sr0              11:0    1 1024M  0 rom

发现已经成功添加新硬盘sdb。

七、使用fdisk创建新分区

[root@localhost ~]# fdisk /dev/sdb
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.Device does not contain a recognized partition table
 Building a new DOS disklabel with disk identifier 0x03295b0a.Command (m for help):


然后输入 p ,回车:

Command (m for help): p
Disk /dev/sdb: 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: 0x03295b0aDevice Boot Start End
Command (m for help):

输入 n 回车:

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):

输入 p 回车, 1 回车,回车,回车。

Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): 
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set

输入 p ,回车

Command (m for help): p
Disk /dev/sdb: 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: 0x03295b0aDevice Boot Start End Blocks Id System
/dev/sdb1 2048 10485759 5241856 83 LinuxCommand (m for help):

输入 w ,回车,保存退出。至此,磁盘分区完成。

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
 Syncing disks.

八、使用mkfs.ext4命令进行格式化

先查看分区表

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sr0              11:0    1 1024M  0 rom

发现新分区是sdb1(我只分了一个主分区,所以是sdb1。)然后我们对sdb1进行格式化

[root@localhost ~]# mkfs.ext4 /dev/sdb1 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310464 blocks
65523 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
 Writing inode tables: done
 Creating journal (32768 blocks): done
 Writing superblocks and filesystem accounting information: done

至此格式化完成。

九、挂载分区(扩容)

我以挂载到根分区的data目录为例:

新建/data目录

[root@localhost /]# mkdir data

挂载分区到/data目录

[root@localhost /]# mount /dev/sdb1 /data

查看磁盘使用情况

[root@localhost /]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        10G  1.3G   16G   8% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  148M  867M  15% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb1               ext4      4.8G   20M  4.6G   1% /data

至此,虚拟机添加磁盘操作结束。

十、扩展/dev/mapper/centos-root

扩容/dev/mapper/centos-root,前八步相同,将第九步骤换成第十步骤就可以了。

  1. 创建pv(给刚刚新建的分区 /dev/sdb1)
[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdd1" successfully created
  1. 把pv加入vg中,相当于扩充vg的大小
  • 先使用vgs查看vg组
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- 19.51g 40.00m<br>
  • 扩展vg,使用vgextend命令
[root@localhost ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
  1. 我们成功把vg卷扩展了,在用vgs查看一下
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   2   0 wz--n- 39.50g 20.04g

  • 虽然我们把vg扩展了,但是lv还没有扩展
[root@localhost ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 17.47g                                                   
  swap centos -wi-ao----  2.00g
  1. 扩展lv,使用lvextend命令
[root@localhost ~]# lvextend -L +20G /dev/mapper/centos-root
  Size of logical volume centos/root changed from 17.47 GiB (4472 extents) to 37.47 GiB (9592 extents).
  Logical volume root successfully resized.
  • 查看lv大小
[root@localhost ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 37.47g                                                   
  swap centos -wi-ao----  2.00g
  • df -h 发现centos-root空间没有变化
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   10G  10G   44m    100% /
devtmpfs                 479M     0  479M    0% /dev
tmpfs                    489M     0  489M    0% /dev/shm
tmpfs                    489M  6.7M  483M    2% /run
tmpfs                    489M     0  489M    0% /sys/fs/cgroup
/dev/sda1                497M  125M  373M   25% /boot
tmpfs                     98M     0   98M    0% /run/user/0<br><br>没有变化。
  1. 那么我们要使用命令使系统重新读取大小
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1144832 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=4579328, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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 4579328 to 9822208
  • 再使用df -h查看
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   30G  6.7G   24G   23% /
devtmpfs                 470M     0  470M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  8.4M  478M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/sda1                197M  157M   41M   80% /boot
tmpfs                     98M   12K   98M    1% /run/user/42
tmpfs                     98M     0   98M    0% /run/user/0
/dev/sdb1                 30G   16G   13G   54% /opt

  • 到这里我们就成功扩展了。