VMware下CentOS7扩容根目录磁盘容量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hellboy0621/article/details/84345672

虚拟机硬盘预设是20G,用着用着发现不够用了,需要扩展根目录磁盘容量,以此文记录扩容过程,方便后续查看,如果哪里有错误,望大家不吝批评指正,谢谢!

1、在VMware上扩展磁盘容量,由20G扩展到40G;

2、开启虚拟机,使用ssh连接服务器;

3、查看磁盘信息,可以看到系统下只有一块硬盘sda;

[root@server1 ~]# 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
[root@server1 ~]# 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 - 保存分区信息到磁盘并退出

[root@server1 ~]# 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);

[root@server1 ~]# 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 - 保存分区信息到磁盘并退出

[root@server1 ~]# 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了;

[root@server1 ~]# 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分区创建为文件系统

[root@server1 ~]# 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、查看当前的物理卷

[root@server1 ~]# pvdiskplay
-bash: pvdiskplay: command not found
[root@server1 ~]# 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指令将分区初始化为物理卷;

[root@server1 ~]# 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、再次查看当前物理卷

[root@server1 ~]# 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;

[root@server1 ~]# 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通过向卷组中添加物理卷来增加卷组的容量;

[root@server1 ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

再次查看VG信息,发现倒数第2行的Free PE下20G的可扩展容量;

[root@server1 ~]# 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;

[root@server1 ~]# 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;

[root@server1 ~]# 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;

[root@server1 ~]# 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文件系统;

[root@server1 ~]# 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;

[root@server1 ~]# 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

 

展开阅读全文

没有更多推荐了,返回首页