VMware虚拟机中Centos 6.x系统磁盘空间扩容实战

简介: VMware虚拟机中Centos 6.x系统磁盘空间扩容实战

环境:


CentOS 6.9


 平常在VMware上创建Linux系统虚拟机的时候,往往当时不会给太多的磁盘空间,在后期的使用过程中经常会遇到磁盘空间不足的情况,所以需要对Linux系统扩展磁盘空间。


扩展磁盘步骤:


1.磁盘扩展


注意:只能在虚拟机关机的情况下操作,并且这个虚拟机上没有磁盘快照文件,如有需要删除才可以扩展。

1.png2.png

此时虽然在VMware虚拟机管理界面的扩容操作已经完成,但是这还只是扩容的第一步,后面还需要到操作系统内部进行操作。


2.查看分区信息

[root@test-centos6 ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a16bc
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM
Disk /dev/mapper/vg_testcentos6-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_testcentos6-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@test-centos6 ~]# lsblk      //列出块设备信息
NAME                              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                                11:0    1  447M  0 rom
sda                                 8:0    0   60G  0 disk
├─sda1                              8:1    0  500M  0 part /boot
└─sda2                              8:2    0 19.5G  0 part
  ├─vg_testcentos6-lv_root (dm-0) 253:0    0 17.5G  0 lvm  /
  └─vg_testcentos6-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]

可以看到目前磁盘/dev/sda已经有两个分区,分别为sda1、sda2。


3.新建分区sda3

[root@test-centos6 ~]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
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
   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     //建立新的磁盘分区
Command action
   e   extended        //逻辑分区
   p   primary partition (1-4)    //主分区
 p                                //建立主分区
Partition number (1-4): 3        //输入分区号
First cylinder (2611-7832, default 2611):    //分区起始位置(可直接回车也可以根据情况输入)
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-7832, default 7832):     //回车
Using default value 7832
Command (m for help): p       //列出磁盘目前的分区情况
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a16bc
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM
/dev/sda3            2611        7832    41939020   83  Linux
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: 设备或资源忙.
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.             //正在同步磁盘
[root@test-centos6 ~]# fdisk -l      //查看分区创建情况
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a16bc
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM
/dev/sda3            2611        7832    41939020   83  Linux
Disk /dev/mapper/vg_testcentos6-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_testcentos6-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

4.重启系统并格式化分区sda3文件系统

[root@test-centos6 ~]# reboot
[root@test-centos6 ~]# lsblk
NAME                              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                                11:0    1  447M  0 rom
sda                                 8:0    0   60G  0 disk
├─sda1                              8:1    0  500M  0 part /boot
├─sda2                              8:2    0 19.5G  0 part
│ ├─vg_testcentos6-lv_root (dm-0) 253:0    0 17.5G  0 lvm  /
│ └─vg_testcentos6-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
└─sda3                              8:3    0   40G  0 part
[root@test-centos6 ~]# mkfs.ext4 /dev/sda3   //将/dev/sda3分区文件系统格式化为ext4
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10484755 blocks
524237 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
320 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
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

5.创建物理卷(pv)


 此步骤可省略:当将分区直接添加到卷组(vg)时,LVM会自动将分区转换为物理卷(pv)。

[root@test-centos6 ~]# lvm
lvm> pvcreate /dev/sda3      //创建物理卷pv
  Physical volume "/dev/sda3" successfully created
lvm> pvdisplay       //显示物理卷pv信息 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_testcentos6      
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               jMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC
  "/dev/sda3" is a new physical volume of "40.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               40.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               PVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp
lvm> pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_testcentos6
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               jMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC
  "/dev/sda3" is a new physical volume of "40.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               40.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               PVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp

6.查看并创建卷组(vg)

lvm> vgdisplay          //查看需要扩展的卷组vg名称
  --- Volume group ---
  VG Name               vg_testcentos6          //卷组vg名称
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               59.49 GiB
  PE Size               4.00 MiB
  Total PE              17790
  Alloc PE / Size       15232 / 19.50 GiB
  Free  PE / Size       2558 / 9.99 GiB
  VG UUID               yVVWYi-mV3I-f1WI-2QeH-HkVT-oBXP-2KkQ8v
lvm> vgextend vg_testcentos6 /dev/sda3     //扩展卷组(这里的“vg_testcentos6”名称为上面的卷组名称)
  Volume group "vg_testcentos6" successfully extended

7.扩展逻辑卷(lv)

[root@test-centos6 ~]# df -h         //查看扩展路径,这里也可以用fdisk -l查看
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_testcentos6-lv_root           -----》  //**扩展路径**
                       18G   18G     0 100% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             485M   58M  403M  13% /boot
[root@test-centos6 ~]# lvm       //进入LVM管理器
lvm> lvextend -L +39.99G /dev/mapper/vg_testcentos6-lv_root     //扩展逻辑卷lv大小(通过fdisk -l/df -h查看要扩展过去的磁盘)
  Rounding size to boundary between physical extents: 39.99 GiB.
  Size of logical volume vg_testcentos6/lv_root changed from 17.51 GiB (4482 extents) to 57.50 GiB (14720 extents)
  Logical volume lv_root successfully resized.
lvm> lvscan
  ACTIVE            '/dev/vg_testcentos6/lv_root' [57.50 GiB] inherit
  ACTIVE            '/dev/vg_testcentos6/lv_swap' [2.00 GiB] inherit
lvm> pvscan
  PV /dev/sda2   VG vg_testcentos6   lvm2 [19.51 GiB / 0    free]
  PV /dev/sda3   VG vg_testcentos6   lvm2 [39.99 GiB / 0    free]
  Total: 2 [59.50 GiB] / in use: 2 [59.50 GiB] / in no VG: 0 [0   ]
lvm> exit
  Exiting.

8.查看分区情况

[root@test-centos6 ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a16bc
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM
/dev/sda3            2611        7832    41939020   83  Linux
Disk /dev/mapper/vg_testcentos6-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_testcentos6-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

注意:上面虽然显示物理卷/dev/sdb3有40G空闲,但是这里不能全部使用,可能只有39.99G可以使用。


7.激活即重新读取磁盘信息


 查看/dev/mapper/vg_testcentos6-lv_root的Type为ext4,所以使用resize2fs;如果文件类型是 xfs ,则使用xfs_growfs命令格式化。

[root@test-centos6 ~]# blkid      //查看文件系统
/dev/sda1: UUID="32a489a3-65c2-43a4-9aff-12c03eabc4dd" TYPE="ext4"
/dev/sda2: UUID="jMIPOc-sduo-sl4T-6iQ5-ledQ-1w9O-cweeEC" TYPE="LVM2_member"
/dev/sda3: UUID="PVrdc7-fUEX-PAxZ-ME92-IuNU-khDC-c5tYlp" TYPE="LVM2_member"
/dev/mapper/vg_testcentos6-lv_root: UUID="e8adf717-e78c-4845-a750-2a22a60689ed" TYPE="ext4"    //可以看到文件系统格式化为ext4
/dev/mapper/vg_testcentos6-lv_swap: UUID="ac33f130-95cc-4046-8bdd-db2d7c450f6d" TYPE="swap"
[root@test-centos6 ~]# resize2fs /dev/mapper/vg_testcentos6-lv_root    //ext4文件系统使用resize2fs
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_testcentos6-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/mapper/vg_testcentos6-lv_root to 15073280 (4k) blocks.
The filesystem on /dev/mapper/vg_testcentos6-lv_root is now 15073280 blocks long.
[root@test-centos6 ~]# df -h       //查看激活状态
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_testcentos6-lv_root
                       57G  4.4G   50G   9% /
tmpfs                 1.9G  244K  1.9G   1% /dev/shm
/dev/sda1             485M   58M  403M  13% /boot

 可以看到现在我们的磁盘已经从最开始的19G扩展到了57G。

相关文章
|
10天前
|
Linux
CentOS系统中rpm包与源码包的主要区别
总的来说,RPM包和源码包各有优缺点,选择哪种包主要取决于你的需求和技术水平。希望这个答案能帮助你更好地理解RPM包和源码包的区别。
66 27
|
10天前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
166 26
|
10天前
|
Linux 开发工具 C语言
在CentOS系统中编译安装TinyXml2的方法
以上就是在CentOS系统中编译安装TinyXml2的方法。这个过程可能会有些复杂,但只要你按照上面的步骤一步一步来,你应该能够成功地完成这个任务。如果你在这个过程中遇到任何问题,你可以在网上搜索相关的资料,或者在相关的论坛上寻求帮助。希望这个指南能够帮助你成功地在你的CentOS系统中编译安装TinyXml2。
53 22
|
27天前
|
存储 安全 Linux
CentOS 7.9系统备份:每日定期发送最新备份文件到另一台服务器。
注意,这个解决方案忽略了很多细节,例如错误处理和通知、备份版本控制、循环处理旧的备份文件等等。此外,你也应该尽量保持源服务器和目标服务器之间快速,稳定且安全的网络连接,并且目标服务器应该有足够的空间用于存放每天的备份文件。如果你需要更高级的备份解决方案,可能需要考虑一下使用专门的备份工具或者服务。
67 18
|
1月前
|
存储 安全 Linux
Centos 8系统ext4文件系统类型进行扩容缩容 (LVM)
通过使用这些技术和工具,可以有效管理CentOS 8系统上的存储资源,确保系统在高负载和高并发情况下的稳定运行。
106 9
|
监控 Linux
centos磁盘空间满的问题处理.
1.清理大文件.删除多余文件 2.内容迁移.磁盘扩容
539 0
centos磁盘空间满的问题处理.
|
12天前
|
Kubernetes Linux 网络安全
CentOS 7.8下使用kubeadm安装Kubernetes 1.26
这就是所有的前线报告,冒险家们,你们已经做好准备,开始在CentOS 7.8上通过Kubeadm安装Kubernetes 1.26的挑战了吗?走上这段旅程,让你的代码飞翔吧。
62 16
|
20天前
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
49 10
|
1月前
|
Linux 虚拟化 Windows
VMware隐藏黑科技!CentOS安装这个神器效率翻倍 文件互传竟比U盘还快?
### 安装VMtools的作用及步骤 安装VMtools可以实现Windows与CentOS之间的字符串和命令粘贴、文件夹共享等操作。在CentOS中,通过VMware菜单栏选择“VMware Tools”,将下载的压缩包拷贝到`/opt`目录并解压。接着进入终端,使用`./vmware-install.pl`命令完成安装。安装后重启系统,即可实现Windows与CentOS之间的字符和文件共享。具体步骤包括解压文件、启动命令行终端、安装VMtools以及配置文件共享。
97 16
VMware隐藏黑科技!CentOS安装这个神器效率翻倍 文件互传竟比U盘还快?
|
21天前
|
Linux 网络安全 数据安全/隐私保护
在CentOS 7.9中安装sshpass教程
如果在控制台显示出sshpass的使用方法,那就说明我们的大餐已经准备好,sshpass已经成功安装。 以上就是在 CentOS 7.9 中安装 sshpass 的彩色步骤,专业而生动,就像大厨在厨房一样顺利。
58 8
下一篇
oss创建bucket