mk-vmimage.sh (f6490fe596e86bc170f7617bfe12e99e8009f328) | mk-vmimage.sh (1e7c1f17428a0b342afd622fad341a389bed7a3f) |
---|---|
1#!/bin/sh 2#- 3# Copyright (c) 2014 The FreeBSD Foundation 4# All rights reserved. 5# 6# This software was developed by Glen Barber under sponsorship 7# from the FreeBSD Foundation. 8# --- 18 unchanged lines hidden (view full) --- 27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28# SUCH DAMAGE. 29# 30# mk-vmimage.sh: Create virtual machine disk images in various formats. 31# 32# $FreeBSD$ 33# 34 | 1#!/bin/sh 2#- 3# Copyright (c) 2014 The FreeBSD Foundation 4# All rights reserved. 5# 6# This software was developed by Glen Barber under sponsorship 7# from the FreeBSD Foundation. 8# --- 18 unchanged lines hidden (view full) --- 27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28# SUCH DAMAGE. 29# 30# mk-vmimage.sh: Create virtual machine disk images in various formats. 31# 32# $FreeBSD$ 33# 34 |
35PATH="/bin:/usr/bin:/sbin:/usr/sbin" 36export PATH | 35main() { 36 local arg 37 while getopts "C:c:d:f:i:o:s:S:" arg; do 38 case "${arg}" in 39 C) 40 VMBUILDCONF="${OPTARG}" 41 ;; 42 c) 43 VMCONFIG="${OPTARG}" 44 ;; 45 d) 46 DESTDIR="${OPTARG}" 47 ;; 48 f) 49 VMFORMAT="${OPTARG}" 50 ;; 51 i) 52 VMBASE="${VMBASE}" 53 ;; 54 o) 55 VMIMAGE="${OPTARG}" 56 ;; 57 s) 58 VMSIZE="${OPTARG}" 59 ;; 60 S) 61 WORLDDIR="${OPTARG}" 62 ;; 63 *) 64 ;; 65 esac 66 done 67 shift $(( ${OPTIND} - 1)) |
37 | 68 |
38usage() { 39 echo "$(basename ${0}) <command> <target> [...]" 40 if [ -z "${MAKEFLAGS}" ]; then 41 echo "It is probably not safe to run this by hand yet..." | 69 if [ -z "${VMBASE}" -o \ 70 -z "${WORLDDIR}" -o \ 71 -z "${DESTDIR}" -o \ 72 -z "${VMSIZE}" -o \ 73 -z "${VMIMAGE}" -o \ 74 -z "${VMCONFIG}" ]; 75 then 76 usage |
42 fi | 77 fi |
43 exit 1 44} | |
45 | 78 |
46panic() { 47 rc="${1}" 48 shift 1 49 msg="${@}" 50 printf "${msg}\n" 51 if [ ! -z "${mddev}" ]; then 52 mdconfig -d -u ${mddev} | 79 if [ -z "${VMBUILDCONF}" ] || [ ! -e "${VMBUILDCONF}" ]; then 80 echo "Must provide the path to vmimage.subr." 81 return 1 |
53 fi | 82 fi |
54 # Do not allow one failure case to chain through any remaining image 55 # builds. 56 exit 0 57} | |
58 | 83 |
59vm_create_baseimage() { 60 # Creates the UFS root filesystem for the virtual machine disk, 61 # written to the formatted disk image with mkimg(1). 62 i=0 63 mkdir -p ${DESTDIR} 64 truncate -s ${VMSIZE} ${VMBASE} 65 mddev=$(mdconfig -f ${VMBASE}) 66 newfs -j /dev/${mddev} 67 mount /dev/${mddev} ${DESTDIR} 68 cd ${WORLDDIR} && \ 69 ${IMAKE} DESTDIR=${DESTDIR} \ 70 installworld installkernel distribution || \ 71 panic 1 "\n\nCannot install the base system to ${DESTDIR}." 72 chroot ${DESTDIR} /usr/bin/newaliases 73 echo '# Custom /etc/fstab for FreeBSD VM images' \ 74 > ${DESTDIR}/etc/fstab 75 echo '/dev/gpt/rootfs / ufs rw 2 2' \ 76 >> ${DESTDIR}/etc/fstab 77 echo '/dev/gpt/swapfs none swap sw 0 0' \ 78 >> ${DESTDIR}/etc/fstab 79 sync 80 while ! umount ${DESTDIR}; do 81 i=$(( $i + 1 )) 82 if [ $i -ge 10 ]; then 83 # This should never happen. But, it has happened. 84 msg="Cannot umount(8) ${DESTDIR}\n" 85 msg="${msg}Something has gone horribly wrong." 86 panic 1 "${msg}" 87 fi 88 sleep 1 89 done | 84 . "${VMBUILDCONF}" |
90 | 85 |
91 return 0 92} 93 94vm_create_vmdisk() { 95 mkimg_version=$(mkimg --version 2>/dev/null | awk '{print $2}') 96 97 # We need mkimg(1) '--version' output, at minimum, to be able to 98 # tell what virtual machine disk image formats are available. 99 # Bail if mkimg(1) reports an empty '--version' value. 100 if [ -z "${mkimg_version}" ]; then 101 msg="Cannot determine mkimg(1) version.\n" 102 msg="${msg}Cannot continue without a known mkimg(1) version." 103 panic 0 "${msg}" | 86 if [ ! -z "${VMCONFIG}" ] && [ -e "${VMCONFIG}" ]; then 87 . "${VMCONFIG}" |
104 fi 105 | 88 fi 89 |
106 if ! mkimg --formats 2>/dev/null | grep -q ${FORMAT}; then 107 panic 0 "'${FORMAT}' is not supported by this mkimg(1).\n" 108 fi | 90 vm_install_base 91 vm_extra_install_base 92 vm_extra_install_packages 93 vm_extra_install_ports 94 vm_extra_enable_services 95 vm_extra_pre_umount 96 vm_create_disk 97 vm_extra_create_disk |
109 | 98 |
110 case ${FORMAT} in 111 vhd) 112 mkimg_format=vhdf 113 ;; 114 *) 115 mkimg_format=${FORMAT} 116 ;; 117 esac 118 119 mkimg -f ${mkimg_format} -s gpt \ 120 -b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \ 121 -p freebsd-swap/swapfs::1G \ 122 -p freebsd-ufs/rootfs:=${VMBASE} \ 123 -o ${VMIMAGE} 124 | |
125 return 0 126} 127 | 99 return 0 100} 101 |
128main() { 129 cmd="${1}" 130 131 case ${TARGET}/${TARGET_ARCH} in 132 amd64/amd64|i386/i386) 133 # FALLTHROUGH 134 ;; 135 *) 136 # EX_CANTCREAT 137 return 0 138 ;; 139 esac 140 141 case ${cmd} in 142 vm-base) 143 eval vm_create_baseimage "$@" || return 0 144 ;; 145 vm-image) 146 eval vm_create_vmdisk "$@" || return 0 147 ;; 148 *|\?) 149 usage 150 ;; 151 esac 152 153 return 0 154} 155 | |
156main "$@" | 102main "$@" |