【できませんでした】さくらVPS+UbuntuにVirtualBoxを複数台起動させる

環境
- さくらVPS 1GB
- ubuntu 24.04
作りたいVM環境
- WEB/APサーバーとDBサーバーの両方を同時に起動
- ゲストOSはどちらもubuntu 24.04
結論
さくらVPSではできません!!
さくらのクラウドではできるみたいなので、どうしてもやりたければそっち。

VirtualBoxのインストール
インストールする
~$ sudo apt-get install virtualbox
バージョン確認
~$ VBoxManage -v
7.0.16_Ubuntur162802
Vagrantのインストール
インストールする
~$ wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant
バージョン確認
~$ vagrant -v
Vagrant 2.4.1
vagrant boxで各仮想マシンのベースとなるファイルを追加
~$ vagrant box add bento/ubuntu-24.04
==> box: Loading metadata for box 'bento/ubuntu-24.04'
box: URL: https://vagrantcloud.com/api/v2/vagrant/bento/ubuntu-24.04
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) libvirt
2) parallels
3) qemu
4) virtualbox
5) vmware_desktop
Enter your choice: 4
==> box: Adding box 'bento/ubuntu-24.04' (v202404.26.0) for provider: virtualbox (amd64)
box: Downloading: https://vagrantcloud.com/bento/boxes/ubuntu-24.04/versions/202404.26.0/providers/virtualbox/amd64/vagrant.box
==> box: Successfully added box 'bento/ubuntu-24.04' (v202404.26.0) for 'virtualbox (amd64)'!
Vagrantfileを追加
~$ vagrant init bento/ubuntu-24.04 \
--box-version 202404.26.0
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
Vagrantfileを編集
~$ vi Vagrantfile
config.vm.defineでWEB/APサーバーとDBサーバーをそれぞれ定義する
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-24.04"
config.vm.box_version = "202404.26.0"
config.vm.define "dev-ap" do |server|
server.vm.network "private_network", ip: "192.168.1.20"
end
config.vm.define "dev-db" do |server|
server.vm.network "private_network", ip: "192.168.1.31"
end
end
Vagrant起動
が、コケた
~$ vagrant up
Bringing machine 'dev-ap' up with 'virtualbox' provider...
Bringing machine 'dev-db' up with 'virtualbox' provider...
==> dev-ap: Importing base box 'bento/ubuntu-24.04'...
==> dev-ap: Matching MAC address for NAT networking...
==> dev-ap: Checking if box 'bento/ubuntu-24.04' version '202404.26.0' is up to date...
==> dev-ap: Setting the name of the VM: ubuntu_dev-ap_1721617715286_93074
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
https://www.virtualbox.org/manual/ch04.html#sharedfolders
This option can be disabled globally with an environment variable:
VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> dev-ap: Clearing any previously set network interfaces...
The IP address configured for the host-only network is not within the
allowed ranges. Please update the address used to be within the allowed
ranges and run the command again.
Address: 192.168.1.20
Ranges: 192.168.56.0/21
Valid ranges can be modified in the /etc/vbox/networks.conf file. For
more information including valid format see:
https://www.virtualbox.org/manual/ch06.html#network_hostonly
ホストのみのネットワークに構成された IP アドレスが、許可された範囲内にありません。使用中のアドレスを許可された範囲内に更新し、コマンドを再度実行してください。
アドレス: 192.168.1.20
範囲: 192.168.56.0/21有効な範囲は /etc/vbox/networks.conf ファイルで変更できます。有効な形式を含む詳細については、以下を参照してください:
https://www.virtualbox.org/manual/ch06.html#network_hostonly
というわけでVagrantfileを修正
~$ vi Vagrantfile
ipを範囲内にする
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-24.04"
config.vm.box_version = "202404.26.0"
config.vm.define "dev-ap" do |server|
server.vm.network "private_network", ip: "192.168.56.20"
end
config.vm.define "dev-db" do |server|
server.vm.network "private_network", ip: "192.168.56.30"
end
end
Vagrant起動(2回目)
が、またコケた
~$ vagrant up
Bringing machine 'dev-ap' up with 'virtualbox' provider...
Bringing machine 'dev-db' up with 'virtualbox' provider...
==> dev-ap: Checking if box 'bento/ubuntu-24.04' version '202404.26.0' is up to date...
==> dev-ap: Clearing any previously set network interfaces...
==> dev-ap: Preparing network interfaces based on configuration...
dev-ap: Adapter 1: nat
dev-ap: Adapter 2: hostonly
==> dev-ap: Forwarding ports...
dev-ap: 22 (guest) => 2222 (host) (adapter 1)
==> dev-ap: Booting VM...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["startvm", "b9c812d1-bd08-4324-94ad-ae7f656c15b8", "--type", "headless"]
Stderr: VBoxManage: error: The machine 'ubuntu_dev-ap_1721617715286_93074' is already locked by a session (or being locked or unlocked)
VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component MachineWrap, interface IMachine, callee nsISupports
VBoxManage: error: Context: "LaunchVMProcess(a->session, sessionType.raw(), ComSafeArrayAsInParam(aBstrEnv), progress.asOutParam())" at line 880 of file VBoxManageMisc.cpp
標準エラー: VBoxManage: エラー: マシン 'ubuntu_dev-ap_1721617715286_93074' はセッションによってすでにロックされています (またはロックまたはロック解除されています)
コケたときのゴミが残っちゃってるっぽい
これで解決できそう
というわけで、やってみる
まずvmのuuidを検索
~$ VBoxManage list vms
"ubuntu_dev-ap_1721617715286_93074" {b9c812d1-bd08-4324-94ad-ae7f656c15b8}
{}で囲まれているのがuuid
なので、消す
~$ VBoxManage startvm b9c812d1-bd08-4324-94ad-ae7f656c15b8 --type emergencystop
Vagrant起動(3回目)
が、またコケた
~$ vagrant up
Bringing machine 'dev-ap' up with 'virtualbox' provider...
Bringing machine 'dev-db' up with 'virtualbox' provider...
==> dev-ap: Checking if box 'bento/ubuntu-24.04' version '202404.26.0' is up to date...
==> dev-ap: Clearing any previously set forwarded ports...
==> dev-ap: Clearing any previously set network interfaces...
==> dev-ap: Preparing network interfaces based on configuration...
dev-ap: Adapter 1: nat
dev-ap: Adapter 2: hostonly
==> dev-ap: Forwarding ports...
dev-ap: 22 (guest) => 2222 (host) (adapter 1)
==> dev-ap: Booting VM...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["startvm", "b9c812d1-bd08-4324-94ad-ae7f656c15b8", "--type", "headless"]
Stderr: VBoxManage: error: VT-x is not available (VERR_VMX_NO_VMX)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole
VBoxManage: エラー: VT-x は利用できません (VERR_VMX_NO_VMX)
VBoxManage: エラー: 詳細: コード NS_ERROR_FAILURE (0x80004005)、コンポーネント ConsoleWrap、インターフェイス IConsole
さくらVPSではnestedVMできないみたい。泣