Skip to content

Instantly share code, notes, and snippets.

@noslin005
Created February 20, 2026 01:29
Show Gist options
  • Select an option

  • Save noslin005/e565053fd4ee5753338df470d31119cf to your computer and use it in GitHub Desktop.

Select an option

Save noslin005/e565053fd4ee5753338df470d31119cf to your computer and use it in GitHub Desktop.
Create a Bootable Raw Disk - Fedora

Create a Bootable Raw Disk

Attach Virtual Disk

sudo kpartx -avs test.img

Create Partition

sudo parted /dev/loop0 mktable msdos
sudo parted /dev/loop0 mkpart primary ext4 1 100%
sudo parted /dev/loop0 set 1 boot on

Detatch and Re-attach the disk to update the partition

sudo kpartx -dvs test.img
sudo kpartx -avs test.img

Make Filesystem

sudo mkfs.ext4 /dev/mapper/loop0p1

Mount filesystem

sudo mount /dev/mapper/loop0p1 /mnt/chroot/

Install a Minimal OS

sudo yum install -y \
    --use-host-config \
    --releasever=43 \
    --installroot=/mnt/chroot/ \ 
    systemd passwd sudo fedora-release kernel grub2 awk vim NetworkManager

Setup a chroot environment

sudo mount --bind /dev /mnt/chroot/dev
sudo mount --bind /proc /mnt/chroot/proc
sudo mount --bind /sys /mnt/chroot/sys

Configure Grub

cat <<EOF|sudo tee /mnt/chroot/etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="vconsole.keymap=en vconsole.font=latarcyrheb-sun16"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true
EOF

sudo chroot /mnt/chroot/ /bin/sh -c 'grub2-mkconfig -o /boot/grub2/grub.cfg'
sudo chroot /mnt/chroot/ /bin/sh -c 'grub2-install --target=i386-pc /dev/loop0'

Create a Password

echo -e 'YourPassword'|openssl passwd -6 -stdin

Change Root Password

sudo chroot /mnt/chroot/ /bin/sh -c 'usermod -p <root_encrypted_password> root'

Create a User Account and Set Password

sudo chroot /mnt/chroot/ /bin/sh -c 'useradd -m -G wheel -p <sysadmin_encrypted_password> sysadmin'

sudo chroot /mnt/chroot/ /bin/sh -c 'echo "sysadmin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/sysadmin'

Umount the filesystems recursively

sudo umount -R /mnt/chroot

Detach the image

sudo kpartx -dvs test.img

Test the System

qemu-kvm -enable-kvm -m 2G -drive file=/VMs/test/test.img,format=raw,media=disk -boot order=d

Convert Image to VMDK

  • Default adapter (IDE)

      qemu-img convert -f raw -O vmdk -p test.img test2.vmdk
      dd if=test2.vmdk bs=1 skip=4 count=2 status=none | xxd
      # 00000000: 0100
      This means The file format is version 1
    
  • LSI Adapter (Compatible with vSphere)

      qemu-img convert -f raw -O vmdk -o adapter_type=lsilogic,subformat=streamOptimized,compat6=on -p test.img test.vmdk
      dd if=test.vmdk bs=1 skip=4 count=2 status=none | xxd
      # Output: 00000000: 0300
      # This means The file format is version 3
    
  • Change VMDK format

      printf '\x03' | dd conv=notrunc of=test.vmdk bs=1 seek=$((0x4))
    

Qemu Image Convert Get Help

# Get help about vmdk options
qemu-img convert -O vmdk -o help

Links

- https://bugzilla.redhat.com/show_bug.cgi?id=1299250
- https://raw.githubusercontent.com/erik-smit/one-liners/refs/heads/master/qemu-img.vmdk3.hack.sh
- https://github.com/erik-smit/one-liners/blob/master/qemu-img.vmdk3.hack.sh
- https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/1646240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment