1. 程式人生 > >VMware下CentOS7擴容根目錄磁碟容量

VMware下CentOS7擴容根目錄磁碟容量

虛擬機器硬碟預設是20G,用著用著發現不夠用了,需要擴充套件根目錄磁碟容量,以此文記錄擴容過程,方便後續檢視,如果哪裡有錯誤,望大家不吝批評指正,謝謝!

1、在VMware上擴充套件磁碟容量,由20G擴充套件到40G;

2、開啟虛擬機器,使用ssh連線伺服器;

3、檢視磁碟資訊,可以看到系統下只有一塊硬碟sda;

[[email protected] ~]# df -lh
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G   17G  647M  97% /
devtmpfs                 2.9G     0  2.9G   0% /dev
tmpfs                    2.9G     0  2.9G   0% /dev/shm
tmpfs                    2.9G   12M  2.9G   1% /run
tmpfs                    2.9G     0  2.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    579M     0  579M   0% /run/user/1000
tmpfs                    579M     0  579M   0% /run/user/0
[
[email protected]
~]# fdisk -l Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b20df Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

4、使用fdisk分割槽工具給sda磁碟分割槽

其中:

m - 列印目錄列表

n - 增加一個新分割槽

p - 代表主分割槽

3 - 分割槽編號

w - 儲存分割槽資訊到磁碟並退出

[[email protected] ~]# fdisk /dev/sda
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.


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 (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (41943040-83886079, default 41943040):
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-83886079, default 83886079):
Using default value 83886079
Partition 3 of type Linux and of size 20 GiB is set

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.

5、再次檢視分割槽資訊

其中sda3即為新增加的分割槽,看最後一列,sda3的System為Linux(Id為83),需要修改為Linux LVM(Id為8e);

[[email protected] ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b20df

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    83886079    20971520   83  Linux

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

6、修改sda3為LVM卷檔案系統

其中:

m - 列印目錄列表

t - 修改分割槽的System Id

3 - 分割槽編號

8e - LVM卷檔案系統對應的System Id

w - 儲存分割槽資訊到磁碟並退出

[[email protected] ~]# fdisk /dev/sda
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.


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): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.

7、再次檢視分割槽資訊,sda3已經改為Linux LVM了;

[[email protected] ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b20df

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    83886079    20971520   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

8、重啟系統;

9、將sda3分割槽建立為檔案系統

[[email protected] ~]# mkfs -t ext4 /dev/sda3
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
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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

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

10、檢視當前的物理卷

[[email protected] ~]# pvdiskplay
-bash: pvdiskplay: command not found
[[email protected] ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               25WUH5-s8di-Cupq-4FJS-JcgU-jv80-d1zcKP

11、將物理硬碟分割槽初始化為物理卷,以便被LVM使用;

只有System Id修改為8e之後,才能使用pvcreat指令將分割槽初始化為物理卷;

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

12、再次檢視當前物理卷

[[email protected] ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               25WUH5-s8di-Cupq-4FJS-JcgU-jv80-d1zcKP

  "/dev/sda3" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Z9htWg-egxy-cNR0-EBTq-D0U6-a32n-X65URf

13、動態擴充套件VG

通過vgdisplay檢視當前需要擴充的lvm組名,本機為centos;

[[email protected] ~]# vgdisplay
  --- 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               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               kn9EEY-Dt0L-cxsh-cmCa-UBBx-2Jah-vmxwse

vgextend通過向卷組中新增物理捲來增加捲組的容量;

[[email protected] ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

再次檢視VG資訊,發現倒數第2行的Free PE下20G的可擴充套件容量;

[[email protected] ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               38.99 GiB
  PE Size               4.00 MiB
  Total PE              9982
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               kn9EEY-Dt0L-cxsh-cmCa-UBBx-2Jah-vmxwse

14、LV擴充套件

通過lvdisplay檢視根目錄的LV Path為/dev/centos/root;

[[email protected] ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                QCd4Hj-QHy0-P5TJ-tHGs-TGDr-BBxD-Haf1XY
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-10-17 16:21:31 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  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                yiZIHh-mPQU-IFS6-kIFa-a1lT-ADkk-9v0o10
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-10-17 16:21:31 +0800
  LV Status              available
  # open                 1
  LV Size                <36.80 GiB
  Current LE             9420
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

擴充套件空間要小於VG 的Free PE的大小,即小於20GB,這裡使用19.8G;

[[email protected] ~]# lvextend -L +19.8G /dev/centos/root /dev/sda3
  Rounding size to boundary between physical extents: 19.80 GiB.
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <36.80 GiB (9420 extents).
  Logical volume centos/root successfully resized.

15、檢視分割槽檔案系統型別

CentOS7預設使用xfs,CentOS6預設使用ext4;

[[email protected] ~]# df -T
Filesystem              Type     1K-blocks     Used Available Use% Mounted on
/dev/mapper/centos-root xfs       38574080 17151340  21422740  45% /
devtmpfs                devtmpfs   2951704        0   2951704   0% /dev
tmpfs                   tmpfs      2963976        0   2963976   0% /dev/shm
tmpfs                   tmpfs      2963976    11956   2952020   1% /run
tmpfs                   tmpfs      2963976        0   2963976   0% /sys/fs/cgroup
/dev/sda1               xfs        1038336   145252    893084  14% /boot
tmpfs                   tmpfs       592796        0    592796   0% /run/user/0

 16、同步檔案系統

檔案系統型別是xfs,只能使用xfs_growfs命令同步檔案系統,不能使用resize2fs、e2fsck、dumpe2fs;

resize2fs命令,用於擴大或者縮小未掛載的ext2,ext3或者是ext4檔案系統;

[[email protected] ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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 4455424 to 9646080

17、檢視磁碟資訊,發現根目錄已經擴充套件了20G,由17G變為37G;

[[email protected] ~]# df -lh
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   37G   17G   21G  45% /
devtmpfs                 2.9G     0  2.9G   0% /dev
tmpfs                    2.9G     0  2.9G   0% /dev/shm
tmpfs                    2.9G   12M  2.9G   1% /run
tmpfs                    2.9G     0  2.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    579M     0  579M   0% /run/user/1000
tmpfs                    579M     0  579M   0% /run/user/0