KVM on Debian

Neskol’ko nedel’ nazad ya vybral sebe KVM dlya virtualizatsii. Srazu otvechu na voprosy «pochemu?»: virtual’nyye mashiny pod KVM shustry i pozvolyayut zapuskat’ lyubuyu OS. Yereticheskiye OS ya ne proboval, no Debian i FreeBSD begayut pod KVM+Debian ochen’ i ochen’ neplokho — eto priznayut dazhe byvalyye administratory.

Yesli vy gotovy za skorost’ zaplatit’ udobstvom nastroyki i ispol’zovaniya khost-mashiny — prosim pod kat.

Ustanavlival ya KVM na mashinku s protsessorom core i7. Rekomenduyu ispol’zovat’ imenno mnogoyadernyye protsessory s HT. Pri tom ne stoit otdavat’ virtual’nym mashinam virtual’nykh protsessorov bol’she, chem yest’ yader na khost-mashine. V sluchaye s i7 ya poluchil 8 yader, kotoryye v dal’neyshem smogu razdelit’ kak pozhelayu mezhdu konteynerami. Yesli razdat’ bol’she protsessorov chem yest’ — eto mozhet skazat’sya na proizvoditel’nosti.

Pervym delom proveryayem — podderzhivayet li protsessor apparatnuyu virtualizatsiyu:
cat /proc/cpuinfo | egrep ‘(svm|vmx)’
Yesli posle vvoda komandy na ekrane poyavilsya kakoy-libo tekst — to vash protsessor podderzhivayet apparatnuyu virtualizatsiyu. Yesli vyvod komandy pustoy — to ne stoit ispol’zovat’ KVM. Da i voobshche ne stoit ispol’zovat’ sovremennyye gipervizory.

Teper’ ustanavlivayem neobkhodimyye pakety:

sudo apt-get install kvm libvirt-bin virtinst bridge-utils

Posle chego stoit perezagruzit’ khost. Konechno zhe stoit ispol’zovat’ posledneye dostupnoye stabil’noye linux-yadro, dostupnoye v repozitoriyakh.

Proveryayem, ustanovilsya li KVM:

virsh -c qemu:///system list —all

Vyvod komandy opyat’ zhe dolzhen byt’ ne pustym i ne soderzhat’ oshibok vida «komanda ne naydena».

Pristupim k nastroyke seti na khost mashine. Privedem fayl /etc/network/interfaces k sleduyushchemu vidu:

auto lo

iface lo inet loopback
auto eth0
iface eth0 inet static
auto br0
iface br0 inet static
address 178.khkhkh.khkhkh.137
netmask 255.255.255.192
network 178.khkhkh.khkhkh.0
broadcast 178.khkhkh.khkhkh.191
gateway 178.khkhkh.khkhkh.129
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

Posle chego primenyayem vse setevyye nastroyki:

/etc/init.d/networking restart

Dlya udobstva sozdadim katalogi:

mkdir /iso && mkdir /vms

I skachayem obraz ustanovochnyy obraz debian:

cd /iso && wget -c http://cdimage.debian.org/debian-cd/5.0.4/amd64/iso-cd/debian-504-amd64-CD-1.iso

Teper’ pereydom k sozdaniyu virtual’noy mashiny:

virt-install -n vm1 -r 1024 -f /vms/vm1.img -s 50 -c /iso/debian-cd/5.0.4/amd64/iso-cd/debian-504-amd64-CD-1.iso —accelerate —os-type=linux —os-variant=generic26 -v —vnc -w bridge:br0

Rasskazhu o privedennykh vyshe optsiyakh:

-n vm1 — imya domena (virtual’noy mashiny)

-r 1024 — kolichestvo operativnoy pamyati

-f /vms/vm1.img — raspolozheniye «zhestkogo diska».

-s 50 — razmer «zhestkogo diska»

-c /iso/debian-cd/5.0.4/amd64/iso-cd/debian-504-amd64-CD-1.iso — ukazyvayem iso obraz, s kotorogo budem proizvodit’ ustanovku sistemy. Rekomenduyu zapuskat’ 64kh bitnyye obrazy pod 64y bitnoy OS khost-mashiny.

—accelerate —os-type=linux —os-variant=generic26 -v — optimiziruyem VDS dlya raboty s virtual’nym Linux s yadrom 2.6.khkh

—vnc — zapuskayem VNC-server dlya dostupa k virtual’noy mashine.

-w bridge:br0 — ukazyvayem, chto virtual’naya mashina dolzhna ispol’zovat’ setevly most br0 khost-mashiny

Teper’ pereydem k ustanovke OS na virtual’nuyu mashinu.

Na vashem komp’yutere (ne na servere, a na rabochey stantsii s graficheskoy sredoy) vvodim komandy:

ssh -L 5900:localhost:5900 root@178.khkhkh.khkhkh.137

I vo vtoroy konsoli:

vncviewer localhost:5900

Teper’ vy dolzhny uvidet’ v VNC ustanovshchik Debian. Stavim vso kak obychno (vozmozhno vam budet nedostupna set’). Posle togo, kak ustanovshchik Debian otpravit sistemu v perezagruzku — virtual’naya mashina vyklyuchitsya, vmesto togo, chtoby perezagruzit’sya.

Samoye vremya izmenit’ kolichestvo vydelennykh protsessorov dlya virtual’noy mashiny:

