vmware虚拟机扩大硬盘

记录一下对vmware虚拟机扩大硬盘的过程。操作有风险,重要数据请先进行备份。

1、首先在vcenter中将虚拟机下电,然后编辑虚拟机,将虚拟机硬盘扩大。具体操作见下图
这里写图片描述

2、打开虚拟机电源,利用fdisk -l可以看到虚拟机的硬盘变大了,但是df -h查看磁盘还是原来的。见下图
这里写图片描述

root@demo:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            997M  4.0K  997M   1% /dev
tmpfs           201M  692K  200M   1% /run
/dev/sda1       3.9G  1.6G  2.2G  42% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none           1001M     0 1001M   0% /run/shm
none            100M     0  100M   0% /run/user
root@demo:~# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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 identifier: 0x00052d8e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     8388607     4193280   83  Linux
/dev/sda2         8390654    12580863     2095105     5  Extended
/dev/sda5         8390656    12580863     2095104   82  Linux swap / Solaris

3、利用fdisk命令扩大硬盘
(本例子中计算sector号有误,把total 41943040 sectors当成了blocks来算的,导致扩大后/dev/sda2和/dev/sda5变小了。1个block应该对应多个sector。中间需要做一个换算。具体视自己系统而定)

root@demo:~# fdisk /dev/sda

Command (m for help): d
Partition number (1-5): 5

Command (m for help): d
Partition number (1-5): 2

Command (m for help): d
Selected partition 1

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): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 37752831
# (更新)此处应该填入41943040-2095105*2 ,也就是总的扇区值减去/dev/sda2 block数的2倍,这样出来的结果就不会缩小了/dev/sda2 

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2): 2
First sector (37752832-41943039, default 37752832): 
Using default value 37752832
Last sector, +sectors or +size{K,M,G} (37752832-41943039, default 41943039): 39847936
#此处填入默认值即可

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (37754880-39847936, default 37754880): 
Using default value 37754880
#此处填入默认值即可
Last sector, +sectors or +size{K,M,G} (37754880-39847936, default 39847936): 
Using default value 39847936
#此处填入默认值即可

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)

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.

4、重启虚拟机后,利用resize2fs 扩大/dev/sda1

root@demo:~# resize2fs /dev/sda1
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/sda1 is now 4718848 blocks long.

root@demo:~# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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 identifier: 0x00052d8e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    37752831    18875392   83  Linux
/dev/sda2        37752832    39847936     1047552+   5  Extended
/dev/sda5        37754880    39847936     1046528+  82  Linux swap / Solaris
root@demo:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            997M   12K  997M   1% /dev
tmpfs           201M  684K  200M   1% /run
/dev/sda1        18G  1.6G   16G  10% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none           1001M     0 1001M   0% /run/shm
none            100M     0  100M   0% /run/user

5、重新建立逻辑分区

root@demo:~# mkswap /dev/sda5
Setting up swapspace version 1, size = 1046524 KiB
no label, UUID=5289e58e-730f-493b-844b-fdffe322c1f9
#将此步生成的UUID填入/etc/fstab
root@demo:~# swapon /dev/sda5

root@demo:~# vim /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=fabf2996-790a-40a1-ad4c-a3a9bf699f4c /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=5289e58e-730f-493b-844b-fdffe322c1f9 none            swap    sw              0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0

6、重启虚拟机结束操作

posted @ 2017-08-18 18:16  0pandas0  阅读(345)  评论(0编辑  收藏  举报