Skip to content

Commit

Permalink
do not compress qcow2 using -c (too slow), add support for qcow2.zst
Browse files Browse the repository at this point in the history
refactor code for determine which commands to execute for converting
  • Loading branch information
Mrfai committed Jun 25, 2023
1 parent 2ca9e1d commit ba94b5a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 38 deletions.
71 changes: 41 additions & 30 deletions bin/fai-diskimage
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# This script is part of FAI (Fully Automatic Installation)
#
# Copyright (C) 2016-2022 Thomas Lange, lange@cs.uni-koeln.de
# Copyright (C) 2016-2023 Thomas Lange, lange@cs.uni-koeln.de
# Universitaet zu Koeln

die() {
Expand Down Expand Up @@ -62,7 +62,7 @@ usage() {
Create a disk image name.raw using FAI and a list of FAI classes.
This can be used for a virtual machine or a cloud instance. If you
use another suffix the image will be converted. Following formats are
supported: .raw.xz, .raw.zst, .qcow2, .vdi, .vhdx, .vmdk, .simg.
supported: .raw.xz, .raw.zst, .qcow2, .qcow2.zst, .vdi, .vhdx, .vmdk, .simg.
-h|--help print help
-v|--verbose be verbose
Expand Down Expand Up @@ -142,47 +142,58 @@ done
[ "$1" ] || usage
image=$1

iname=${image%.*}
ext=${image##*.}
iname=${image%.*} # strip last suffix
iname2=${iname%.*} # strip next to last suffix
rawname="$iname.raw"
case $ext in
raw) convert=0 ;;
xz) convert=2

case "$image" in
*.raw.xz)
convert=2
rawname=$iname
export XZ_OPT=${XZ_OPT-"-1 -T0"}
cmd="xz $rawname"
;;

*.raw.zst)
convert=2
rawname=$iname
cmd="zstd --rm -q -9 -T0 $rawname"
;;

*.qcow2.zst)
convert=3
rawname="$iname2.raw"
copt="-O qcow2 "
qcowname="$iname2.qcow2"
cmd="zstd --rm -q -9 -T0 $qcowname"
;;

*.raw)
convert=0
;;

*.qcow2)
copt="-O qcow2 "
qcowname="$iname.qcow2"
;;
zst) convert=2
rawname=$iname
# check if zstd support threads
flagtest=$(zstd -h | grep threads)
if [ -z "$flagtest" ]; then
cmd="zstd --rm -q -9 $rawname"
else
cmd="zstd --rm -q -9 -T0 $rawname"
fi
unset flagtest
;;
simg) convert=2

*.simg) convert=2
cmd="img2simg $rawname $iname.simg"
if ! command -v img2simg >&/dev/null; then
echo "img2simg not found. Install the package img2simg."
error=1
fi
;;
qcow2) copt="-c -O qcow2 "
qcowname="$iname.qcow2"
;;
vdi) copt="-O vdi"
*.vdi) copt="-O vdi"
qcowname="$iname.vdi"
;;
vmdk) copt="-O vmdk"
*.vmdk) copt="-O vmdk"
qcowname="$iname.vmdk"
;;
vhdx) copt="-O vhdx"
*.vhdx) copt="-O vhdx"
qcowname="$iname.vhdx"
;;
*) die 8 "Unknown suffix .$ext in name of disk image. Please use raw, qcow2, vdi, vmdk or vhdx."
*) die 8 "Unknown suffix. Please use raw, raw.zst, raw.xz, qcow2, qcow2.zst, vdi, vmdk or vhdx, simg."
esac

if [ -z "$classes" -a -z "$renew" ]; then
Expand Down Expand Up @@ -226,13 +237,13 @@ cleanup
trap - EXIT

# convert if needed
if [ $convert -eq 1 ]; then
[ $verbose -eq 1 ] && echo "Converting $rawname to $qcowname."
if [ $convert -eq 1 ] || [ $convert -eq 3 ]; then
[ $verbose -eq 1 ] && echo "Converting $rawname to $qcowname"
qemu-img convert -f raw $rawname $copt $qcowname
rm $rawname
fi
if [ $convert -eq 2 ]; then
[ $verbose -eq 1 ] && echo "Converting $rawname to $rawname.$ext."
if [ $convert -ge 2 ]; then
[ $verbose -eq 1 ] && echo "Converting to $image"
$cmd
fi
echo -n "Size of disk image and filename: "; du -h $image
Expand Down
20 changes: 12 additions & 8 deletions man/fai-diskimage.8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" Hey, EMACS: -*- nroff -*-
.TH fai-diskimage 8 "September 2018" "FAI 5.7"
.TH fai-diskimage 8 "June 2023" "FAI 6"

.SH NAME
fai-diskimage \- create a disk image for booting a VM
Expand All @@ -11,7 +11,7 @@ creates a disk image that can be used with Qemu/KVM, VirtualBox,
VMware, Xen, Android device or by your cloud infrastructure. It runs the Fully
Automatic Installation using a list of FAI classes. In the end you
have a bootable disk image. Following formats are
supported: .raw.xz, .raw.zst, .qcow2, .vdi, .vhdx, .vmdk, .simg.
supported: .raw, .raw.xz, .raw.zst, .qcow2, .qcow2.zst, .vdi, .vhdx, .vmdk, .simg.
.SH OPTIONS
.TP
.B \-c, --class \fICLASS\fR[,\fICLASS\fR]
Expand Down Expand Up @@ -48,19 +48,23 @@ configuration including several examples.
You can now build your fist disk image.

# export FAI_BASEFILEURL=https://fai-project.org/download/basefiles/
# cl="DEBIAN,BULLSEYE64,AMD64,FAIBASE,GRUB_PC,DHCPC,DEMO,CLOUD,LAST"
# cl="DEBIAN,BOOKWORM64,AMD64,FAIBASE,GRUB_PC,DHCPC,DEMO,CLOUD,LAST"
# fai-diskimage -vu cloud3 -S2G -c$cl cloud.raw

Creates a Debian system with a small set of software packages without
graphical desktop. The disk image cloud.raw will be of size 2 GB and
a graphical desktop. The disk image cloud.raw will be of size 2 GB and
the host is called cloud3.


# export FAI_BASEFILEURL=https://fai-project.org/download/basefiles/
# cl=DHCPC,UBUNTU,XENIAL,XENIAL64,AMD64,XORG,LAST
# fai-diskimage -vNu ubuntu -S7G -c$cl ubuntu.qcow2

Creates a disk image of size 7GB called ubuntu.qcow2 for a Ubuntu 16.04 desktop.
# export NIC1=ens3
# cl=DEBIAN,FAIBASE,UBUNTU,JAMMY64,AMD64,GRUB_PC,DHCPC,XORG,DEMO,LAST
# fai-diskimage -vNu ubuntu -S15G -c$cl ubuntu.qcow2

Creates a disk image of size 15GB called ubuntu.qcow2 for a Ubuntu 22.04 desktop.
You have to set the variable NIC1 to the name of the network
interface inside the environment you will run this disk image. Here,
ens3 is the network name inside a KVM/QEMU virtual machine.

.SH NOTES
fai-diskimage will use zerofree if it's available on the host for
Expand Down

0 comments on commit ba94b5a

Please sign in to comment.