LoginSignup
150
161

More than 5 years have passed since last update.

VirtualBoxで仮想HDDを追加して Linuxのディスク空き容量を増やす

Posted at

VirtualBox上のCentOS7にあれやこれや入れてたら、仮想HDDの2GBでは少な過ぎたようで、ルートパスの使用率が96%に。

$ df
ファイルシス            1K-ブロック    使用 使用可 使用% マウント位置
/dev/mapper/centos-root     1361920 1306184  55736   96% /
devtmpfs                     242508       0 242508    0% /dev
tmpfs                        251028       0 251028    0% /dev/shm
tmpfs                        251028    4408 246620    2% /run
tmpfs                        251028       0 251028    0% /sys/fs/cgroup
/dev/sda1                    508588  136960 371628   27% /boot

なにも考えず、とりあえず仮想HDDをもう2GB追加してルートパス /を拡張してみます。

VirtualBoxで新しい仮想HDDを追加

追加するVMのストレージ設定画面を開きます。
「コントローラー: SATA」を選択するとの右のほうにHDD追加ボタンHDD追加アイコン.PNG
がでるので、それをクリックします。

DiskADD01.PNG

「新規ディスクの作成」をクリックします。

DiskADD02.PNG

私の場合は、既存のディスクがVHDだったので同じVHDを選択しました。
デフォルトのVDIでも結構です。

DiskADD03.PNG

可変サイズを選択。固定でも大丈夫です。
(固定だといきなり指定サイズのファイルが作成される)

DiskADD04.PNG

仮想ディスクイメージのファイル名と、サイズを設定します。

DiskADD05.PNG

最後に作成をクリックするとDiskが追加されます。

DiskADD06.PNG

追加した仮想HDDをLinuxのボリュームへ追加

ルート / の空きを増やすには以下の作業が必要です。

  1. 追加した仮想HDDにパーティションを作成
  2. 作成したパーティションで物理ボリュームを作成
  3. / にマウントしている論理ボリュームが割当てられているボリュームグループへ、物理ボリュームを追加
  4. 論理ボリュームを拡張
  5. ファイルシステムを拡張

CentOS7に加えて、Ubuntu Server 14.04LTSでも試しています。
Ubuntuのrootボリュームグループ名はホスト名によって変るので、hostnameコマンドで確認して置き換えて下さい。

パーティション作成

まずは、認識されているHDDを探します。
デバイス名はsd?(もしくはhd?)を探します。

CentOS
$ ll /dev/sd*
brw-rw----. 1 root disk 8,  0  2月 00 00:00 /dev/sda
brw-rw----. 1 root disk 8,  1  2月 00 00:00 /dev/sda1
brw-rw----. 1 root disk 8,  2  2月 00 00:00 /dev/sda2
brw-rw----. 1 root disk 8, 16  2月 00 00:00 /dev/sdb
Ubuntu
$ ll /dev/sd*
brw-rw----. 1 root disk 8,  0  2月 00 00:00 /dev/sda
brw-rw----. 1 root disk 8,  1  2月 00 00:00 /dev/sda1
brw-rw----. 1 root disk 8,  2  2月 00 00:00 /dev/sda2
brw-rw----. 1 root disk 8,  5  2月 00 00:00 /dev/sda5
brw-rw----. 1 root disk 8, 16  2月 00 00:00 /dev/sdb

まだパーティションが作成されていない /dev/sdb が見つかりました。
fdiskを使用して、/dev/sdb に新しいパーティションを追加します。

CentOS
$ sudo fdisk /dev/sdb
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x1c46cb44.

コマンド (m でヘルプ):
Ubuntu
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf591a1f7.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help):

pコマンドで現在のパーティションを確認します。

fdisk
コマンド (m でヘルプ): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x1c46cb44

デバイス ブート      始点        終点     ブロック   Id  システム

初期状態なので何もありません。

nコマンド新しいプライマリ・パーティションを作成します。
パーティション番号は1、2GiB全て割当てるために、セクターは全て(2048-4194303)使用します。

※2048開始なのは、あらゆるRAIDでストライプ・サイズの倍数になっているから、セクタ位置合せで都合がよいのだとか。

fdisk
コマンド (m でヘルプ): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
パーティション番号 (1-4, default 1): 1
最初 sector (2048-4194303, 初期値 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, 初期値 4194303): 4194303
Partition 1 of type Linux and of size 2 GiB is set

コマンド (m でヘルプ): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O サイズ (最小 / 推奨): 512 バイト / 512 バイト
Disk label type: dos
ディスク識別子: 0x1c46cb44

デバイス ブート      始点        終点     ブロック   Id  システム
/dev/sdb1            2048     4194303     2096128   83  Linux

新しいパーティション /dev/sdb1 が作成されました。
wコマンドでパーティションを書込みます。

fdisk
コマンド (m でヘルプ): w
パーティションテーブルは変更されました!

ioctl() を呼び出してパーティションテーブルを再読込みします。
ディスクを同期しています。

ファイルシステムからも /dev/sdb1 が確認できます。

CentOS
$ ll /dev/sd*
brw-rw----. 1 root disk 8,  0  2月 00 00:00 /dev/sda
brw-rw----. 1 root disk 8,  1  2月 00 00:00 /dev/sda1
brw-rw----. 1 root disk 8,  2  2月 00 00:00 /dev/sda2
brw-rw----. 1 root disk 8, 16  2月 00 00:00 /dev/sdb
brw-rw----. 1 root disk 8, 17  2月 00 00:00 /dev/sdb1
Ubuntu
$ ll /dev/sd*
brw-rw----. 1 root disk 8,  0  2月 00 00:00 /dev/sda
brw-rw----. 1 root disk 8,  1  2月 00 00:00 /dev/sda1
brw-rw----. 1 root disk 8,  2  2月 00 00:00 /dev/sda2
brw-rw----. 1 root disk 8,  5  2月 00 00:00 /dev/sda5
brw-rw----. 1 root disk 8, 16  2月 00 00:00 /dev/sdb
brw-rw----. 1 root disk 8, 17  2月 00 00:00 /dev/sdb1

ボリューム拡張

冒頭のdfコマンドで確認した通り、/dev/mapper/centos-root がルートにマウントされています。(Ubuntuは /dev/mapper/ホスト名--vg-root)
ボリュームグループ centos に物理ボリュームとして/dev/sdb1を追加して、論理ボリュームrootの容量を拡張します。

ボリュームグループ拡張

vgdisplayコマンドで現在のボリュームグループを確認します。

CentOS
$ sudo vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1.51 GiB
  PE Size               4.00 MiB
  Total PE              386
  Alloc PE / Size       386 / 1.51 GiB
  Free  PE / Size       0 / 0
  VG UUID               UPHnYU-zXwc-1L3w-LxQu-1uEP-K1wO-vCUXwc
Ubuntu
$ sudo vgdisplay
  --- Volume group ---
  VG Name               ホスト名-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               7.76 GiB
  PE Size               4.00 MiB
  Total PE              1986
  Alloc PE / Size       1978 / 7.73 GiB
  Free  PE / Size       8 / 32.00 MiB
  VG UUID               JkiHt9-3wvZ-dJYz-qhPp-xGxd-QRWq-5HsliN

VG Sizeが1.51GiBになっています。(Ubuntuは7.76GiB)

pvcreateコマンドでパーティション /dev/sdb1 の物理ボリュームを作成して、ボリュームグループ centos へ追加します。(Ubuntuはボリュームグループ ホスト名-vg)

CentOS
$ sudo pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
$ sudo vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
$ sudo vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               3.50 GiB
  PE Size               4.00 MiB
  Total PE              897
  Alloc PE / Size       386 / 1.51 GiB
  Free  PE / Size       511 / 2.00 GiB
  VG UUID               UPHnYU-zXwc-1L3w-LxQu-1uEP-K1wO-vCUXwc
Ubuntu
$ sudo pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
$ sudo vgextend ホスト名-vg /dev/sdb1
  Volume group "ホスト名-vg" successfully extended
$ sudo vgdisplay
  --- Volume group ---
  VG Name               ホスト名-vg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               9.75 GiB
  PE Size               4.00 MiB
  Total PE              2497
  Alloc PE / Size       1978 / 7.73 GiB
  Free  PE / Size       519 / 2.03 GiB
  VG UUID               JkiHt9-3wvZ-dJYz-qhPp-xGxd-QRWq-5HsliN

Free Sizeが2.00GiBに増えて、VG Sizeが3.50GiBになりました。(
Ubuntuは9.75GiBに)

この2.00GiBをすべて論理ボリューム root に割当てます。

論理ボリューム拡張

lvdisplayコマンドで現在のLV SizeとLV Pathを確認します。

CentOS
$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                T2yPcS-nfFm-QNQF-NXK9-IiOM-ZUz4-c4GY3V
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-02-00 00:00:00 +0900
  LV Status              available
  # open                 2
  LV Size                204.00 MiB
  Current LE             51
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                NhZmMs-2PF4-Izbl-glMg-Pm26-GPh0-cPnP0s
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-02-00 00:00:00 +0900
  LV Status              available
  # open                 1
  LV Size                1.31 GiB
  Current LE             335
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
Ubuntu
$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ホスト名-vg/root
  LV Name                root
  VG Name                ホスト名-vg
  LV UUID                wfJe7W-SFJC-y3oK-2Qz2-fgR5-n8D2-8Jvb1a
  LV Write Access        read/write
  LV Creation host, time ホスト名, 2014-09-00 00:00:00 +0900
  LV Status              available
  # open                 1
  LV Size                7.23 GiB
  Current LE             1851
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Path                /dev/ホスト名-vg/swap_1
  LV Name                swap_1
  VG Name                ホスト名-vg
  LV UUID                5lMqR5-0r5b-WfRz-21Rs-sF4z-foms-H3VYmY
  LV Write Access        read/write
  LV Creation host, time ホスト名, 2014-09-00 00:00:00 +0900
  LV Status              available
  # open                 2
  LV Size                508.00 MiB
  Current LE             127
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

lvextendコマンドでLV Path /dev/centos/root を指定し、論理ボリュームrootを拡張します(CentOSで、なんか2Gを指定したらエクステントが一個足らんって怒られたので、1.99Gにしました)

CentOS
$ sudo lvextend -L+1.99G /dev/centos/root
  Rounding size to boundary between physical extents: 1.99 GiB
  Extending logical volume root to 3.30 GiB
  Logical volume root successfully resized
$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                T2yPcS-nfFm-QNQF-NXK9-IiOM-ZUz4-c4GY3V
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-02-19 10:44:47 +0900
  LV Status              available
  # open                 2
  LV Size                204.00 MiB
  Current LE             51
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                NhZmMs-2PF4-Izbl-glMg-Pm26-GPh0-cPnP0s
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-02-19 10:44:47 +0900
  LV Status              available
  # open                 1
  LV Size                3.30 GiB
  Current LE             845
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
Ubuntu
$ sudo lvextend -L+2G /dev/ホスト名-vg/root
  Extending logical volume root to 9.23 GiB
  Logical volume root successfully resized

rootのLV Sizeが3.30GiBに増えました。(Ubuntuは9.23GiB)

ファイルシステム拡張(xfs)

最後にファイルシステムを拡張します。
CentOS7の場合、デフォルトのファイルシステムはxfsになっています。(Ubuntuはext4)
df -Tでファイルシステムタイプを確認してみます。

CentOS
$ df -T
ファイルシス            タイプ   1K-ブロック    使用 使用可 使用% マウント位置
/dev/mapper/centos-root xfs          1361920 1306176  55744   96% /
devtmpfs                devtmpfs      242508       0 242508    0% /dev
tmpfs                   tmpfs         251028       0 251028    0% /dev/shm
tmpfs                   tmpfs         251028    4440 246588    2% /run
tmpfs                   tmpfs         251028       0 251028    0% /sys/fs/cgroup
/dev/sda1               xfs           508588  136960 371628   27% /boot
Ubuntu
$ df -T
Filesystem                    Type     1K-blocks    Used Available Use% Mounted on
/dev/mapper/ubuntu01--vg-root ext4       7331536 1841792   5094276  27% /
none                          tmpfs            4       0         4   0% /sys/fs/cgroup
udev                          devtmpfs    239652      12    239640   1% /dev
tmpfs                         tmpfs        50180     436     49744   1% /run
none                          tmpfs         5120       0      5120   0% /run/lock
none                          tmpfs       250892       0    250892   0% /run/shm
none                          tmpfs       102400       0    102400   0% /run/user
/dev/sda1                     ext2        240972   37074    191457  17% /boot

xfsの拡張はxfs_growfsコマンドを使用します。

CentOS
$ sudo xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=85760 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=343040, 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 343040 to 865280

$ df
ファイルシス            1K-ブロック    使用  使用可 使用% マウント位置
/dev/mapper/centos-root     3450880 1306424 2144456   38% /
devtmpfs                     242508       0  242508    0% /dev
tmpfs                        251028       0  251028    0% /dev/shm
tmpfs                        251028    4408  246620    2% /run
tmpfs                        251028       0  251028    0% /sys/fs/cgroup
/dev/sda1                    508588  136960  371628   27% /boot

無事にルートの空きが増えて、使用率が38%へ落ちました。

ext4の拡張はresize2fsコマンドを使用します。

Ubuntu
$ sudo resize2fs /dev/ホスト名-vg/root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/ubuntu01-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/ubuntu01-vg/root is now 2419712 blocks long.
$ df
Filesystem                    1K-blocks    Used Available Use% Mounted on
/dev/mapper/ホスト名--vg-root   9395856 1841792   7075532  21% /
none                                  4       0         4   0% /sys/fs/cgroup
udev                             239652      12    239640   1% /dev
tmpfs                             50180     436     49744   1% /run
none                               5120       0      5120   0% /run/lock
none                             250892       0    250892   0% /run/shm
none                             102400       0    102400   0% /run/user
/dev/sda1                        240972   37074    191457  17% /boot
150
161
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
150
161