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 _OBJDIR="$(make -C ${WORLDDIR} -V .OBJDIR)" 18 if [ -d "${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}" ]; then 19 BOOTFILES="${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}/usr/src/sys/boot" 20 else 21 BOOTFILES="${_OBJDIR}/usr/src/sys/boot" 22 fi 23 24 case "${TARGET}:${TARGET_ARCH}" in 25 amd64:amd64 | i386:i386) 26 mkimg -s gpt -b ${BOOTFILES}/i386/pmbr/pmbr \ 27 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \ 28 ${SWAPOPT} \ 29 -p freebsd-ufs/rootfs:=${VMBASE} \ 30 -o ${VMIMAGE} 31 ;;
| 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 _OBJDIR="$(make -C ${WORLDDIR} -V .OBJDIR)" 18 if [ -d "${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}" ]; then 19 BOOTFILES="${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}/usr/src/sys/boot" 20 else 21 BOOTFILES="${_OBJDIR}/usr/src/sys/boot" 22 fi 23 24 case "${TARGET}:${TARGET_ARCH}" in 25 amd64:amd64 | i386:i386) 26 mkimg -s gpt -b ${BOOTFILES}/i386/pmbr/pmbr \ 27 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \ 28 ${SWAPOPT} \ 29 -p freebsd-ufs/rootfs:=${VMBASE} \ 30 -o ${VMIMAGE} 31 ;;
|
32 powerpc:powerpc*) 33 mkimg -s apm \ 34 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \ 35 ${SWAPOPT} \ 36 -p freebsd-ufs/rootfs:=${VMBASE} \ 37 -o ${VMIMAGE} 38 ;; 39 *) 40 # ENOTSUPP 41 return 1 42 ;; 43 esac 44 45 return 0 46} 47 48err() { 49 printf "${@}\n" 50 cleanup 51 return 1 52} 53 54cleanup() { 55 if [ -c "${DESTDIR}/dev/null" ]; then 56 umount_loop ${DESTDIR}/dev 2>/dev/null 57 fi 58 umount_loop ${DESTDIR} 59 if [ ! -z "${mddev}" ]; then 60 mdconfig -d -u ${mddev} 61 fi 62 63 return 0 64} 65 66vm_create_base() { 67 # Creates the UFS root filesystem for the virtual machine disk, 68 # written to the formatted disk image with mkimg(1). 69 70 mkdir -p ${DESTDIR} 71 truncate -s ${VMSIZE} ${VMBASE} 72 mddev=$(mdconfig -f ${VMBASE}) 73 newfs /dev/${mddev} 74 mount /dev/${mddev} ${DESTDIR} 75 76 return 0 77} 78 79vm_copy_base() { 80 # Creates a new UFS root filesystem and copies the contents of the 81 # current root filesystem into it. This produces a "clean" disk 82 # image without any remnants of files which were created temporarily 83 # during image-creation and have since been deleted (e.g., downloaded 84 # package archives). 85 86 mkdir -p ${DESTDIR}/old 87 mdold=$(mdconfig -f ${VMBASE}) 88 mount /dev/${mdold} ${DESTDIR}/old 89 90 truncate -s ${VMSIZE} ${VMBASE}.tmp 91 mkdir -p ${DESTDIR}/new 92 mdnew=$(mdconfig -f ${VMBASE}.tmp) 93 newfs /dev/${mdnew} 94 mount /dev/${mdnew} ${DESTDIR}/new 95 96 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new 97 98 umount_loop /dev/${mdold} 99 rmdir ${DESTDIR}/old 100 mdconfig -d -u ${mdold} 101 102 umount_loop /dev/${mdnew} 103 rmdir ${DESTDIR}/new 104 tunefs -j enable /dev/${mdnew} 105 mdconfig -d -u ${mdnew} 106 mv ${VMBASE}.tmp ${VMBASE} 107} 108 109vm_install_base() { 110 # Installs the FreeBSD userland/kernel to the virtual machine disk. 111 112 cd ${WORLDDIR} && \ 113 make DESTDIR=${DESTDIR} \ 114 installworld installkernel distribution || \ 115 err "\n\nCannot install the base system to ${DESTDIR}." 116 117 echo '# Custom /etc/fstab for FreeBSD VM images' \ 118 > ${DESTDIR}/etc/fstab 119 echo '/dev/gpt/rootfs / ufs rw 1 1' \ 120 >> ${DESTDIR}/etc/fstab 121 if [ -z "${NOSWAP}" ]; then 122 echo '/dev/gpt/swapfs none swap sw 0 0' \ 123 >> ${DESTDIR}/etc/fstab 124 fi 125 126 mkdir -p ${DESTDIR}/dev 127 mount -t devfs devfs ${DESTDIR}/dev 128 chroot ${DESTDIR} /usr/bin/newaliases 129 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 130 umount_loop ${DESTDIR}/dev 131 132 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 133 134 return 0 135} 136 137vm_extra_install_base() { 138 # Prototype. When overridden, runs extra post-installworld commands 139 # as needed, based on the target virtual machine image or cloud 140 # provider image target. 141 142 return 0 143} 144 145vm_extra_enable_services() { 146 if [ ! -z "${VM_RC_LIST}" ]; then 147 for _rcvar in ${VM_RC_LIST}; do 148 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 149 done 150 fi 151 152 return 0 153} 154 155vm_extra_install_packages() { 156 if [ -z "${VM_EXTRA_PACKAGES}" ]; then 157 return 0 158 fi 159 mkdir -p ${DESTDIR}/dev 160 mount -t devfs devfs ${DESTDIR}/dev 161 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 162 /usr/sbin/pkg bootstrap -y 163 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 164 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 165 umount_loop ${DESTDIR}/dev 166 167 return 0 168} 169 170vm_extra_install_ports() { 171 # Prototype. When overridden, installs additional ports within the 172 # virtual machine environment. 173 174 return 0 175} 176 177vm_extra_pre_umount() { 178 # Prototype. When overridden, installs additional ports within the 179 # virtual machine environment. 180 181 rm -f ${DESTDIR}/etc/resolv.conf 182 return 0 183} 184 185vm_extra_pkg_rmcache() { 186 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 187 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 188 /usr/local/sbin/pkg clean -y -a 189 fi 190 191 return 0 192} 193 194umount_loop() { 195 DIR=$1 196 i=0 197 sync 198 while ! umount ${DIR}; do 199 i=$(( $i + 1 )) 200 if [ $i -ge 10 ]; then 201 # This should never happen. But, it has happened. 202 echo "Cannot umount(8) ${DIR}" 203 echo "Something has gone horribly wrong." 204 return 1 205 fi 206 sleep 1 207 done 208 209 return 0 210} 211 212vm_create_disk() { 213 echo "Creating image... Please wait." 214 echo 215 216 write_partition_layout || return 1 217 218 return 0 219} 220 221vm_extra_create_disk() { 222 223 return 0 224} 225
| 39 powerpc:powerpc*) 40 mkimg -s apm \ 41 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \ 42 ${SWAPOPT} \ 43 -p freebsd-ufs/rootfs:=${VMBASE} \ 44 -o ${VMIMAGE} 45 ;; 46 *) 47 # ENOTSUPP 48 return 1 49 ;; 50 esac 51 52 return 0 53} 54 55err() { 56 printf "${@}\n" 57 cleanup 58 return 1 59} 60 61cleanup() { 62 if [ -c "${DESTDIR}/dev/null" ]; then 63 umount_loop ${DESTDIR}/dev 2>/dev/null 64 fi 65 umount_loop ${DESTDIR} 66 if [ ! -z "${mddev}" ]; then 67 mdconfig -d -u ${mddev} 68 fi 69 70 return 0 71} 72 73vm_create_base() { 74 # Creates the UFS root filesystem for the virtual machine disk, 75 # written to the formatted disk image with mkimg(1). 76 77 mkdir -p ${DESTDIR} 78 truncate -s ${VMSIZE} ${VMBASE} 79 mddev=$(mdconfig -f ${VMBASE}) 80 newfs /dev/${mddev} 81 mount /dev/${mddev} ${DESTDIR} 82 83 return 0 84} 85 86vm_copy_base() { 87 # Creates a new UFS root filesystem and copies the contents of the 88 # current root filesystem into it. This produces a "clean" disk 89 # image without any remnants of files which were created temporarily 90 # during image-creation and have since been deleted (e.g., downloaded 91 # package archives). 92 93 mkdir -p ${DESTDIR}/old 94 mdold=$(mdconfig -f ${VMBASE}) 95 mount /dev/${mdold} ${DESTDIR}/old 96 97 truncate -s ${VMSIZE} ${VMBASE}.tmp 98 mkdir -p ${DESTDIR}/new 99 mdnew=$(mdconfig -f ${VMBASE}.tmp) 100 newfs /dev/${mdnew} 101 mount /dev/${mdnew} ${DESTDIR}/new 102 103 tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new 104 105 umount_loop /dev/${mdold} 106 rmdir ${DESTDIR}/old 107 mdconfig -d -u ${mdold} 108 109 umount_loop /dev/${mdnew} 110 rmdir ${DESTDIR}/new 111 tunefs -j enable /dev/${mdnew} 112 mdconfig -d -u ${mdnew} 113 mv ${VMBASE}.tmp ${VMBASE} 114} 115 116vm_install_base() { 117 # Installs the FreeBSD userland/kernel to the virtual machine disk. 118 119 cd ${WORLDDIR} && \ 120 make DESTDIR=${DESTDIR} \ 121 installworld installkernel distribution || \ 122 err "\n\nCannot install the base system to ${DESTDIR}." 123 124 echo '# Custom /etc/fstab for FreeBSD VM images' \ 125 > ${DESTDIR}/etc/fstab 126 echo '/dev/gpt/rootfs / ufs rw 1 1' \ 127 >> ${DESTDIR}/etc/fstab 128 if [ -z "${NOSWAP}" ]; then 129 echo '/dev/gpt/swapfs none swap sw 0 0' \ 130 >> ${DESTDIR}/etc/fstab 131 fi 132 133 mkdir -p ${DESTDIR}/dev 134 mount -t devfs devfs ${DESTDIR}/dev 135 chroot ${DESTDIR} /usr/bin/newaliases 136 chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart 137 umount_loop ${DESTDIR}/dev 138 139 cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf 140 141 return 0 142} 143 144vm_extra_install_base() { 145 # Prototype. When overridden, runs extra post-installworld commands 146 # as needed, based on the target virtual machine image or cloud 147 # provider image target. 148 149 return 0 150} 151 152vm_extra_enable_services() { 153 if [ ! -z "${VM_RC_LIST}" ]; then 154 for _rcvar in ${VM_RC_LIST}; do 155 echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf 156 done 157 fi 158 159 return 0 160} 161 162vm_extra_install_packages() { 163 if [ -z "${VM_EXTRA_PACKAGES}" ]; then 164 return 0 165 fi 166 mkdir -p ${DESTDIR}/dev 167 mount -t devfs devfs ${DESTDIR}/dev 168 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 169 /usr/sbin/pkg bootstrap -y 170 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 171 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} 172 umount_loop ${DESTDIR}/dev 173 174 return 0 175} 176 177vm_extra_install_ports() { 178 # Prototype. When overridden, installs additional ports within the 179 # virtual machine environment. 180 181 return 0 182} 183 184vm_extra_pre_umount() { 185 # Prototype. When overridden, installs additional ports within the 186 # virtual machine environment. 187 188 rm -f ${DESTDIR}/etc/resolv.conf 189 return 0 190} 191 192vm_extra_pkg_rmcache() { 193 if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then 194 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ 195 /usr/local/sbin/pkg clean -y -a 196 fi 197 198 return 0 199} 200 201umount_loop() { 202 DIR=$1 203 i=0 204 sync 205 while ! umount ${DIR}; do 206 i=$(( $i + 1 )) 207 if [ $i -ge 10 ]; then 208 # This should never happen. But, it has happened. 209 echo "Cannot umount(8) ${DIR}" 210 echo "Something has gone horribly wrong." 211 return 1 212 fi 213 sleep 1 214 done 215 216 return 0 217} 218 219vm_create_disk() { 220 echo "Creating image... Please wait." 221 echo 222 223 write_partition_layout || return 1 224 225 return 0 226} 227 228vm_extra_create_disk() { 229 230 return 0 231} 232
|