centos7扩展根分区

  购买的云主机,安装centos7后,发现系统根分区只有8.5G,而云主机硬盘大小是110G。

现在想把剩余的硬盘大小找出来,然后扩容至根分区。

  本文主题是:centos扩展根分区。由于阿里云,腾讯云在购买主机时,硬盘大小即为根分区

大小,没有遇到到需要扩容的情况。

  1、查看硬盘大小信息

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  8.5G  1.5G  7.0G  18% /
devtmpfs                 231M     0  231M   0% /dev
tmpfs                    241M     0  241M   0% /dev/shm
tmpfs                    241M  4.3M  237M   2% /run
tmpfs                    241M     0  241M   0% /sys/fs/cgroup
/dev/sda1                494M  124M  371M  25% /boot
tmpfs                     49M     0   49M   0% /run/user/0

# 发现只有8.5G,总大小没有110G
  发现根目录只有8.5G,总大小没有110G,然后使用fdisk -l命令查看,发现剩余的空间在sdb第二个磁盘上:
[root@localhost ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x000f1903

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20971519     9972736   8e  Linux LVM

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/centos-root: 116.5 GB, 116505182208 bytes, 227549184 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

  发现sdb磁盘有3个分区,分别是:sdb1、sdb2、sdb3,上面截图是我完成后的,所以看到sdb只有一个分区。

  2、删除分区、磁盘分区

  # fdisk /dev/sdb #将磁盘分区  #先删除分区,再将磁盘分为一个区

  m -----> 打印help信息

  d -----> 删除分区

  p -----> 打印sdb磁盘的分区信息

  n -----> 创建新分区,进到分区项后,按p直接分区,默认分为一个区,自行选择分区数量。

  w -----> 保存分区信息,不然分区无法正确完成。

[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 0xef6f1f81.

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

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): 
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0xef23a9cb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209715199   104856576   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

  3、格式化磁盘

# mkfs.ext4 /dev/sdb  #格式化为ext4格式
[root@localhost ~]# mkfs.ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
800 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, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

  4、创建物理卷

# pvcreate /dev/sdb
[root@localhost ~]# pvcreate /dev/sdb
WARNING: ext4 signature detected on /dev/sdb at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb.
  Physical volume "/dev/sdb" successfully created

  5、查看卷组名称

# vgdisplay -v
# 查看到我的VG Name为“centos”
[root@localhost ~]# vgdisplay -v
    Using volume group(s) on command line.
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.51 GiB
  PE Size               4.00 MiB
  Total PE              2434
  Alloc PE / Size       2424 / 9.47 GiB
  Free  PE / Size       10 / 40.00 MiB
  VG UUID               OyB5dN-0LOk-6vb1-Qmtu-1EQp-1GtB-Szuco0
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                9SLqUm-dXfi-bNle-wdX1-cnZQ-HCun-nGWdXk
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-06-28 02:34:19 +0800
  LV Status              available
  # open                 2
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                J3ANyS-BTG5-YJ8M-2Zsq-sBUo-YVWg-jyFmvA
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-06-28 02:34:19 +0800
  LV Status              available
  # open                 1
  LV Size                8.47 GiB
  Current LE             2168
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Physical volumes ---
  PV Name               /dev/sda2     
  PV UUID               P6xf6f-NPgl-s3Dm-123h-B5e3-NJBh-GbNM1n
  PV Status             allocatable
  Total PE / Free PE    2434 / 10

  6、将物理卷扩展到卷组

  # vgextend centos /dev/sdb

[root@localhost ~]# vgextend centos /dev/sdb
  Volume group "centos" successfully extended

  7、将卷组中的空闲空间扩展到根分区逻辑卷

  #lvextend -l +100%FREE /dev/centos/root

[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-root 
  Size of logical volume centos/root changed from 8.47 GiB (2168 extents) to 108.50 GiB (27777 extents).
  Logical volume root successfully resized.

  8、刷新根分区

  # xfs_growfs /dev/centos/root

[root@localhost ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=555008 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2220032, 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=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2220032 to 28443648

  9、查看扩展是否成功

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  109G  1.5G  108G   2% /
devtmpfs                 231M     0  231M   0% /dev
tmpfs                    241M     0  241M   0% /dev/shm
tmpfs                    241M  4.3M  237M   2% /run
tmpfs                    241M     0  241M   0% /sys/fs/cgroup
/dev/sda1                494M  124M  371M  25% /boot
tmpfs                     49M     0   49M   0% /run/user/0

  看到根分区已为109G大小,扩展根分区成功!