LAB: How to make usb flash with clonezilla using qemu (libvirt) (with emulated usb flash drive).
# 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 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
VM> shutdown now
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.
HOST> qemu-img create -f raw /QEMU/debian12-flash.raw 8G
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
HOST> virt-viewer guest
HOST> virsh start guest
# press ESC in virt-viewer to view boot menu; boot from cdrom
- Use
device-image
in clonezilla menu. - Use usb flash as
/home/partimag
- Select usb flash root as image registry
- Use
savedisk
menu and go forward.... - Use
vda
as image source - Clonezilla make disk image
- Poweroff
HOST> virsh start guest
VM> mkdir /THIS_IS_NEW_DIRECTORY_THAT_DOESNT_EXISTS_IN_SAVED_IMAGE
VM> poweroff
HOST> virt-viewer guest
HOST> virsh start guest
# press ESC in virt-viewer to view boot menu; boot from usb flash
- Use
device-image
in clonezilla menu. - Use usb flash as
/home/partimag
- Select usb flash root as image registry
- Restore saved disk image to
vda
- Reboot and check that directory
/THIS_IS_NEW_DIRECTORY_THAT_DOESNT_EXISTS_IN_SAVED_IMAGE
doesnt exists.
also: https://stackoverflow.com/questions/65848953/how-to-create-an-automated-and-unattended-clonezilla-restore-solution