Skip to content

Instantly share code, notes, and snippets.

@noslin005
Created September 30, 2025 20:55
Show Gist options
  • Save noslin005/636c8de0a58f323bac8005077e3e20b0 to your computer and use it in GitHub Desktop.
Save noslin005/636c8de0a58f323bac8005077e3e20b0 to your computer and use it in GitHub Desktop.
Create a Bootable Installer Disk (An alternative to ISO)
#!/bin/bash
set -euo pipefail
# --- Settings ---
IMG="rocky_installer.img"
VMDK="rocky_installer.vmdk"
SIZE="20G"
LABEL="ROCKY_INSTALLER"
ISO="/VMs/isos/Rocky-9.5-x86_64-dvd.iso"
MNT_ISO="/mnt/iso"
MNT_DISK="/mnt/disk"
[[ $(id -u) -eq 0 ]] || { echo "Run as root"; exit 1; }
# --- Cleanup on exit ---
cleanup() {
umount -R "$MNT_ISO" 2>/dev/null || true
umount -R "$MNT_DISK" 2>/dev/null || true
[[ -n "${LOOP:-}" ]] && losetup -d "$LOOP" 2>/dev/null || true
}
trap cleanup EXIT
set -x
# --- Create disk image ---
rm -f "$IMG"
qemu-img create -f raw "$IMG" "$SIZE"
LOOP=$(losetup -f --show -P "$IMG")
parted -s "$LOOP" mklabel msdos
parted -s "$LOOP" mkpart primary ext4 1MiB 100%
parted -s "$LOOP" set 1 boot on
mkfs.ext4 -L "$LABEL" "${LOOP}p1"
# --- Mount and copy ---
mkdir -p "$MNT_ISO" "$MNT_DISK"
mountpoint -q "${MNT_ISO}" || mount -o loop "$ISO" "$MNT_ISO"
mount "${LOOP}p1" "$MNT_DISK"
rsync -aHAXP "$MNT_ISO/" "$MNT_DISK/"
# --- Syslinux setup ---
mkdir -p "$MNT_DISK/boot/syslinux"
cp -a "$MNT_DISK/isolinux/"* "$MNT_DISK/boot/syslinux/"
mv "$MNT_DISK/boot/syslinux/isolinux.cfg" "$MNT_DISK/boot/syslinux/syslinux.cfg"
# Update boot parameters
sed -i "s;\(inst.stage2=hd:LABEL=\)[^ ]*;\1${LABEL} inst.repo=hd:LABEL=${LABEL};g" \
"$MNT_DISK/boot/syslinux/syslinux.cfg"
# Install Syslinux bootloader
extlinux --install "$MNT_DISK/boot/syslinux"
# Write MBR
MBR_BIN=""
for path in /usr/share/syslinux/mbr.bin /usr/share/syslinux/mbr/mbr.bin; do
[[ -f "$path" ]] && MBR_BIN="$path" && break
done
if [[ -z "$MBR_BIN" ]]; then
echo "ERROR: Could not find syslinux MBR binary" >&2
exit 1
fi
dd if="$MBR_BIN" of="$LOOP" conv=notrunc status=progress
# --- Convert to VMDK (thin) ---
# Alternative: subformat=monolithicSparse
qemu-img convert -p -f raw -O vmdk -o subformat=streamOptmized,compat6 "${IMG}" "${VMDK}"
echo "[*] Done. Created bootable disk: $IMG"
@noslin005
Copy link
Author

#!/bin/bash

qemu-kvm \
  -m 4096 \
  -cpu host \
  -smp 2 \
  -hda rocky_installer.img \
  -boot c \
  -vga virtio \
  -display sdl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment