Virt-install usage example
From Newroco Tech Docs
Revision as of 07:07, 11 March 2019 by Emilian.mitocariu (talk | contribs)
#!/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="br0" ip_adress='10.0.10.222' netmask='255.255.0.0' gateway='10.0.100.40' nameservers='10.0.10.70' 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 none \ --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' else echo "File preseed.cfg does not exist." fi
preseed.cfg file can be found here.