Debian Codegraph @

bin/fai-diskimage

raw symbols SCIP
1 #! /bin/bash
2
3 # fai-diskimage - create a disk image for booting a VM
4
5 # This script is part of FAI (Fully Automatic Installation)
6 #
7 # Copyright (C) 2016-2025 Thomas Lange, [email protected]
8 # Universitaet zu Koeln
9
10 die() {
11
12 local e=$1 # first parameter is the exit code
13 shift
14
15 echo "ERROR: $*" >&2 # print error message
16 exit $e
17 }
18
19 cleanup() {
20
21 local dir
22 for dir in $(mount | awk '{print $3}'| grep $mnt | sort -r); do
23 # try umount several times, because some postinst jobs may still run and use the filesystems
24 for i in {1..15}; do
25 umount $dir >&/dev/null && break
26 [ $((i % 3)) -eq 0 ] && echo "Waiting for background jobs to finish."
27 [ $i -gt 12 ] && echo "Still cannot umount $dir. Will try again."
28 sleep $i
29 done
30 echo "ERROR: fai-diskimage cannot umount $dir" >&2
31 done
32
33 # call zerofree for ext2/3/4 devices if available
34 if [ -f /var/run/fai/zerofree.$$ ] && [ -x /usr/sbin/zerofree ]; then
35 . /var/run/fai/zerofree.$$ 2>/dev/null
36 rm /var/run/fai/zerofree.$$
37 fi
38 # if FAI created a volume group, we can remove it after the loop device is removed
39 if [ -f /var/run/fai/vgremove.$$ ]; then
40 . /var/run/fai/vgremove.$$ 2>/dev/null
41 rm /var/run/fai/vgremove.$$
42 fi
43 rm -rf $mnt
44 losetup -d $loop
45 if [ -f /var/run/fai/FAI_INSTALLATION_IN_PROGRESS ]; then
46 if pgrep -F /var/run/fai/FAI_INSTALLATION_IN_PROGRESS; then
47 :
48 else
49 rm /var/run/fai/FAI_INSTALLATION_IN_PROGRESS
50 fi
51 fi
52 if [ -d /sys/modules/loop ]; then
53 rmmod loop
54 fi
55 }
56
57 usage() {
58
59 echo "Usage: $0 name.raw
60
61 Create a disk image name.raw using FAI and a list of FAI classes.
62 This can be used for a virtual machine or a cloud instance. If you
63 use another suffix the image will be converted. Following formats are
64 supported: .raw.xz, .raw.zst, .qcow2, .vdi, .vhdx, .vmdk, .simg.
65
66 -h|--help print help
67 -v|--verbose be verbose
68 -N|--new execute scripts class/[0-9]* for defining classes
69 -c|--class <class,...> define list of FAI classes
70 -C|--cfdir <dir> Use dir for reading the config files (default /etc/fai)
71 -S|--size <size> size of raw image (suffixes k M G T are supported)
72 -s|--cspace <uri> location of the config space
73 -u|--hostname <name> set hostname to name
74 "
75 exit $1
76 }
77
78 check_commands() {
79
80 local error=0
81 if ! command -v qemu-img >&/dev/null; then
82 echo "qemu-img not found. Install the package qemu-utils."
83 error=1
84 fi
85 if ! command -v setup-storage >&/dev/null; then
86 echo "setup-storage not found. Install the package fai-setup-storage."
87 error=1
88 fi
89 if [ $error -eq 1 ]; then
90 die 5 "Aborted."
91 fi
92 }
93
94 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95
96 call="$0 $*"
97 TEMP=$(getopt -o C:NS:s:u:hvc: --long new,cfdir:,cspace:,hostname:,class:,size:,help,verbose -n "$0" -- "$@")
98 if [ $? != 0 ] ; then die 6 "Wrong option. Terminating." >&2 ; fi
99 # Note the quotes around `$TEMP': they are essential!
100 eval set -- "$TEMP"
101 unset TEMP
102
103 verbose=0
104 convert=1
105
106 while true ; do
107 case "$1" in
108 -h|--help)
109 usage 0 ;;
110 -v|--verbose)
111 export verbose=1
112 shift ;;
113 -c|--class)
114 export classes="-c $2"
115 shift 2 ;;
116 -C|--cfdir)
117 cfdir="-C $2 "
118 shift 2 ;;
119 -N|--new)
120 renew="-N"
121 shift ;;
122 -S|--size)
123 size=$2
124 shift 2 ;;
125 -s|--cspace)
126 cspace=$2
127 shift 2 ;;
128 -u|--hostname)
129 export hname=$2
130 shift 2 ;;
131 --)
132 shift
133 break ;;
134 *)
135 die 1 "$0: command line parsing error ! $*" >&2 ;;
136 esac
137 done
138
139 # check options, set defaults
140
141 [ "$1" ] || usage
142 image=$1
143
144 iname=${image%.*} # strip last suffix
145 iname2=${iname%.*} # strip next to last suffix
146 rawname="$iname.raw"
147
148 case "$image" in
149 *.raw.xz)
150 convert=2
151 rawname=$iname
152 export XZ_OPT=${XZ_OPT-"-1 -T0"}
153 cmd="xz $rawname"
154 ;;
155
156 *.raw.zst)
157 convert=2
158 rawname=$iname
159 cmd="zstd --rm -q -9 -T0 $rawname"
160 ;;
161
162 *.raw)
163 convert=0
164 ;;
165
166 *.qcow2)
167 copt="-O qcow2 -c -o compression_type=zstd "
168 qcowname="$iname.qcow2"
169 ;;
170
171 *.simg) convert=2
172 cmd="img2simg $rawname $iname.simg"
173 if ! command -v img2simg >&/dev/null; then
174 echo "img2simg not found. Install the package img2simg."
175 error=1
176 fi
177 ;;
178 *.vdi) copt="-O vdi"
179 qcowname="$iname.vdi"
180 ;;
181 *.vmdk) copt="-O vmdk"
182 qcowname="$iname.vmdk"
183 ;;
184 *.vhdx) copt="-O vhdx"
185 qcowname="$iname.vhdx"
186 ;;
187 *) die 8 "Unknown suffix. Please use raw, raw.zst, raw.xz, qcow2, vdi, vmdk or vhdx, simg."
188 esac
189
190 if [ -z "$classes" ] && [ -z "$renew" ]; then
191 die 7 "No classes are defined. Use -c or -N."
192 fi
193
194 : ${cspace:=/srv/fai/config}
195 # if cspace starts with /, add prefix file://
196 uri=${cspace/#\//file:///}
197
198 : ${size:=800M}
199 : ${hname:=debian.example.com}
200
201 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202
203 check_commands
204
205 # check root
206 if [ $(id -u) != "0" ]; then
207 die 1 "Run this program as root."
208 fi
209
210 if command -v vgs >&/dev/null; then
211 export SS_IGNORE_VG=$(vgs --unbuffered --noheadings -o name --rows)
212 fi
213
214 # create empty disk image, loop setup, temp mount point
215 rm -f $rawname
216 qemu-img create $rawname $size
217 loop=$(losetup -P -f --show $rawname)
218 loopdev=${loop/\/dev\//}
219 export disklist=$loopdev
220 mnt=$(mktemp -d -t fai-diskimage.XXXXXX)
221
222 trap "cleanup" EXIT
223
224 LC_ALL=C fai ${cfdir}${renew} -u $hname -s $uri $classes install $mnt
225 error=$?
226 cleanup
227
228 trap - EXIT
229
230 # convert if needed
231 if [ $convert -eq 1 ] || [ $convert -eq 3 ]; then
232 [ $verbose -eq 1 ] && echo "Converting $rawname to $qcowname"
233 qemu-img convert -f raw $rawname $copt $qcowname
234 rm $rawname
235 fi
236 if [ $convert -ge 2 ]; then
237 [ $verbose -eq 1 ] && echo "Converting to $image"
238 $cmd
239 fi
240 echo -n "Size of disk image and filename: "; du -h $image
241 echo "Image created by: $call"
242 exit $error