virsh -c qemu:///system setvcpus vm1 2
Eta komanda srabotayet, tol’ko yesli virtual’naya mashina nakhoditsya v vyklyuchennom sostoyanii. Teper’ zapustim yeyo snova:
virsh -c qemu:///system start vm1
Teper’ nuzhno nastroit’ set’ na virtual’noy mashine. Zakhodim na neyo po VNC i privodim fayl /etc/network/interfaces k sleduyushchemu vidu:

auto lo

iface lo inet loopback

allow-hotplug eth0

iface eth0 inet static

address 178.xxx.xxx.177

netmask 255.255.255.192

network 178.xxx.xxx.128

broadcast 178.xxx.xxx.191

gateway 178.xxx.xxx.137

Shlyuzom neobkhodimo ukazat’ ip-adres khost-servera. Tak zhe stoit propisat’ NS v fayl /etc/resolv.conf

Teper’ vasha virtual’naya mashina rabotayet i dostupna iz seti (a v moyom sluchaye ona stala dostupna i iz interneta).

Ostalos’ sdelat’ aptitude update && aptitude upgrade && aptitude install openssh-server i zayti na virtual’nyy server po SSH.

Pol’zuyas’ sluchayem rasskazhu vam, chto u khostera QS.biz sdayutsya v arendu VDS na KVM.
A few weeks ago, I chose KVM virtualization. Just answer the questions of “why?”: KVM virtual machines Hustler and allow you to run any operating system. Heretical OS I have not tried, but Debian and FreeBSD running under KVM + Debian is very, very good – it is recognized by even experienced administrators.

If you’re willing to pay for speed and ease of configuration of using the host machine – ask for cat.

I KVM installed on the machine with a processor core i7. Recommended to use multi-core processors with HT. While it is not necessary to give virtual machines virtual CPUs greater than the cores have on the host machine. In the case of the i7 I got 8 cores, which then can be divided as desired between containers. If you give more processors than it is – it can affect performance.

First of all check – whether the processor supports hardware virtualization:
cat / proc / cpuinfo | egrep ‘(svm | vmx)’
If you type the command on the screen appeared a text – then your CPU supports hardware virtualization. If the output of a blank – it is not necessary to use KVM. And indeed it is not necessary to use modern hypervisors.

Now install the packages:

sudo apt-get install kvm libvirt-bin virtinst bridge-utils

Then reboot the host is. Of course you should use the latest available stable linux-kernel available in the repositories.

Check whether to install the KVM:

virsh -c qemu: /// system list -all

The output again should not be empty and contain no errors type “command not found”.

Proceed to configure the network on the host machine. Here is the file / etc / network / interfaces to the following form:

auto lo

iface lo inet loopback
auto eth0
iface eth0 inet static
auto br0
iface br0 inet static
address 178.hhh.hhh.137
netmask 255.255.255.192
network 178.hhh.hhh.0
broadcast 178.hhh.hhh.191
gateway 178.hhh.hhh.129
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

Then use all the network settings:

/etc/init.d/networking restart

For convenience, create the following directories:

mkdir / iso && mkdir / vms

And download image installation image debian:

cd / iso && wget -c http://cdimage.debian.org/debian-cd/5.0.4/amd64/iso-cd/debian-504-amd64-CD-1.iso

Now to create a virtual machine:

virt-install -n vm1 -r 1024 -f /vms/vm1.img -s 50 -c /iso/debian-cd/5.0.4/amd64/iso-cd/debian-504-amd64-CD-1.iso -accelerate -os-type = linux -os-variant = generic26 -v -vnc -w bridge: br0

I’ll tell you about the above options:

-n vm1 – the domain name (the virtual machine)

-r 1024 – the amount of RAM

-f /vms/vm1.img – the location of the “Hard Disk”.

-s 50 – the size of “Hard Disk”

-c /iso/debian-cd/5.0.4/amd64/iso-cd/debian-504-amd64-CD-1.iso – indicate the iso image that will perform the installation of the system. I recommend to run the 64-bit images at 64th bit OS host.

-accelerate -os-type = linux -os-variant = generic26 -v – VDS optimize for virtual Linux kernel 2.6.hh

-vnc – run the VNC-server to access a virtual machine.

-w bridge: br0 – indicate that the virtual machine is to use the bridge br0 setevly Host

Now let’s move to install the OS on a virtual machine.

On your computer (not on the server and a workstation with a graphical environment) to enter commands:

ssh -L 5900: localhost: 5900 root@178.ххх.ххх.137

And on the second console:

vncviewer localhost: 5900

Now you should see VNC Installer Debian. We put as usual (maybe you will be unavailable network). After the Debian installer system will send reboot – switch off the virtual machine, instead of rebooting.

It’s time to change the number of processors assigned to the virtual machine:

virsh -c qemu: /// system setvcpus vm1 2
This command will work only if the virtual machine is turned off. Now run it again:
virsh -c qemu: /// system start vm1
Now you need to configure the network on the virtual machine. We go at it for VNC and give the file / etc / network / interfaces to the following form:

auto lo

iface lo inet loopback

allow-hotplug eth0

iface eth0 inet static

address 178.xxx.xxx.177

netmask 255.255.255.192

network 178.xxx.xxx.128

broadcast 178.xxx.xxx.191

gateway 178.xxx.xxx.137

Gateway must specify the ip-address of the host server. It is also worth to register NS in /etc/resolv.conf

Now your virtual machine is running and is accessible from the network (and in my case it has been available from the Internet).

You’re making aptitude update && aptitude upgrade && aptitude install openssh-server and go to the virtual server via SSH.

I take this opportunity to tell you that the host QS.biz rented VDS on KVM.