Virt-install usage example: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
==Build VMs up to Ubuntu 18.04== | |||
<syntaxhighlight lang="bash">#!/bin/bash | <syntaxhighlight lang="bash">#!/bin/bash | ||
Line 65: | Line 66: | ||
preseed.cfg file can be found [[preseed file|here]]. | preseed.cfg file can be found [[preseed file|here]]. | ||
==Build Ubuntu 20.04 VMs== | |||
In Ubuntu 20.04 the Debian Installer (d-i) has been tagged as legacy which made virt-install unable to detect the linux kernel and initrd files, so we need to specify their exact location. But to do this we need at least virt-install 2.2.0. | |||
===On Ubuntu 18.04 or older hosts=== | |||
Comes with versions older than 2.2.0 so we need to manually install a newer version. First install these dependencies ('''Note''': the list might be incomplete, needs testing) | |||
<pre>apt-get install python-libvirt | |||
apt-get install pkg-config | |||
apt-get install python3-pip | |||
apt-get install libglib2.0 | |||
apt-get install libxml2 | |||
pip3 install libxml2 | |||
apt-get install gobject-introspection | |||
apt-get install python-docutils</pre> | |||
Download virt-manager 3.0.0 (includes virt-install) from here https://github.com/virt-manager/virt-manager/archive/v3.0.0.zip | |||
<pre>wget https://github.com/virt-manager/virt-manager/archive/v3.0.0.zip | |||
unzip v3.0.0.zip</pre> | |||
Check that you have all dependencies by running virt-install | |||
<pre>cd virt-manager-3.0.0 | |||
./virt-install --version</pre> | |||
If it runs alright run the installer. If not check missing dependencies. | |||
<pre>./setup.py install</pre> | |||
===On Ubuntu 20.04 hosts=== | |||
Comes with version 2.2.1 so just install from ubuntu repo if you didn't already. | |||
<pre>apt-get install virtinst</pre> | |||
===Script for building Ubuntu 20.04=== | |||
<syntaxhighlight lang="bash">#!/bin/bash | |||
#####Setup up for the guest | |||
host_name='vm-name' | |||
user_name='username' | |||
password='password' | |||
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 \ | |||
--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" \ | |||
--initrd-inject=preseed.cfg \ | |||
--extra-args 'locale=en_US 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</syntaxhighlight> | |||
Main difference is that "--location" option got replaced by "--install" and variable "distro" disappeared. The [[preseed file|preseed.cfg]] file is the same as the one above |
Revision as of 11:26, 30 September 2020
Build VMs up to Ubuntu 18.04
#!/bin/bash
#####Setup up for the guest
host_name='vm-name'
user_name='username'
password='password'
distro='bionic'
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 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 made virt-install unable to detect the linux kernel and initrd files, so we need to specify their exact location. But to do this we need at least virt-install 2.2.0.
On Ubuntu 18.04 or older hosts
Comes with versions older than 2.2.0 so we need to manually install a newer version. First install these dependencies (Note: the list might be incomplete, needs testing)
apt-get install python-libvirt apt-get install pkg-config apt-get install python3-pip apt-get install libglib2.0 apt-get install libxml2 pip3 install libxml2 apt-get install gobject-introspection apt-get install python-docutils
Download virt-manager 3.0.0 (includes virt-install) from here https://github.com/virt-manager/virt-manager/archive/v3.0.0.zip
wget https://github.com/virt-manager/virt-manager/archive/v3.0.0.zip unzip v3.0.0.zip
Check that you have all dependencies by running virt-install
cd virt-manager-3.0.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
Script for building Ubuntu 20.04
#!/bin/bash
#####Setup up for the guest
host_name='vm-name'
user_name='username'
password='password'
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 \
--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" \
--initrd-inject=preseed.cfg \
--extra-args 'locale=en_US 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
Main difference is that "--location" option got replaced by "--install" and variable "distro" disappeared. The preseed.cfg file is the same as the one above