# Virtual Machine on Ubuntu20.04
###### tags: `Medium`
## [0. System and Hardware Information](https://www.tecmint.com/commands-to-collect-system-and-hardware-information-in-linux/)
:::success
This is the information for my Ubuntu20.04 that I'm using to create Virtual Machines (VMs) on. But this guide should be applicable to all Ubuntu20.04 OS
:::
- System Information:
```bash
$ uname
Linux
$ uname -m
x86_64
```
- System Hardware Information
```bash
$ lshw -short
H/W path Device Class Description
============================================================
system Computer
/0 bus Motherboard
/0/0 memory 128GiB System memory
/0/1 processor Intel(R) Xeon(R) Gold 6212U CPU @ 2.40GHz
```
- CPU Information
```shell
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 48
On-line CPU(s) list: 0-47
Thread(s) per core: 2
Core(s) per socket: 24
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6212U CPU @ 2.40GHz
```
## 1. Create our first VM
:::success
We will create a Ubuntu18.04 using the [**ubuntu-18.04.6-desktop-amd64.iso**](https://releases.ubuntu.com/18.04/) image and enable openSSH for ease of development.
:::
- [KVM Installation](https://ubuntu.com/blog/kvm-hyphervisor)
```bash
# Step 1: Install required packages
$ sudo apt -y install bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu qemu-kvm
# Step 2: Check virtualisation capabilities
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
```
- Create a guess network using NAT interface
```bash
# Step 1: Create a network configuration file with your prefered subnet range
$ cat kvm-network.xml
<network>
<name>kvm</name>
<forward mode='nat'/>
<domain name='kvm'/>
<ip address='10.0.3.1' netmask='255.255.255.0'>
<dhcp>
<range start='10.0.3.2' end='10.0.3.254'/>
</dhcp>
</ip>
</network>
# Step2 : Define network
$ virsh net-define kvm-network.xml
Network kvm defined from kvm-network.xml
# Step 3: Start the network and mark as autostart
$ virsh net-start kvm
Network kvm started
$ virsh net-autostart kvm
Network kvm marked as autostarted
# Step 4: Check network configuration
$ virsh net-list
Name State Autostart Persistent
-----------------------------------------
kvm active yes yes
```
- Create Ubuntu18.04 VM
```bash
# Step 1: Download the image
$ wget https://releases.ubuntu.com/18.04/ubuntu-18.04.6-desktop-amd64.iso
# Step 2: Create a qemu qcow2 storage
$ qemu-img create -f qcow2 node-0.qcow2 32G
Formatting 'node-0.qcow2', fmt=qcow2 size=34359738368 cluster_size=65536 lazy_refcounts=off refcount_bits=16
# Step 3: Create the VM
$ virt-install \
--name node-0 \
--memory 32768 \
--vcpus 16 \
--cdrom [path/to/your/mini.iso] \
--disk [path/to/your/node-0.qcow2],bus=virtio,size=10,format=qcow2 \
--network network=kvm \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--os-type=linux \
--os-variant=ubuntu18.04
Starting install...
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
```
- Continue our [Ubuntu installation](https://docs.openstack.org/image-guide/ubuntu-image.html) using the KVM's GUI `$ virt-manager`. I will use all default config except the following options:
- Hostname: `node-0`
- Username and Password: `master`
- Before we restart our VM to finish the installation, we can configure static IP address for our VM:
- First, take note of the current MAC address and IP address using the KVM's GUI

- Second, edit the guess network configuration file and restart the network and VM for the change to take effect
```bash
# Step 1: Add the following configuration under network.ip.dhcp
$ virsh net-edit kvm
<network>
<ip address='10.0.3.1' netmask='255.255.255.0'>
<dhcp>
<range start='10.0.3.2' end='10.0.3.254'/>
<host mac='52:54:00:2e:c8:4c' name='node-0' ip='10.0.3.2'/>
</dhcp>
</ip>
</network>
# Step 2: Restart the network
$ virsh net-destroy kvm
$ virsh net-start kvm
# Step 3: Restart the VM for the change to take effect
$ virsh shutdown node-0
$ virsh start node-0
# Step 4: Check the newly assigned IP
$ virsh net-dhcp-leases kvm
Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
-----------------------------------------------------------------------------------------------------------------------------------------
2022-11-22 17:07:24 52:54:00:2e:c8:4c ipv4 10.0.3.2/24 node-0 ff:56:50:4d:98:00:02:00:00:ab:11:56:78:25:52:47:eb:44:93
```
- We can login to our VM to install some main utilities and enable SSH
```bash
$ sudo apt-get update
$ sudo apt-get install openssh-server ubuntu-desktop tmux -y
$ systemctl enable ssh --now
```
- We can make use of snapshot to safeguard our development progress:
```bash
# Create snapshot (shut down first to capture both memory and disk state)
virsh shutdown node-0
virsh snapshot-create-as \
--name [your-snapshot-name] \
--domain node-0
virsh start node-0
# Check snapshot
virsh snapshot-list node-0
# Revert to snapshot
virsh shutdown node-0
virsh snapshot-revert \
--snapshotname [your-snapshot-name] \
--running \
--domaain node-0
# Delete snapshot
virsh snapshot-delete \
--snapshotname [your-snapshot-name] \
--domain node-0
```