Virt-install usage example: Difference between revisions
From Newroco Tech Docs
Jump to navigationJump to search
No edit summary |
|||
Line 5: | Line 5: | ||
#####Setup up for the guest | #####Setup up for the guest | ||
host_name=' | host_name='vm-name' | ||
user_name=' | user_name='your-username' | ||
password=' | password='password' | ||
distro='bionic' | distro='bionic' | ||
virt_ram=' | virt_ram='1024' | ||
disk_path='/var/lib/kvm' | disk_path='/var/lib/kvm' | ||
disk_size= | disk_size=11 | ||
virt_cpu='2' | virt_cpu='2' | ||
network_bridge=" | network_bridge="virbr0" # virbr0 | ||
ip_adress='192.168. | ip_adress='192.168.11.36' | ||
netmask='255.255.255.0' | netmask='255.255.255.0' | ||
gateway='192.168. | gateway='192.168.11.1' | ||
nameservers='192.168. | nameservers='192.168.11.1' | ||
##### Change | 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/get_domain string.*|d-i netcfg/get_domain string $host_name|" preseed.cfg | |||
sed -i "s|^d-i netcfg/ | 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/ | 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 | sed -i "s|^d-i passwd/user-fullname string.*|d-i passwd/user-fullname string $user_name|" preseed.cfg | ||
sed -i "s|^d-i | sed -i "s|^d-i passwd/username string.*|d-i passwd/username string $user_name|" preseed.cfg | ||
sed -i "s|^d-i | sed -i "s|^d-i passwd/user-password password.*|d-i passwd/user-password password $password|" preseed.cfg | ||
sed -i "s|^d-i | sed -i "s|^d-i passwd/user-password-again password.*|d-i passwd/user-password-again password $password|" preseed.cfg | ||
##### Create folder where disk will be stored | ##### Create folder where disk will be stored | ||
mkdir -p ${disk_path}/${host_name} | |||
#### Run virt-install command | |||
virt-install \ | |||
#### Run virt-install command | --name $host_name \ | ||
--ram $virt_ram \ | |||
virt-install \ | --disk path=${disk_path}/${host_name}/${host_name}.qcow2,size=${disk_size},format=qcow2,bus=virtio \ | ||
--name $host_name \ | --vcpus ${virt_cpu} \ | ||
--ram $virt_ram \ | --os-type linux \ | ||
--disk path=${disk_path}/${host_name}/${host_name}.qcow2,size=${disk_size},format=qcow2,bus=virtio \ | --os-variant auto \ | ||
--vcpus ${virt_cpu} \ | --autostart \ | ||
--os-type linux \ | --network bridge=$network_bridge \ | ||
--os-variant auto \ | --graphics none \ | ||
--autostart \ | --console pty,target_type=serial \ | ||
--network bridge=$network_bridge \ | --location "http://archive.ubuntu.com/ubuntu/dists/${distro}/main/installer-amd64/" \ | ||
--graphics none \ | --initrd-inject=preseed.cfg \ | ||
--console pty,target_type=serial \ | --extra-args 'locale=en_US auto=true priority=critical console=ttyS0,115200n8 serial file=file:/preseed.cfg' | ||
--location "http://archive.ubuntu.com/ubuntu/dists/${distro}/main/installer-amd64/" \ | else | ||
--initrd-inject=preseed.cfg \ | echo "File preseed.cfg does not exist." | ||
--extra-args 'locale=en_US auto=true priority=critical console=ttyS0,115200n8 serial file=file:/preseed.cfg'</pre> | fi</pre> | ||
preseed.cfg file can be found [[preseed file|here]]. | preseed.cfg file can be found [[preseed file|here]]. |
Revision as of 06:41, 8 August 2018
Building VMs with virt-install
The script that builds the VM:
#!/bin/bash #####Setup up for the guest host_name='vm-name' user_name='your-username' password='password' distro='bionic' virt_ram='1024' disk_path='/var/lib/kvm' disk_size=11 virt_cpu='2' network_bridge="virbr0" # virbr0 ip_adress='192.168.11.36' netmask='255.255.255.0' gateway='192.168.11.1' nameservers='192.168.11.1' 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/get_domain string.*|d-i netcfg/get_domain string $host_name|" 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 ##### 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 \ --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.