Skip to content

Commit

Permalink
Merge pull request #33 from sbates130272/dev/stephen/gen-vm
Browse files Browse the repository at this point in the history
dev/stephen/gen vm
  • Loading branch information
sbates130272 authored Jun 23, 2023
2 parents e9928f5 + 66a9682 commit d5399ab
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 7 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ to get the command line arguments supported.
This script will automatically create a snapshot image so you can revert
to the original image by deleting images/jessie.qcow2.

## New VM Creation

There are a few methods for VM creation in this repo. They are all in
various stages of repair.

### ./qemu/gen-vm

A QEMU-only (no libvirt) way to generate new VMs. Uses the Ubuntu
cloud image ISO and a cloud-init script. Note there is an issue that
the files for this cannot reside on a guest's virtfs (i.e. running
this script inside a VM to generate a nested VM won't work due to file
permission issues). This may be better with virtio-fs.

### ./libvirt/virt-install-ubuntu

A libvirt-based script that uses cloud images and cloud-init and
virt-install. Thie seems to work quite well on bare-metal.

### ./scripts/create

The original and somewhat outdated method from Logan that starts with
a base backing issue (based on Debian) and then uses deboostrap to
setup the user and install some packages.

## QEMU Executable

This repo does not include the QEMU executable. You can specific a
Expand Down
11 changes: 4 additions & 7 deletions libvirt/virt-install-ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
# NOAUTOCONSOLE and PACKAGES only apply to focal or later.
#
# PACKAGES is a file of packages to be installed via cloud-init. A
# couple of my favourite collections can be found in the packages.d
# folder.
# couple of my favourite collections can be found in the top-level
# packages.d folder.
#
# FILESYSTEM can be a directory on the host that is then shared into
# the guest via the tag "hostfs" and the virtiofs driver. To access
Expand All @@ -80,7 +80,7 @@ SSH_KEY_FILE=${SSH_KEY_FILE:-~/.ssh/id_rsa.pub}
NOAUTOCONSOLE=${NOAUTOCONSOLE:-false}
USERNAME=${USERNAME:-ubuntu}
PASS=${PASS:-password}
PACKAGES=${PACKAGES:-none}
PACKAGES=${PACKAGES:-../packages.d/packages-default}
FILESYSTEM=${FILESYSTEM:-none}
BRIDGE=${BRIDGE:-none}

Expand Down Expand Up @@ -132,17 +132,14 @@ fi
#
# We use vol-create and vol-upload to add the disk images to the
# libvirt images folder.
#
# Default password for root and ubuntu user is 'change'. Use hash in
# these cloud-init scripts.

set -e

cleanup() {
rm -f cloud-config-${NAME} network-config-${NAME} ${NAME}.qcow2 \
${NAME}-seed.qcow2
}
trap cleanup ERR EXIT
trap cleanup SIGINT ERR EXIT

if [ ! -f ${RELEASE}-server-cloudimg-${ARCH}.img ]; then
wget https://cloud-images.ubuntu.com/${RELEASE}/current/${RELEASE}-server-cloudimg-${ARCH}.img
Expand Down
File renamed without changes.
File renamed without changes.
152 changes: 152 additions & 0 deletions qemu/gen-vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/bin/bash
#
# gen-vm
#
# (C) Stephen Bates <sbates@raithlin>
#
# A simple script generate a Ubuntu Jammy VM using bare QEMU (i.e. not
# libvirt) and cloud-init.
#
# Jammy (or Later)
# ----------------
#
# Note that for Jammy you need the cloud-init as well as
# cloud-image-utils and for focal a good generic call would be:
#
# RELEASE=jammy VM_NAME=stephen ./gen-vm
#
# This creates a system with an user called ubuntu and the password
# change who has full sudo access to the system. This also sets up the
# networking as NAT to the host and you can then ssh into the machine
# using the ubuntu user.
#
# See [1] for a good HOWTO for cloud-init.
#
# [1] https://fabianlee.org/2020/02/23/kvm-testing-cloud-init-locally-using-kvm-for-an-ubuntu-cloud-image/
#
# Note that after running this script you might want to remove the
# cloud-init disk from the VM. See [1] for info on how to do that.

# PACKAGES is a file of packages to be installed via cloud-init. A
# couple of my favourite collections can be found in the top-level
# packages.d folder.
#

VM_NAME=${VM_NAME:-qemu-minimal}
SIZE=${SIZE:-64}
IMAGES=${IMAGES:-../images}
RELEASE=${RELEASE:-jammy}
ARCH=${ARCH:-amd64}
VCPUS=${VCPUS:-2}
VMEM=${VMEM:-4096}
SSH_KEY_FILE=${SSH_KEY_FILE:-~/.ssh/id_rsa.pub}
USERNAME=${USERNAME:-ubuntu}
PASS=${PASS:-password}
PACKAGES=${PACKAGES:-../packages.d/packages-default}
SSH_PORT=${SSH_PORT:-2222}
KVM=${KVM:-enable}

# Focal and above prefers us to use cloud images and
# cloud-init. Download the focal cloud image and set it up using a
# cloud-config file. Create am ubuntu user with password-less root
# access.

set -e

