嵌入式开发之Vmware虚拟机磁盘扩容

参考文档

  1. VMware下ubuntu扩展磁盘空间

提示:

本文档的适用范围为Vmware+Ubuntu的开发环境

嵌入式开发中一般会使用Vmware中安装Ubuntu虚拟机来进行软件开发,为了提高虚拟机的磁盘性能推荐在创建虚拟机的时候采用预先分配磁盘的方式,如下图所示:

这里写图片描述

如上图提示文字如果立即分配所有磁盘空间可以提高性能,但是随之带来的问题就是当创建虚拟机时的磁盘空间不够用时扩容比较麻烦。下面将具体分析在预先分配磁盘的情况如何进行磁盘扩容。

首先在预先分配磁盘空间的基础上再分配新的空间(示例从85GB扩展90GB)

这里写图片描述

点击扩展按钮,经过一段比较长时间的等待之后新磁盘的分配申请工作已经完成,但是如界面提示“扩展操作仅增加磁盘的大小,分区和文件系统的大小不受影响”所示还需要进入到Ubuntu系统中进行文件系统分区的大小调整

提示:所有Ubuntu分区相关操作需要在root用户下进行

查看现有磁盘相关信息:fdisk -l

建议记录现在的磁盘情况供之后重新分配分区参考

root:~#fdisk -l
Disk /dev/sda: 90 GiB, 96636764160 bytes, 188743680 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
Disklabel type: dos
Disk identifier: 0x6a1fff92

Device     Boot     Start       End   Sectors Size Id Type
/dev/sda1  *         2048 174065663 174063616  83G 83 Linux
/dev/sda2       174067710 178255871   4188162   2G  5 Extended
/dev/sda5       174067712 178255871   4188160   2G 82 Linux swap / Solaris

从fdisk命令的信息来看可以发现/dev/sda的空间已经扩展为90GiB,但是Ubuntu下面的/dev/sda1、/dev/sda2、/dev/sda5分区依然是按照扩展前的85GiB来分布的,所以现在的思路是先删除掉现有分区然后根据新的磁盘空间来重新创建分区

关闭交换分区功能

root@ubuntu:# swapoff -a

确保交换分区已经关闭:Swap行全部为0

root@ubuntu:# free -m
              total        used        free      shared  buff/cache   available
Mem:           1982         630         467          14         885        1110
Swap:             0           0           0

使用fdisk命令删除所有分区

root@ubuntu:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


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

Partition 1 has been deleted.

Command (m for help): d
Partition number (2,5, default 5): 2

Partition 2 has been deleted.

Command (m for help): p
Disk /dev/sda: 90 GiB, 96636764160 bytes, 188743680 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
Disklabel type: dos
Disk identifier: 0x6a1fff92

重新创建分区

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-188743679, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-188743679, default 188743679): 184555520

Created a new partition 1 of type 'Linux' and of size 88 GiB.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (184555521-188743679, default 184557568): 
Last sector, +sectors or +size{K,M,G,T,P} (184557568-188743679, default 188743679): 

Created a new partition 2 of type 'Linux' and of size 2 GiB.

Command (m for help): p
Disk /dev/sda: 90 GiB, 96636764160 bytes, 188743680 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
Disklabel type: dos
Disk identifier: 0x6a1fff92

Device     Boot     Start       End   Sectors Size Id Type
/dev/sda1            2048 184555520 184553473  88G 83 Linux
/dev/sda2       184557568 188743679   4186112   2G 83 Linux

这里sda1的End需要通过计算得到,计算方法为/dev/sda总Sector数 - 交换分区占用Sector数

/dev/sda总Sector数可以从之前保存的分区信息得到为188743680
交换分区占用Sector数为4188160
新的sda1分区的End位置为188743680-4188160=184555520

更新sda2分区的Id

Command (m for help): t
Partition number (1,2, default 2): 
Partition type (type L to list all types): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p
Disk /dev/sda: 90 GiB, 96636764160 bytes, 188743680 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
Disklabel type: dos
Disk identifier: 0x6a1fff92

Device     Boot     Start       End   Sectors Size Id Type
/dev/sda1            2048 184555520 184553473  88G 83 Linux
/dev/sda2       184557568 188743679   4186112   2G 82 Linux swap / Solaris

保存分区信息

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).

重启Ubuntu虚拟机

更新交换分区的UUID,否则将无法正常使用交换分区功能

root@ubuntu:~# awk '/swap/ { print $1 }' /etc/fstab
#
UUID=d3dc807d-3d2b-477a-a77b-03b783b0317e
root@xiaoyalun:~# dd if=/dev/zero of=/dev/sda2
dd: writing to '/dev/sda2': No space left on device
4186113+0 records in
4186112+0 records out
2143289344 bytes (2.1 GB, 2.0 GiB) copied, 68.846 s, 31.1 MB/s
root@ubuntu:~# mkswap -U d3dc807d-3d2b-477a-a77b-03b783b0317e /dev/sda2
Setting up swapspace version 1, size = 2 GiB (2143285248 bytes)
no label, UUID=d3dc807d-3d2b-477a-a77b-03b783b0317e

首先是从/etc/fstab查询到新的UUID,通过mkswap命令创建新的交换分区

调整分区大小

root@ubuntu:~# resize2fs /dev/sda1
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 6, new_desc_blocks = 6
The filesystem on /dev/sda1 is now 23069184 (4k) blocks long.

至此虚拟机磁盘扩容已经完成

猜你喜欢

转载自blog.csdn.net/sanallen/article/details/78996253
今日推荐