VirtualBox

VBoxManage

Установить VirtualBox на сервер не сложно
Переходим https://www.virtualbox.org/wiki/Linux_Downloads
И действуем по инструкции


Полезная статья https://eax.me/vboxmanage/
Из этой статьи взял https://github.com/afiskon/py-vm - упрощает рядовые операции

Еще больше упрощает жизнь bash-autocompletion - https://github.com/gryf/vboxmanage-bash-completion

Я себе заделал алиас alias vbm='vboxmanage'
Чтобы autocompletion работал и с этим алиасом нужно добавить

root@s1 ~ # tail -n 3 /usr/share/bash-completion/vboxmanage
complete -o default -F _VBoxManage VBoxManage
complete -o default -F _VBoxManage vboxmanage
complete -o default -F _VBoxManage vbm # <- нужно добавить такое

Так же для работы vboxmanage требуется установить linux-headers-5.10.0-9-amd64 gcc make perl и выполнить /sbin/vboxconfig


Для облегчения жизни сделал еще вот такое

root@s1 ~ # type vmips
vmips is a function
vmips ()
{
    for vm in `vbm list vms | cut -d'"' -f2`;
    do
        VMMACAddr="`vbm showvminfo $vm | grep -oP "MAC: .*?," | cut -d' ' -f2 | cut -d',' -f1`";
        vboxmanage dhcpserver findlease --mac-address $VMMACAddr --network NatNetwork | grep 'IP' | sed "s/IP Address:/${vm}:\t/g";
    done
}

Позволяет вывести список машин и их айпишники

root@s1 ~ # vmips
template:  10.0.5.10
pgsql-edu:  10.0.5.8

list vms покажет тачки
Опция --long даст значительно более подробный вывод

root@s1 ~ # vbm list vms | wc -l
1
root@s1 ~ # vbm list vms --long | wc -l
116

Создаем машину

# vbm createvm --name test-debian-11 --ostype Debian_64 --register

Создаем для нее жесткий диск

# vbm createmedium disk --filename /root/virtualboxvms/test-debian-11/test-debian-11.vdi --size 20000

Добавляем на машину sata контроллер и прикручиваем через него созданный жесткий диск

# vbm storagectl test-debian-11 --name sata-controller --add sata --controller IntelAHCI
# vbm storageattach test-debian-11 --storagectl sata-controller --port 0 --device 0 --type hdd --medium /root/virtualboxvms/test-debian-11/test-debian-11.vdi

Аналогично поступаем с dvd контроллером и образом установочного диска

# vbm storagectl test-debian-11 --name ide-controller --add ide --controller PIIX4
# vbm storageattach test-debian-11 --storagectl ide-controller --port 1 --device 0 --type dvddrive --medium /root/virtualboxvms/debian-11.2.0-amd64-netinst.iso

Включаем rdp

# vbm modifyvm test-debian-11 --vrde on

Находим extpack под нашу версию (версию можно узнать так vbm --version)
https://www.virtualbox.org/wiki/Download_Old_Builds

# wget https://download.virtualbox.org/virtualbox/6.1.28/Oracle_VM_VirtualBox_Extension_Pack-6.1.28.vbox-extpack
# vbm extpack install Oracle_VM_VirtualBox_Extension_Pack-6.1.28.vbox-extpack
# vbm list extpacks
Extension Packs: 1
Pack no. 0:   Oracle VM VirtualBox Extension Pack
Version:      6.1.28
Revision:     147628
Edition:
Description:  Oracle Cloud Infrastructure integration, USB 2.0 and USB 3.0 Host Controller, Host Webcam, VirtualBox RDP, PXE ROM, Disk Encryption, NVMe.
VRDE Module:  VBoxVRDP
Usable:       true
Why unusable:

Запускаем машину (headless тк запускаем без графики)

# vbm startvm test-debian-11 --type headless
Waiting for VM "test-debian-11" to power on...
VM "test-debian-11" has been successfully started.

Дальше можно подключиться через какой-нибудь rdp клиент
Screenshot_2021_02_02-12_49_03-2021-12-21_virtualbox-rdp.png

И получить kernel panic
Screenshot_2021_02_02-12_49_03-2021-12-21-at-kernelpanic.png

