ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

VMwareで作ったCentOSに割り当てたディスク領域を拡張する方法

VMwareで作成したCentOSのディスク容量を増やしたかったのですが、VMwareの設定のみではだめでした。
VMwareの設定の他に、Linuxの場合はパーティションの再設定等の作業もしないといけなかったので
忘れそうなのでメモを残しておこうと思います。

やりたいこと

仮想マシンとして作ったCentOSに割り当てているディスク領域を20GBから40GBに拡張したい。

作業内容

VMwareの作業

ディスク容量の拡張

CentOSの仮想マシンをパワーオフにした状態で、VMwareの仮想マシン設定パネルを開きます。
「ハードディスク」を選択して、「拡張」をクリックします。
f:id:a1026302:20201211132107j:plain
ディスク容量の拡張というサブウィンドウが表示されるので、ディスクの最大サイズを変更します。
f:id:a1026302:20201211132233j:plain

CentOSの仮想マシン起動

CentOSの仮想マシンを「パワーオン」します。

仮想サーバー(CentOSの作業)

現在のディスクの状態を確認

ディスクの状態を確認します。「/dev/sda3」の使用量が「87%」です。

[test@localhost ~]$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
devtmpfs         3978692        0   3978692   0% /dev
tmpfs            3995032        0   3995032   0% /dev/shm
tmpfs            3995032    12648   3982384   1% /run
tmpfs            3995032        0   3995032   0% /sys/fs/cgroup
/dev/sda3       18555904 15978528   2577376  87% /
/dev/sda1         303788   207760     96028  69% /boot
tmpfs             799008        8    799000   1% /run/user/42
tmpfs             799008        0    799008   0% /run/user/1000
ハード・ディスクのパーティションを設定

CentOS側の容量を拡張します。

[test@localhost ~]$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

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-3, default 3): 3
Partition 3 is deleted

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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 label type: dos
Disk identifier: 0x0000d44c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (4810752-83886079, default 4810752): 
Using default value 4810752
Last sector, +sectors or +size{K,M,G} (4810752-83886079, default 83886079): 
Using default value 83886079
Partition 3 of type Linux and of size 37.7 GiB is set

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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 label type: dos
Disk identifier: 0x0000d44c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    83886079    39537664   83  Linux

設定したパーティションで保存(w)して fdisk を終了する。

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.

警告が出て、再起動後に設定が反映されると言われたので再起動します。

再起動後の作業

再起動後、dfコマンドで確認するが、まだ「/dev/sda3」の容量は変わっていませんでした。

[test@localhost ~]$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
devtmpfs         3978692        0   3978692   0% /dev
tmpfs            3995032        0   3995032   0% /dev/shm
tmpfs            3995032    12648   3982384   1% /run
tmpfs            3995032        0   3995032   0% /sys/fs/cgroup
/dev/sda3       18555904 15978528   2577376  87% /
/dev/sda1         303788   207760     96028  69% /boot
tmpfs             799008        8    799000   1% /run/user/42
tmpfs             799008        0    799008   0% /run/user/1000

「xfs_growfs」コマンドで「/dev/sda3」のサイズを変更します。

[test@localhost ~]$ sudo xfs_growfs /dev/sda3
[sudo] password for test:
meta-data=/dev/sda3              isize=256    agcount=4, agsize=1160384 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4641536, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4641536 to 9884416

dfコマンドで「/dev/sda3」の容量が増加していることを確認します。
(ディスク使用量が「87%」->「41%」になっている)

[test@localhost ~]$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
devtmpfs         3978692        0   3978692   0% /dev
tmpfs            3995032        0   3995032   0% /dev/shm
tmpfs            3995032    12652   3982380   1% /run
tmpfs            3995032        0   3995032   0% /sys/fs/cgroup
/dev/sda3       39527424 15978768  23548656  41% /
/dev/sda1         303788   207760     96028  69% /boot
tmpfs             799008        8    799000   1% /run/user/42
tmpfs             799008        0    799008   0% /run/user/1000

参考にしたサイト

nashikachi.hatenablog.com