centos7擴展根分區

  購買的雲主機,安裝centos7後,發現系統根分區只有8.5G,而云主機硬盤大小是110G。node

如今想把剩餘的硬盤大小找出來,而後擴容至根分區。linux

  本文主題是:centos擴展根分區。因爲阿里雲,騰訊雲在購買主機時,硬盤大小即爲根分區centos

大小,沒有遇到到須要擴容的狀況。app

  一、查看硬盤大小信息

[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個分區,分別是:sdb一、sdb二、sdb3,上面截圖是我完成後的,因此看到sdb只有一個分區。dom

  二、刪除分區、磁盤分區

  # fdisk /dev/sdb       #將磁盤分區  #先刪除分區,再將磁盤分爲一個區ide

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

  d -----> 刪除分區this

  p ----->  打印sdb磁盤的分區信息阿里雲

  n ----->  建立新分區,進到分區項後,按p直接分區,默認分爲一個區,自行選擇分區數量。centos7

  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.

 

  三、格式化磁盤

# 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 

  四、建立物理卷

# 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

  五、查看卷組名稱

# 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

  六、將物理卷擴展到卷組

  # vgextend centos /dev/sdb

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

  七、將卷組中的空閒空間擴展到根分區邏輯卷

  #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.

  八、刷新根分區

  # 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

  九、查看擴展是否成功

[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大小,擴展根分區成功!

相關文章
相關標籤/搜索