Skip to content

Instantly share code, notes, and snippets.

@tai271828
Last active November 17, 2020 14:36
Show Gist options
  • Save tai271828/85315b7ef7da247d4a438911521e8621 to your computer and use it in GitHub Desktop.
Save tai271828/85315b7ef7da247d4a438911521e8621 to your computer and use it in GitHub Desktop.
cookbook-virt-inst - for example creating a KVM domain to boot from PXE by default via virt-inst

Cookbook of virt-install

cookbook-virt-inst

#!/bin/bash
#
# Author: Taihsiang Ho (tai271828) <[email protected]>
#
# Usage:
# An easy script for you to create and define a KVM xml with PXE boot-entry
# at the 1st order. This script is useful when creating KVM domains for
# MaaS.
#
# Depending on your configuration of your KVM host. You may want to:
# - Change MAC_ADDR or automatically assigned/generated by this script
# - Change NETWORK_INTERFACE to be your real ethernet interface,
# bridge, or the virtual device provided by libvirtd.
#
# Depending on your plan, you could:
# - Change `--ram` to have larger memory
# - Change `--disk size` to have larger disk image
#
# To make it work with MaaS, after executing this script, please do:
# 1. Tweak the new enlisted node of your MaaS, including the power
# control
# 2. Make sure your 1st boot entry of your domain is always pxe-boot.
# For example, you could:
# - virsh dumpxml pxe-boot-node pxe-boot-node.xml
# - append <boot dev='network'/> under <os> label so we could have pxe
# boot as the 1st boot permanently
# 3. virsh define pxe-boot-node.xml
# 4. commission your kvm domain
#
# You could always monitor the domain by:
#
# virt-viewer --connect qemu+ssh://ubuntu@<KVM host IP>/system pxe-boot-node
#
DOMAIN_NAME="pxe-boot-node"
MAC_ADDR=$(echo ${DOMAIN_NAME} | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
NETWORK_INTERFACE="bridge:br1"
ROOT_STORAGE="/var/lib/libvirt/images"
virt-install \
-n ${DOMAIN_NAME} \
--description "PXE node to test PXE boot under an external DHCP" \
--vcpus=1 \
--ram=4096 \
--disk path=${ROOT_STORAGE}/${DOMAIN_NAME}.img,bus=virtio,size=10 \
--mac=${MAC_ADDR} \
--network ${NETWORK_INTERFACE} \
--pxe
DOMAIN_NAME="vm1"
MAC_ADDR=$(echo ${DOMAIN_NAME} | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
NETWORK_INTERFACE="bridge:brsriov01"
ROOT_STORAGE="/var/lib/libvirt/images"
TARGET_ISO="ubuntu-20.04.1-live-server-arm64.iso"
virt-install \
-n ${DOMAIN_NAME} \
--description "Ubuntu installation via CD-ROM" \
--vcpus=1 \
--ram=4096 \
--disk path=${ROOT_STORAGE}/${DOMAIN_NAME}.img,bus=virtio,size=10 \
--mac=${MAC_ADDR} \
--network ${NETWORK_INTERFACE} \
--cdrom=${HOME}/${TARGET_ISO}
# pre-requisite
## get ready to use virsh
sudo apt-get install libvirt-bin virtinst
# in order to issue virsh directly. reboot to make it effective
sudo usermod -a -G libvirtd ubuntu
# One time tasks
# it is ok to skip this step because virt-install will do it for you
#qemu-img create -f qcow2 -o preallocation=metadata,lazy_refcounts=on /var/lib/libvirt/images/my-domain.img 20G
# if pxe works, you don't need this
#virsh destroy
# use this if you want to restart the cycle
#virsh undefine pxe-boot-text
# Working cycles
virsh edit pxe-boot-test
# and add this line: <boot dev='network'/>
virsh start pxe-boot-test
# watching the process
virt-viewer --connect qemu+ssh://ubuntu@<IP>/system pxe-boot-test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment