LoginSignup
9
16

More than 3 years have passed since last update.

VagrantのCentOSのディスク容量を増やす手順

Posted at

概要

私の周りでもローカル開発環境がvagrantからDockerに移り変わろうとしていますが
まだまだ過去に作成したvagrantのローカル開発環境は現役で活用しています。

さらに活用するためにディスク容量を増やそうと思いvagrant-disksizeプラグインを導入したのですが、実際にディスクが利用できるまで手順が複雑だったので備忘録を残しておきます。

環境

ホストOS:Windows 10 Home
ゲストOS:CentOS 7.2

前提条件

プロバイダはVirtual Boxを前提としています

手順

プラグインのインストール

> vagrant plugin install vagrant-disksize

設定の追加

Vagrantfileに以下の設定を追記

Vagrantfile
config.disksize.size = '100GB'

vagrantを起動してからVirtual Boxでスペックをディスクストレージのサイズを確認すると100GBとなっています。

Virtual Box.png

しかし、dfコマンドを実行してみると8GBしか認識していません。

df -lhの実行例
$ df -lh
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  8.3G  4.3G  4.1G  52% /
devtmpfs                 299M     0  299M   0% /dev
tmpfs                    308M     0  308M   0% /dev/shm
tmpfs                    308M  4.4M  304M   2% /run
tmpfs                    308M     0  308M   0% /sys/fs/cgroup
/dev/sda1                497M  164M  333M  33% /boot

パーティションの作成

パーティションテーブルの確認

まずfdiskコマンドで現在のパーティションテーブルを確認します。
パーティションテーブルを見ると/dev/sdaが100GBになっているのがわかります。

fdisk -lの実行例
$ sudo fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0000ca5e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20479999     9726976   8e  Linux LVM

Disk /dev/mapper/centos-root: 8866 MB, 8866758656 bytes, 17317888 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 /dev/mapper/centos-swap: 1048 MB, 1048576000 bytes, 2048000 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

パーティションの作成

fdiskコマンドを実行すると対話式でパーティションの設定ができます。
まずは「n」コマンドでパーティションを作成します。

$ 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): 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 (20480000-209715199, default 20480000):
Using default value 20480000
Last sector, +sectors or +size{K,M,G} (20480000-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 90.2 GiB is set

パーティションタイプの変更

続けて「t」コマンドでパーティションタイプを変更します。

/dev/sda2を拡張するので「Hex code」にはパーティションテーブルを確認したときの/dev/sda2の「Id」に記載されたコードを入力します。

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

ここまで実行したらvagrantを再起動します

パーティションテーブルの再確認

再びfdiskコマンドでパーティションテーブルを表示すると/dev/sda3ができているのがわかります。

fdisk -lの実行例
sudo fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0000ca5e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20479999     9726976   8e  Linux LVM
/dev/sda3        20480000   209715199    94617600   8e  Linux LVM

Disk /dev/mapper/centos-root: 8866 MB, 8866758656 bytes, 17317888 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 /dev/mapper/centos-swap: 1048 MB, 1048576000 bytes, 2048000 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

しかし、まだルートディレクトリは8GBしか認識していません。

ボリュームの割り当て

物理ボリュームの作成

$ sudo pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

物理ボリュームの確認

作成した物理ボリュームを確認します

pvdisplayの実行例

$ sudo pvdisplay

  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               9.28 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              2374
  Free PE               10
  Allocated PE          2364
  PV UUID               grTv3L-mzhC-TS8a-z5ln-y6rS-3kJi-qX82hr

  "/dev/sda3" is a new physical volume of "90.23 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               90.23 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               JC26oa-WjHk-yJrm-Ky8Y-d4RE-1EUp-pC9VAH

物理ボリュームをボリュームグループに追加

ボリュームグループ名はpvdisplayで確認した/dev/sda2の「VG Name」を指定します。

$ sudo vgextend centos /dev/sda3
  Volume group "centos" successfully extended

論理ボリュームを拡張

ルートディレクトリの論理ボリュームに空き領域すべてを割り当てます

$ sudo lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 8.26 GiB (2114 extents) to 98.53 GiB (25223 extents).
  Logical volume root successfully resized.

XFSファイルシステムの拡張

CentOS7のファイルシステムはXFSなのでファイルシステムの拡張に xfs_growfs コマンドを使います。

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

最後に

再度dfコマンドでディスク容量を確認すると100GB割り当てられたのが確認できます。

df -lhの実行例
$ df -lh
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   99G  4.3G   95G   5% /
devtmpfs                 299M     0  299M   0% /dev
tmpfs                    308M     0  308M   0% /dev/shm
tmpfs                    308M  4.4M  304M   2% /run
tmpfs                    308M     0  308M   0% /sys/fs/cgroup
/dev/sda1                497M  164M  333M  33% /boot

参考記事

https://qiita.com/TsutomuNakamura/items/93c6333c8dd32aeb197a
https://qiita.com/fetaro/items/d7dc74262633ba474bc8

9
16
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
9
16