CentOS7 系统盘扩容

  • Post author:
  • Post category:其他


我拿到一台新机器,df -Th 查看挂载情况,如下:

fdisk -l 查看磁盘情况,如下:

上图结合 df -Th 可知,

/dev/vda 盘已经分区过了,只有 /dev/vda2分区挂载了,dev/vda2 分区大小 36G,挂载目录 /

/dev/vda1 分区大小为4G:(8390655 – 2048)*512/1024/1024/1024=4G。 可知 /dev/vda盘还有60G为分区

/dev/vdb 盘有250G 容量,还未分区(更别提挂载了)

使用 parted /dev/vda 可以查看已经分好的区的情况(显示的分区大小不准):

[root@ecs-a3b8-0206426 ~]#

parted /dev/vda


GNU Parted 3.1

Using /dev/vda

Welcome to GNU Parted! Type ‘help’ to view a list of commands.

(parted)

p


// 输入p 打印查看 已经分好的区的情况


Model: Virtio Block Device (virtblk)

Disk /dev/vda: 107GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos

Disk Flags:

Number  Start   End     Size    Type     File system     Flags

1      1049kB  4296MB  4295MB  primary  linux-swap(v1)

2      4296MB  42.9GB  38.7GB  primary  ext3            boot

(parted)

q


// 输入 q 退出


[root@ecs-a3b8-0206426 ~]#

——————————————————————

二  对 /dev/vdb 盘分区

1. 通过命令fdisk-l查看硬盘信息:


物理卷 pv 和逻辑卷 lv 可不创建,省略这一步。

2. 对 /dev/vdb 盘分区:

执行命令 fdisk /dev/vdb, 进入fdisk模式,开始对新增数据盘执行分区操作。— f说明:fdisk /dev/vdb 操作,即是创建物理分区

[root@ecs-a3b8-0206426 ~]# fdisk /dev/vdb
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 0x5e1d7d4e.

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
   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)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):        //保持默认,也可输入1,新建 /dev/vdb 的第一个主分区
First sector (2048-524287999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-524287999, default 524287999): 209713152   // 算好了 100G
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (209713153-524287999, default 209715200): 
Using default value 209715200
Last sector, +sectors or +size{K,M,G} (209715200-524287999, default 524287999):  // 保持默认直接回车,即将剩下的150G全部分到第2个区里
Using default value 524287999
Partition 2 of type Linux and of size 150 GiB is set

接下来输入p,回车,查看新建分区的详细信息。

Command (m for help): p

Disk /dev/vdb: 268.4 GB, 268435456000 bytes, 524288000 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: 0x5e1d7d4e

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   209713152   104855552+  83  Linux
/dev/vdb2       209715200   524287999   157286400   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@ecs-a3b8-0206426 ~]# 

如上图,接着输入w保存,将分区结果写入分区表中。

若回显信息如下,则说明分区成功:

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

若万一回显信息 显示的是 The new table will be used at the next reboot , 意思就是这个分区在下次启动的时候才能使用,我们可以执行一个命令, 让这个分区马上生效,命令为 partprobe ,将新的分区表变更同步至操作系统。 注意:也有 partprobe /dev/vdb 这样执行的

fdisk -l 查看磁盘情况,发现 /dev/vdb 盘已经分好了2个区

3 格式化分区 /dev/vdb1

执行以下命令,将新建分区文件系统设为系统所需格式。

执行的命令为:

mkfs.ext3 /dev/vdb1

或

mkfs -t ext3 /dev/vdb1

4 格式化分区 /dev/vdb2

执行以下命令,将新建的分区文件系统设为所需格式

mkfs.ext3 /dev/vdb2

或

mkfs -t ext3 /dev/vdb2

5. 将格式化后的分区 /dev/vdb2 挂载到  /data 目录下

创建 /data 目录

mkdir /data

编辑

vim /etc/fstab

文件,挂载


mount -a


(mount -a 的意思是将/etc/fstab的所有内容重新加载)

扩展:

执行 lsblk 可查看磁盘分区情况和各分区挂载情况(只要分过区,不管有没有格式化分区都会精确到显示出磁盘的分区信息)

扩展:

我们不采用 /etc/fstab 直接指定 /dev/vdb2 的方式设置开机自动挂载,而推荐使用 UUID 来配置自动挂载数据盘。

先执行命令 blkid /dev/vdb2 ,查询磁盘分区的UUID:

再查看 /etc/fstab , 会发现 /dev/vda1 和 /dev/vda2 就是采用 UUID 的方式设置的开机自动挂载

所以,/etc/fstab 文件中,/dev/vdb2 也可以改成  UUID方式。方式参照 /dev/vda1, /dev/vda2。

扩展:

如果对数据盘未进行过卷组划分等相关操作

使用 vgdisplay查看卷组情况,lvdisolay 查看逻辑卷组情况 ,是查不出数据的:

如果进行了卷组划分等相关操作

使用 vgdisplay查看卷组情况,lvdisolay 查看逻辑卷组情况 (一个卷组vg可以划分出多个逻辑卷 lv):

———————————–

对没有物理卷,卷组信息,逻辑分区的系统盘扩容

以前面格式化好分区的系统为例:

注意点:前面的操作,其实已经有了物理卷,但没有创建物理卷,卷组,逻辑卷,直接mount的。

1. 查看磁盘信息:分别执行 df -Th,  lsblk 命令查看

2. 创建物理卷

pvcreate /dev/vdb1

[root@ecs-a3b8-0206426 ~]# pvcreate /dev/vdb1
WARNING: ext3 signature detected on /dev/vdb1 at offset 1080. Wipe it? [y/n]: y   //输入y,即同意
  Wiping ext3 signature on /dev/vdb1.
  Physical volume "/dev/vdb1" successfully created.
[root@ecs-a3b8-0206426 ~]# 

3. 查看系统里所有的物理卷   pvdisplay

[root@ecs-a3b8-0206426 ~]# pvdisplay
  "/dev/vdb1" is a new physical volume of "<100.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               <100.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               IQhByQ-5zev-82Te-5Re5-ZARN-WaDu-giBCTu
   
[root@ecs-a3b8-0206426 ~]# 

也可使用 pvs 命令:查看系统里的物理卷,更直观简洁。

4 对系统分区创建物理卷, 如果拿到的系统一开始就没有创建系统分区物理卷,则对系统分区创建物理卷时会失败

所以,没有系统分区物理卷的系统,想扩容系统分区挂载根目录是不可能的。



版权声明:本文为wudinaniya原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。