Index: head/release/scripts/mk-vmimage.sh =================================================================== --- head/release/scripts/mk-vmimage.sh (revision 280298) +++ head/release/scripts/mk-vmimage.sh (revision 280299) @@ -1,111 +1,112 @@ #!/bin/sh #- # Copyright (c) 2014, 2015 The FreeBSD Foundation # All rights reserved. # # This software was developed by Glen Barber under sponsorship # from the FreeBSD Foundation. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # mk-vmimage.sh: Create virtual machine disk images in various formats. # # $FreeBSD$ # usage() { echo "${0} usage:" echo "${@}" return 1 } main() { local arg VMCONFIG="/dev/null" while getopts "C:c:d:f:i:o:s:S:" arg; do case "${arg}" in C) VMBUILDCONF="${OPTARG}" ;; c) VMCONFIG="${OPTARG}" ;; d) DESTDIR="${OPTARG}" ;; f) VMFORMAT="${OPTARG}" ;; i) VMBASE="${OPTARG}" ;; o) VMIMAGE="${OPTARG}" ;; s) VMSIZE="${OPTARG}" ;; S) WORLDDIR="${OPTARG}" ;; *) ;; esac done shift $(( ${OPTIND} - 1)) if [ -z "${VMBASE}" -o \ -z "${WORLDDIR}" -o \ -z "${DESTDIR}" -o \ -z "${VMSIZE}" -o \ -z "${VMIMAGE}" ]; then usage || exit 0 fi if [ -z "${VMBUILDCONF}" ] || [ ! -e "${VMBUILDCONF}" ]; then echo "Must provide the path to vmimage.subr." return 1 fi . "${VMBUILDCONF}" if [ ! -z "${VMCONFIG}" ] && [ ! -c "${VMCONFIG}" ]; then . "${VMCONFIG}" fi vm_create_base vm_install_base vm_extra_install_base vm_extra_install_packages vm_extra_install_ports vm_extra_enable_services vm_extra_pre_umount vm_extra_pkg_rmcache cleanup + vm_copy_base vm_create_disk || return 0 vm_extra_create_disk return 0 } main "$@" Index: head/release/tools/vmimage.subr =================================================================== --- head/release/tools/vmimage.subr (revision 280298) +++ head/release/tools/vmimage.subr (revision 280299) @@ -1,185 +1,214 @@ #!/bin/sh # # $FreeBSD$ # # # Common functions for virtual machine image build scripts. # export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" trap "cleanup" INT QUIT TRAP ABRT TERM write_partition_layout() { if [ -z "${NOSWAP}" ]; then SWAPOPT="-p freebsd-swap/swapfs::1G" fi case "${TARGET}:${TARGET_ARCH}" in amd64:amd64 | i386:i386) mkimg -s gpt -b /boot/pmbr \ -p freebsd-boot/bootfs:=/boot/gptboot \ ${SWAPOPT} \ -p freebsd-ufs/rootfs:=${VMBASE} \ -o ${VMIMAGE} ;; powerpc:powerpc*) mkimg -s apm \ -p apple-boot/bootfs:=/boot/boot1.hfs \ ${SWAPOPT} \ -p freebsd-ufs/rootfs:=${VMBASE} \ -o ${VMIMAGE} ;; *) # ENOTSUPP return 1 ;; esac return 0 } err() { printf "${@}\n" cleanup return 1 } cleanup() { umount ${DESTDIR}/dev 2>/dev/null umount ${DESTDIR} if [ ! -z "${mddev}" ]; then mdconfig -d -u ${mddev} fi return 0 } vm_create_base() { # Creates the UFS root filesystem for the virtual machine disk, # written to the formatted disk image with mkimg(1). mkdir -p ${DESTDIR} truncate -s ${VMSIZE} ${VMBASE} mddev=$(mdconfig -f ${VMBASE}) newfs -j /dev/${mddev} mount /dev/${mddev} ${DESTDIR} return 0 } +vm_copy_base() { + # Creates a new UFS root filesystem and copies the contents of the + # current root filesystem into it. This produces a "clean" disk + # image without any remnants of files which were created temporarily + # during image-creation and have since been deleted (e.g., downloaded + # package archives). + + mkdir -p ${DESTDIR}/old + mdold=$(mdconfig -f ${VMBASE}) + mount /dev/${mdold} ${DESTDIR}/old + + truncate -s ${VMSIZE} ${VMBASE}.tmp + mkdir -p ${DESTDIR}/new + mdnew=$(mdconfig -f ${VMBASE}.tmp) + newfs -j /dev/${mdnew} + mount /dev/${mdnew} ${DESTDIR}/new + + tar -cf- -C ${DESTDIR}/old . | tar -xf- -C ${DESTDIR}/new + + umount /dev/${mdold} + rmdir ${DESTDIR}/old + mdconfig -d -u ${mdold} + + umount /dev/${mdnew} + rmdir ${DESTDIR}/new + mdconfig -d -u ${mdnew} + mv ${VMBASE}.tmp ${VMBASE} +} + vm_install_base() { # Installs the FreeBSD userland/kernel to the virtual machine disk. cd ${WORLDDIR} && \ make DESTDIR=${DESTDIR} \ installworld installkernel distribution || \ err "\n\nCannot install the base system to ${DESTDIR}." echo '# Custom /etc/fstab for FreeBSD VM images' \ > ${DESTDIR}/etc/fstab echo '/dev/gpt/rootfs / ufs rw 1 1' \ >> ${DESTDIR}/etc/fstab if [ -z "${NOSWAP}" ]; then echo '/dev/gpt/swapfs none swap sw 0 0' \ >> ${DESTDIR}/etc/fstab fi mkdir -p ${DESTDIR}/dev mount -t devfs devfs ${DESTDIR}/dev chroot ${DESTDIR} /usr/bin/newaliases chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart umount ${DESTDIR}/dev cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf return 0 } vm_extra_install_base() { # Prototype. When overridden, runs extra post-installworld commands # as needed, based on the target virtual machine image or cloud # provider image target. return 0 } vm_extra_enable_services() { if [ ! -z "${VM_RC_LIST}" ]; then for _rcvar in ${VM_RC_LIST}; do echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf done fi return 0 } vm_extra_install_packages() { if [ -z "${VM_EXTRA_PACKAGES}" ]; then return 0 fi mkdir -p ${DESTDIR}/dev mount -t devfs devfs ${DESTDIR}/dev chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ /usr/sbin/pkg bootstrap -y chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES} umount ${DESTDIR}/dev return 0 } vm_extra_install_ports() { # Prototype. When overridden, installs additional ports within the # virtual machine environment. return 0 } vm_extra_pre_umount() { # Prototype. When overridden, installs additional ports within the # virtual machine environment. rm -f ${DESTDIR}/etc/resolv.conf return 0 } vm_extra_pkg_rmcache() { if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \ /usr/local/sbin/pkg clean -y -a fi return 0 } vm_umount_base() { i=0 sync while ! umount ${DESTDIR}/dev ${DESTDIR}; do i=$(( $i + 1 )) if [ $i -ge 10 ]; then # This should never happen. But, it has happened. msg="Cannot umount(8) ${DESTDIR}\n" msg="${msg}Something has gone horribly wrong." err "${msg}" fi sleep 1 done return 0 } vm_create_disk() { echo "Creating image... Please wait." echo write_partition_layout || return 1 return 0 } vm_extra_create_disk() { return 0 }