VirtualBox の HDD領域を拡張する(1) うまくいかない。。。

VirtualBox の仮想OS を使っているうちに
思ったよりもHDD使っちゃうことって多いですよね。

空き領域たりない!ってなった時 新たなディスクを作って
適当なところにマウントするのもいいですが
今の領域を増やせるならそれが一番いいですよね。

ということで調べてみました。


まず拡張したい仮想OSのvdiファイルを見つけます。
設定を見れば場所がわかりますね。


次に VirtualBox の付属ツール VBoxManage を使ってvdi ファイルを拡張します
VBoxManage はデフォルトでのインストールであれば C:\Program Files\Oracle\VirtualBox あたりにあると思われます。

# os.vdi を 20G に拡張したい場合
VBoxManage modifyhd "os.vdi" --resize 20480
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

次に仮想OSにログインしての作業です
ここでは Ubuntu を使ってます。

まず lsbsk コマンドでパーティションの状態を確認します

root@ubuntu:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
|-sda1   8:1    0    5G  0 part /
|-sda2   8:2    0    1K  0 part
`-sda5   8:5    0    3G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom

この例では、sda デバイスに 20G の使用可能なストレージがあり、5G, 1k 3G のパーティションが含まれています。

parted というコマンドを使ってパーティションを編集します。
入っていない場合はインストールします

root@ubuntu:~# apt-get install parted

さて、おおきくしてみましょう

※ sda デバイスを編集します
root@ubuntu:~# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) 

※パーティション測定単位をセクターに変更し状態を表示します
(parted) unit s
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 41943040s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start      End        Size       Type      File system     Flags
 1      2048s      10485759s  10483712s  primary   ext4            boot
 2      10487806s  16775167s  6287362s   extended
 5      10487808s  16775167s  6287360s   logical   linux-swap(v1)

※primaryを大きくしたい。
※とりあえずよくわかんないけど消してみます
(parted) rm 1

※Volumeの拡張されたprimaryをつくってみる
(parted) mkpart Linux 2048s 100%
parted: invalid token: Linux
Partition type?  primary/logical? primary
File system type?  [ext2]? ext4
Start? 2048s
End? 100%
Warning: You requested a partition from 2048s to 41943039s (sectors
2048..41943039).
The closest location we can manage is 2048s to 10487805s (sectors
2048..10487805).
Is this still acceptable to you?
Yes/No? yes

※結果確認
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 41943040s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start      End        Size       Type      File system     Flags
 1      2048s      10487805s  10485758s  primary   ext4            lba
 2      10487806s  16775167s  6287362s   extended
 5      10487808s  16775167s  6287360s   logical   linux-swap(v1)

※ブートフラグ消えたので修復
(parted) set 1 boot on
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 41943040s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start      End        Size       Type      File system     Flags
 1      2048s      10487805s  10485758s  primary   ext4            boot, lba
 2      10487806s  16775167s  6287362s   extended
 5      10487808s  16775167s  6287360s   logical   linux-swap(v1)

※おわり!
(parted) quit
Information: You may need to update /etc/fstab.

アンマウントできないから次回起動時に強制的にfsckをかける

root@ubuntu:~# touch /forcefsck
root@ubuntu:~# shutdown -r now

ん?なんかおかしい。。。

root@ubuntu:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
|-sda1   8:1    0    5G  0 part /
`-sda5   8:5    0    3G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom

root@ubuntu:~# df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       5.2G  864M  4.1G  18% /
none            4.1k     0  4.1k   0% /sys/fs/cgroup
udev            1.6G  4.1k  1.6G   1% /dev
tmpfs           316M  287k  316M   1% /run
none            5.3M     0  5.3M   0% /run/lock
none            1.6G     0  1.6G   0% /run/shm
none            105M     0  105M   0% /run/userroot

参考
http://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/storage_expand_partition.html