Skip to content

Instantly share code, notes, and snippets.

@dafanasiev
Created January 26, 2025 19:32
Show Gist options
  • Save dafanasiev/0ab03d85915490bd44dd2ca03689889e to your computer and use it in GitHub Desktop.
Save dafanasiev/0ab03d85915490bd44dd2ca03689889e to your computer and use it in GitHub Desktop.
LAB-clonezilla-qemu-usb-flash

WTF

LAB: How to make usb flash with clonezilla using qemu (libvirt) (with emulated usb flash drive).

Prepare images

# disk storage
HOST> mkdir -p /QEMU/

# image storage
HOST> mkdir -p /QEMU-img/

HOST> wget -O /QEMU-img/debian-12.9.0-amd64-netinst.iso https://gemmei.ftp.acc.umu.se/debian-cd/current/amd64/iso-cd/debian-12.9.0-amd64-netinst.iso

HOST> wget -O /QEMU-img/clonezilla-live-3.2.0-5-amd64.iso https://kumisystems.dl.sourceforge.net/project/clonezilla/clonezilla_live_stable/3.2.0-5/clonezilla-live-3.2.0-5-amd64.iso?viasf=1

Install and configure OS in VM (gui)

# install VM and do what you need
# NOTE: dont forget to `rm /etc/machine-id` or use cloud-init...
HOST> virt-install \
--name guest \
--memory 2048 \
--vcpus 2 \
--disk path=/QEMU/debian12-rootfs.qcow2,size=4,format=qcow2 \
--cdrom /QEMU-img/debian-12.9.0-amd64-netinst.iso

Poweroff VM

VM> shutdown now

Create clonezilla usb stick

Attach cdrom with clonezilla

HOST:> virsh edit --domain guest

add some xml entries:

    <os>
    ...
        <boot dev='cdrom'/>        <!-- ADDED -->
        <bootmenu enable='yes'/>   <!-- ADDED -->
    </os>
    ...
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/QEMU-img/clonezilla-live-3.2.0-5-amd64.iso'/>   <!-- ADDED -->
      <target dev='sda' bus='sata'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>

save xml and quit - virsh will accept changes.

Create usb flash drive

HOST> qemu-img create -f raw /QEMU/debian12-flash.raw 8G

Run VM

HOST> virsh start guest

list drives inside VM:

VM> lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1  436M  0 rom  
vda    254:0    0    4G  0 disk 
|-vda1 254:1    0    3G  0 part /
|-vda2 254:2    0    1K  0 part 
`-vda5 254:5    0  975M  0 part [SWAP]

attach use flash:

HOST> virsh attach-disk guest --targetbus usb --source /QEMU/debian12-flash.raw --target hda

list drives inside VM after attach:

VM> lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0    8G  0 disk                         <--- this is out flash drive
sr0     11:0    1  436M  0 rom  
vda    254:0    0    4G  0 disk 
├─vda1 254:1    0    3G  0 part /
├─vda2 254:2    0    1K  0 part 
└─vda5 254:5    0  975M  0 part [SWAP]

lets create partition and format out flash drive:

# install some tools
VM> apt update && apt install dosfstools parted mtools

VM> fdisk /dev/sda
o
n
p
w
q

# at this point /dev/sda1 will be added - see in lsblk

# format partition
VM> mkfs.vfat -F 32 /dev/sda1

# copy clonezilla from cdrom to usb flash
VM> mkdir /mnt/cdrom
VM> mkdir /mnt/flash

VM> mount /dev/sr0 /mnt/cdrom/
VM> mount /dev/sda1 /mnt/flash/

# copy all files
VM> cp -a /mnt/cdrom/. /mnt/flash

# install MBR into sda
VM> cd /mnt/flash/utils/linux
VM> ./makeboot.sh /dev/sda1

VM> shutdown now

Start VM and boot from cdrom

HOST> virt-viewer guest
HOST> virsh start guest

# press ESC in virt-viewer to view boot menu; boot from cdrom

Make initial golden image with clonezilla

  1. Use device-image in clonezilla menu.
  2. Use usb flash as /home/partimag
  3. Select usb flash root as image registry
  4. Use savedisk menu and go forward....
  5. Use vda as image source
  6. Clonezilla make disk image
  7. Poweroff

Start VM and boot from hdd (emulate changes)

HOST> virsh start guest

VM> mkdir /THIS_IS_NEW_DIRECTORY_THAT_DOESNT_EXISTS_IN_SAVED_IMAGE
VM> poweroff

Start VM and boot from usb flash (check)

HOST> virt-viewer guest
HOST> virsh start guest

# press ESC in virt-viewer to view boot menu; boot from usb flash
  1. Use device-image in clonezilla menu.
  2. Use usb flash as /home/partimag
  3. Select usb flash root as image registry
  4. Restore saved disk image to vda
  5. Reboot and check that directory /THIS_IS_NEW_DIRECTORY_THAT_DOESNT_EXISTS_IN_SAVED_IMAGE doesnt exists.
@dafanasiev
Copy link
Author

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