コンテンツにスキップ

Linux Tips集

Created: 2024/01/18

概要

linuxに関するTipsです。

仮想マシンのストレージを大きくしたい

https://qiita.com/Eustace/items/9c31a9dc381f627feb64

環境

  • Ubuntu 22.04 (OVA)
  • VMware ESXi

現状を確認

$ df -h
Filesystem                                 Size  Used Avail Use% Mounted on
tmpfs                                      392M  1.7M  390M   1% /run
/dev/sda1                                   20G   19G  744M  97% /
tmpfs                                      2.0G     0  2.0G   0% /dev/shm
tmpfs                                      5.0M     0  5.0M   0% /run/lock
/dev/sda15                                 105M  6.1M   99M   6% /boot/efi

ESXiで容量を拡張する

仮想マシンから拡張したいインスタンスを選択し、ストレージの設定を拡張します。

partedでパーティションを拡張する

/sys/class/block/sda/device/rescanを実行するとストレージサイズを検知してくれる。

$ sudo su -
root@vmdev1:~# echo 1 > /sys/class/block/sda/device/rescan
root@vmdev1:~# exit

partedに入りサイズを拡張する。

$ sudo parted /dev/sda
GNU Parted 3.4
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
        17.4kB  1049kB  1031kB  Free Space
14      1049kB  5243kB  4194kB                     bios_grub
15      5243kB  116MB   111MB   fat32              boot, esp
 1      116MB   21.5GB  21.4GB  ext4
        21.5GB  42.9GB  21.5GB  Free Space

(parted) unit s
(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 83886080s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start      End        Size       File system  Name  Flags
        34s        2047s      2014s      Free Space
14      2048s      10239s     8192s                         bios_grub
15      10240s     227327s    217088s    fat32              boot, esp
 1      227328s    83789062s  83561735s  ext4
        83789063s  83886046s  96984s     Free Space

(parted) resizepart 1
Warning: Partition /dev/sda1 is being used. Are you sure you want to continue?
Yes/No? yes
End?  [83789062s]? 83886046s
(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 83886080s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start    End        Size       File system  Name  Flags
        34s      2047s      2014s      Free Space
14      2048s    10239s     8192s                         bios_grub
15      10240s   227327s    217088s    fat32              boot, esp
 1      227328s  83886046s  83658719s  ext4

ファイルシステムを拡張する

$ sudo resize2fs /dev/sda1
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/sda1 is now 10457339 (4k) blocks long.

結果

$ df -h
Filesystem                                 Size  Used Avail Use% Mounted on
tmpfs                                      392M  1.7M  390M   1% /run
/dev/sda1                                   39G   19G   21G  48% /
tmpfs                                      2.0G     0  2.0G   0% /dev/shm
tmpfs                                      5.0M     0  5.0M   0% /run/lock
/dev/sda15                                 105M  6.1M   99M   6% /boot/efi
192.168.10.100:/mnt/tank/services/misskey  2.5T  2.5G  2.5T   1% /mnt/misskey
tmpfs                                      392M  4.0K  392M   1% /run/user/1001