VIrtualBoxにCLIを使って仮想環境を構築

はじめに

VIrtualBoxLinuxの環境をCLIで構築することができるか、確認を行う。

環境

構築

isoのインストール

https://old-releases.ubuntu.com/releases/jammy/ からUbuntuのisoをダウンロードする。 ubuntu-22.04-live-server-amd64.iso をダウンロードした。

VBoxManage を使った環境の構築

作成する環境は以下とする。

- 名前:Ubuntu22_1
- memory:2GB
- Disk:10GB

適当なフォルダに移動する。

cd C:\Users\<username>\VirtualBox VMs

まず、新しい仮想マシンを作成する。

VBoxManage createvm --name "Ubuntu22_1" --register
cd Ubuntu22_1

次に、仮想マシンのハードウェア設定を行う。以下のコマンドは、RAMを1024MB、VRAMを128MBに設定する。

VBoxManage modifyvm "Ubuntu22_1" --memory 1024 --vram 128

仮想ハードディスクを作成する。以下のコマンドは、10GBのハードディスクを作成する。

VBoxManage createhd --filename "Ubuntu22_1.vdi" --size 10000

作成したハードディスクを仮想マシンに接続する。

VBoxManage storagectl "Ubuntu22_1" --name "SATA Controller" --add sata --controller IntelAhci
VBoxManage storageattach "Ubuntu22_1" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "Ubuntu22_1.vdi"

ダウンロードしたLinuxのISOイメージを、それを仮想マシン光学ドライブに挿入する。パスの指定は適宜に行う。

VBoxManage storagectl "Ubuntu22_1" --name "IDE Controller" --add ide
VBoxManage storageattach "Ubuntu22_1" --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium /path/to/your/linux.iso

最後に、仮想マシンを起動する。

VBoxManage startvm "Ubuntu22_1"

これにより、仮想環境が起動していることを確認した。

残課題

cloud-initを使えば、初期設定もできるが 今回に使用したSeverのisoファイルだとGUIの操作が残ってしまった。 cloud-initのisoをWindowsで作れれば、CLIで構築を完結できそうだ。

以下が参考になりそうだ。

qiita.com

参考

qiita.com