📜 ⬆️ ⬇️

QEMU on FreeBSD-9.0-RELEASE-amd64

Task: Run, configure n (in the example we will run 3) qemu machines with OS ubuntu-12.04.1-server-amd64-1pcs. and FreeBSD-9.0-RELEASE-amd64. The amd64 version is recommended by ubuntu developers.

1. Installation (Do not forget to update the ports comrades BSD'delniki)


main [16: 54: emulators / qemu] # cd / usr / ports / emulators / qemu
main [16: 55: emulators / qemu] # make install clean

')
So:


main [17: 25: emulators / qemu-devel] # pkg_info | grep "qemu"
kqemu-kmod-devel-1.4.0.p1_5 Kernel Accelerator for QEMU CPU Emulator (development versi
qemu-0.11.1_11 QEMU CPU Emulator
main [17: 25: emulators / qemu-devel] #


Accelerator and processor emulator installed.

2. Creating a workspace and setting the environment



main [17: 40: / usr / QEMU] #mkdir —p / usr / QEMU / UbuEx1


Download the boot disk image ubuntu-12.04.1-server-amd64.iso from the official site. We load modules of a kernel and we add them to loader.conf (we create if necessary)


main [17: 53: / usr / QEMU] # touch /boot/loader.conf
main [17: 53: / usr / QEMU] # echo 'kqemu_load = aqu YES "'>> /boot/loader.conf
main [17: 53: / usr / QEMU] # echo 'aio_load = & aquo; YES "'>> /boot/loader.conf


kqemu.ko - Acceleration Module.
aio.ko - Asynchronous I / O module.

Or you can add the aio module to the kernel, just to emulate networking, we need a tap device, and therefore the following modules need to be loaded into the kernel:


options VFS_AIO
device tap
options IPFILTER
options IPFILTER_LOG

main [17: 53: / usr / QEMU] # cd / usr / src
main [17: 53: / usr / QEMU] # make buildkernel KERNCONF = <kernel_name>
...
main [17: 53: / usr / QEMU] # make installkernel KERNCONF = <kernel_name>
...
main [17: 53: / usr / QEMU] # shutdown —r now


If you do not load the aio.ko module, then QEMU will fall with a shout:


Bad system call: 12 (core dumped)


In the event that you are planning on networking between hosted FreeBSD and guest systems, you must also load the following modules:


main [18: 14: ~] # kldload if_bridge
main [18: 14: ~] # kldload if_tap


After the reboot, we will see our modules as deep in the kernel:


main [18: 22: ~] # kldstat
Id Refs Address Size Name
1 28 0xffffffff80200000 1203808 kernel
2 1 0xffffffff81404000 2bdd0 kqemu.ko
3 1 0xffffffff81612000 42a7 linprocfs.ko
4 1 0xffffffff81617000 1e00b linux.ko
5 1 0xffffffff81636000 1582 fdescfs.ko
6 1 0xffffffff81638000 64b00 radeon.ko
7 1 0xffffffff8169d000 13a2b drm.ko
8 1 0xffffffff816b1000 2655 if_tap.ko
9 1 0xffffffff816b4000 55fe if_bridge.ko
10 1 0xffffffff816ba000 353d bridgestp.ko


bridgestp.ko - loaded automatically.

Next, make sure that your main IDE-master bus supports DMA tenology (most of the architectures currently support it)

Since the kernel modules are already loaded, we will continue to prepare the host OS for the upcoming network configuration in the guest OS. After loading the module if_tap.ko, among the system variables there will appear those that regulate the operation of tap * interfaces. We are interested in two -
main [19: 26: ~] # sysctl -a | grep "net.link.tap"
net.link.tap.debug: 0
net.link.tap.devfs_cloning: 1
net.link.tap.up_on_open: 0
net.link.tap.user_open: 0


net.link.tap.up_on_open - sets the interface to the up position when / dev / tap is open. net.link.tap.user_open - allow users to open / dev / tap. To limit the use of devices / dev / tap *, change its owner and access rights to it. This can be done by setting the devfs rules in the /etc/devfs.rules file, by default it does not exist in FreeBSD 9.0, so we create it.


main [20: 12: ~] # touch /etc/devfs.rules
main [20: 18: ~] # echo "[sysqemu = 11]" >> /etc/devfs.rules
main [20: 17: ~] # echo "add path 'tap *' mode 0660 group qemu" >> /etc/devfs.rules


Now change the variables:


main [20: 20: ~] # sysctl net.link.tap.user_open = 1
net.link.tap.user_open: 0 -> 1
main [20: 21: ~] # echo net.link.tap.up_on_open = 1
net.link.tap.up_on_open = 1


Now prepare the network. It is necessary for me that it was possible to connect to all machines from outside by IP and they had access to the local network of virtual machines. So it is necessary to create a bridge interface combining 3 tap interfaces into it.


main [20: 54: ~] # ifconfig bridge0 create
main [20: 54: ~] # ifconfig bridge0 addm bfe0 addm tap0 addm tap1 addm tap2 up
main [20: 58: ~] # ifconfig bridge0
bridge0: flags = 8843 <UP, BROADCAST, RUNNING, SIMPLEX, MULTICAST> metric 0 mtu 1500
ether 02: a5: 80: 8e: f1: 00
id 00: 00: 00: 00: 00: 00 priority 32768 hellotime 2 fwddelay 15
maxage 20 holdcnt 6 proto rstp maxaddr 100 timeout 1200
root id 00: 00: 00: 00: 00: 00 priority 32768 ifcost 0 port 0
member: tap2 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 14 priority 128 path cost 2000000
member: tap1 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 12 priority 128 path cost 2000000
member: tap0 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 11 priority 128 path cost 2000000
member: bfe0 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 7 priority 128 path cost 200000


Where bfe0 is my physical interface.

The next step is to create a disk image, which we will later connect to our virtual machine. Qemu supports several image formats, they are described in detail in the man pages. I’ll focus on qemu native and the most suitable qcow2 format for the project. Its peculiarity lies in the fact that once having created with preset settings, later on when it is reluctant to use an image with the same settings, we point to the original image, and the disk of the new machine will exactly match the template, but all changes made to the new image will not be recorded in the template. This qemu process is called cow “Copy On Write”. Also this format supports encryption, creating snapshots, beer with girls and zlib compression.
In order to make changes to the base image, you need to use the command

commit [-f fmt] filename


main [23: 20: QEMU / UbuEx1] # qemu-img create -f qcow2 ubuntu_tamplate.qcow2 7G
main [23: 22: QEMU / UbuEx1] # ls -al
total 200
drwxr-xr-x 2 root wheel 512 Sep 22 23:22.
drwxr-xr-x 5 root wheel 512 Sep 21 17: 40 ...
-rw-r - r-- 1 root wheel 262144 Sep 22 23:22 ubuntu_tamplate.qcow2


This is where the creation of the neighborhood is completed. Pistup to start the emulator and install the OS ubuntu-12.04.1-server.

3. Starting and installing the OS


Consider the command syntax. Without further ado, I will sort out the most frequently needed options.
Common format:


main [13: 57: QEMU / UbuEx1] # qemu [options] [disk_image]


For the emulation of architecors ot from X86_32 we use the commands:

qemu-system-mips qemu-system-ppcemb
qemu-system-mips64 qemu-system-sh4
qemu-system-arm qemu-system-mips64el qemu-system-sh4eb
qemu-system-cris qemu-system-mipsel qemu-system-sparc
qemu-system-m68k qemu-system-ppc qemu-system-sparc64
qemu-system-microblaze qemu-system-ppc64 qemu-system-x86_64

The most common options are:



1. -smp n

Multiprocessing support, n = {1..255}, for Sparc32 or Linux architecture n = {1..4}.
2. -numa opts

Support NUMA system, uneven memory access where memory is shared between processors, which gives a performance benefit.

image

3. -fda file

-fdb file
Using an image file or a real device / dev / fd0 - a floppy host as a floppy of a QEMU machine.

4. -hda file

-hdb file
-hdc file
-hdd file
Using the file as a hard disk QEMU machine. In the x86 architecture notation -a -b floppy, -c first hdd, -d cdrom
5. -cdrom file

Using an image file as a CDROM of a QEMU machine. You cannot use the -hdc and -cdrom options at the same time. You can also use the host cdrom / dev / cdrom.
6. -drive option [, option [, option [, ...]]]

Device Definition. Images can be connected as devices, media devices, the ability to create snapshots of these devices, device types is determined.
For example:
connection cdrom
qemu-driver file = file, index = 2, media = cdrom
to the ide bus
qemu-driver file = file, if = ide, index = 1, media = cdrom
without specifying the file option, we are showing an empty device
qemu -drive if = ide, index = 1, media = cdrom

connection of disks
qemu-driver file = file, index = 0, media = disk
qemu-driver file = file, index = 1, media = disk
qemu-driver file = file, index = 2, media = disk
qemu-driver file = file, index = 3, media = disk
connect floppy drive
qemu-driver file = file, index = 0, if = floppy
qemu -drive file = file, index = 1, if = floppy
SCSI connection with unit ID = 6 bus = 0
qemu-driver file = file, if = scsi, bus = 0, unit = 6

The default interface is ide, index is incremented.
qemu -drive file = a -drive file = b is equivalent to qemu -hda a -hdb b
7. -mtdblock file

-sd file
-pflash file
Different types of flash memory.
8. -boot [order = drives] [, once = drives] [, menu = on | off]

Option defining the boot order. Drives = ab (flopy) c (hdd) d (cdrom) n (network). -boot order = ac - boot first from floppy, then from hdd
-boot once = d - boot from cdrom then reboot
9. -snapshot

Write shots to temporary files instead of raw image.
10. -m megs

Set the value of RAM memory. In GB or MB.
11. -k language

Language table:
ar de-ch es fo fr-ca hu ja mk no pt-br sv
da en-gb et fr fr-ch is lt nl pl ru th
de en-us fi fr-be hr it lv nl-be pt sl tr
default is "en-us".
12. -usb

USB driver connection.
13. -usbdevice devname
Extraction USB. Types of devices can be a mouse, tablet, disk, etc.
14. -name name

-uuid uuid
The name of the guest system. Guest ID.
15. -vga type

Type VGA card for emulation.
irrus - supported by the Windows OS family since Windows 95
std - used for high resolution. Cirrus and std are suitable in most cases.
vmware
none
16. -net tap [, vlan = n] [, name = name] [, fd = h] [, ifname = name] [, script = file] [, downscript = dfile]

Creating a network interface in the guest system. Example from man:

qemu linux.img-net nic, vlan = 0-net tap, vlan = 0, ifname = tap0 \
-net nic, vlan = 1 -net tap, vlan = 1, ifname = tap1

We see the creation of several vlan interfaces of different types, with different names. -net nic creates a pci card with a vlan interface. -net tap will connect the host tap device to the vlan0 and vlan1 interfaces.
17. -d

Log to /tmp/qemu.log
kqemu.ko
The QEMU emulator has many options, such as redirecting the host's physical devices and others, the description of all its tasty properties is beyond the scope of this article.
So, it's time to start the VM of the reference machine.


Due to some instability of the kqemu.ko module operation in FreeBSD OS, before starting the VM, we unload this module from the memory during the installation of the guest OS.

main [14: 41: dassi / QEMU] # kldunload kqemu.k



main [0: 08: dassi / QEMU] # qemu-system-x86_64-net nic, model = e1000-net tap, name = tap0, script = no-cdrom ubuntu-12.04.1-server-amd64.iso -hda tamplates /Ubuntu_T/ubuntu_tamplate.qcow2 -boot order = dc m 256 -localtime
VNC server running on `127.0.0.1:5901 '
Warning: no scancode found for keysym 0
Warning: no scancode found for keysym 0


Now connect to the VNC server and start the installation.



image

image

The next step is to configure the local network between the host and the guest OS. To get started, how it works. We need to create a physical guest OS interface that will be connected to the host OS via a virtual switch. Such a switch in qemu notation is called vlan.

image

There are several options for connecting the guest and host OS in LAN:


1. In user mode, the guest machine uses the built-in exchange system with the host.


main [19: 45: dassi / QEMU] # qemu-system-x86_64 -net nic, vlan = 0 -net user, vlan0 -hda tamplates / FreeBSD_T / freebsd_tamplate.qcow -boot c -m 256 -localtime


After running in the guest OS, you need to configure the network using the built-in dhcp server.
Get the address in the range 10.0.2.0/8.
2. Linking the host interface tun * \ tap * with the interface of the guest system.



main [19: 45: dassi / QEMU] # qemu-system-x86_64-net nic, vlan = 0-net tap, vla0, script = no, downscript = no-hda tamplates / FreeBSD_T / freebsd_tamplate.qcow -boot c -m 256 -localtime


At startup, it is recommended to configure the files / usr / local / etc / qemu-ifup and / usr / local / etc / qemu-ifdown, these are the network configuration scripts at startup / shutdown.

image

3. The third method is used when it is necessary to access guest machines from the outside.

This is achieved by creating a bridge type connection in the host machine and placing there its fzic interface and virtual tun * \ tap *. Such a connection is created between the host OS tool or by editing the startup scripts of the qemu network system. In the final setup, it repeats item 2.

image

It should be noted that the qemu system provides the ability to combine virtual machines into a network via the host OS socket, bind vlan * to the port, dump the network traffic of a specific vlan, as well as the -net none option if the network interface does not need to be canceled -net nic -net user values

So, here is a sequence of actions for organizing the network’s previous design.



main [21: 38: dassi / QEMU] # ifconfig bridge0 create
main [21: 38: dassi / QEMU] # ifconfig tap0 10.100.0.1/24
main [21: 38: dassi / QEMU] # ifconfig tap1 10.101.0.1/24
main [21: 38: dassi / QEMU] # ifconfig tap2 10.102.0.1/24
main [22: 14: dassi / QEMU] # ifconfig bridge0 addm tap0 addm tap1 addm tap2 addm bfe0
main [22: 14: dassi / QEMU] # ifconfig bridge0
bridge0: flags = 8802 <BROADCAST, SIMPLEX, MULTICAST> metric 0 mtu 1500
ether 02: a5: 80: 8e: f1: 00
id 00: 00: 00: 00: 00: 00 priority 32768 hellotime 2 fwddelay 15
maxage 20 holdcnt 6 proto rstp maxaddr 100 timeout 1200
root id 00: 00: 00: 00: 00: 00 priority 0 ifcost 0 port 0
member: bfe0 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 7 priority 128 path cost 55
member: tap2 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 14 priority 128 path cost 2000000
member: tap1 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 12 priority 128 path cost 2000000
member: tap0 flags = 143 <LEARNING, DISCOVER, AUTOEDGE, AUTOPTP>
ifmaxaddr 0 port 11 priority 128 path cost 2000000


(At this stage, the host setup is hung up, as you noticed - a pair of tap host interface + a virtual interface of the guest OS will be found in the same subnet with mask 24. bfe0 - my physical interface is looking to the internal network)

Now it is enough to start our machines and by means of the gothic OS to assign the corresponding addresses 10.0.100.2, 10.0.101.2 and 10.0.102.2, respectively, to the virtual interfaces. Great, now our cars see the internal network and are visible from it.

RESULT: We have 3 machines running Ubuntu-1pcs and FreBSD-2pcs running, working "seeing" LAN and "visible" from it. Task completed.

Resources used



man frebsd;
http://wiki.freebsd.org/qemu ;
http://www.lissyara.su/articles/freebsd/programms/qemu_lan/ .

Source: https://habr.com/ru/post/156887/


All Articles