Skip to content

Instantly share code, notes, and snippets.

@minazou67
Last active October 11, 2020 03:44
Show Gist options
  • Save minazou67/b1222c07363057741345bbfdc0c4b1bf to your computer and use it in GitHub Desktop.
Save minazou67/b1222c07363057741345bbfdc0c4b1bf to your computer and use it in GitHub Desktop.
How to expand disk size of Linux on VMware

How to expand disk size of Linux on VMware

VMware 上の Linux のディスクサイズを拡大する方法です。
仮想ディスクの縮小は面倒なため、不用意にサイズを拡大しすぎないこと。

Environment

  • VMware ESXi 6.0.0
  • VMware vSphere Client 6.0.0
  • Debian jessie 8.5
  • Non-LVM
  • ext4 filesystem

How to expand disk size

Preparation

全てのスナップショットを削除し、仮想マシンを停止させて、データストア上の各種ファイルをバックアップします。
バックアップが完了したら、仮想マシンを起動します。

Expansion of the virtual disk

仮想マシンの「設定の編集」画面で仮想ディスクのサイズを拡大し、仮想マシンを再起動します。
この段階では、OS のパーティションやファイルシステムはまだ拡大されていません。

Delete the partition

既存のパーティションを削除します。

$ sudo fdisk /dev/sda
Command (m for help): p
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 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: 0x0a1664e6

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 15988735 15986688  7.6G 83 Linux
/dev/sda2       15990782 16775167   784386  383M  5 Extended
/dev/sda5       15990784 16775167   784384  383M 82 Linux swap / Solaris

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

Partition 5 has been deleted.

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

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): p
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 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: 0x0a1664e6

Create a new partition

新しいパーティションを作成します。スワップ領域のサイズは、物理 RAM のサイズが 2GB 以下の場合は物理 RAM の2倍、2GB 以上の場合は物理 RAM のサイズと同程度を目安とします。

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-67108863, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-67108863, default 67108863): 65011711

Created a new partition 1 of type 'Linux' and of size 31 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): e
Partition number (2-4, default 2): 2
First sector (65011712-67108863, default 65011712):
Last sector, +sectors or +size{K,M,G,T,P} (65011712-67108863, default 67108863):

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

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 (65013760-67108863, default 65013760):
Last sector, +sectors or +size{K,M,G,T,P} (65013760-67108863, default 67108863):

Created a new partition 5 of type 'Linux' and of size 1023 MiB.

Command (m for help): p
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 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: 0x0a1664e6

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1           2048 65011711 65009664   31G 83 Linux
/dev/sda2       65011712 67108863  2097152    1G  5 Extended
/dev/sda5       65013760 67108863  2095104 1023M 83 Linux

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

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

Command (m for help): p
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 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: 0x0a1664e6

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1           2048 65011711 65009664   31G 83 Linux
/dev/sda2       65011712 67108863  2097152    1G  5 Extended
/dev/sda5       65013760 67108863  2095104 1023M 82 Linux swap / Solaris

Writing of the partition information

パーティション情報を書き込み、OS を再起動します。

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: デバイスもしくはリソースがビジー状態です

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).

$ sudo reboot

Resizing of the file system

ファイルシステムをリサイズし、ディスク容量とパーティション情報を確認します。

$ df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/sda1        7.4G  2.6G  4.5G   37% /
udev              10M     0   10M    0% /dev
tmpfs            794M  8.7M  785M    2% /run
tmpfs            2.0G     0  2.0G    0% /dev/shm
tmpfs            5.0M     0  5.0M    0% /run/lock
tmpfs            2.0G     0  2.0G    0% /sys/fs/cgroup

$ sudo resize2fs /dev/sda1
resize2fs 1.42.12 (29-Aug-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 8126208 (4k) blocks long.

$ df -h
ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/sda1         31G  2.6G   27G    9% /
udev              10M     0   10M    0% /dev
tmpfs            794M  8.7M  785M    2% /run
tmpfs            2.0G     0  2.0G    0% /dev/shm
tmpfs            5.0M     0  5.0M    0% /run/lock
tmpfs            2.0G     0  2.0G    0% /sys/fs/cgroup

$ sudo fdisk -l
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 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: 0x0a1664e6

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1           2048 65011711 65009664   31G 83 Linux
/dev/sda2       65011712 67108863  2097152    1G  5 Extended
/dev/sda5       65013760 67108863  2095104 1023M 82 Linux swap / Solaris

Creating a swap space

スワップ領域を作成します。

$ sudo mkswap /dev/sda5
Setting up swapspace version 1, size = 1047548 KiB
no label, UUID=a7494b0a-09b3-4b22-a8c7-ae2086372433

Automatic mounting of swap space

OS の再起動時にスワップ領域が自動でマウントされるように設定(UUID を更新)します。

$ sudo blkid
/dev/sda1: UUID="33c1066f-0842-40f2-bac2-48c20d25c278" TYPE="ext4" PTTYPE="dos" PARTUUID="0a1664e6-01"
/dev/sda5: UUID="a7494b0a-09b3-4b22-a8c7-ae2086372433" TYPE="swap" PARTUUID="0a1664e6-05"

$ sudo vi /etc/fstab
# swap was on /dev/sda5 during installation
UUID=a7494b0a-09b3-4b22-a8c7-ae2086372433 none swap sw 0 0

Enabling swap space

スワップ領域を有効化します。

$ sudo swapon -av
swapon /dev/sda5
swapon: /dev/sda5: found swap signature: version 1d, page-size 4, same byte order
swapon: /dev/sda5: pagesize=4096, swapsize=1072693248, devsize=1072693248

$ sudo swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda5                               partition       1047548 0       -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment