LoginSignup
1
0

More than 3 years have passed since last update.

VirtualBox上のLinux(CentOS7)に仮想ディスクを追加して空き容量を増やす方法

Posted at

VirtualBox上のLinux(CentOS7)に仮想ディスクをして空き容量を増やす方法を紹介します。

CentOS7はデフォルトでLVM(Logical Volume Manager)でディスクが管理されています。
LVM(Logical Volume Manager)は、ディスク(ボリューム)管理をするツールで、複数のハードディスクやパーティションにまたがった記憶領域を一つのディスクのように扱うことのできるディスク管理機能です。
(LVMについては「【Linuxの基礎知識】LVMとは?LVMを理解しよう!」のページの説明がわかりやすいです。)

LVMの構成要素として、ざっくりと以下の3つがあります。

  • PV(Physical volume):LVMに登録する物理的なディスク領域です。
  • VG(Volume group):PVを集めたものです。複数のPVをひとつのディスク領域の様に扱うことが可能です。
  • LV(Logical volume):VGから割り当てたディスク領域で作成した論理的なパーティションです。これにファイルシステムを作成しマウントして利用可能です。

ここでは
1. VirtualBoxで仮想ディスクを作成
2. パーティション作成
3. PV(Physical volume)作成
4. 作成したPV(Physical volume)をVG(Volume group)に追加
5. VGから既存のLV(Logical volume)を拡張
6. ファイルシステム(xfs)の拡張
の方法で「/」配下の空き容量を増やします。
(他の方法としてLVを新規に作成し、ファイルシステムにマウントさせるといった方法もあります。)

環境

  • OS:CentOS Linux release 7.7.1908
[root@CENTOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@CENTOS7 ~]#

手順

1. VirtualBoxで仮想ディスクを作成

(1)「VirtualBox」を起動し、対象のOS(ここでは「CentOS-7-1908」)を選択し、「設定」をクリックします。
01.png

(2)「ストレージ」を選択し、「コントローラー:SATA」を選択します。
02.png

(3)「ハードディスクの追加」のアイコンをクリックします。
00.png

(4)「作成」をクリックします。
03.png

(5)「VDI (VirtualBox Disk Image)」を選択し、「次へ」をクリックします。
04.png

(6)「固定サイズ」を選択し、「次へ」をクリックします。
05.png

(7)「32.00GB」を入力し、「作成」をクリックします。
06.png

作成が完了するまでしばらく待ちます。
07.png

(8)追加されたハードディスクを選択し、「選択」をクリックします。
08.png

(9)ストレージデバイスの「コントローラー:SATA」にハードディスクが追加されていることを確認し、「OK」をクリックします。
09.png

(10)対象のOS(ここでは「CentOS-7-1908」)を起動します。
10.png

2. パーティション作成

以下のコマンドで接続されているディスク一覧を参照できます。

lsscsi