Останавливаем машину, даем ей больше ресурсов и запускаем

# vbm controlvm test-debian-11 poweroff
# vbm modifyvm test-debian-11 --cpus 4 --memory 4000
# vbm startvm test-debian-11 --type headless # не забываем про headless, иначе не запустится

Screenshot_2021_02_02-12_49_03-2021-12-21-withoutkernelpanic.png

И радуемся тому что больше нет kernel panic


Можно скачать готовый образ с https://www.osboxes.org и делать из него машины

Создаем машины

# vbm createvm --name pgsql-1 --ostype Debian_64 --register
Virtual machine 'pgsql-1' is created and registered.
UUID: ea1335c4-4afc-48da-8152-cd77289bec70
Settings file: '/root/virtualboxvms/pgsql-1/pgsql-1.vbox'
# vbm modifyvm pgsql-1 --cpus 4 --memory 4000
# vbm createvm --name pgsql-2 --ostype Debian_64 --register
# vbm createvm --name pgsql-3 --ostype Debian_64 --register
# vbm modifyvm pgsql-2 --cpus 4 --memory 4000
# vbm modifyvm pgsql-3 --cpus 4 --memory 4000

root@s1 ~/virtualboxvms # tree
.
├── debian-11.2.0-amd64-netinst.iso
├── debian-11-server.vdi
├── Oracle_VM_VirtualBox_Extension_Pack-6.1.28.vbox-extpack
├── pgsql-1
│   ├── pgsql-1.vbox
│   └── pgsql-1.vbox-prev
├── pgsql-2
│   ├── pgsql-2.vbox
│   └── pgsql-2.vbox-prev
└── pgsql-3
    ├── pgsql-3.vbox
    └── pgsql-3.vbox-prev

Добавляем sata

# vbm storagectl pgsql-1 --name sata-controller --add sata --controller IntelAHCI
# vbm storagectl pgsql-2 --name sata-controller --add sata --controller IntelAHCI
# vbm storagectl pgsql-3 --name sata-controller --add sata --controller IntelAHCI

Копируем VDI из скачанного с osboxes в диск для созданных машин

# cp debian-11-server.vdi pgsql-1/pgsql-1.vdi
# cp debian-11-server.vdi pgsql-2/pgsql-2.vdi
# cp debian-11-server.vdi pgsql-3/pgsql-3.vdi

Аттачим эти vdi к машинам

# vbm storageattach pgsql-1 --storagectl sata-controller --port 0 --device 0 --type hdd --medium /root/virtualboxvms/pgsql-1/pgsql-1.vdi
# vbm storageattach pgsql-2 --storagectl sata-controller --port 0 --device 0 --type hdd --medium /root/virtualboxvms/pgsql-2/pgsql-2.vdi
VBoxManage: error: Cannot register the hard disk '/root/virtualboxvms/pgsql-2/pgsql-2.vdi' {4e1934ce-f42f-489c-be67-12317fb24b80} because a hard disk '/root/virtualboxvms/pgsql-1/pgsql-1.vdi' with UUID {4e1934ce-f42f-489c-be67-12317fb24b80} already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())" at line 187 of file VBoxManageDisk.cpp
VBoxManage: error: Invalid UUID or filename "/root/virtualboxvms/pgsql-2/pgsql-2.vdi"

И на второй машине получаем ошибку

Данная ошибка появляется в результате того, что мы скопировали файл образа диска виртуальной машины, а UUID остался прежним

Делаем вот так

# vbm internalcommands sethduuid pgsql-2/pgsql-2.vdi
UUID changed to: 088d52d2-323f-41e8-981e-9c2d1964943a
# vbm internalcommands sethduuid pgsql-3/pgsql-3.vdi
UUID changed to: 2c24c4a1-2c15-47b7-aae6-f3bea7f96457

И теперь аттачинг отрабатывает без ошибки

# vbm storageattach pgsql-2 --storagectl sata-controller --port 0 --device 0 --type hdd --medium /root/virtualboxvms/pgsql-2/pgsql-2.vdi
# vbm storageattach pgsql-3 --storagectl sata-controller --port 0 --device 0 --type hdd --medium /root/virtualboxvms/pgsql-3/pgsql-3.vdi

