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