Skip to content

Instantly share code, notes, and snippets.

@glennswest
Created February 23, 2021 14:02
Show Gist options
  • Save glennswest/045de6d39e1e418d977d283b1bce84a7 to your computer and use it in GitHub Desktop.
Save glennswest/045de6d39e1e418d977d283b1bce84a7 to your computer and use it in GitHub Desktop.
variant: fcos
version: 1.1.0
storage:
files:
- path: /usr/local/bin/create-datastore
mode: 0755
contents:
inline: |
#!/bin/bash
LABEL=datastore
OFFSET=2G
set -euo pipefail
cmdline=( $(</proc/cmdline) )
karg() {
local name="$1" value="${2:-}"
for arg in "${cmdline[@]}"; do
if [[ "${arg%%=*}" == "${name}" ]]; then
value="${arg#*=}"
fi
done
echo "${value}"
}
# Get install device
device="$(karg coreos.inst.install_dev)"
if [[ -z $device ]]; then
echo "Install device not specified."
exit 1
fi
# Append /dev/ if missing
device="/dev/${device##/dev/}"
# Wait for device nodes
udevadm settle
# Check for partitions other than the system ones
if lsblk --pairs --output NAME,TYPE,PARTLABEL "${device}" |\
awk '/TYPE="part"/ && !/PARTLABEL="(boot|EFI-SYSTEM|BIOS-BOOT|root|luks_root)"/ {print; exit 1}'
then
echo "Creating data partition \"${LABEL}\""
# Relocate second GPT header to end of disk and create partition
sgdisk --move-second-header \
--new=0:+"${OFFSET}":0 --change-name=0:"${LABEL}" \
"${device}"
# Wait for device node
udevadm settle
# Get the device node
partition=$(lsblk "${device}" --paths --pairs --output NAME,PARTLABEL |\
awk '/PARTLABEL="'${LABEL}'"/ && match($0, "NAME=\"([a-zA-Z0-9/_-]+)\"", a) {print a[1]; exit 0}')
# Create ext4 filesystem in it
mkfs.ext4 -F -L "${LABEL}" "${partition}"
else
echo "Found existing data partition; not creating a new one"
fi
systemd:
units:
- name: create-datastore.service
enabled: true
contents: |
[Unit]
Description=Create data partition if one doesn't already exist
After=coreos-installer.service
Before=coreos-installer.target
ConditionKernelCommandLine=create-datastore
[Service]
Type=oneshot
ExecStart=/usr/local/bin/create-datastore
[Install]
WantedBy=coreos-installer.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment