vmimage.subr (e70eb40271512dfbca7cecf823e4b445e3989c2e) vmimage.subr (1ca8842f3ad9725863c9affc044d1974a51818a9)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5#
6# Common functions for virtual machine image build scripts.
7#
8
9scriptdir=$(dirname $(realpath $0))
10. ${scriptdir}/../../tools/boot/install-boot.sh
11
12export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
13trap "cleanup" INT QUIT TRAP ABRT TERM
14
1#!/bin/sh
2#
3# $FreeBSD$
4#
5#
6# Common functions for virtual machine image build scripts.
7#
8
9scriptdir=$(dirname $(realpath $0))
10. ${scriptdir}/../../tools/boot/install-boot.sh
11
12export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
13trap "cleanup" INT QUIT TRAP ABRT TERM
14
15write_partition_layout() {
16 if [ -z "${NOSWAP}" ]; then
17 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
18 fi
15# Platform-specific large-scale setup
16# Most platforms use GPT, so put that as default, then special cases
17PARTSCHEME=gpt
18ROOTLABEL="gpt"
19case "${TARGET}:${TARGET_ARCH}" in
20 powerpc:powerpc*)
21 PARTSCHEME=mbr
22 ROOTLABEL="ufs"
23 NOSWAP=yes # Can't label swap partition with MBR, so no swap
24 ;;
25esac
19
26
20 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
21 WITH_UNIFIED_OBJDIR=yes \
22 make -C ${WORLDDIR}/stand -V .OBJDIR)"
23 BOOTFILES="$(realpath ${BOOTFILES})"
24
25 case "${TARGET}:${TARGET_ARCH}" in
26 amd64:amd64 | i386:i386)
27 ESP=yes
28 SCHEME=gpt
29 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
30 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot"
31 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
32 ;;
33 arm64:aarch64 | riscv:riscv64*)
34 ESP=yes
35 SCHEME=gpt
36 BOOTPARTS=
37 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
38 ;;
39 powerpc:powerpc*)
40 ESP=no
41 SCHEME=apm
42 BOOTPARTS="-p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs"
43 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
44 ;;
45 *)
46 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
47 exit 1
48 ;;
49 esac
50
51 if [ ${ESP} = "yes" ]; then
52 # Create an ESP
53 espfilename=$(mktemp /tmp/efiboot.XXXXXX)
54 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
55 BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
56
57 # Add this to fstab, requires temporarily remounting the fs
58 mddev=$(mdconfig -f ${VMBASE})
59 mount /dev/${mddev} ${DESTDIR}
60 mkdir -p ${DESTDIR}/boot/efi
61 echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \
62 >> ${DESTDIR}/etc/fstab
63 umount ${DESTDIR}
64 mdconfig -d -u ${mddev}
65 fi
66
67 mkimg -s ${SCHEME} -f ${VMFORMAT} \
68 ${BOOTPARTS} \
69 ${SWAPOPT} \
70 ${ROOTFSPART} \
71 -o ${VMIMAGE}
72
73 if [ ${ESP} = "yes" ]; then
74 rm ${espfilename}
75 fi
76
77 return 0
78}
79
80err() {
81 printf "${@}\n"
82 cleanup
83 return 1
84}
85
86cleanup() {
87 if [ -c "${DESTDIR}/dev/null" ]; then
88 umount_loop ${DESTDIR}/dev 2>/dev/null
89 fi
27err() {
28 printf "${@}\n"
29 cleanup
30 return 1
31}
32
33cleanup() {
34 if [ -c "${DESTDIR}/dev/null" ]; then
35 umount_loop ${DESTDIR}/dev 2>/dev/null
36 fi
90 umount_loop ${DESTDIR}
91 if [ ! -z "${mddev}" ]; then
92 mdconfig -d -u ${mddev}
93 fi
94
95 return 0
96}
97
98vm_create_base() {
99 # Creates the UFS root filesystem for the virtual machine disk,
100 # written to the formatted disk image with mkimg(1).
101
102 mkdir -p ${DESTDIR}
37
38 return 0
39}
40
41vm_create_base() {
42 # Creates the UFS root filesystem for the virtual machine disk,
43 # written to the formatted disk image with mkimg(1).
44
45 mkdir -p ${DESTDIR}
103 truncate -s ${VMSIZE} ${VMBASE}
104 mddev=$(mdconfig -f ${VMBASE})
105 newfs -L rootfs /dev/${mddev}
106 mount /dev/${mddev} ${DESTDIR}
107
108 return 0
109}
110
111vm_copy_base() {
46
47 return 0
48}
49
50vm_copy_base() {
112 # Creates a new UFS root filesystem and copies the contents of the
113 # current root filesystem into it. This produces a "clean" disk
114 # image without any remnants of files which were created temporarily
115 # during image-creation and have since been deleted (e.g., downloaded
116 # package archives).
117
118 mkdir -p ${DESTDIR}/old
119 mdold=$(mdconfig -f ${VMBASE})
120 mount /dev/${mdold} ${DESTDIR}/old
121
122 truncate -s ${VMSIZE} ${VMBASE}.tmp
123 mkdir -p ${DESTDIR}/new
124 mdnew=$(mdconfig -f ${VMBASE}.tmp)
125 newfs -L rootfs /dev/${mdnew}
126 mount /dev/${mdnew} ${DESTDIR}/new
127
128 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
129
130 umount_loop /dev/${mdold}
131 rmdir ${DESTDIR}/old
132 mdconfig -d -u ${mdold}
133
134 umount_loop /dev/${mdnew}
135 rmdir ${DESTDIR}/new
136 tunefs -n enable /dev/${mdnew}
137 mdconfig -d -u ${mdnew}
138 mv ${VMBASE}.tmp ${VMBASE}
51 # Defunct
139}
140
141vm_install_base() {
142 # Installs the FreeBSD userland/kernel to the virtual machine disk.
143
144 cd ${WORLDDIR} && \
145 make DESTDIR=${DESTDIR} \
146 installworld installkernel distribution || \

--- 124 unchanged lines hidden (view full) ---

271
272 return 0
273}
274
275vm_create_disk() {
276 echo "Creating image... Please wait."
277 echo
278
52}
53
54vm_install_base() {
55 # Installs the FreeBSD userland/kernel to the virtual machine disk.
56
57 cd ${WORLDDIR} && \
58 make DESTDIR=${DESTDIR} \
59 installworld installkernel distribution || \

--- 124 unchanged lines hidden (view full) ---

184
185 return 0
186}
187
188vm_create_disk() {
189 echo "Creating image... Please wait."
190 echo
191
279 write_partition_layout || return 1
192 if [ -z "${NOSWAP}" ]; then
193 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
194 fi
280
195
196 BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
197 WITH_UNIFIED_OBJDIR=yes \
198 make -C ${WORLDDIR}/stand -V .OBJDIR)"
199 BOOTFILES="$(realpath ${BOOTFILES})"
200
201 case "${TARGET}:${TARGET_ARCH}" in
202 amd64:amd64 | i386:i386)
203 ESP=yes
204 BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
205 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot"
206 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
207 MAKEFSARGS="-B little"
208 ;;
209 arm64:aarch64 | riscv:riscv64*)
210 ESP=yes
211 BOOTPARTS=
212 ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
213 MAKEFSARGS="-B little"
214 ;;
215 powerpc:powerpc*)
216 ESP=no
217 BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1"
218 ROOTFSPART="-p freebsd:=${VMBASE}"
219 if [ ${TARGET_ARCH} = powerpc64le ]; then
220 MAKEFSARGS="-B little"
221 else
222 MAKEFSARGS="-B big"
223 fi
224 ;;
225 *)
226 echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
227 exit 1
228 ;;
229 esac
230
231 if [ ${ESP} = "yes" ]; then
232 # Create an ESP
233 espfilename=$(mktemp /tmp/efiboot.XXXXXX)
234 make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
235 BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
236
237 # Add this to fstab
238 mkdir -p ${DESTDIR}/boot/efi
239 echo "/dev/${ROOTLABEL}/efiesp /boot/efi msdosfs rw 2 2" \
240 >> ${DESTDIR}/etc/fstab
241 fi
242
243 echo "Building filesystem... Please wait."
244 makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \
245 -s ${VMSIZE} ${VMBASE} ${DESTDIR}
246
247 echo "Building final disk image... Please wait."
248 mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \
249 ${BOOTPARTS} \
250 ${SWAPOPT} \
251 ${ROOTFSPART} \
252 -o ${VMIMAGE}
253
254 if [ ${ESP} = "yes" ]; then
255 rm ${espfilename}
256 fi
257
281 return 0
282}
283
284vm_extra_create_disk() {
285
286 return 0
287}
288
258 return 0
259}
260
261vm_extra_create_disk() {
262
263 return 0
264}
265