Virt-install usage example

From Newroco Tech Docs
Jump to navigationJump to search

After running this script you have to use command below to enter manually your username.

#!/bin/bash
virsh console <domain_name>


#!/bin/bash

#####Setup up for the guest

host_name='vm-name'
user_name='username'
password='password'

distro='focal'
virt_ram='1024'   #size in MB
swap='2048'       #size in MB
disk_path='/var/lib/kvm'
disk_size=12      #size in GB, NOTE: the swap will be subtracted from this size, so use more if you also need more swap 
virt_cpu='2'

network_bridge="virbr1"
ip_adress='172.17.xxxx.xxxx'
netmask='255.255.255.0'
gateway='172.17.xxxx.2'
nameservers='172.17.xxxx.2'

if [ -f preseed.cfg ]; then
        ##### Change variable into the preseed.cfg file
        sed -i "s|^d-i netcfg/get_hostname string.*|d-i netcfg/get_hostname string $host_name|" preseed.cfg
        sed -i "s|^d-i netcfg/hostname string.*|d-i netcfg/hostname string $host_name|" preseed.cfg
        sed -i "s|^d-i netcfg/get_domain string.*|d-i netcfg/get_domain string defaultdomain|" preseed.cfg

        sed -i "s|^d-i netcfg/get_ipaddress string.*|d-i netcfg/get_ipaddress string $ip_adress|" preseed.cfg
        sed -i "s|^d-i netcfg/get_netmask string.*|d-i netcfg/get_netmask string $netmask|" preseed.cfg
        sed -i "s|^d-i netcfg/get_gateway string.*|d-i netcfg/get_gateway string $gateway|" preseed.cfg
        sed -i "s|^d-i netcfg/get_nameservers string.*|d-i netcfg/get_nameservers string $nameservers|" preseed.cfg

        sed -i "s|^d-i passwd/user-fullname string.*|d-i passwd/user-fullname string $user_name|" preseed.cfg
        sed -i "s|^d-i passwd/username string.*|d-i passwd/username string $user_name|" preseed.cfg
        sed -i "s|^d-i passwd/user-password password.*|d-i passwd/user-password password $password|" preseed.cfg
        sed -i "s|^d-i passwd/user-password-again password.*|d-i passwd/user-password-again password $password|" preseed.cfg

        sed -i "s|^              .* .* .* linux-swap|              $swap $swap $swap linux-swap|" preseed.cfg

        ##### Create folder where disk will be stored
        mkdir -p ${disk_path}/${host_name}

        #### Run virt-install command
        virt-install \
        --name $host_name \
        --ram $virt_ram \
        --disk path=${disk_path}/${host_name}/${host_name}.qcow2,size=${disk_size},format=qcow2,bus=virtio \
        --vcpus ${virt_cpu} \
        --os-type linux \
        --os-variant auto \
        --autostart \
        --network bridge=$network_bridge,model=virtio \
        --graphics spice --video qxl --channel spicevmc \
        --console pty,target_type=serial \
        --location "http://archive.ubuntu.com/ubuntu/dists/${distro}/main/installer-amd64/" \
        --initrd-inject=preseed.cfg \
        --extra-args 'locale=en_US.UTF-8 auto=true priority=critical console=ttyS0,115200n8 serial file=file:/preseed.cfg'

        #### Add host name and IP address to /etc/hosts

        echo "$ip_adress $host_name nrc-$host_name" >> /etc/hosts
else
        echo "File preseed.cfg does not exist."
fi

preseed.cfg file can be found here.

Build Ubuntu 20.04 VMs

In Ubuntu 20.04 the Debian Installer (d-i) has been tagged as legacy which makes virt-install versions older than 3.0.0 unable to detect the linux kernel and initrd files.

On Ubuntu 18.04 or older hosts

Comes with version 1.5.1 so we need to manually install a newer version. First install these dependencies

apt-get install gobject-introspection pkg-config python-docutils libvirt-dev python3-libxml2 gettext python3-libvirt libglib2.0-dev python3-requests

Download virt-manager 3.1.0 (includes virt-install) from here https://github.com/virt-manager/virt-manager/archive/v3.1.0.zip

wget https://github.com/virt-manager/virt-manager/archive/v3.1.0.zip
unzip v3.1.0.zip

Check that you have all dependencies by running virt-install

cd virt-manager-3.1.0
./virt-install --version

If it runs alright run the installer. If not check missing dependencies.

./setup.py install

On Ubuntu 20.04 hosts

Comes with version 2.2.1 so just install from ubuntu repo if you didn't already.

apt-get install virtinst

This version doesn't detect the kernel/initrd files either but has an option to manually specify their exact location. In the script replace

--location "http://archive.ubuntu.com/ubuntu/dists/${distro}/main/installer-amd64/" \

with

--install kernel="http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/current/legacy-images/netboot/ubuntu-installer/amd64/linux",initrd="http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/current/legacy-images/netboot/ubuntu-installer/amd64/initrd.gz" \