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.

virsh console <domain_name>


#####Setup up for the guest


virt_ram='1024'   #size in MB
swap='2048'       #size in MB
disk_size=12      #size in GB, NOTE: the swap will be subtracted from this size, so use more if you also need more swap 


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 "${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
        echo "File preseed.cfg does not exist."

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


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.

./ 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 "${distro}/main/installer-amd64/" \


--install kernel="",initrd="" \