Аттачим машины к netnat

# vboxmanage natnetwork add --netname debian-network --network 10.0.5.0/24   --enable --dhcp on --ipv6 off
# vm stop pgsql-1
# vbm modifyvm pgsql-1 --nic2 natnetwork --nat-network2 debian-network
# vm stop pgsql-2
# vbm modifyvm pgsql-2 --nic2 natnetwork --nat-network2 debian-network
# vm stop pgsql-3
# vbm modifyvm pgsql-3 --nic2 natnetwork --nat-network2 debian-network
# vm start pgsql-1
# vm start pgsql-2
# vm start pgsql-3

Портфорвардинг

# vm stop pgsql-3
# vm stop pgsql-2
# vm stop pgsql-1
# vbm modifyvm pgsql-1 --natpf1 "ssh-forwarding,tcp,,22001,,22"
# vbm modifyvm pgsql-2 --natpf1 "ssh-forwarding,tcp,,22002,,22"
# vbm modifyvm pgsql-3 --natpf1 "ssh-forwarding,tcp,,22003,,22"
# vm start pgsql-1
# vm start pgsql-2
# vm start pgsql-3

VirtualBox User Manual

VirtualBox User Manual

1. First Steps

1.1 Why is Virtualization Useful?

1.2 Some Terminology

1.3 Features Overview

1.5 Installing Oracle VM VirtualBox and Extension Packs

On each host platform, Oracle VM VirtualBox uses the installation method that is most common and easy to use

VirtualBox is split into the following components:

1.8 Running Your Virtual Machine

To start a virtual machine, you have several options:

1.8.1 Starting a New VM for the First Time

When a VM is started for the first time, the First Start Wizard, is displayed. This wizard helps you to select an installation medium
The wizard helps you to select a medium to install an OS from:

1.8.2 Capturing and Releasing Keyboard and Mouse

VirtualBox provides a virtual USB tablet device to new virtual machines through which mouse events are communicated to the guest OS. If you are running a modern guest OS that can handle such devices, mouse support may work out of the box without the mouse being captured as described below
Otherwise, if the virtual machine detects only standard PS/2 mouse and keyboard devices, since the OS in the virtual machine does not know that it is not running on a real computer, it expects to have exclusive control over your keyboard and mouse. But unless you are running the VM in full screen mode, your VM needs to share keyboard and mouse with other applications and possibly other VMs on your host
After installing a guest OS and before you install the Guest Additions, described later, either your VM or the rest of your computer can “own” the keyboard and the mouse. Both cannot own the keyboard and mouse at the same time
You activate the VM by clicking inside it. To return ownership of keyboard and mouse to your host OS, VirtualBox reserves a special key on your keyboard: the Host key. By default, this is the right Ctrl key on your keyboard

As this behavior is inconvenient, Oracle VM VirtualBox provides a set of tools and device drivers for guest systems called the VirtualBox Guest Additions. These tools make VM keyboard and mouse operations much more seamless

1.8.3 Typing Special Characters

Some OSes expect certain key combinations to initiate certain procedures. The key combinations that you type into a VM might target the host OS, the Oracle VM VirtualBox software, or the guest OS. The recipient of these keypresses depends on a number of factors, including the key combination itself

1.8.4 Changing Removable Media

While a virtual machine is running, you can change removable media in the Devices menu of the VM’s window
guest-additions-VB.jpg

The settings are the same as those available for the VM in the Settings dialog of the VirtualBox main window
vm-settings-harddisk.png

But as the Settings dialog is disabled while the VM is in the Running or Saved state, the Devices menu saves you from having to shut down and restart the VM every time you want to change media

1.8.5 Resizing the Machine’s Window

1.8.6 Saving the State of the Machine

When you close VM's window, VirtualBox asks you whether you want to save or power off the VM

1.9 Using VM Groups

1.10 Snapshots

With snapshots, you can save a particular state of a virtual machine for later use. A snapshot of a virtual machine is thus similar to a machine in Saved state, but there can be many of them, and these saved states are preserved

1.10.1 Taking, Restoring, and Deleting Snapshots

1.10.2 Snapshot Contents

A snapshot consists of the following:

1.13 Cloning Virtual Machines