実行結果
[root@CENTOS7 ~]# lsscsi
[0:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr0
[1:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr1
[2:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sda
[3:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sdb
[root@CENTOS7 ~]#

以下のコマンドで認識されているディスクを参照します。

ls -l /dev/sd*

実行結果
[root@CENTOS7 ~]# ls -l /dev/sd*
brw-rw---- 1 root disk 8,  0  9月 29 22:22 /dev/sda
brw-rw---- 1 root disk 8,  1  9月 29 22:22 /dev/sda1
brw-rw---- 1 root disk 8,  2  9月 29 22:22 /dev/sda2
brw-rw---- 1 root disk 8, 16  9月 29 22:22 /dev/sdb
[root@CENTOS7 ~]#

fdiskコマンドで「/dev/sdb」に新しいパーティションを追加します。

fdisk /dev/sdb

実行結果
[root@CENTOS7 ~]# 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 0x32eaedf2.

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

mを入力し、ヘルプを参照します。

実行結果
コマンド (m でヘルプ): m
コマンドの動作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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

pを入力し、現在のパーティションを確認します。

実行結果
コマンド (m でヘルプ): p

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

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

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

初期状態なのでパーティションはありません。

nを入力し、新しいパーティションを作成します。

実行結果
コマンド (m でヘルプ): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):

pを入力し、プライマリ・パーティションを作成します。

実行結果
Select (default p): p
パーティション番号 (1-4, default 1):

パーティション番号にデフォルトの1を入力します。

実行結果
パーティション番号 (1-4, default 1): 1
最初 sector (2048-67108863, 初期値 2048):

最初のsectorに2048を入力します。

実行結果
最初 sector (2048-67108863, 初期値 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-67108863, 初期値 67108863):

Last sectorにフルの67108863を入力します。

実行結果
Last sector, +sectors or +size{K,M,G} (2048-67108863, 初期値 67108863): 67108863
Partition 1 of type Linux and of size 32 GiB is set

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

pを入力し、再度パーティションを確認します。

実行結果
コマンド (m でヘルプ): p

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

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

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

新しいパーティション「/dev/sdb1」が作成されていることを確認します。

wを入力し、パーティションを書き込みます。

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

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

ファイルシステムに「/dev/sdb1」が追加されていることを確認します。

ls -l /dev/sd*

実行結果
[root@CENTOS7 ~]# ls -l /dev/sd*
brw-rw---- 1 root disk 8,  0  9月 29 22:22 /dev/sda
brw-rw---- 1 root disk 8,  1  9月 29 22:22 /dev/sda1
brw-rw---- 1 root disk 8,  2  9月 29 22:22 /dev/sda2
brw-rw---- 1 root disk 8, 16  9月 29 23:46 /dev/sdb
brw-rw---- 1 root disk 8, 17  9月 29 23:46 /dev/sdb1
[root@CENTOS7 ~]#

3. PV(Physical volume)作成

pvcreateコマンドでパーティション「/dev/sdb1」の物理ボリュームを作成します。

pvcreate /dev/sdb1

実行結果
[root@CENTOS7 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@CENTOS7 ~]#

pvdisplayコマンドでPV(Physical volume)が作成されていることを確認します。

pvdisplay

実行結果
[root@CENTOS7 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <15.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               0
  Allocated PE          3839
  PV UUID               bKwnMo-BTE9-BWlx-iXu2-cC6H-Dj0r-8bOowr

  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               centos
  PV Size               <32.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              8191
  Free PE               0
  Allocated PE          8191
  PV UUID               874u5U-1pDD-OpBx-dswz-n4fa-l05i-B4Cf2K

[root@CENTOS7 ~]#

「/dev/sdb1」が作成されていることが確認できます。

4. 作成したPV(Physical volume)をVG(Volume group)に追加

vgdisplayコマンドでVG(Volume group)の名前(VG Name)を確認します。

vgdisplay

実行結果
[root@CENTOS7 ~]# 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               <15.00 GiB
  PE Size               4.00 MiB
  Total PE              3839
  Alloc PE / Size       3839 / <15.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               DpKrR4-ksQz-HctA-1NeF-SaVx-7bm6-FcC4Di

[root@CENTOS7 ~]#

VG Nameは「centos」となっていることがわかりました。

vgextendコマンドでPV(/dev/sdb1)をVG(centos)へ追加します。

vgextend centos /dev/sdb1

実行結果
[root@CENTOS7 ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended
[root@CENTOS7 ~]#

vgdisplayコマンドで再度VGを確認します。

実行結果
[root@CENTOS7 ~]# 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               46.99 GiB
  PE Size               4.00 MiB
  Total PE              12030
  Alloc PE / Size       3839 / <15.00 GiB
  Free  PE / Size       8191 / <32.00 GiB
  VG UUID               DpKrR4-ksQz-HctA-1NeF-SaVx-7bm6-FcC4Di

[root@CENTOS7 ~]#

VG Sizeが「46.99 GiB」に増加していることを確認します。

5. VGから既存のLV(Logical volume)を拡張

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

lvdisplay

実行結果
[root@CENTOS7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                hzKez7-P7zQ-eGFj-ByQd-6I9z-Z8Fv-EXBirC
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 2
  LV Size                1.60 GiB
  Current LE             410
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ZCpYbq-N8Jm-Wlei-iN5X-TVYs-CEFc-XL9XvV
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 1
  LV Size                13.39 GiB
  Current LE             3429
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@CENTOS7 ~]#

lvextendコマンドでrootのLV(LV Path:/dev/centos/root、LV Name:root)を拡張します。

lvextend -l +100%FREE /dev/centos/root

実行結果
[root@CENTOS7 ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 45.39 GiB (11620 extents).
  Logical volume centos/root successfully resized.
[root@CENTOS7 ~]#

lvdisplayコマンドで再度LVを確認します。

実行結果
[root@CENTOS7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                hzKez7-P7zQ-eGFj-ByQd-6I9z-Z8Fv-EXBirC
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 2
  LV Size                1.60 GiB
  Current LE             410
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ZCpYbq-N8Jm-Wlei-iN5X-TVYs-CEFc-XL9XvV
  LV Write Access        read/write
  LV Creation host, time centos7, 2019-10-12 17:03:32 +0900
  LV Status              available
  # open                 1
  LV Size                45.39 GiB
  Current LE             11620
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@CENTOS7 ~]#

「root」の「LV Size」が45.39 GiBと増えています。

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

df -Tでファイルシステムタイプを確認してみます。

df -T

実行結果
[root@CENTOS7 ~]# df -T
ファイルシス            タイプ   1K-ブロック     使用  使用可 使用% マウント位置
devtmpfs                devtmpfs      490768        0  490768    0% /dev
tmpfs                   tmpfs         507380        0  507380    0% /dev/shm
tmpfs                   tmpfs         507380     7588  499792    2% /run
tmpfs                   tmpfs         507380        0  507380    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs         14034944 12479804 1555140   89% /
/dev/sda1               xfs          1038336   218388  819948   22% /boot
tmpfs                   tmpfs         101480       12  101468    1% /run/user/42
tmpfs                   tmpfs         101480        0  101480    0% /run/user/0
[root@CENTOS7 ~]#

xfs_growfsコマンドを使用してマウントポイントが「/」のファイルシステム(xfs)を拡張します。

xfs_growfs /

実行結果
[root@CENTOS7 ~]# xfs_growfs /
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=877824 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3511296, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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 3511296 to 11898880
[root@CENTOS7 ~]#

再度df -Tでファイルシステムタイプを確認してみます。

実行結果
[root@CENTOS7 ~]# df -T
ファイルシス            タイプ   1K-ブロック     使用   使用可 使用% マウント位置
devtmpfs                devtmpfs      490768        0   490768    0% /dev
tmpfs                   tmpfs         507380        0   507380    0% /dev/shm
tmpfs                   tmpfs         507380     7588   499792    2% /run
tmpfs                   tmpfs         507380        0   507380    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs         47585280 12480312 35104968   27% /
/dev/sda1               xfs          1038336   218388   819948   22% /boot
tmpfs                   tmpfs         101480       12   101468    1% /run/user/42
tmpfs                   tmpfs         101480        0   101480    0% /run/user/0
[root@CENTOS7 ~]#

マウントポイント「/」のサイズが増加し、使用率が減少しています。

参考

【Linuxの基礎知識】LVMとは?LVMを理解しよう!
CentOS7のLVMを使ってHDD追加&パーティションの拡張をしてみる。(メモ)


以上

1
0
0

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
1
0