月別: 2017年10月

WindowsにインストールしたVagrantゲストのストレージ容量を増やす

Linuxのvagrantに対してはやったことあったけどWindowsでやったことなかったのでメモ。

ちなみにCドライブが足りなくなりそうになったので事前に以下の記事を頼りにVirtualBoxとVagrantをDドライブに引っ越し。

WindowsでVagrant環境を作ろう!容量足りない場合は作業フォルダ変更で対応

※引っ越しはいろいろトラブルがでるので素直に入れなおしたほうがいいかもしれない。

以下ストレージの拡張手順(Linuxでやった時とほぼ同じだけどPowerShellが辛かった)

【Windows側の作業】

■作業フォルダ確認(拡張対象ゲストのストレージファイルvmdkの確認)

PS D:\work\VirtualBox VMs\Docker_default_1507875958159_4618> dir


    ディレクトリ: D:\work\VirtualBox VMs\Docker_default_1507875958159_4618


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2017/10/13     15:31                Logs
-a----       2017/10/13     15:39     2050097152 box-disk1.vmdk
-a----       2017/10/13     15:39           4543 Docker_default_1507875958159_4618.vbox
-a----       2017/10/13     15:31           4543 Docker_default_1507875958159_4618.vbox-prev

■vmdkをvdiに変換

PS D:\work\VirtualBox VMs\Docker_default_1507875958159_4618> & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' clonehd box-disk1.vmdk box-disk1.vdi --format VDI
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'VDI'. UUID: dc5bade4-95e3-41b6-ba02-f42ead82b5b8
PS D:\work\VirtualBox VMs\Docker_default_1507875958159_4618> dir


    ディレクトリ: D:\work\VirtualBox VMs\Docker_default_1507875958159_4618


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2017/10/13     15:31                Logs
-a----       2017/10/13     15:51     2065694720 box-disk1.vdi
-a----       2017/10/13     15:39     2050097152 box-disk1.vmdk
-a----       2017/10/13     15:51           4543 Docker_default_1507875958159_4618.vbox
-a----       2017/10/13     15:39           4543 Docker_default_1507875958159_4618.vbox-prev

■100GBにvdiのサイズ変更(100 * 1024MB = 102400)

PS D:\work\VirtualBox VMs\Docker_default_1507875958159_4618> & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' modifyhd box-disk1.vdi --resize 102400
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
PS D:\work\VirtualBox VMs\Docker_default_1507875958159_4618>

■できたvdiの確認

PS D:\work\VirtualBox VMs\Docker_default_1507875958159_4618> & 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' showhdinfo box-disk1.vdi
UUID:           dc5bade4-95e3-41b6-ba02-f42ead82b5b8
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       D:\work\VirtualBox VMs\Docker_default_1507875958159_4618\box-disk1.vdi
Storage format: VDI
Format variant: dynamic default
Capacity:       102400 MBytes
Size on disk:   1970 MBytes
Encryption:     disabled
PS D:\work\VirtualBox VMs\Docker_default_1507875958159_4618>

■GUIでvdiの追加とvmdkの削除

■HDDをVDIに差し替えたらゲストを起動

【ゲスト側の作業】

■fdiskで/dev/sdaの容量が増えている事を確認する。

[root@localhost ~]# 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: 0x000944b9

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

Disk /dev/mapper/centos-root: 8996 MB, 8996782080 bytes, 17571840 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: 1065 MB, 1065353216 bytes, 2080768 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

■パーティションを追加

[root@localhost ~]# 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): 
Using default response p
Partition number (3,4, default 3): 3
First sector (20766720-209715199, default 20766720): 
Using default value 20766720
Last sector, +sectors or +size{K,M,G} (20766720-209715199, default 209715199): 
Using default value 209715199
Partition 3 of type Linux and of size 90.1 GiB is set

Command (m for help): p

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: 0x000944b9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20766719     9870336   8e  Linux LVM
/dev/sda3        20766720   209715199    94474240   83  Linux

「/dev/sda3」が追加された

■/dev/sda3のシステムをLinux LVMにする

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'

Command (m for help): p

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: 0x000944b9

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20766719     9870336   8e  Linux LVM
/dev/sda3        20766720   209715199    94474240   8e  Linux LVM

■反映して終了する

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.

■ゲストの再起動

# shutdown -r now

■Physical Volumeの状態を確認

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               9.41 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2409
  Free PE               10
  Allocated PE          2399
  PV UUID               P0ooC6-nfdy-vZkK-cniC-MG1G-2dIn-t1115F

■pvcreateコマンドでPhysical Volumeを作成する。

[root@localhost ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

■作成内容を確認。

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               9.41 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2409
  Free PE               10
  Allocated PE          2399
  PV UUID               P0ooC6-nfdy-vZkK-cniC-MG1G-2dIn-t1115F
   
  "/dev/sda3" is a new physical volume of "<90.10 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               <90.10 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               54AaA1-O1Wt-IR5v-dq03-8ahU-et00-tIgoZI

■Volume GroupにPhysical Volumeを追加する

[root@localhost ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended

■Volume Groupの確認

[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  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               99.50 GiB
  PE Size               4.00 MiB
  Total PE              25473
  Alloc PE / Size       2399 / 9.37 GiB
  Free  PE / Size       23074 / 90.13 GiB
  VG UUID               WvQvjv-xDJK-VV4R-tIf0-tXwC-GXMB-H9V2pw

■Logical Volumeの容量を増やす
増やしたいマウントポイントのLogical Volumeをdfコマンドで確認。

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  8.4G  1.4G  7.0G  17% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.4M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                497M  109M  389M  22% /boot
tmpfs                    380M     0  380M   0% /run/user/1001

今回の対象は「/dev/mapper/centos-root」

■lvdisplayコマンドでLogical Volumeの詳細を確認。

[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                CpIsLo-dOUb-SSCf-Xql9-l6Kv-3jfu-W0Lxs8
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-14 05:07:53 +0000
  LV Status              available
  # open                 2
  LV Size                1016.00 MiB
  Current LE             254
  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                5g6VL9-tahk-ioUm-sPyh-F7Ws-RQF8-SOw6Th
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-14 05:07:54 +0000
  LV Status              available
  # open                 1
  LV Size                <8.38 GiB
  Current LE             2145
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

■Logical Volumeのサイズを拡張。(計算しなくてもいいバージョン)

[root@localhost ~]# lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from <8.38 GiB (2145 extents) to 98.51 GiB (25219 extents).
  Logical volume centos/root successfully resized.

■Logical Volumeの確認。

[root@localhost ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                CpIsLo-dOUb-SSCf-Xql9-l6Kv-3jfu-W0Lxs8
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-14 05:07:53 +0000
  LV Status              available
  # open                 2
  LV Size                1016.00 MiB
  Current LE             254
  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                5g6VL9-tahk-ioUm-sPyh-F7Ws-RQF8-SOw6Th
  LV Write Access        read/write
  LV Creation host, time localhost, 2015-07-14 05:07:54 +0000
  LV Status              available
  # open                 1
  LV Size                98.51 GiB
  Current LE             25219
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

【ここからファイルシステムのリサイズ】
■CentOS7でファイルシステムがxfsだったのでresize2fsの代わりにxfs_growfsを使用

[root@localhost ~]# xfs_growfs /dev/centos/root 
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=549120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2196480, 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 2196480 to 25824256

ストレージの拡張完了

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   99G  1.4G   98G   2% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.4M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1                497M  109M  389M  22% /boot
tmpfs                    380M     0  380M   0% /run/user/1001

これでDockerコンテナ沢山使える。