Skip to content

Instantly share code, notes, and snippets.

@tonidy
Forked from mpreu/fcos-qemu-demo
Created May 25, 2021 05:41
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 tonidy/8c20015dd96f3a6e96747a65a0d62b2f to your computer and use it in GitHub Desktop.
Save tonidy/8c20015dd96f3a6e96747a65a0d62b2f to your computer and use it in GitHub Desktop.
Fedora CoreOS - Example script to setup a FCOS virtual machine using QEMU. Originates from this blog post: https://www.matthiaspreu.com/posts/fedora-coreos-first-steps/.
#!/bin/env bash
set -eo pipefail
#
# Part of an introductory Fedora CoreOS blog post on my website:
# https://www.matthiaspreu.com/posts/fedora-coreos-first-steps/
#
# Script sets up a FCOS virtual machine using QEMU.
#
# Initial user "matthias" with password "test" can be used.
# One SSH keypair for the users "core" and "matthias" without a
# passphrase is created as well.
#
# Script sets up additional storage in the virtual machine to demonstrate
# some possibilities with Ignition.
#
# Generate password hash
function generateHash () {
local hash="$(echo "test" | mkpasswd --method=SHA-512 --rounds=4096 -s)"
echo "$hash"
}
# Generate ssh key
function generateSSHKey () {
rm ./id_rsa_fcos_example ./id_rsa_fcos_example.pub &>/dev/null || true
local pubKey="$(ssh-keygen -t rsa -b 4096 -q -N "" -f ./id_rsa_fcos_example && ssh-keygen -y -f ./id_rsa_fcos_example)"
echo "$pubKey"
}
# Generate the FCC configuration file
function generateFCC () {
local pubKey="$1"
local hash="$2"
cat << EOF > fcos.fcc
variant: fcos
version: 1.0.0
passwd:
users:
- name: core
ssh_authorized_keys:
- "$pubKey"
- name: matthias
ssh_authorized_keys:
- "$pubKey"
password_hash: "$hash"
groups:
- wheel
storage:
disks:
- device: /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_hd1
wipe_table: true
partitions:
- label: "disk.test"
number: 0
wipe_partition_entry: true
filesystems:
- path: /var/storage
device: /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_hd1
wipe_filesystem: true
format: ext4
label: STORAGE
files:
- path: /etc/hostname
overwrite: true
mode: 0644
contents:
inline: fcos-server-test
systemd:
units:
- name: var-storage.mount
enabled: true
contents: |
[Mount]
What=/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_hd1
Where=/var/storage
Type=ext4
[Install]
WantedBy=local-fs.target
EOF
}
# Download FCOS qcow2 image
ARTEFACT="fedora-coreos-31.20200113.3.1-qemu.x86_64.qcow2.xz"
IMAGENAME="fedora-coreos-31.20200113.3.1-qemu.x86_64.qcow2"
if [ ! -f "$IMAGENAME" ]; then
curl -o $ARTEFACT https://builds.coreos.fedoraproject.org/prod/streams/stable/builds/31.20200113.3.1/x86_64/$ARTEFACT
xz -v --decompress $ARTEFACT
fi
# Create a snapshot of the image to not modify original image
rm fcos.qcow2 &>/dev/null || true
qemu-img create -f qcow2 -b fedora-coreos-31.20200113.3.1-qemu.x86_64.qcow2 fcos.qcow2
# Create additional hard disk to demonstrate handling of storage
rm storage.qcow2 &>/dev/null || true
qemu-img create -f qcow2 storage.qcow2 500M
# Generate the FCC configuration file
generateFCC "$(generateSSHKey)" "$(generateHash)"
# Generate Ignition file from FCC configuration
podman run -i --rm quay.io/coreos/fcct:v0.2.0 -pretty -strict < fcos.fcc > fcos.ign
# Launch VM using qemu
qemu-system-x86_64 -machine accel=kvm \
-m 2048 -cpu host -nographic \
-device virtio-rng-pci \
-device virtio-scsi-pci,id=scsi \
-drive file=fcos.qcow2,if=none,id=hd0 -device scsi-hd,drive=hd0 \
-drive file=storage.qcow2,if=none,id=hd1 -device scsi-hd,drive=hd1 \
-fw_cfg name=opt/com.coreos/config,file=./fcos.ign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment