1#!/bin/sh 2# 3# $FreeBSD$ 4# 5# 6# Common functions for virtual machine image build scripts. 7# 8 9export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" 10trap "cleanup" INT QUIT TRAP ABRT TERM 11 12write_partition_layout() { 13 14 case "${TARGET}:${TARGET_ARCH}" in 15 amd64:amd64 | i386:i386) 16 mkimg -s gpt -b /boot/pmbr \ 17 -p freebsd-boot/bootfs:=/boot/gptboot \ 18 -p freebsd-swap/swapfs::1G \ 19 -p freebsd-ufs/rootfs:=${VMBASE} \ 20 -o ${VMIMAGE} 21 ;; 22 powerpc:powerpc*) 23 mkimg -s apm \ 24 -p apple-boot/bootfs:=/boot/boot1.hfs \ 25 -p freebsd-swap/swapfs::1G \ 26 -p freebsd-ufs/rootfs:=${VMBASE} \ 27 -o ${VMIMAGE} 28 ;; 29 *) 30 # ENOTSUPP 31 return 1 32 ;; 33 esac 34 35 return 0 36} 37 38err() { 39 printf "${@}\n" 40 cleanup 41 return 1 42} 43 44cleanup() { 45 if [ ! -z "${mddev}" ]; then 46 mdconfig -d -u ${mddev} 47 fi 48 umount ${DESTDIR}/dev 49 umount ${DESTDIR} 50 51 return 0 52} 53 54vm_create_base() { 55 # Creates the UFS root filesystem for the virtual machine disk, 56 # written to the formatted disk image with mkimg(1). 57 58 mkdir -p ${DESTDIR} 59 truncate -s ${VMSIZE} ${VMBASE} 60 mddev=$(mdconfig -f ${VMBASE}) 61 newfs -j /dev/${mddev} 62 mount /dev/${mddev} ${DESTDIR} 63 64 return 0 65} 66 67vm_install_base() { 68 # Installs the FreeBSD userland/kernel to the virtual machine disk. 69 70 cd ${WORLDDIR} && \ 71 make DESTDIR=${DESTDIR} \ 72 installworld installkernel distribution || \ 73 err "\n\nCannot install the base system to ${DESTDIR}." 74 75 echo '# Custom /etc/fstab for FreeBSD VM images' \ 76 > ${DESTDIR}/etc/fstab 77 echo '/dev/gpt/rootfs / ufs rw 1 1' \ 78 >> ${DESTDIR}/etc/fstab 79 echo '/dev/gpt/swapfs none swap sw 0 0' \ 80 >> ${DESTDIR}/etc/fstab 81 82 mkdir -p ${DESTDIR}/dev 83 mount -t devfs devfs ${DESTDIR}/dev 84 chroot ${DESTDIR} /usr/bin/newaliases 85 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 86 umount ${DESTDIR}/dev 87 88 return 0 89} 90 91vm_extra_install_base() { 92 # Prototype. When overridden, runs extra post-installworld commands 93 # as needed, based on the target virtual machine image or cloud 94 # provider image target. 95 96 return 0 97} 98 99vm_extra_enable_services() { 100 if [ ! -z "${VM_RC_LIST}" ]; then 101 for _rcvar in ${VM_RC_LIST}; do 102 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 103 done 104 fi 105 106 return 0 107} 108 109vm_extra_install_packages() { 110 mkdir -p ${DESTDIR}/dev 111 mount -t devfs devfs ${DESTDIR}/dev 112 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 113 /usr/sbin/pkg bootstrap -y 114 if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then 115 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 116 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 117 fi 118 umount ${DESTDIR}/dev 119 120 return 0 121} 122 123vm_extra_install_ports() { 124 # Prototype. When overridden, installs additional ports within the 125 # virtual machine environment. 126 127 return 0 128} 129 130vm_extra_pre_umount() { 131 # Prototype. When overridden, installs additional ports within the 132 # virtual machine environment. 133 134 return 0 135} 136 137vm_umount_base() { 138 i=0 139 sync 140 while ! umount ${DESTDIR}/dev ${DESTDIR}; do 141 i=$(( $i + 1 )) 142 if [ $i -ge 10 ]; then 143 # This should never happen. But, it has happened. 144 msg="Cannot umount(8) ${DESTDIR}\n" 145 msg="${msg}Something has gone horribly wrong." 146 err "${msg}" 147 fi 148 sleep 1 149 done 150 151 return 0 152} 153 154vm_create_disk() { 155 echo "Creating image... Please wait." 156 echo 157 158 write_partition_layout || return 1 159 160 return 0 161} 162 163vm_extra_create_disk() { 164 165 return 0 166} 167 168