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