fix initial commit

This commit is contained in:
Dhya 2024-10-17 15:04:16 -07:00
parent 0b812638f6
commit 68f39098fd
5 changed files with 279 additions and 0 deletions

22
kvm/get-kvm-link-local Executable file
View file

@ -0,0 +1,22 @@
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "USAGE: $0 <domain>"
exit 0
else
domain="$1"
fi
if [[ ! -e /etc/libvirt/qemu/${domain}.xml ]]; then
echo "Error: ${domain} configuration not found on this system"
exit 1
fi
macAddress=$(sudo grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' /etc/libvirt/qemu/${domain}.xml)
# courtesy of https://stackoverflow.com/a/48378567
printf "%02x%s" $(( 16#${macAddress:0:2} ^ 2#00000010 )) "${macAddress:2}" \
| sed -E -e 's/([0-9a-zA-Z]{2})*/0x\0|/g' \
| tr -d ':\n' \
| xargs -d '|' \
printf "fe80::%02x%02x:%02xff:fe%02x:%02x%02x\n"

55
kvm/kvm-setup Executable file
View file

@ -0,0 +1,55 @@
#!/bin/bash
# Some notes:
#
# Prior to running this script it is recommended to update the osinfo-db:
# git clone https://gitlab.com/libosinfo/osinfo-db.git
# apt install gettext osinfo-db-tools libosinfo-bin
# cd osinfo-db
# make && make install
# osinfo-query os
#
# Some ISO images are not in the osinfo-db and therefore require initrd= and kernel= arguments provided to --location, for example:
# --location $isoImage,initrd=install.amd/initrd.gz,kernel=install.amd/vmlinuz
#
# To find them in the ISO image use:
# isoinfo -J -i <ISO image> -l | grep -e '(initrd|vmlinuz)'
# and:
# isoinfo -J -i <ISO image> -l | less
# to find what directory they are in. `isoinfo` is provided by the `genisoimage` package.
#
# The --boot line below selects UEFI non-secure-boot and requires `ovmf` to be installed
#
# The --extra-args allows console access to the installation
#
# This script assumes you have a bridge named 'br0'. Otherwise you might want to use:
# --network network:default
machineName="rock9-base"
isoImage="/mnt/e/Downloads/ISO/Rocky-9.4-x86_64-dvd.iso"
machineMemory=2 # in GB
diskSize=20
cpuNumber=2
# run `osinfo-query os` to get the name of the OS variant
osVariant="rocky9"
#--location $isoImage,initrd=install.amd/initrd.gz,kernel=install.amd/vmlinuz \
virt-install \
--connect qemu:///system \
--virt-type kvm \
--name "$machineName" \
--hvm \
--memory $(( $machineMemory * 1024 )) \
--boot loader=/usr/share/OVMF/OVMF_CODE.fd,loader.readonly=yes,loader.type=pflash,nvram.template=/usr/share/OVMF/OVMF_VARS.fd,loader_secure=no \
--disk size="$diskSize",bus=virtio,format=qcow2 \
--location $isoImage \
--vcpus $cpuNumber \
--cpu host \
--osinfo detect=on \
--os-variant "$osVariant" \
--network bridge=br1 \
--graphics none \
--console pty,target_type=serial \
--extra-args 'console=ttyS0,115200n8 --- console=ttyS0,115200n8'

51
kvm/kvm-setup-from-img Executable file
View file

@ -0,0 +1,51 @@
#!/bin/bash
# Some notes:
#
# Prior to running this script it is recommended to update the osinfo-db:
# git clone https://gitlab.com/libosinfo/osinfo-db.git
# apt install gettext osinfo-db-tools libosinfo-bin
# cd osinfo-db
# make && make install
# osinfo-query os
#
# Some ISO images are not in the osinfo-db and therefore require initrd= and kernel= arguments provided to --location, for example:
# --location $isoImage,initrd=install.amd/initrd.gz,kernel=install.amd/vmlinuz
#
# To find them in the ISO image use:
# isoinfo -J -i <ISO image> -l | grep -e '(initrd|vmlinuz)'
# and:
# isoinfo -J -i <ISO image> -l | less
# to find what directory they are in. `isoinfo` is provided by the `genisoimage` package.
#
# The --boot line below selects UEFI non-secure-boot and requires `ovmf` to be installed
#
# The --extra-args allows console access to the installation
#
# This script assumes you have a bridge named 'br0'. Otherwise you might want to use:
# --network network:default
machineName="deb12-gencld"
diskImage="/mnt/e-lin/Downloads/ISO/debian-12-genericcloud-amd64.qcow2"
machineMemory=2 # in GB
cpuNumber=2
# run `osinfo-query os` to get the name of the OS variant
osVariant="debian12"
virt-install \
--connect qemu:///system \
--virt-type kvm \
--name "$machineName" \
--hvm \
--memory $(( $machineMemory * 1024 )) \
--boot loader=/usr/share/OVMF/OVMF_CODE.fd,loader.readonly=yes,loader.type=pflash,nvram.template=/usr/share/OVMF/OVMF_VARS.fd,loader_secure=no \
--disk ${diskImage},bus=virtio,format=qcow2 \
--import \
--vcpus $cpuNumber \
--cpu host \
--os-variant "$osVariant" \
--network bridge=br1 \
--graphics none \
--console pty,target_type=serial \
#--extra-args 'console=ttyS0,115200n8 --- console=ttyS0,115200n8'

32
kvm/vclone Executable file
View file

@ -0,0 +1,32 @@
#!/bin/bash
if [[ $# -ne 2 ]]; then
echo "USAGE: $0 <source> <dest>"
exit 0
else
orig="$1"
dest="$2"
fi
sudo virt-clone --original "${orig}" --name "${dest}" --auto-clone
sudo virt-sysprep \
-d "${dest}" \
--operations defaults,-ssh-userdir \
--hostname "${dest}" \
--edit /etc/hosts:"s/${orig}/${dest}/" \
--edit /etc/hostname:"s/${orig}/${dest}/" \
--edit /etc/motd:"s/${orig}/${dest}/" \
--firstboot-command 'dpkg-reconfigure openssh-server' \
--firstboot-command 'systemctl restart sshd'
getLinkLocalAddress() {
local macAddress=$(sudo grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' /etc/libvirt/qemu/${dest}.xml)
printf "%02x%s" $(( 16#${macAddress:0:2} ^ 2#00000010 )) "${macAddress:2}" \
| sed -E -e 's/([0-9a-zA-Z]{2})*/0x\0|/g' \
| tr -d ':\n' \
| xargs -d '|' \
printf "fe80::%02x%02x:%02xff:fe%02x:%02x%02x"
}
printf "The link-local IP address of %s is:\n %s\n" "${dest}" "$(getLinkLocalAddress)"