VMware Linux(Centos)虚拟机扩容根目录磁盘空间

1:编辑虚拟机设置,点击[硬盘],选择[扩展] 填写大小:

image
image

点击扩展后VMware提示磁盘已经成功扩展。

image

完成扩展后,需从客户机操作系统内部对磁盘【重新分区】和【扩展文件系统】

2:启动虚拟机操作系统Centos,查看磁盘情况:

# fdisk -l

显示如下:

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x00094582

   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_rxf-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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_rxf-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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

可以看见分区情况如下:

   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

3:创建分区sda3:
输入指令

# 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): n  #输入n表示创建新分区
Command action
   e   extended
   p   primary partition (1-4)
p       #输入p
Partition number (1-4): 3 #输入3,表示sda3,前面已经有2个了
First cylinder (2611-3916, default 2611): #回车,默认值
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-3916, default 3916):   #回车,默认值
Using default value 3916

Command (m for help): w #输入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.

再查看分区情况

# fdisk -l

情况如下:

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x00094582

   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        3916    10483750   83  Linux

Disk /dev/mapper/vg_rxf-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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_rxf-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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

可以看见现在的分区情况如下:

   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        3916    10483750   83  Linux

这时分区已经变成3个了,但是还没有挂载,还不能用。
4:重启操作系统

# reboot

5:格式化新分区ext3

# mkfs -t ext3 /dev/sda3

显示情况如下:

mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2620937 blocks
131046 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2684354560
80 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

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

6:将物理硬盘分区初始化为物理卷,以便LVM使用,输入指令

# lvs

显示如下:

  LV      VG     Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_root vg_rxf -wi-ao---- 17.57g                                             
  lv_swap vg_rxf -wi-ao----  1.94g  

输入指令:

# pvcreate /dev/sda3

显示如下:

  Physical volume "/dev/sda3" successfully created

7:向卷组中添加物理卷来增加容量。
输入df -h 查看lvm组名

# df -h

Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vg_rxf-lv_root   18G   18G  112M 100% /
tmpfs                       931M  4.0K  931M   1% /dev/shm
/dev/sda1                   485M   35M  425M   8% /boot

其中vg_rxf为根所在组名.
输入指令,增加卷组的容量

# vgextend vg_rxf /dev/sda3

  Volume group "vg_rxf" successfully extended

8:查看可以扩展的空间大小:

vgdisplay

  --- Volume group ---
  VG Name               vg_rxf
  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               29.50 GiB
  PE Size               4.00 MiB
  Total PE              7553
  Alloc PE / Size       4994 / 19.51 GiB
  Free  PE / Size       2559 / 10.00 GiB
  VG UUID               KFyuD1-cj5n-cukx-z1Zf-Gg2T-IQLT-v6BD4N

Free PE / Size 就是可以提供分配的自由空间,最多10.00 GiB,在扩展时输入小于该值
9:扩充磁盘空间

# lvextend -L+9G /dev/mapper/vg_rxf-lv_root /dev/sda3

  Extending logical volume lv_root to 26.57 GiB
  Logical volume lv_root successfully resized

10:使用e2fsck指令查看检测文件系统错误

# e2fsck -a /dev/mapper/vg_rxf-lv_root

/dev/mapper/vg_rxf-lv_root is mounted.
e2fsck: 无法继续, 中止.

11:使用resize2fs指令来增大或者搜索未加载的"ext2/ext3"文件系统的大小(此处可能出现问题,没办法增大扩容空间)

resize2fs /dev/mapper/vg_rxf-lv_root

Filesystem at /dev/mapper/vg_rxf-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg_rxf-lv_root to 6965248 (4k) blocks.
The filesystem on /dev/mapper/vg_rxf-lv_root is now 6965248 blocks long.

11-1(如果11步出现问题,因为我的centos7的某些分区用的是xfs的文件系统(使用df -T查看即可知道)

[root@vnode1 ~]# df -T
文件系统                           类型          1K-块        已用        可用          已用%  挂载点
/dev/mapper/centos-root   xfs           37173520 5574340  31599180  15%      /
devtmpfs                      devtmpfs  3987400   0              3987400    0%       /dev
tmpfs                                 tmpfs        3997856   0              3997856    0%      /dev/shm
tmpfs                                 tmpfs        3997856   8628        3989228     1%     /run
tmpfs                                 tmpfs        3997856   0               3997856     0%    /sys/fs/cgroup
/dev/sda1                          xfs            508588      127152    381436       26%  /boot
tmpfs                                tmpfs         799572      0              799572       0%   run/user/0
[root@vnode1 ~]#

将resize2fs替换为xfs_growfs,重新执行一遍即可,如下:

[root@vnode1 ~]# xfs_growfs /dev/mapper/vg_rxf-lv_root

12:查看扩展空间后的结果:

# df -h

Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/vg_rxf-lv_root   27G   18G  8.9G  66% /
tmpfs                       931M  4.0K  931M   1% /dev/shm
/dev/sda1                   485M   35M  425M   8% /boot

可以看见,大功告成。

转载:https://www.jianshu.com/p/578816f4e9b8
备注:转载的地方有不对的地方已经在此文进行了修改,看这一篇就够了

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335