-
-
Save ravnoor/b17cf53e4d8fc66e5d38979b80143e53 to your computer and use it in GitHub Desktop.
libguestfs examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# may be templated in the future | |
network: | |
version: 2 | |
renderer: networkd | |
ethernets: | |
eth0: | |
dhcp4: true | |
optional: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -o errexit | |
set -o pipefail | |
source ${LABKEY_DA:?}/lib/redo/leaf/kvm.bash | |
readonly KVMCPU_NAME=${FOLDER_NAME:?} | |
# clone image for use as dom | |
set -o errexit | |
set -o pipefail | |
virt-clone \ | |
--original {{ image }}-vi \ | |
--name {{ name }} \ | |
--auto-clone --debug | |
# https://www.cyberciti.biz/faq/reset-a-kvm-clone-virtual-machines-with-virt-sysprep-on-linux/ | |
# ignore ssh rules to preserve key-based access | |
readonly SYSPREP_OPERATIONS=$(virt-sysprep --list | cut -d* -f1 | cut -d' ' -f1 | grep -v ssh | pyp "','.join(pp)") | |
sudo virt-sysprep \ | |
-d ${KVMCPU_NAME:?} \ | |
--no-network \ | |
--operations "${SYSPREP_OPERATIONS}" | |
sudo virt-customize \ | |
--no-network \ | |
-d ${KVMCPU_NAME:?} \ | |
--hostname ${KVMCPU_NAME:?} | |
virt-xml \ | |
${KVMCPU_NAME:?} \ | |
--edit \ | |
--vcpus {{ vcpus }} | |
virt-xml \ | |
${KVMCPU_NAME:?} \ | |
--edit \ | |
--memory {{ ram }} | |
virt-xml \ | |
${KVMCPU_NAME:?} \ | |
--edit \ | |
--network "network={{ network }}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -o errexit | |
set -o pipefail | |
readonly CODENAME="{{ os_version }}" | |
# image file name | |
readonly IMG_FR="${CODENAME:?}-server-cloudimg-amd64.img" | |
# cloud image uri | |
readonly IMG_URI=https://cloud-images.ubuntu.com/${CODENAME:?}/current/${IMG_FR:?} | |
# absolute path to cloud image file | |
readonly IMG_FA="${KVM_IMAGE_DA:?}/${IMG_FR:?}" | |
# absolute path to virtbuilder file | |
readonly VB_FA="${KVM_IMAGE_DA:?}/${VIRTBUILDER_NAME:?}.${KVM_IMAGE_FORMAT:?}" | |
# does img file exist? | |
[[ -e "${IMG_FA:?}" ]] || { | |
curl "${IMG_URI:?}" -o "${IMG_FA:?}" | |
} | |
# copy downloaded cloud img to qcow2 | |
cp "${IMG_FA:?}" "${VB_FA:?}" | |
# bug in cloud image | |
# https://bugs.launchpad.net/cloud-images/+bug/1573095 | |
# generates <name>-vb.qcow2 | |
virt-customize \ | |
--format "${KVM_IMAGE_FORMAT:?}" \ | |
--no-network \ | |
--hostname "${VIRTBUILDER_NAME:?}" \ | |
--ssh-inject "root:file:$HOME/.ssh/id_rsa.pub" \ | |
--copy-in build/01-netcfg.yaml:/etc/netplan \ | |
--root-password "file:$HOME/.ssh/kvm.password" \ | |
--run-command "sed -i 's/ console=ttyS0//g' /etc/default/grub.d/50-cloudimg-settings.cfg" \ | |
--run-command "sed -i 's/GRUB_CMDLINE_LINUX/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0 console=tty1\"/g' /etc/default/grub" \ | |
--run-command "update-grub" \ | |
--run-command "sed -i 's/ibm-p8-kvm-03-guest-02.virt.pnr.lab.eng.rdu2.redhat.com//g' /etc/hosts" \ | |
--run-command "systemctl mask apt-daily.service apt-daily-upgrade.service" \ | |
--firstboot-command "netplan generate && netplan apply" \ | |
--firstboot-command "dpkg-reconfigure openssh-server" \ | |
--firstboot-command "sync" \ | |
-a "${VB_FA:?}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -o errexit | |
set -o pipefail | |
# Remove prior instance if it exists | |
sudo rm -f "${VIRTINSTALL_FA:?}" | |
cp -v "${VIRTBUILDER_FA:?}" "${VIRTINSTALL_FA:?}" 1>&2 | |
qemu-img resize "${VIRTINSTALL_FA:?}" "{{ disk }}" | |
virt-customize \ | |
--hostname ${VIRTINSTALL_NAME:?} \ | |
--install qemu-guest-agent,molly-guard \ | |
--firstboot-install cloud-initramfs-growroot \ | |
--firstboot-command "growpart /dev/vda 1" \ | |
--firstboot-command "resize2fs /dev/vda1" \ | |
{%- if graphics == 'mate' %} | |
--firstboot-install tasksel \ | |
--firstboot-command "tasksel install ubuntu-mate-core" \ | |
--firstboot-command "poweroff" \ | |
{%- endif %} | |
--add "${VIRTINSTALL_FA:?}" | |
virt-install \ | |
--name "${VIRTINSTALL_NAME:?}" \ | |
--network "network={{ network }},model=virtio" \ | |
--ram "{{ ram }}" \ | |
--vcpus {{ vcpus }} \ | |
--os-variant "{{ os_variant }}" \ | |
--virt-type kvm \ | |
{%- if (graphics == 'spice' or graphics == 'mate') %} | |
--video qxl --channel spicevmc --graphics "spice" \ | |
{%- else %} | |
--graphics "{{ graphics }}" \ | |
{%- endif %} | |
--console "{{ console }}" \ | |
{%- if graphics != 'mate' %} | |
--noautoconsole \ | |
{%- endif %} | |
--import \ | |
--disk "path=${VIRTINSTALL_FA:?},device=disk,bus=virtio,format=${KVM_IMAGE_FORMAT:?}" 1>&2 | |
# change ownership to current user | |
sudo chown ${USER:?}:kvm ${VIRTINSTALL_FA:?} | |
# Attach extra disks if configured | |
{% if raid is defined %} | |
{%- for letter in raid.letters %} | |
EXTRA_DISK_{{ letter | upper }}=${KVM_IMAGE_DA:?}/${VIRTINSTALL_NAME:?}-{{ letter }}.${KVM_IMAGE_FORMAT:?} | |
EXTRA_DISK_DEVICE_{{ letter | upper }}=vd{{ letter }} | |
qemu-img create \ | |
-f "${KVM_IMAGE_FORMAT:?}" \ | |
"${EXTRA_DISK_{{ letter | upper }}}" \ | |
"{{ raid.size }}" | |
virsh attach-disk ${VIRTINSTALL_NAME:?} \ | |
--source ${EXTRA_DISK_{{ letter | upper }}} \ | |
--target ${EXTRA_DISK_DEVICE_{{ letter | upper }}} \ | |
--driver qemu \ | |
--subdriver "${KVM_IMAGE_FORMAT:?}" \ | |
--targetbus virtio \ | |
--persistent | |
{%- endfor %} | |
{% endif %} | |
virt-xml ${VIRTINSTALL_NAME:?} --edit --cpu mode='host-passthrough' | |
# shut down virtual machine | |
readonly running_target=$(virsh list --all | grep "running" | gawk '{ print $2 }' | grep "^${VIRTINSTALL_NAME:?}$") | |
if [[ ! -z "${running_target}" ]]; then | |
virsh destroy ${VIRTINSTALL_NAME:?} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment