Skip to content

Instantly share code, notes, and snippets.

@cvrolf
Last active November 1, 2017 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvrolf/2aad36d78e7803b7970cefa4452b76d9 to your computer and use it in GitHub Desktop.
Save cvrolf/2aad36d78e7803b7970cefa4452b76d9 to your computer and use it in GitHub Desktop.
#@RunAs: Open a Terminal with user root @ Master Machine*****
###
### Bash Shell behavior
# @doc -e Exit immediately if a command returns a non-zero status.
# @doc -u Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error when performing parameter expansion.
# @doc -x Print a trace of commands after they are expanded and before they are executed.
set -u
set -x
###
### Global vars
MYDOMAIN="myvmtest"
MYIPADDRESS=192.168.122.102
MYMACADDRESS=52:54:00:ff:ff:66
###
### Check you are on the correct host: Host Machine (not the VM)
hostname
whoami
if [ "`hostname`" == "${MYDOMAIN}" ]; then
echo "ABORT. This is not a session on the Host Machine"
sleep 10
exit 1
else
echo "OK. This is a session on the Host Machine"
fi
###
### Prepare
mkdir ~/tmp
cd ~/tmp
##
## Requirement: Check CPU supports VM's
apt --yes install cpu-checker
if ! kvm-ok; then
echo "ABORT. This system lacks Virtualization Technology.";
sleep 10;
exit 1;
else
echo "OK. This system supports Virtualization Technology.";
fi
##
## Install libvirt
# virtinst (admin): Programs to create and clone virtual machines
# libvirt-clients (admin): Programs for the libvirt library
# libvirt-bin (devel): programs for the libvirt library. Provides libvirtd which you need to administer VM instances using libvirt.
# virt-manager (admin): desktop application for managing virtual machines
# libosinfo-bin (libs): Library for managing information about operating systems and hypervisors [universe]
apt --yes install \
qemu-kvm \
libvirt-bin libvirt-clients virt-manager virtinst \
libosinfo-bin
##
## Linux User Config
# @doc In recent releases (>= Ubuntu Yakkety) the group was renamed from libvirtd to libvirt. Upgraded systems get a new libvirt group with the same gid as the libvirtd group to match that.
# @fix QEMU: ISO permission denied for user <root>.
cat /etc/group | grep "libvirt"
adduser root libvirt
adduser ubuntu libvirt
adduser root libvirtd # legacy
adduser ubuntu libvirtd # legacy
MYFILE=/etc/libvirt/qemu.conf
MYBACKUPFILE=~/--etc--libvirt--qemu.conf.backup
if [ ! -f ${MYBACKUPFILE} ]; then
echo "OK. Making backup..."
cp --verbose ${MYFILE} ${MYBACKUPFILE}
else
echo "OK. Backup file already exists ${MYBACKUPFILE} :)"
fi
if [ ! -f ${MYFILE} ]; then
echo "ERROR. Cannot find file ${MYFILE}"
else
sed --in-place \
--expression 's/^#user = "root"/\n###RHMOD @fix ISO permission denied. Uncomment.\n# DEFAULT #user = "root"\nuser = "root"\n/' \
--expression 's/^#group = "root"/\n###RHMOD @fix ISO permission denied. Uncomment.\n# DEFAULT #group = "root"\ngroup = "root"\n/' \
${MYFILE}
egrep 'user = ' ${MYFILE} && echo "OK" || echo "NOT OK"
egrep 'group = ' ${MYFILE} && echo "OK" || echo "NOT OK"
fi
##
## DAEMON libvirtd Config
# 1: DEBUG
# 2: INFO
# 3: WARNING
# 4: ERROR
MYFILE=/etc/libvirt/libvirtd.conf
MYBACKUPFILE=~/--etc--libvirt--libvirtd.conf.backup
if [ ! -f ${MYBACKUPFILE} ]; then
echo "OK. Making backup..."
cp --verbose ${MYFILE} ${MYBACKUPFILE}
else
echo "OK. Backup file already exists ${MYBACKUPFILE} :)"
fi
if [ ! -f ${MYFILE} ]; then
echo "ERROR. Cannot find file ${MYFILE}"
else
sed --in-place \
--expression 's/^#log_level = 3/\n###RHMOD\n#log_level = 3\nlog_level = 2\n/' \
--expression 's/^#log_outputs="3:syslog:libvirtd"/\n###RHMOD\n#log_outputs="3:syslog:libvirtd"\nlog_outputs="2:syslog:libvirtd"\n/' \
${MYFILE}
egrep 'log_level =' ${MYFILE} && echo "OK" || echo "NOT OK"
egrep 'log_outputs=' ${MYFILE} && echo "OK" || echo "NOT OK"
fi
##
## DAEMON libvirtd Restart
systemctl restart libvirtd; systemctl --no-pager status libvirtd;
###
### Add VM DNS name to /etc/hosts
MYFILE=/etc/hosts
if grep "${MYIPADDRESS} ${MYDOMAIN}" ${MYFILE}; then
echo "OK. File already patched ${MYFILE}"
else
cat >> ${MYFILE} <<THETEXTBLOCK
##RHMOD Append this
##RHMOD VM ${MYDOMAIN}
${MYIPADDRESS} ${MYDOMAIN}
THETEXTBLOCK
cat ${MYFILE}
fi
###
### Libvirt DHCP: reserve a static IP Address for the Guest VM
# @important DO this BEFORE creating the VM.
echo "mac: ${MYMACADDRESS} ip:${MYIPADDRESS}"
virsh net-update default delete ip-dhcp-host "<host ip='${MYIPADDRESS}'/>" --config --live
virsh net-update default add ip-dhcp-host "<host mac='${MYMACADDRESS}' name='${MYDOMAIN}' ip='${MYIPADDRESS}'/>" --config --live
virsh net-destroy default
virsh net-start default
###
### Create VM GUEST: VCPU 1 | Memory 2048MB | Disk 8GB | LAN default NAT
# @doc Download ISO image
# => http://releases.ubuntu.com/17.10/ubuntu-17.10-desktop-amd64.iso
# => http://releases.ubuntu.com/17.10/ubuntu-17.10-server-amd64.iso
# @doc VIRT INSTALLER
# @doc --os-variant ubuntu16.04 Always declare the latest Ubuntu LTS version (Ubuntu 16.10 LTS Zenial). You cannot specify non-LTS releases such as Ubuntu 17.10 Artful).
# @doc --network network=default Use a NAT-based virtual network (opposed to a bridged network).;
# @doc --video qxl Required for the new Waterland Gnome Display Manager (which replaces X.org in Ubuntu Artful).
# @doc --video vga/cirrus/vmvga is not compatible with Waterland. The new Gnome Desktop is then not available and the system fallbacks to the old X.org
# @doc The SPICE remote viewer is recommended (instead of VNC).
MYISODIR="/var/lib/libvirt/iso/"
MYISOFILE="ubuntu-17.10-desktop-amd64.iso"
MYISOPATH="${MYISODIR}${MYISOFILE}"
mkdir --parents ${MYISODIR}
cd ${MYISODIR}
if [ ! -f ${MYISOPATH} ]; then
echo "OK. ISO not yet downloaded. Start downloading..."
wget http://releases.ubuntu.com/17.10/${MYISOFILE}
else
echo "OK. ISO already downloaded."
fi
ll ${MYISOPATH}
virt-install \
--debug \
--name ${MYDOMAIN} \
--cdrom ${MYISOPATH} \
--hvm \
--vcpus 1 \
--memory 2048 \
--disk path=/var/lib/libvirt/images/${MYDOMAIN}.img,size=8,bus=virtio \
--network network=default,mac=${MYMACADDRESS} \
--video qxl \
--os-variant ubuntu16.04 \
--console pty,target_type=serial \
--graphics spice,listen=0.0.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment