Virtualization... virtualization...
Now everyone is trying to squeeze as much as possible out of their hardware resources. Having several separate computers with different operating systems is a little expensive and not all organizations will go for it. But there is a way out, you can use virtual machines. And it is possible that many people use the wonderful product from Sun "VirtualBox" for this purpose. I'm sure most people use a GUI interface for setup. it is very clear and convenient. But how many have thought about the possibility of managing VirtualBox through the console.
The task was set: Install two operating systems (Ubuntu and Windows) on one server. At the same time there was a working server under running Ubuntu 9.04 Server Edition. Therefore, it was decided to put Windows XP as a guest system.
But there was a problem on Ubuntu NOT INSTALLED any graphic environment, i.e. there is only a bare console.
So what do we have:
P.S. Windows XP was needed for only one reason. The project needed to use MS SQL Server.
Sometimes it becomes necessary to run a virtual machine on a host without Xs. I will talk about how to do this, having access to the host system only via ssh + rdp (Remote Desktop Protocol). I will describe the process for OC Ubuntu 9.10 as a host.
Let's start by installing VirtualBox.
You must first install the dkms (Dynamic Kernel Module Support Framework) package:
sudo apt-get install dkms
The site offers 2 options: register the package source (deb download.virtualbox.org/virtualbox/debian karmic non-free) in /etc/apt/sources.list or download and install the deb package. When I registered the source and did sudo apt-get install virtualbox-3.1, I got a bunch of packages from dependencies (including some for the GUI interface). Therefore, it is better to download the deb package. Download, install:
sudo dpkg -i virtualbox-3.1_3.1.0-55467_Ubuntu_karmic_i386.deb
Perhaps dependencies will also be required here (some libraries for parsing xml, which stores configs, but there are much fewer of them than in the first case). If the installation failed due to dependencies, you can simply do
sudo apt-get -f install
This will install dependencies and VirtualBox
OK. Installed VirtualBox. Let's start creating guest machines.
We create the car itself:
VBoxManage createvm --name ubuntu --ostype Ubuntu --register
(name - machine name, ostype - system type. A complete list of all types can be found with the VBoxManage list ostypes command)
Setting up
VBoxManage modifyvm ubuntu --memory 512 --floppy disabled --audio none --nic1 bridged --bridgeadapter1 eth0 --vram 4 --accelerate3d off --boot1 disk --acpi on --cableconnected1 on --usb off --vrdp on --vrdpport 3390
Everything is more clear here. you can also specify NAT as the network type (--nic1 nat). also enable rdp
We create hdd disk for virtual machine:
VBoxManage createhd --filename /home/user/vbox/ubuntu.vdi --size 20000 --register
Adding an IDE controller to our machine
VBoxManage storagectl ubuntu --name "IDE Controller" --add ide
We cling to IDE0 the previously created hdd
VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /home/user/vbox/ubuntu.vdi
On IDE1 we cling the installation image
VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /home/user/vbox/iso/ubuntu-9.10-alternate-i386.iso
Telling the machine to boot from disk
VBoxManage modifyvm ubuntu --boot1 dvd
We start the car
In order to install the base system, we will use the rdp client (I have KDE, KRDC is included in the standard package). connect to the host machine on the port specified in the settings (--vrdpport 3390), install the system, do sudo apt-get install openssh-server . now you can get to the virtual machine via ssh
Stopping the virtual machine
VBoxManage controlvm ubuntu acpipowerbutton
via acpi
Or more hard
VBoxManage controlvm ubuntu poweroff
We say loaded from hdd
VBoxManage modifyvm ubuntu --boot1 disk
You can also unhook the installation disk
VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --medium none
And we start again
Nohup VBoxHeadless --startvm ubuntu &
More useful commands:
VBoxManage list runningvms
view all running machines
VBoxManage showvminfo ubuntu
view information about the virtual machine
Thus, on one machine with a minimum installed system you can raise several virtual ones for various purposes and experiments
1.Create a virtual machine named windows_xp
VBoxManage createvm --name windows_xp --regist er
The created virtual machine is XML file, which is located in the home directory /root/VirtualBox\ VMs/windows_xp/windows_xp.vbox
2. Next, you need to create a virtual machine disk
VBoxManage createhd --filename /date/VirtualBox/windows_xp.vdi --size 15000 --variant Fixe d
By default, the disk is created in the user's home folder, we created the windows_xp.vdi disk in the /date/VirtualBox/ directory with a size of 15 gigabytes; the size is fixed, i.e. its increase does not occur dynamically.
3. Create a virtual disk controller
VBoxManage storagectl windows_xp --name "IDE Controller" --add ide --controller PIIX4
This command creates an IDE controller named “IDE Controller”, the controller type is PIIX4.
4. Connect virtual disk to the virtual machine:
VBoxManage storageattach windows_xp --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /date/VirtualBox/windows_xp.vdi
5. We connect the virtual image of the OS we want to install
VBoxManage storageattach windows_xp --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium /date/public/winxp_pro_eng_sp3.iso
6. Set the size random access memory for virtual machine 512mb
vboxmanage modifyvm windows_xp --memory 512
7. Set system type VBoxManage modifyvm windows_xp --ostype WindowsX P
view all possible ostypes: VBoxManage list ostypes
8. Allow to connect to the console via RDP protocol
vboxmanage modifyvm windows_xp --vrde on
To make it more convenient to manage virtual machines, you need a set of add-ons that includes USB 2.0 and a built-in RDP server. Download addons and install: wget http://dlc.sun.com.edgesuite.net/virtualbox/4.2.12/Oracle_VM_VirtualBox_Extension_Pack-4.2.12-84980.vbox-extpack
VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.2.12-84980.vbox-extpack
9. Start the virtual machine
vboxheadless -s windows_xp or in the background:
VBoxManage startvm windows_xp --type headless
to run the guest OS on the base computer you are sshing to, you need to use the --type=headless switch:
10. Connect via RDP ip_servera and install win_xp as usual
11. You can turn off the machine like this VBoxManage controlvm windows_xp poweroff
12. Mount the VboxGuestAdditions disk
VBoxManage storageattach windows_xp --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium /usr/share/virtualbox/VBoxGuestAdditions.iso
Start the machine and install VboxGuestAdditions
13. Detailed information about a specific virtual OS can be obtained with the VBoxManage showvminfo windows_xp command
14. This command creates a bridged NIC network interface
VBoxManage modifyvm windows_xp --nic1 bridged --bridgeadapter1 eth0
15. If the disk of the virtual machine needs to be cloned, then this is done using the VBoxManage utility:
VBoxManage clonehd /path/from/copy/image1.vdi /path/to/copy/image2.vdi
After that, everything connects wonderfully to the new virtual machine.
16.Create a shared folder with the server
vboxmanage sharedfolder add "windows_xp" --name share-name --hostpath /mnt
Connect shared folder from windows_xp execute
\\vboxsvr\share-name
Ordinary VirtualBox users may not be aware of the tremendous power that lies in the command line interface.
People usually ask: "Why work on the command line when you have a perfectly good graphical interface?" The answer is the vast possibilities of the command line. Since 1995 there has been a whole generation computer users, who believe that a computer is a collection of windows and graphics, but, in reality, this is very far from the truth. Why, 1995, you ask? With the advent of Windows 95 in 1995, a new era of computing was ushered in—the era of graphical interfaces. Shortly after that fateful August, FVWM95 was released, which was Linux's answer to the Windows 95 Explorer interface. It seems to me that in those good old days command line disappeared forever. Nobody wanted to use DOS anymore and stopped being afraid of Linux. But the command line is still alive and well in the Linux environment. It's still quite alive on Windows. And now that Macs are based on Unix, they also have a lot of stuff that doesn't require a GUI.
It may come as a surprise to you that underneath VirtuaBox's pretty GUI, with its bright graphics, clear Settings page, and plenty of drop-down lists, lurks the occult world of the command line. The true power of this dark world will be revealed only to those who dare to enter these musty catacombs. After you get to grips with the VirtualBox command line, you might decide that the GUI is little more than a pretty drape made for those who would otherwise just ignore the boring black rectangle of an actual running application.
The black rectangle I was talking about is the terminal window. The command line allows you to take advantage of the full power of automatic command execution. The only way automation that I know of on *nix systems is the use of scripts written in Perl, shell scripts, or scripts in some other language. And the system scheduler daemon (cron) will help you with right time run the script. This command-line power that, thanks to its autorun capability, requires system administrators to also be able to write scripts, is their baggage of skill. For every job that requires System Administrator, it will be indicated that the applicant must be able to write scripts in at least one of the interpreted languages listed in the vacancy.
As far as virtualization is concerned, the command line will allow you to quickly create virtual disks, add a new network interface, start virtual machines, and create new virtual machines, all without using a GUI. Automatic execution using scripts will also allow you to schedule the start and stop of virtual machines without your direct intervention. And, if you're a really great scripter, you can write a script that, with the help of hints, will help you create a virtual machine, run it, or install and modify it with very minimal intervention on your part. If you don't like to do everything manually, you will make a list of virtual machines that are used all the time, then set up scripts (templates) for each type of used virtual machines and, if necessary, create a virtual machine within a few seconds.
Let's look at simple and elegant functionality command line in VirtualBox.
From the VirtualBox Linux Downloads link, download the binary distribution for your system. If you want, you can download source. For the option I chose (for RHEL5/CentOS5 in rpm format), the following directories were created:
If your distribution does not have latest version VirtualBox (version 3.1.2 at the time of this writing), use one of the packages that you can download, or download the source code.
The hardest part about using the command line is knowing where to start. IN GUI you need to use the New button. The command line is a little more difficult to reveal its secrets. In order to create and set up a new virtual machine, you must open a terminal window, or use an ssh session to access the Linux system where VirtualBox is installed and run the following command:
$ /usr/bin/VBoxManage createvm --name Debian5 --register
This command creates a new virtual machine named Debian5 and registers it with VirtualBox. The created virtual machine is an XML file that is located in the home directory ~/.VirtualBox/Machines/Debian5/Debian5.xml.
Create a virtual disk for your virtual machine:
$ /usr/bin/VBoxManage createhd --filename Debian5.vdi --size 4000 --variant Fixed
You have created a 4 GB (4000 MB) virtual disk named Debian5.vdi; the size is fixed, i.e. its increase does not occur dynamically.
The virtual disk is created as a file ~/.VirtualBox/HardDisks/Debian.vdi
Create a disk device controller to which you will attach the virtual disk and virtual CD/DVD device
$ /usr/bin/VBoxManage storagectl Debian5 --name "IDE Controller" --add ide --controller PIIX4
This command creates an IDE controller named “IDE Controller”, controller type is PIIX4.
Now let's connect the virtual disk to your virtual machine:
$ /usr/bin/VBoxManage storageattach Debian5 --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium Debian5.vdi
We connect iso image toy operating system you want to install:
$ /usr/bin/VBoxManage storageattach Debian5 --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium /ISO/debian-40r3-i386-netinst.iso
Setting up the network:
$ /usr/bin/VBoxManage modifyvm Debian5 --nic1 bridged --cableconnected1 on --bridgeadapter1 eth0
This command creates a bridge type NIC network interface (bridge): the cable is connected at startup Linux systems, adapter is eth0.
We start the virtual machine:
$ /usr/bin/VBoxManage startvm Debian5
If everything goes as planned, you will see the following message, and then you will see the virtual machine window ready for installation:
Waiting for the remote session to open... Remote session has been successfully opened.
You can see that creating and configuring a virtual machine on the command line frees you from endless mouse clicks and opens up a whole new world of virtual machine automation possibilities. And we haven't gone into much depth here, but I hope this will pique your curiosity and encourage you to master the tremendous power that the command line has.
Sometimes it becomes necessary to run a virtual machine on a host without Xs. I will talk about how to do this, having access to the host system only via ssh + rdp (Remote Desktop Protocol). I will describe the process for OC Ubuntu 9.10 as a host.
Let's start by installing VirtualBox.
You must first install the dkms (Dynamic Kernel Module Support Framework) package:
sudo apt-get install dkms
The site offers 2 options: register the package source (deb download.virtualbox.org/virtualbox/debian karmic non-free) in /etc/apt/sources.list or download and install the deb package. When I registered the source and did sudo apt-get install virtualbox-3.1, I got a bunch of packages from dependencies (including some for the GUI interface). Therefore, it is better to download the deb package. Download, install:
sudo dpkg -i virtualbox-3.1_3.1.0-55467_Ubuntu_karmic_i386.deb
Perhaps dependencies will also be required here (some libraries for parsing xml, in which configs are stored, but there are much fewer of them than in the first case). If the installation failed due to dependencies, you can simply do
sudo apt-get -f install
This will install dependencies and VirtualBox
OK. Installed VirtualBox. Let's start creating guest machines.
We create the car itself:
VBoxManage createvm --name ubuntu --ostype Ubuntu --register
(name - machine name, ostype - system type. A complete list of all types can be found with the VBoxManage list ostypes command)
Setting up
VBoxManage modifyvm ubuntu --memory 512 --floppy disabled --audio none --nic1 bridged --bridgeadapter1 eth0 --vram 4 --accelerate3d off --boot1 disk --acpi on --cableconnected1 on --usb off --vrdp on --vrdpport 3390
Everything is more clear here. you can also specify NAT as the network type (--nic1 nat). also enable rdp
Create an hdd disk for the virtual machine:
VBoxManage createhd --filename /home/user/vbox/ubuntu.vdi --size 20000 --register
Adding an IDE controller to our machine
VBoxManage storagectl ubuntu --name "IDE Controller" --add ide
We cling to IDE0 the previously created hdd
VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /home/user/vbox/ubuntu.vdi
On IDE1 we cling the installation image
VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /home/user/vbox/iso/ubuntu-9.10-alternate-i386.iso
Telling the machine to boot from disk
VBoxManage modifyvm ubuntu --boot1 dvd
We start the car
In order to install the base system, we will use the rdp client (I have KDE, KRDC is included in the standard package). connect to the host machine on the port specified in the settings (--vrdpport 3390), install the system, do sudo apt-get install openssh-server . now you can get to the virtual machine via ssh
Stopping the virtual machine
VBoxManage controlvm ubuntu acpipowerbutton
via acpi
Or more hard
VBoxManage controlvm ubuntu poweroff
We say loaded from hdd
VBoxManage modifyvm ubuntu --boot1 disk
You can also unhook the installation disk
VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --medium none
And we start again
Nohup VBoxHeadless --startvm ubuntu &
More useful commands:
VBoxManage list runningvms
view all running machines
VBoxManage showvminfo ubuntu
view information about the virtual machine
Thus, on one machine with a minimally installed system, you can raise several virtual ones for various purposes and experiments.