Debian Codegraph @

bin/fai-diskimage

raw symbols SCIP
1 #! /bin/bashscip-shell filesystem . . `/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 diescip-shell shell fai 6.6 die().() {
11
12 local e=$1 # first parameter is the exit code
13 shift
14
15 echoscip-shell system . . echo. "ERROR: $*" >&2 # print error message
16 exit $e
17 }
18
19 cleanupscip-shell shell fai 6.6 cleanup().() {
20
21 local dir
22 for dir in $(mountscip-shell system . . mount. | awkscip-shell system . . awk. '{print $3}'| grepscip-shell system . . grep. $mnt | sortscip-shell system . . 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 umountscip-shell system . . umount. $dir >&/dev/null && break
26 [scip-shell system . . `[`. $((i % 3)) -eq 0 ] && echoscip-shell system . . echo. "Waiting for background jobs to finish."
27 [scip-shell system . . `[`. $i -gt 12 ] && echoscip-shell system . . echo. "Still cannot umount $dir. Will try again."
28 sleepscip-shell system . . sleep. $i
29 done
30 echoscip-shell system . . echo. "ERROR: fai-diskimage cannot umount $dir" >&2
31 done
32
33 # call zerofree for ext2/3/4 devices if available
34 if [scip-shell system . . `[`. -f /var/run/fai/zerofree.$$ ] && [scip-shell system . . `[`. -x /usr/sbin/zerofreescip-shell filesystem . . `/usr/sbin/zerofree`. ]; then
35 . /var/run/fai/zerofree.$$ 2>/dev/nullscip-shell filesystem . . `/dev/null`.
36 rmscip-shell system . . 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 [scip-shell system . . `[`. -f /var/run/fai/vgremove.$$ ]; then
40 . /var/run/fai/vgremove.$$ 2>/dev/nullscip-shell filesystem . . `/dev/null`.
41 rmscip-shell system . . rm. /var/run/fai/vgremove.$$
42 fi
43 rmscip-shell system . . rm. -rf $mnt
44 losetupscip-shell system . . losetup. -d $loop
45 if [scip-shell system . . `[`. -f /var/run/fai/FAI_INSTALLATION_IN_PROGRESSscip-shell filesystem . . `/var/run/fai/FAI_INSTALLATION_IN_PROGRESS`. ]; then
46 if pgrepscip-shell system . . pgrep. -F /var/run/fai/FAI_INSTALLATION_IN_PROGRESSscip-shell filesystem . . `/var/run/fai/FAI_INSTALLATION_IN_PROGRESS`.; then
47 :
48 else
49 rmscip-shell system . . rm. /var/run/fai/FAI_INSTALLATION_IN_PROGRESSscip-shell filesystem . . `/var/run/fai/FAI_INSTALLATION_IN_PROGRESS`.
50 fi
51 fi
52 if [scip-shell system . . `[`. -d /sys/modules/loopscip-shell filesystem . . `/sys/modules/loop`. ]; then
53 rmmod loop
54 fi
55 }
56
57 usagescip-shell shell fai 6.6 usage().() {
58
59 echoscip-shell system . . 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_commandsscip-shell shell fai 6.6 check_commands().() {
79
80 local error=0
81 if ! command -v qemu-img >&/dev/null; then
82 echoscip-shell system . . 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 echoscip-shell system . . echo. "setup-storage not found. Install the package fai-setup-storage."
87 error=1
88 fi
89 if [scip-shell system . . `[`. $error -eq 1 ]; then
90 diescip-shell shell fai 6.6 die(). 5 "Aborted."
91 fi
92 }
93
94 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95
96 call="$0 $*"
97 TEMP=$(getoptscip-shell system . . getopt. -o C:NS:s:u:hvc: --long new,cfdir:,cspace:,hostname:,class:,size:,help,verbose -n "$0" -- "$@")
98 if [scip-shell system . . `[`. $? != 0 ] ; then diescip-shell shell fai 6.6 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 truescip-shell system . . true. ; do
107 case "$1" in
108 -h|--help)
109 usagescip-shell shell fai 6.6 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 diescip-shell shell fai 6.6 die(). 1 "$0: command line parsing error ! $*" >&2 ;;
136 esac
137 done
138
139 # check options, set defaults
140
141 [scip-shell system . . `[`. "$1" ] || usagescip-shell shell fai 6.6 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 echoscip-shell system . . 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 *) diescip-shell shell fai 6.6 die(). 8 "Unknown suffix. Please use raw, raw.zst, raw.xz, qcow2, vdi, vmdk or vhdx, simg."
188 esac
189
190 if [scip-shell system . . `[`. -z "$classes" ] && [scip-shell system . . `[`. -z "$renew" ]; then
191 diescip-shell shell fai 6.6 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_commandsscip-shell shell fai 6.6 check_commands().
204
205 # check root
206 if [scip-shell system . . `[`. $(idscip-shell system . . id. -u) != "0" ]; then
207 diescip-shell shell fai 6.6 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 rmscip-shell system . . rm. -f $rawname
216 qemu-img create $rawname $size
217 loop=$(losetupscip-shell system . . losetup. -P -f --show $rawname)
218 loopdev=${loop/\/dev\//}
219 export disklist=$loopdev
220 mnt=$(mktempscip-shell system . . 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 cleanupscip-shell shell fai 6.6 cleanup().
227
228 trap - EXIT
229
230 # convert if needed
231 if [scip-shell system . . `[`. $convert -eq 1 ] || [scip-shell system . . `[`. $convert -eq 3 ]; then
232 [scip-shell system . . `[`. $verbose -eq 1 ] && echoscip-shell system . . echo. "Converting $rawname to $qcowname"
233 qemu-img convert -f raw $rawname $copt $qcowname
234 rmscip-shell system . . rm. $rawname
235 fi
236 if [scip-shell system . . `[`. $convert -ge 2 ]; then
237 [scip-shell system . . `[`. $verbose -eq 1 ] && echoscip-shell system . . echo. "Converting to $image"
238 $cmd
239 fi
240 echoscip-shell system . . echo. -n "Size of disk image and filename: "; duscip-shell system . . du. -h $image
241 echoscip-shell system . . echo. "Image created by: $call"
242 exit $error