---
# System prepended metadata

title: LS Lab 1 -  Hypervisors & Virtualization
tags: [finished]

---

###### tags: `finished`

:::success
# LS Lab 1 -  Hypervisors & Virtualization
:::

## Task 1 - Choose virtualization technology
I chose Linux KVM because it has more instructions and because it is a standard solution.

## Task 2 - Local implementation

:::info
1. **Check** `uname −r` and l`sb_release −a` first
:::

<center>
    
![](https://i.imgur.com/iFSb9tP.png)
    
Figure 1 - Checking information about the kernel version and the installed distribution
</center>



:::info
2. **Host install** – Install the hypervisor and host tools on a physical machine (make sure VT/AMD-V is enabled)
:::

First, let's check whether nested virtualization is enabled in our system:

```
cat /sys/module/kvm_intel/parameters/nested
```
<center>
    
![](https://i.imgur.com/NiQHgbV.png)
    
Figure 2 - Positive result
</center>

Now that I know that virtualization is enabled, I need to check if there are any running virtual machines.

<center>
    
![](https://i.imgur.com/MvvsAIK.png)
Figure 3 - Virsh command
</center>

To enable the nested feature permanently create `/etc/modprobe.d/kvm_intel.conf` and write following line (just for fun, it doesn't have really practice meaning in my case as I think):

```
options kvm_intel nested=1
```

:::info
3. **Guest install** – Install a guest with a local virtual disk with whatever method fits best, just to validate that you get a VM up and running. If using XEN, be clear and provide details on what type of guest you chose.
:::

<center>
    
![](https://i.imgur.com/WbATn2R.png)
    
FIgure 4 - Proof that I have a VM and it is running
</center>

Very easy way:

```
#for graphic interface of installation manager
sudo apt install virt-manager

#way for bootloaders
cd /var/lib/libvirt/boot/

#centos 8 just for example OS
sudo wget https://mirrors.edge.kernel.org/centos/8/isos/x86_64/CentOS-8.5.2111-x86_64-boot.iso
```



Verify the ISO image using the sha256sum:

<center>

![](https://i.imgur.com/5FBDpvo.png)
FIgure 5 - CHEKSUM 
</center>




:::info
4. **Sparse-file virtual disk** – Install another guest hard and DIY way. Create a SPARSE file (either with dd seek or QCOW2, for example), mount it in a folder and use debootstrap to get a Ubuntu Server system over there, quick & dirty. For VMware/HyperV users, try to do something similar.
:::

I downloaded the Ubuntu Server image and generated a 4GB sparse file:

```
dd if=/dev/zero of=ubuntu-20.04.3-live-server-amd64.iso bs=1M count=1 seek=4095

#setting up a cyclic device so that we can process the image 
#in the same way as a regular disk
sudo losetup --show -f ubuntu-20.04.3-live-server-amd64.iso
#the path to the disk 
/dev/loop15

```

The next step is to create a partition table, partition and format the disk.

```
sudo parted /dev/loop15 mklabel msdos

#create a primary partition that fills the disk
sudo parted /dev/loop15 mkpart pri ext2 0% 100%

#create an ext4 filesystem in the new partition
sudo mkfs.ext4 /dev/loop15p1

#mount the new partition to a location on my system
sudo mount /dev/loop15p1 /mnt

```

<center>
    
![](https://i.imgur.com/fFylnLI.png)
Figure 6 - Sparse and partition
</center>

Build the Image:

Now use debootstrap to build a base operating system image.
```
sudo apt install debootstrap
sudo debootstrap focal /mnt http://ru.archive.ubuntu.com/ubuntu

#mount the /dev tree inside the image so that 
#the disk devices on your system are accessible while in the chroot
sudo mount -o bind /dev /mnt/dev

# chroot into the image to get a root shell
LANG=C sudo chroot /mnt

#mount /proc and /sys so the chroot environment 
#looks like a normal root file system 
mount -t proc proc /proc
mount -t sysfs sysfs /sys
```

<center>
    
![](https://i.imgur.com/idDsKKx.png)
Figure 7 - Debootstrap
</center>

For configure image add an entry for the root filesystem to `/etc/fstab`:

```
UUID=$(blkid /dev/sdb1 | cut -d\" -f2) / ext4 errors=remount-ro 0 1
```

Add the latest versions of packages to the image `/etc/apt/sources.list`:

```
deb http://ru.archive.ubuntu.com/ubuntu focal main restricted universe multiverse
deb http://ru.archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu focal-security main restricted universe multiverse

#and update
apt update
apt -y dist-upgrade
```

Install the kernel image, the GRUB bootloader, SSH and some other useful tools for the system:

```
apt -y install linux-image-generic grub2-common openssh-server bridge-utils ethtool
```
Clear out the package cache to reduce the image size a little:
```
apt clean
```

Add the following config to /etc/default/grub to enable the serial console:

```
GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"
 
# Uncomment to disable graphical terminal (grub-pc only)
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
```
<center>
    
![](https://i.imgur.com/dCGcoS2.png)
Figure 8 - Editing /etc/default/grub
</center>


Disable the GRUB OS prober – this prevents the GRUB update command from adding entries for operating systems on other disks in the host system:
```
rm /etc/grub.d/30_os-prober
```

Update GRUB config to apply the above changes and then write the GRUB bootloader to the disk:
```
update-grub
grub-install /dev/loop15
```
Set the hostname:
```
echo ubuntu > /etc/hostname
```
Add group:
```
groupadd admin
useradd -s /bin/bash -m -d /home/user -G admin user
passwd user
```
<center>
    
![](https://i.imgur.com/ryUC2D9.png)
Figure 9 - Final steps
</center>


Unmount process:
```
exit
sudo umount /mnt/{dev,proc,sys} /mnt

#clean partition
sudo losetup -d /dev/loop0
```

<center>
    
![](https://i.imgur.com/BoEhpAX.png)
Figue 10 - Run VM
</center>


:::info
**Network** – Setup the network manually (always show how you do it in the report). For Community XEN you need to setup a bridge. For KVM/libvirt, please get rid of the default setup and do it yourself. In other words, make your guests capable to obtain a DHCP lease from the SNE network (or any other local network in the case of remote work)
:::


First of all, I changed the settings of the netplan at the stage of mounting the VM's image. Add this lines:

```
network:
    version: 2
    renderer: networkd
    ethernets:
        enp1s0:
            dhcp4: true
            
```
<center>            

![](https://i.imgur.com/zN7k3XZ.png)
Figure 11 - Checking network interfaces
</center>

Let's follow Vasiliy's guide:
1. disable NetworkManager on host:

```
#ok, not disable, just stop, because I need this service after system reboot for my previos deals
sudo systemctl stop NetworkManager.service
#for disable
sudo systemctl disable NetworkManager.service
```

2. bridge-utils packet in the host system:

```
sudo apt-get install bridge-utils
```
3. You need to set up a connection to the VM via a network bridge. It means to make changes of the host netplan config and add bridge interface, e.g. add interface eno1 under bridge br0. It creates a network bridge for the virtual machine called br0. 

    3.1 Via this interface, the VM will receive an IP address from the some DHCP pool. In turn, the host's network settings remain the same and work through the existing interface. It could not work, so find what you have to add on xml file of br0.xml on /etc/libvirt/qemu/networks/

<center>
    
![](https://i.imgur.com/bzb3Ul1.png)
Figure 12 - br0
</center>

```
#for backup
sudo cp /etc/network/interfaces /etc/network/interfaces.bakup-24-Jan-2022

#This will create a virtual interface br0
sudo vim /etc/network/interfaces
sudo /etc/init.d/networking restart
```

<center>
    
![](https://i.imgur.com/D7C3cfN.png)
    
![](https://i.imgur.com/Q66OB5Z.png)
Figure 13 - Network interfaces and DHCP
</center>

<center>
    
![](https://i.imgur.com/bKsWl6a.png)
Figure 14 - Create bridge-network.xml
</center>

4. Set up network traffic redirectin:

<center>
    
![](https://i.imgur.com/fXCB4RJ.png)
Figure 15 - Check stat of forwarding
</center>

5. network — type of network with the "network bridge" interface" should be when you write xml config for you guest:

<center>
    
![](https://i.imgur.com/5dKjBp9.png)
Figure 16 - New network type
</center>


<center>
    
![](https://i.imgur.com/fhjFXix.png)
Figure 17 - Ping check
</center>

:::info
**Text console** – Make sure you can reach the text console of the guest from the host. What configurations allows for both, the kernel and the userland system to show up there? Eventually disable the graphical console.
:::

Initially, I changed the console type in the grub settings, but since everything didn't work the first time, I had to use several service commands:

```
#logon VM and type this for enable & start serial console services
systemctl enable serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service
```

<center>
    
![](https://i.imgur.com/KzwOhUS.png)
Figure 18 - Check XML config with command `virsh edit ubuntu20.04qow2`
</center>

All is Ok, after this steps all works good.


<center>

![](https://i.imgur.com/UEwKWej.png)

    
![](https://i.imgur.com/rBopWkM.png)
Figures 19 - Text console in virsh & View mode "Serial 1" in VirtManager
</center>



:::info
**Snapshot** – Proceed with a hot-snapshot, meaning while the guest is running, take it. Attempt to make sure that the file system was properly dealt with...
:::

For this I used following commands:

```
#convert raw image to qcow2 format, 
#raw disk format is not supported by kvm virsh command
qemu-img convert -f raw -O qcow2 ubuntu-20.04.3-live-server-amd64.img ubuntu-20.04.3-live-server-amd64.qcow2

#create a snapshot
 virsh snapshot-create-as --domain ubuntu20.04qow2 --name ubuntu20.04qow2_snap --description "first snap for lab"

```

Using the queries below, we can see the snapshots associated with the image, information on each of them and their size. In this case, my snapshot of the Ubuntu Server image weighs 524.288 MB (500 MiB).
<center>
    
![](https://i.imgur.com/4T5vyCV.png)

![](https://i.imgur.com/Y0FStN0.png)
Figures 20 - Snap's stat
    

</center>

## Task 3 - Cluster validation


:::info
Now as a team, choose which one of the two machines is also going to provide the shared storage for virtual disks to live in. Set it up and share both ideally, guests virtual disks and configurations.
:::



1. Take team member 1’s favorite guest (virtual disk and configuration) and put it in the shared storage.
2. Take team member 2’s favorite guest (virtual disk and configuration) and put it in the shared storage.


My mashine will be the server (shared storage) and the Vladimir's workstation will be the host (10.1.1.212). 

First of all we need some tools:


```
#for ssh-connection to the Vladimir's host
sudo apt install ssh-askpass

#for shared storage
sudo apt install nfs-kernel-server

#permission for VM' images
sudo chown nobody:nogroup /var/lib/libvirt/images/
```

For NFS:

```
sudo nano /etc/exports
service nfs-kernel-server restart
```

<center>
    
![](https://i.imgur.com/ExXVuW1.png)
Figure 21 - Adding line for shared storage
    
![](https://i.imgur.com/Fu9fqBU.png)   
Figure 22 - And new filesystem's path for virtual guest
</center>



As you can see, Vladimir can open shared storage:

<center>
![](https://i.imgur.com/pcXQJXk.jpg)
Figure 23 - Shared storage
</center>

<center>
    
![](https://i.imgur.com/XGtEOo9.png)
Figure 24 - Connection to the Vladimir's VM (also will need his pass)
    
![](https://i.imgur.com/Gffefbx.png)
Figure 25 - Result

</center>


3. Eventually fix the pathes in the configuration and validate that both guests run as well as before. 

We don't need it because we mount in the one folder. And the both guests run as well as before. In video "Part2" you can see, how Vladimir can start my img on his own system from shared storage. As you can see in the pictures below I also can take Vladimir's VM image and start it.

<center>
    
![](https://i.imgur.com/F0s8ZFh.png)

![](https://i.imgur.com/MErq615.png)
Figure 26 - Vladimirs's VM img
</center>

4. Now shut them down and run them on the other team member’s machine/host (cold-migration)

As you can see in video "Part 1" I run my machine, logon, create a directory and after that create a file and shut them down.

```
mkdir /test
sudo vim test.txt
        "Hello world!"
:wq
```
After that Vladimir mounted it on his system (video "Part 2"), create VM with my system image and can open and find this file. You can see it on his report and in video, but otherwise, it is look like this:

<center>
    
![](https://i.imgur.com/oi0sL3e.png)
Figure 27 - Vladimir's work in Virt-Manager
</center>

5. Now don’t even shut them down while migrating... (hot-migration/live-migration.

For this I should connect to my VM via Virt-Manager, run it and choose migrate and choose Vladimir's KVM as a host (10.1.1.212). In this video "Part 3" you can see, that VM is running on my workstation and our machines has live-connection (video "Part 4" from Vladimir side). 

<center>
    
![](https://i.imgur.com/VCoHwgI.png)
Figure 28 - Migrate settings
    
    
![](https://i.imgur.com/UxFuJEG.png)
Figure 29 - Migrate process
</center> 

<center>

![](https://i.imgur.com/Cw22m2H.png)
Figure 30 - Migrate process on Vladimir's side
</center>


## References:
1. [Nested virtualization in KVM](https://stafwag.github.io/blog/blog/2018/06/04/nested-virtualization-in-kvm)
2. [Debootstrap Ubuntu/Debian](https://jeremy.geek.nz/tag/debootstrap/)
3. [Serial Comsole](https://help.ubuntu.com/community/SerialConsoleHowto)
4. [Download Ubuntu Server](https://ubuntu.com/download/server)
5. [Snapshot on KVM](https://www.linuxtechi.com/create-revert-delete-kvm-virtual-machine-snapshot-virsh-command/)
6. [Bridge install for KVM](https://linuxconfig.org/how-to-use-bridged-networking-with-libvirt-and-kvm)
7. [KVM Migration](http://www.linux-kvm.org/page/9p_virtio)