]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - release/tools/vmimage.subr
MFS r353177:
[FreeBSD/FreeBSD.git] / release / tools / vmimage.subr
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5 #
6 # Common functions for virtual machine image build scripts.
7 #
8
9 export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
10 trap "cleanup" INT QUIT TRAP ABRT TERM
11
12 write_partition_layout() {
13         if [ -z "${NOSWAP}" ]; then
14                 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
15         fi
16
17         BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
18                 WITH_UNIFIED_OBJDIR=yes \
19                 make -C ${WORLDDIR}/stand -V .OBJDIR)"
20         BOOTFILES="$(realpath ${BOOTFILES})"
21
22         case "${TARGET}:${TARGET_ARCH}" in
23                 amd64:amd64 | i386:i386)
24                         mkimg -s gpt -f ${VMFORMAT} \
25                                 -b ${BOOTFILES}/i386/pmbr/pmbr \
26                                 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
27                                 ${SWAPOPT} \
28                                 -p freebsd-ufs/rootfs:=${VMBASE} \
29                                 -o ${VMIMAGE}
30                         ;;
31                 arm64:aarch64)
32                         mkimg -s mbr -f ${VMFORMAT} \
33                                 -p efi:=${BOOTFILES}/efi/boot1/boot1.efifat \
34                                 -p freebsd:=${VMBASE} \
35                                 -o ${VMIMAGE}
36                         ;;
37                 powerpc:powerpc*)
38                         mkimg -s apm -f ${VMFORMAT} \
39                                 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
40                                 ${SWAPOPT} \
41                                 -p freebsd-ufs/rootfs:=${VMBASE} \
42                                 -o ${VMIMAGE}
43                         ;;
44                 *)
45                         # ENOTSUPP
46                         return 1
47                         ;;
48         esac
49
50         return 0
51 }
52
53 err() {
54         printf "${@}\n"
55         cleanup
56         return 1
57 }
58
59 cleanup() {
60         if [ -c "${DESTDIR}/dev/null" ]; then
61                 umount_loop ${DESTDIR}/dev 2>/dev/null
62         fi
63         umount_loop ${DESTDIR}
64         if [ ! -z "${mddev}" ]; then
65                 mdconfig -d -u ${mddev}
66         fi
67
68         return 0
69 }
70
71 vm_create_base() {
72         # Creates the UFS root filesystem for the virtual machine disk,
73         # written to the formatted disk image with mkimg(1).
74
75         mkdir -p ${DESTDIR}
76         truncate -s ${VMSIZE} ${VMBASE}
77         mddev=$(mdconfig -f ${VMBASE})
78         newfs -L rootfs /dev/${mddev}
79         mount /dev/${mddev} ${DESTDIR}
80
81         return 0
82 }
83
84 vm_copy_base() {
85         # Creates a new UFS root filesystem and copies the contents of the
86         # current root filesystem into it.  This produces a "clean" disk
87         # image without any remnants of files which were created temporarily
88         # during image-creation and have since been deleted (e.g., downloaded
89         # package archives).
90
91         mkdir -p ${DESTDIR}/old
92         mdold=$(mdconfig -f ${VMBASE})
93         mount /dev/${mdold} ${DESTDIR}/old
94
95         truncate -s ${VMSIZE} ${VMBASE}.tmp
96         mkdir -p ${DESTDIR}/new
97         mdnew=$(mdconfig -f ${VMBASE}.tmp)
98         newfs -L rootfs /dev/${mdnew}
99         mount /dev/${mdnew} ${DESTDIR}/new
100
101         tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
102
103         umount_loop /dev/${mdold}
104         rmdir ${DESTDIR}/old
105         mdconfig -d -u ${mdold}
106
107         umount_loop /dev/${mdnew}
108         rmdir ${DESTDIR}/new
109         tunefs -n enable /dev/${mdnew}
110         mdconfig -d -u ${mdnew}
111         mv ${VMBASE}.tmp ${VMBASE}
112 }
113
114 vm_install_base() {
115         # Installs the FreeBSD userland/kernel to the virtual machine disk.
116
117         cd ${WORLDDIR} && \
118                 make DESTDIR=${DESTDIR} \
119                 installworld installkernel distribution || \
120                 err "\n\nCannot install the base system to ${DESTDIR}."
121
122         # Bootstrap etcupdate(8) and mergemaster(8) databases.
123         mkdir -p ${DESTDIR}/var/db/etcupdate
124         etcupdate extract -B \
125                 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
126                 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
127         sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
128                 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
129                 -D ${DESTDIR}
130
131         echo '# Custom /etc/fstab for FreeBSD VM images' \
132                 > ${DESTDIR}/etc/fstab
133         echo "/dev/${ROOTLABEL}/rootfs   /       ufs     rw      1       1" \
134                 >> ${DESTDIR}/etc/fstab
135         if [ -z "${NOSWAP}" ]; then
136                 echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
137                         >> ${DESTDIR}/etc/fstab
138         fi
139
140         local hostname
141         hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
142         echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
143
144         if ! [ -z "${QEMUSTATIC}" ]; then
145                 export EMULATOR=/qemu
146                 cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
147         fi
148
149         mkdir -p ${DESTDIR}/dev
150         mount -t devfs devfs ${DESTDIR}/dev
151         chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
152         chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
153         umount_loop ${DESTDIR}/dev
154
155         cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
156
157         return 0
158 }
159
160 vm_extra_install_base() {
161         # Prototype.  When overridden, runs extra post-installworld commands
162         # as needed, based on the target virtual machine image or cloud
163         # provider image target.
164
165         return 0
166 }
167
168 vm_extra_enable_services() {
169         if [ ! -z "${VM_RC_LIST}" ]; then
170                 for _rcvar in ${VM_RC_LIST}; do
171                         echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
172                 done
173         fi
174
175         if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
176                 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
177                         ${DESTDIR}/etc/rc.conf
178                 # Expand the filesystem to fill the disk.
179                 echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
180                 touch ${DESTDIR}/firstboot
181         fi
182
183         return 0
184 }
185
186 vm_extra_install_packages() {
187         if [ -z "${VM_EXTRA_PACKAGES}" ]; then
188                 return 0
189         fi
190         mkdir -p ${DESTDIR}/dev
191         mount -t devfs devfs ${DESTDIR}/dev
192         chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
193                 /usr/sbin/pkg bootstrap -y
194         chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
195                 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
196         umount_loop ${DESTDIR}/dev
197
198         return 0
199 }
200
201 vm_extra_install_ports() {
202         # Prototype.  When overridden, installs additional ports within the
203         # virtual machine environment.
204
205         return 0
206 }
207
208 vm_extra_pre_umount() {
209         # Prototype.  When overridden, performs additional tasks within the
210         # virtual machine environment prior to unmounting the filesystem.
211         # Note: When overriding this function, removing resolv.conf in the
212         # disk image must be included.
213
214         if ! [ -z "${QEMUSTATIC}" ]; then
215                 rm -f ${DESTDIR}/${EMULATOR}
216         fi
217         rm -f ${DESTDIR}/etc/resolv.conf
218         return 0
219 }
220
221 vm_extra_pkg_rmcache() {
222         if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
223                 chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
224                         /usr/local/sbin/pkg clean -y -a
225         fi
226
227         return 0
228 }
229
230 umount_loop() {
231         DIR=$1
232         i=0
233         sync
234         while ! umount ${DIR}; do
235                 i=$(( $i + 1 ))
236                 if [ $i -ge 10 ]; then
237                         # This should never happen.  But, it has happened.
238                         echo "Cannot umount(8) ${DIR}"
239                         echo "Something has gone horribly wrong."
240                         return 1
241                 fi
242                 sleep 1
243         done
244
245         return 0
246 }
247
248 vm_create_disk() {
249         echo "Creating image...  Please wait."
250         echo
251
252         write_partition_layout || return 1
253
254         return 0
255 }
256
257 vm_extra_create_disk() {
258
259         return 0
260 }
261