cleanup() {
rm -f cloud-config-${VM_NAME} network-config-${VM_NAME}
}
trap cleanup SIGINT ERR EXIT

if [ ! -f ${IMAGES}/${RELEASE}-server-cloudimg-${ARCH}.img ]; then
wget -P ${IMAGES} https://cloud-images.ubuntu.com/${RELEASE}/current/${RELEASE}-server-cloudimg-${ARCH}.img
fi
cp ${IMAGES}/${RELEASE}-server-cloudimg-${ARCH}.img ${IMAGES}/${VM_NAME}-backing.qcow2
qemu-img resize ~/images/${VM_NAME}-backing.qcow2 ${SIZE}G

if [ ! -f $SSH_KEY_FILE ]; then
echo "SSH_KEY_FILE ${SSH_KEY_FILE} does not exist!"
exit 1
fi

if [ ${PACKAGES} != "none" ]; then
if [ -f ${PACKAGES} ]; then
PACKAGES=$(<${PACKAGES})
else
echo "Package manifest file ${PACKAGES} does not exist!"
exit 1
fi
else
PACKAGES=
fi

cat << EOF > cloud-config-${VM_NAME}
#cloud-config
hostname: ${VM_NAME}
disable_root: true
ssh_pwauth: true
users:
- name: ${USERNAME}
plain_text_passwd: '${PASS}'
lock_passwd: false
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
shell: /bin/bash
ssh_authorized_keys: |
$(sed -z 's|\n|\n |g' ${SSH_KEY_FILE})
timezone: America/Edmonton
ntp:
enabled: true
packages:
${PACKAGES}
power_state:
delay: now
mode: poweroff
message: Shutting down
timeout: 2
condition: true
EOF

cat << EOF > network-config-${VM_NAME}
version: 2
ethernets:
eth0:
match:
name: en*
dhcp4: true
# default libvirt network
gateway4: 192.168.122.1
nameservers:
addresses: [ 192.168.122.1,8.8.8.8 ]
EOF

cloud-localds -d qcow2 ${IMAGES}/${VM_NAME}-seed.qcow2 cloud-config-${VM_NAME} \
network-config-${VM_NAME}

if [ ${KVM} == "enable" ]; then
KVM=",accel=kvm"
else
KVM=""
fi

if [ ${ARCH} == "amd64" ]; then
QARCH="x86_64"
QARCH_ARGS="-machine q35${KVM}"
elif [ ${ARCH} == "arm64" ]; then
QARCH="aarch64"
QARCH_ARGS="-machine virt,gic-version=max${KVM} -cpu max -bios /usr/share/qemu-efi-aarch64/QEMU_EFI.fd"
else
echo "Error: No ARCH mapping exists for ${ARCH}! Exiting."; exit -1
fi

qemu-system-${QARCH} \
${QARCH_ARGS} \
-smp cpus=${VCPUS} \
-m ${VMEM} \
-nographic \
-drive if=virtio,format=qcow2,file=${IMAGES}/${VM_NAME}-backing.qcow2 \
-drive if=virtio,format=qcow2,file=${IMAGES}/${VM_NAME}-seed.qcow2 \
-nic user,id=user0,model=virtio-net-pci

qemu-img create -F qcow2 -b ${IMAGES}/${VM_NAME}-backing.qcow2 -f qcow2 \
${IMAGES}/${VM_NAME}.qcow2
49 changes: 49 additions & 0 deletions qemu/run-vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
#
# run-vm
#
# (C) Stephen Bates <sbates@raithlin>
#

VM_NAME=${VM_NAME:-qemu-minimal}
ARCH=${ARCH:-amd64}
VCPUS=${VCPUS:-2}
VMEM=${VMEM:-4096}
FILESYSTEM=${FILESYSTEM:-none}
IMAGES=${IMAGES:-../images}
SSH_PORT=${SSH_PORT:-2222}
KVM=${KVM:-enable}

if [ ${ARCH} == "amd64" ]; then
QARCH="x86_64"
elif [ ${ARCH} == "arm64" ]; then
QARCH="aarch64"
else
echo "Error: No ARCH mapping exists for ${ARCH}! Exiting."; exit -1
fi

if [ ${KVM} == "enable" ]; then
KVM=",accel=kvm"
else
KVM=""
fi

if [ ${ARCH} == "amd64" ]; then
QARCH="x86_64"
QARCH_ARGS="-machine q35${KVM}"
elif [ ${ARCH} == "arm64" ]; then
QARCH="aarch64"
QARCH_ARGS="-machine virt,gic-version=max${KVM} -cpu max -bios /usr/share/qemu-efi-aarch64/QEMU_EFI.fd"
else
echo "Error: No ARCH mapping exists for ${ARCH}! Exiting."; exit -1
fi

qemu-system-${QARCH} \
${QARCH_ARGS} \
-smp cpus=${VCPUS} \
-m ${VMEM} \
-nographic \
-drive if=virtio,format=qcow2,file=${IMAGES}/${VM_NAME}-backing.qcow2 \
-nic user,id=user0,model=virtio-net-pci,hostfwd=tcp::${SSH_PORT}-:22


0 comments on commit d5399ab

Please sign in to comment.