# Install Cuju & libvirt on Ubuntu18
[Install Cuju & libvirt on Centos7](https://hackmd.io/@4jocGoC0SRi106skGiCKfw/rythJQD2D)
Here is the architecture diagram for Cuju, you will need three node: **primary node**, **backup node** and **NFS server** to build a complete system.
![](https://i.imgur.com/NE99Bsd.png)
## NFS server step
1. Install the NFS service (Network FileSystem) in NFS node
```shell
$ sudo apt update
$ sudo apt install nfs-kernel-server
```
2. Create nfsfolder
```shell
$ mkdir /home/[your username]/nfsfolder
```
3. Insert this line in ==/etc/exports== to add your NFS folder:
```shell
/home/[your username]/nfsfolder *(rw,no_root_squash,no_subtree_check)
```
4. Restart nfs-server
```shell
$ /etc/init.d/nfs-kernel-server restart
```
5. Put the VM image in this NFS, you can creat image by youself, or download our example image **[Ubuntu-16.04 VM image](https://drive.google.com/file/d/0B9au9R9FzSWKNjZpWUNlNDZLcEU/view)**
- Or you can use the script below to get the image in terminal
```shell
$ wget --load-cookies /tmp/download.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/download.txt --keep-session-cookies 'https://docs.google.com/uc?export=download&id=0B9au9R9FzSWKNjZpWUNlNDZLcEU' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=0B9au9R9FzSWKNjZpWUNlNDZLcEU" -O 'Ubuntu20G-1604.tar.gz' && rm -rf /tmp/download.txt
$ tar -zxvpf Ubuntu20G-1604.tar.gz -C ~/nfsfolder
```
## Primay and backup node
### Step 1 Install Ubuntu operating system
#### Install
1. Download ubuntu-18.04.1-live-server-amd64.iso, you can download from **[here](http://old-releases.ubuntu.com/releases/18.04.1/ubuntu-18.04.1-live-server-amd64.iso)**
2. Install Ubuntu18
#### Kernel update
Since Cuju is only supported in kernel 4.4&4.15, the kernel in ubuntu18 needs to be downgrade to 4.15. Use command to downgrade your kernel version.
1. Downgrade kernel version
```shell
$ sudo apt update
$ sudo apt-get install aptitude
$ sudo aptitude install linux-image-4.15.0-29-generic linux-headers-4.15.0-29-generic
```
2. Set kernel 4.15 as the default kernel version
```shell
$ sudo vim /etc/default/grub
```
```diff=
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'
-GRUB_DEFAULT=0
+GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 4.15.0-29-generic"
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
```
```shell
sudo update-grub
```
3. Reboot
```shell
$ sudo reboot
```
4. After reboot, check whether the kernel is successfully update
```shell
$ uname -r
```
![](https://i.imgur.com/sOBXBf3.png)
After updating the kernel successfully, start to install Cuju and libvirt
### Step 2 Install Cuju
1. Install require package first
```shell
$ sudo apt install ssh vim qemu tigervnc-viewer nfs-common libvirt-bin libvirt-daemon libvirt-daemon-driver* libvirt-dev libvirt-sanlock
```
2. Download the compressed file **[cuju_libvirt_deb.tar.gz](https://drive.google.com/file/d/1LtmqgyhUkQCcqzO85CZCv6heFuvotUd9/view)** and put it in the home folder. You can use the script below to get the file in terminal
```shell
$ wget --load-cookies /tmp/download.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/download.txt --keep-session-cookies 'https://docs.google.com/uc?export=download&id=1LtmqgyhUkQCcqzO85CZCv6heFuvotUd9' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1LtmqgyhUkQCcqzO85CZCv6heFuvotUd9" -O 'cuju_libvirt_deb.tar.gz' && rm -rf /tmp/download.txt
```
3. Decompress ==cuju_libvirt_deb.tar.gz==
```shell
$ tar -zxvpf cuju_libvirt_deb.tar.gz -C ~/
```
4. Run ==install_cuju.sh== in the cuju folder
```shell
$ cd ~/cuju_libvirt_deb/cuju
$ ./install_cuju.sh
```
### Step 3 Install libvirt
Because we have added some commands to libvirt, you need to remove the original libvirt and install our modified version.
1. Remove original libvirt
```shell
$ sudo apt remove --purge libvirt*
```
2. Run ==install_libvirt.sh== in the libvirt folder
```shell
$ cd ~/cuju_libvirt_deb/libvirt
$ ./install_libvirt.sh
```
3. If the installation is complete, check whether libvirtd is successfully started
```shell
$ sudo systemctl status libvirtd
```
![](https://i.imgur.com/Z4QNGr0.png)
### Step 4 Cuju test environment setting
![](https://i.imgur.com/NE99Bsd.png)
1. Follow cuju's architecture to set up the bridge and network environment on **primary** and **backup** node, for example:
- primary
```shell
$ sudo vim /etc/netplan/01-network-manager-all.yaml
```
```shell
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
ens4:
dhcp4: false
addresses: [192.168.122.11/24]
mtu: 1500
nameservers:
addresses: [8.8.8.8]
bridges:
br0:
interfaces: [ens3]
addresses: [192.168.123.1/24]
gateway4: 192.168.123.254
mtu: 1500
nameservers:
addresses: [140.96.254.98]
parameters:
stp: true
forward-delay: 4
dhcp4: no
```
- backup
```shell
$ sudo vim /etc/netplan/01-network-manager-all.yaml
```
```shell
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
ens4:
dhcp4: false
addresses: [192.168.122.12/24]
mtu: 1500
nameservers:
addresses: [8.8.8.8]
bridges:
br0:
interfaces: [ens3]
addresses: [192.168.123.2/24]
gateway4: 192.168.123.254
mtu: 1500
nameservers:
addresses: [8.8.8.8]
parameters:
stp: true
forward-delay: 4
dhcp4: no
```
2. Restart the network
```shell
$ sudo netplan apply
```
3. Insert your host ip and host name in ==/etc/hosts== to let primary and backup node know each other.
```shell
192.168.122.11 ubuntu1
192.168.122.12 ubuntu2
```
- If the hostnames are same, you need to change them first
```shell
$ sudo hostnamectl set-hostname new_host_sname
```
4. Let **primary** and **backup** node mount the NFS folder
```shell
$ sudo mkdir /mnt/nfs
$ sudo mount -t nfs 192.168.123.3:/home/[your nfs username]/nfsfolder /mnt/nfs
```
5. Create a vm in **primary node**. We have created a sample xml ==cuju_vm.xml==in the cuju_libvirt_deb folder. After modifying the part shown below in the xml file to match your image location and type, define it as a libvirt's VM.
- This version does not support SPICE device and display. (We will update soon.)
```shell
$ cd ~/cuju_libvirt_deb/
$ vim cuju_vm.xml
```
```xml=
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache="none"/>
<source file='/mnt/nfs/Ubuntu20G-1604.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
```
```shell
$ sudo virsh define cuju_vm.xml
```
![](https://i.imgur.com/cl2TGGs.png)
6. Then you can see cuju_vm is defined in libvirt
```shell
sudo virsh list --cuju --all
```
![](https://i.imgur.com/u03B5YL.png)
After performing the above steps, you can start to test the function of Cuju.
## Cuju's Fault Tolerance
### CujuFT state explanation
Here is the all stat `sudo virsh list --cuju --all` will show
![](https://i.imgur.com/LWdqgI7.png)
| stat | explanation |
|:----------------:|:------------------------------------------------------------------------------------------------------------------------------:|
| no FT | VM has not entered FT mode after booting |
| FT mode(primary) | VM is now in FT mode, located on the primary side |
| FT mode(backup) | VM is now in FT mode, located on the backup side |
| no backup | The VM has left the FT mode, which can be seen on the backup side after fail over or on the priamry side after executing no-FT |
### Function 1 Enter FT mode
1. Start cuju_vm vm
```shell
$ sudo virsh start cuju_vm
```
![](https://i.imgur.com/YdPJZgp.png)
2. Use VNC to see the console at the primary node
```shell
$ vncviewer :5900 &
```
- **The default account/password is root/root if you use we provide guest image**
3. Check the VM status on primary and backup node
```shell
$ sudo virsh list --cuju --all
```
- primary node
![](https://i.imgur.com/5IjDVG3.png)
- backup node
![](https://i.imgur.com/C6z3eHN.png)
4. Start Fault Tolerance, libvirt will automatically create VM on the backup node.
```shell
$ sudo virsh migrate --live --cuju cuju_vm qemu+ssh://[your backup node user name]@192.168.122.12/system
```
![](https://i.imgur.com/JyaYWtd.png)
- primary node
![](https://i.imgur.com/YTbKgcX.png)
- backup node
![](https://i.imgur.com/3G1vSy8.png)
### Function 2 Test fail over
1. Kill VM on primary node(Imitating vm was crashed)
```shell
$ sudo kill $(ps aux | grep qemu-kvm | grep guest=cuju_vm | awk '{print $2}')
```
- Or you can use our failover instructions on backup side
```shell
$ sudo virsh failover cuju_vm
```
- primary node
![](https://i.imgur.com/u03B5YL.png)
- backup node
![](https://i.imgur.com/0zp8rmg.png)
2. Use VNC to see the console at the backup node
```shell
$ vncviewer :5900 &
```
### Function 3 Exit FT mode
1. If you want to exit FT mode, you can use this command, it will make primary VM exit FT mode and shut down backup VM
```shell
$ sudo virsh no-ft cuju_vm
```
- primary node
![](https://i.imgur.com/u03B5YL.png)
- backup node
![](https://i.imgur.com/C6z3eHN.png)