From 0bc9be5e7435fb486faadedb649a2e7913efcfc5 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Wed, 1 Oct 2014 19:13:42 +0000 Subject: [PATCH 01/13] Avoid using env(1) to set values passed to mk-vmimage.sh, and instead pass the values as arguments to the script, making it easier to run this by hand, without 'make release'. Add usage_vm_base() and usage_vm_image() usage helpers. Sponsored by: The FreeBSD Foundation --- release/Makefile | 14 +++++----- release/amd64/mk-vmimage.sh | 53 ++++++++++++++++++++++++++++++++----- release/i386/mk-vmimage.sh | 53 ++++++++++++++++++++++++++++++++----- 3 files changed, 100 insertions(+), 20 deletions(-) diff --git a/release/Makefile b/release/Makefile index 6080898330b4..3379b81de584 100644 --- a/release/Makefile +++ b/release/Makefile @@ -317,20 +317,18 @@ install: vm-base: .if defined(WITH_VMIMAGES) && !empty(WITH_VMIMAGES) - env IMAKE=${IMAKE} WORLDDIR=${WORLDDIR} \ - VMBASE=${VMBASE}.img VMSIZE=${VMSIZE} \ - DESTDIR=${.OBJDIR}/${.TARGET} \ - TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ - ${.CURDIR}/${TARGET}/mk-vmimage.sh ${.TARGET} + env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ + ${.CURDIR}/${TARGET}/mk-vmimage.sh ${.TARGET} \ + ${VMBASE}.img ${WORLDDIR} ${.OBJDIR}/${.TARGET} ${VMSIZE} .endif touch ${.TARGET} vm-image: vm-base .if defined(WITH_VMIMAGES) && !empty(WITH_VMIMAGES) . for FORMAT in ${VMFORMATS} - env FORMAT=${FORMAT} VMIMAGE=${VMBASE}.${FORMAT} VMBASE=${VMBASE}.img \ - TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ - ${.CURDIR}/${TARGET}/mk-vmimage.sh ${.TARGET} + env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ + ${.CURDIR}/${TARGET}/mk-vmimage.sh ${.TARGET} \ + ${VMBASE}.img ${FORMAT} ${VMBASE}.${FORMAT} . endfor .endif touch ${.TARGET} diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index b717efd6390e..6ee148248cd5 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -35,8 +35,25 @@ PATH="/bin:/usr/bin:/sbin:/usr/sbin" export PATH +usage_vm_base() { + echo -n "$(basename ${0}) vm-base " + echo " " + return 0 +} + +usage_vm_image() { + echo -n "$(basename ${0}) vm-image " + echo " " + return 0 +} + usage() { - echo "$(basename ${0}) [...]" + echo "Usage:" + echo "$(basename ${0}) [vm-base|vm-image] [...]" + echo + usage_vm_base + echo + usage_vm_image exit 1 } @@ -56,6 +73,20 @@ panic() { vm_create_baseimage() { # Creates the UFS root filesystem for the virtual machine disk, # written to the formatted disk image with mkimg(1). + # + # Arguments: + # vm-base + + VMBASE="${1}" + WORLDDIR="${2}" + DESTDIR="${3}" + VMSIZE="${4}" + + if [ -z "${VMBASE}" -o -z "${WORLDDIR}" -o -z "${DESTDIR}" \ + -o -z "${VMSIZE}" ]; then + usage + fi + i=0 mkdir -p ${DESTDIR} truncate -s ${VMSIZE} ${VMBASE} @@ -63,7 +94,7 @@ vm_create_baseimage() { newfs -j /dev/${mddev} mount /dev/${mddev} ${DESTDIR} cd ${WORLDDIR} && \ - ${IMAKE} DESTDIR=${DESTDIR} \ + make DESTDIR=${DESTDIR} \ installworld installkernel distribution || \ panic 1 "\n\nCannot install the base system to ${DESTDIR}." chroot ${DESTDIR} /usr/bin/newaliases @@ -89,6 +120,19 @@ vm_create_baseimage() { } vm_create_vmdisk() { + # Creates the virtual machine disk image from the raw disk image. + # + # Arguments: + # vm-image " + + VMBASE="${1}" + FORMAT="${2}" + VMIMAGE="${3}" + + if [ -z "${VMBASE}" -o -z "${FORMAT}" -o -z "${VMIMAGE}" ]; then + usage + fi + mkimg_version=$(mkimg --version 2>/dev/null | awk '{print $2}') # We need mkimg(1) '--version' output, at minimum, to be able to @@ -124,10 +168,7 @@ vm_create_vmdisk() { main() { cmd="${1}" - - if [ -z "${MAKEFLAGS}" ]; then - echo "It is probably not safe to run this by hand yet..." - fi + shift 1 case ${cmd} in vm-base) diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index b717efd6390e..6ee148248cd5 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -35,8 +35,25 @@ PATH="/bin:/usr/bin:/sbin:/usr/sbin" export PATH +usage_vm_base() { + echo -n "$(basename ${0}) vm-base " + echo " " + return 0 +} + +usage_vm_image() { + echo -n "$(basename ${0}) vm-image " + echo " " + return 0 +} + usage() { - echo "$(basename ${0}) [...]" + echo "Usage:" + echo "$(basename ${0}) [vm-base|vm-image] [...]" + echo + usage_vm_base + echo + usage_vm_image exit 1 } @@ -56,6 +73,20 @@ panic() { vm_create_baseimage() { # Creates the UFS root filesystem for the virtual machine disk, # written to the formatted disk image with mkimg(1). + # + # Arguments: + # vm-base + + VMBASE="${1}" + WORLDDIR="${2}" + DESTDIR="${3}" + VMSIZE="${4}" + + if [ -z "${VMBASE}" -o -z "${WORLDDIR}" -o -z "${DESTDIR}" \ + -o -z "${VMSIZE}" ]; then + usage + fi + i=0 mkdir -p ${DESTDIR} truncate -s ${VMSIZE} ${VMBASE} @@ -63,7 +94,7 @@ vm_create_baseimage() { newfs -j /dev/${mddev} mount /dev/${mddev} ${DESTDIR} cd ${WORLDDIR} && \ - ${IMAKE} DESTDIR=${DESTDIR} \ + make DESTDIR=${DESTDIR} \ installworld installkernel distribution || \ panic 1 "\n\nCannot install the base system to ${DESTDIR}." chroot ${DESTDIR} /usr/bin/newaliases @@ -89,6 +120,19 @@ vm_create_baseimage() { } vm_create_vmdisk() { + # Creates the virtual machine disk image from the raw disk image. + # + # Arguments: + # vm-image " + + VMBASE="${1}" + FORMAT="${2}" + VMIMAGE="${3}" + + if [ -z "${VMBASE}" -o -z "${FORMAT}" -o -z "${VMIMAGE}" ]; then + usage + fi + mkimg_version=$(mkimg --version 2>/dev/null | awk '{print $2}') # We need mkimg(1) '--version' output, at minimum, to be able to @@ -124,10 +168,7 @@ vm_create_vmdisk() { main() { cmd="${1}" - - if [ -z "${MAKEFLAGS}" ]; then - echo "It is probably not safe to run this by hand yet..." - fi + shift 1 case ${cmd} in vm-base) From a2950cc4e3e6730f1d961fe8c56d225034cf5be8 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Wed, 1 Oct 2014 19:25:02 +0000 Subject: [PATCH 02/13] After evaluating WITH_VMIMAGES is non-empty, ensure the mk-vmimage.sh script exists before running it. Sponsored by: The FreeBSD Foundation --- release/Makefile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/release/Makefile b/release/Makefile index 3379b81de584..7d87013e2d5a 100644 --- a/release/Makefile +++ b/release/Makefile @@ -317,18 +317,22 @@ install: vm-base: .if defined(WITH_VMIMAGES) && !empty(WITH_VMIMAGES) +. if exists(${.CURDIR}/${TARGET}/mk-vmimage.sh) env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ ${.CURDIR}/${TARGET}/mk-vmimage.sh ${.TARGET} \ ${VMBASE}.img ${WORLDDIR} ${.OBJDIR}/${.TARGET} ${VMSIZE} +. endif .endif touch ${.TARGET} vm-image: vm-base .if defined(WITH_VMIMAGES) && !empty(WITH_VMIMAGES) -. for FORMAT in ${VMFORMATS} +. if exists(${.CURDIR}/${TARGET}/mk-vmimage.sh) +. for FORMAT in ${VMFORMATS} env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \ ${.CURDIR}/${TARGET}/mk-vmimage.sh ${.TARGET} \ ${VMBASE}.img ${FORMAT} ${VMBASE}.${FORMAT} -. endfor +. endfor +. endif .endif touch ${.TARGET} From a9278c16be29e08dcd2e39ec72c3f686d1a0f37a Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 2 Oct 2014 00:34:03 +0000 Subject: [PATCH 03/13] Add WITH_COMPRESSED_VMIMAGES variable, which when set enables xz(1) compression of the virtual machine images. This is intentionally separate to allow more fine-grained tuning over which images are compressed, especially in cases where compressing 20GB sparse images can take hours. Sponsored by: The FreeBSD Foundation --- release/Makefile | 4 +++- release/release.conf.sample | 4 ++++ release/release.sh | 8 +++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/release/Makefile b/release/Makefile index 7d87013e2d5a..905063bd3ac5 100644 --- a/release/Makefile +++ b/release/Makefile @@ -24,6 +24,8 @@ # WITH_COMPRESSED_IMAGES: if set, compress installation images with xz(1) # (uncompressed images are not removed) # WITH_VMIMAGES: if set, build virtual machine images with the release +# WITH_COMPRESSED_VMIMAGES: if set, compress virtual machine disk images +# with xz(1) (extremely time consuming) # TARGET/TARGET_ARCH: architecture of built release # @@ -301,7 +303,7 @@ install: cp -p ${VMBASE}.${FORMAT} \ ${DESTDIR}/vmimages/${OSRELEASE}.${FORMAT} . endfor -. if defined(WITH_COMPRESSED_IMAGES) && !empty(WITH_COMPRESSED_IMAGES) +. if defined(WITH_COMPRESSED_VMIMAGES) && !empty(WITH_COMPRESSED_VMIMAGES) # This is very time consuming, so defer it after the images are moved to # the DESTDIR. . for FORMAT in ${VMFORMATS} diff --git a/release/release.conf.sample b/release/release.conf.sample index 9acc0e2d820f..c79ed9f87003 100644 --- a/release/release.conf.sample +++ b/release/release.conf.sample @@ -81,6 +81,10 @@ PORTBRANCH="ports/head@rHEAD" ## release build. #WITH_VMIMAGES= +## Set to a non-empty value to compress virtual machine images with xz(1) +## as part of the release build. +#WITH_COMPRESSED_VMIMAGES= + ## If WITH_VMIMAGES is set to a non-empty value, this is the name of the ## file to use for the installed userland/kernel. #VMBASE="vm" diff --git a/release/release.sh b/release/release.sh index e7f0c39f158c..87b36974070a 100755 --- a/release/release.sh +++ b/release/release.sh @@ -89,6 +89,11 @@ NOPORTS= WITH_DVD= WITH_COMPRESSED_IMAGES= +# Set to non-empty value to build virtual machine images as part of +# the release. +WITH_VMIMAGES= +WITH_COMPRESSED_VMIMAGES= + usage() { echo "Usage: $0 [-c release.conf]" exit 1 @@ -274,4 +279,5 @@ eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel eval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \ release eval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \ - install DESTDIR=/R WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} + install DESTDIR=/R WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \ + WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES} From a8c229a55f98f006dc2bd920d01ceabe16dab741 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 2 Oct 2014 16:00:50 +0000 Subject: [PATCH 04/13] Document the new 'vm-image' target, and associated release.conf variables. Sponsored by: The FreeBSD Foundation --- share/man/man7/release.7 | 65 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/share/man/man7/release.7 b/share/man/man7/release.7 index 8fc75f3da4eb..93aec89618a9 100644 --- a/share/man/man7/release.7 +++ b/share/man/man7/release.7 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd April 11, 2014 +.Dd October 2, 2014 .Dt RELEASE 7 .Os .Sh NAME @@ -351,6 +351,61 @@ Set to the target directory within to check out .Va ${UBOOTSRC}/${UBOOTBRANCH} . .El +.Sh VIRTUAL MACHINE DISK IMAGES +The following +.Fa release.conf +variables are relevant only to virtual machine disk image builds: +.Bl -tag -width Ev +.It Va WITH_VMIMAGES +Set to a non-null value to build virtual machine disk images as part +of the release build. +.Va WITH_VMIMAGES +may also be specified as an envirionment variable passed to +.Xr make 1 . +.Pp +The option requires +.Xr mkimg 1 +version 20140927 or later. +.It Va WITH_COMPRESSED_VMIMAGES +Set to a non-null value to compress the virtual machine disk images with +.Xr xz 1 +as part of the +.Cm install +.Xr make 1 +target. +Note that compressing virtual machine disk images may take a very long +time on some systems. +.It Va VMBASE +Set to change the name of the resulting virtual machine disk image file. +The default value is +.Va vm . +.It Va VMSIZE +Set to change the size of the virtual machine disk capacity. +The default value is +.Va 20G . +See +.Xr truncate 1 +for valid values. +.Pp +Virtual machine disk images are, by default, created as sparse images. +When +.Va WITH_COMPRESSED_VMIMAGES +is used, the resulting files compressed with +.Xr xz 1 +compress to roughly the same size, regardless of the specified disk image +size. +.It Va VMFORMATS +Set to the target virtual disk image format(s) to create. +By default, the +.Va vhdf , Va vmdk , Va qcow2 , +and +.Va raw +formats are created. +See +.Xr mkimg 1 +for valid format values +.Pq requires version 20140927 or later . +.El .Sh MAKEFILE TARGETS The release makefile .Pq Pa src/release/Makefile @@ -407,6 +462,14 @@ Creates a directory named .Pa ftp containing the distribution files used in network installations and suitable for upload to an FTP mirror. +.It Cm vm-image +Creates virtual machine disk images in various formats. +The +.Cm vm-image +target requires the +.Va WITH_VMIMAGES +.Xr make 1 +envirionment variable to be set to a non-null value. .El .Pp Major subtargets called by targets above: From cadb91b0666567a9c31a76883d09c788ad6c21e6 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 2 Oct 2014 16:05:01 +0000 Subject: [PATCH 05/13] Remove two stray comments added during the initial iterations of testing, no longer needed. Sponsored by: The FreeBSD Foundation --- release/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/release/Makefile b/release/Makefile index 905063bd3ac5..8a6ece7d1b58 100644 --- a/release/Makefile +++ b/release/Makefile @@ -99,8 +99,8 @@ IMAGES+= mini-memstick.img VMTARGETS= vm-base vm-image VMFORMATS?= vhd vmdk qcow2 raw -VMSIZE?= 20G # size in Gb -VMBASE?= vm # name of the filesystem image +VMSIZE?= 20G +VMBASE?= vm CLEANFILES= packagesystem *.txz MANIFEST system ${IMAGES} .if defined(WITH_COMPRESSED_IMAGES) && !empty(WITH_COMPRESSED_IMAGES) From d04dbaf8281bc192c27ca5b4f8a4c92099f337f4 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 2 Oct 2014 18:20:01 +0000 Subject: [PATCH 06/13] Remove the first argument to panic(), which was initially intended to be the exit code, however when a non-zero exit code was returned to release/Makefile, this would prevent any remaining (and possibly successful) stages from being attempted. Sponsored by: The FreeBSD Foundation --- release/amd64/mk-vmimage.sh | 12 +++++------- release/i386/mk-vmimage.sh | 12 +++++------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index 6ee148248cd5..30163e7e3895 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -58,8 +58,6 @@ usage() { } panic() { - rc="${1}" - shift 1 msg="${@}" printf "${msg}\n" if [ ! -z "${mddev}" ]; then @@ -67,7 +65,7 @@ panic() { fi # Do not allow one failure case to chain through any remaining image # builds. - exit 0 + return 1 } vm_create_baseimage() { @@ -96,7 +94,7 @@ vm_create_baseimage() { cd ${WORLDDIR} && \ make DESTDIR=${DESTDIR} \ installworld installkernel distribution || \ - panic 1 "\n\nCannot install the base system to ${DESTDIR}." + panic "\n\nCannot install the base system to ${DESTDIR}." chroot ${DESTDIR} /usr/bin/newaliases echo '# Custom /etc/fstab for FreeBSD VM images' \ > ${DESTDIR}/etc/fstab @@ -111,7 +109,7 @@ vm_create_baseimage() { # This should never happen. But, it has happened. msg="Cannot umount(8) ${DESTDIR}\n" msg="${msg}Something has gone horribly wrong." - panic 1 "${msg}" + panic "${msg}" fi sleep 1 done @@ -141,11 +139,11 @@ vm_create_vmdisk() { if [ -z "${mkimg_version}" ]; then msg="Cannot determine mkimg(1) version.\n" msg="${msg}Cannot continue without a known mkimg(1) version." - panic 0 "${msg}" + panic "${msg}" fi if ! mkimg --formats 2>/dev/null | grep -q ${FORMAT}; then - panic 0 "'${FORMAT}' is not supported by this mkimg(1).\n" + panic "'${FORMAT}' is not supported by this mkimg(1).\n" fi case ${FORMAT} in diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index 6ee148248cd5..30163e7e3895 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -58,8 +58,6 @@ usage() { } panic() { - rc="${1}" - shift 1 msg="${@}" printf "${msg}\n" if [ ! -z "${mddev}" ]; then @@ -67,7 +65,7 @@ panic() { fi # Do not allow one failure case to chain through any remaining image # builds. - exit 0 + return 1 } vm_create_baseimage() { @@ -96,7 +94,7 @@ vm_create_baseimage() { cd ${WORLDDIR} && \ make DESTDIR=${DESTDIR} \ installworld installkernel distribution || \ - panic 1 "\n\nCannot install the base system to ${DESTDIR}." + panic "\n\nCannot install the base system to ${DESTDIR}." chroot ${DESTDIR} /usr/bin/newaliases echo '# Custom /etc/fstab for FreeBSD VM images' \ > ${DESTDIR}/etc/fstab @@ -111,7 +109,7 @@ vm_create_baseimage() { # This should never happen. But, it has happened. msg="Cannot umount(8) ${DESTDIR}\n" msg="${msg}Something has gone horribly wrong." - panic 1 "${msg}" + panic "${msg}" fi sleep 1 done @@ -141,11 +139,11 @@ vm_create_vmdisk() { if [ -z "${mkimg_version}" ]; then msg="Cannot determine mkimg(1) version.\n" msg="${msg}Cannot continue without a known mkimg(1) version." - panic 0 "${msg}" + panic "${msg}" fi if ! mkimg --formats 2>/dev/null | grep -q ${FORMAT}; then - panic 0 "'${FORMAT}' is not supported by this mkimg(1).\n" + panic "'${FORMAT}' is not supported by this mkimg(1).\n" fi case ${FORMAT} in From 47480336deb61b71d61682e993fa019a662e25a6 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 2 Oct 2014 18:23:53 +0000 Subject: [PATCH 07/13] If the vm-base target fails, prevent the vm-image target from being run since it cannot possibly succeed. Sponsored by: The FreeBSD Foundation --- release/amd64/mk-vmimage.sh | 11 +++++++++++ release/i386/mk-vmimage.sh | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index 30163e7e3895..e0bc976b2a2d 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -63,6 +63,17 @@ panic() { if [ ! -z "${mddev}" ]; then mdconfig -d -u ${mddev} fi + case ${cmd} in + vm-base) + # If the vm-base target fails, the vm-image target + # cannot possibly succeed. Touch the .TARGET file + # so it is not attempted. + touch vm-image + ;; + *) + # FALLTHROUGH + ;; + esac # Do not allow one failure case to chain through any remaining image # builds. return 1 diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index 30163e7e3895..e0bc976b2a2d 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -63,6 +63,17 @@ panic() { if [ ! -z "${mddev}" ]; then mdconfig -d -u ${mddev} fi + case ${cmd} in + vm-base) + # If the vm-base target fails, the vm-image target + # cannot possibly succeed. Touch the .TARGET file + # so it is not attempted. + touch vm-image + ;; + *) + # FALLTHROUGH + ;; + esac # Do not allow one failure case to chain through any remaining image # builds. return 1 From 655f2eb77006f34364c87647c1be210dc051f926 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 9 Oct 2014 03:20:28 +0000 Subject: [PATCH 08/13] Add /usr/local/bin and /usr/local/sbin to PATH, needed if third-party software needs to use utilities outside of the base system during post-install stages (indexinfo is one culprit). Sponsored by: The FreeBSD Foundation --- release/amd64/mk-vmimage.sh | 2 +- release/i386/mk-vmimage.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index e0bc976b2a2d..b3ffd232f848 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -32,7 +32,7 @@ # $FreeBSD$ # -PATH="/bin:/usr/bin:/sbin:/usr/sbin" +PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" export PATH usage_vm_base() { diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index e0bc976b2a2d..b3ffd232f848 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -32,7 +32,7 @@ # $FreeBSD$ # -PATH="/bin:/usr/bin:/sbin:/usr/sbin" +PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" export PATH usage_vm_base() { From ed79fb4ea9f41c41da98ff469f5aac5dfa435320 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 9 Oct 2014 03:29:52 +0000 Subject: [PATCH 09/13] Add prototypes for build-specific tuning: - vm_prebuild_setup(): Steps to run prior to creating the file-backed disk image. - vm_setup(): Steps to run while the file-backed disk image is mounted. - vm_postbuild_setup(): Steps to run after the file-backed disk image is dismounted. The intention is to override as necessary via build configuration files, as needed. Sponsored by: The FreeBSD Foundation --- release/amd64/mk-vmimage.sh | 12 ++++++++++++ release/i386/mk-vmimage.sh | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index b3ffd232f848..657fa52d37fa 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -35,6 +35,18 @@ PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" export PATH +vm_prebuild_setup() { + return 0 +} + +vm_setup() { + return 0 +} + +vm_postbuild_setup() { + return 0 +} + usage_vm_base() { echo -n "$(basename ${0}) vm-base " echo " " diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index b3ffd232f848..657fa52d37fa 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -35,6 +35,18 @@ PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" export PATH +vm_prebuild_setup() { + return 0 +} + +vm_setup() { + return 0 +} + +vm_postbuild_setup() { + return 0 +} + usage_vm_base() { echo -n "$(basename ${0}) vm-base " echo " " From 4ac2ee7f8cd558a55cb042114d5a0e168ac86d23 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 9 Oct 2014 03:50:38 +0000 Subject: [PATCH 10/13] Allow mk-vmimage.sh to accept a configuration file as an argument to '-c'. Sponsored by: The FreeBSD Foundation --- release/amd64/mk-vmimage.sh | 19 +++++++++++++++++++ release/i386/mk-vmimage.sh | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index 657fa52d37fa..05c103a1f4a7 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -188,9 +188,28 @@ vm_create_vmdisk() { } main() { + vm_config= + while getopts "c:" arg; do + case ${arg} in + c) + vm_config="${OPTARG}" + ;; + *) + ;; + esac + done + shift $(( ${OPTIND} - 1 )) + cmd="${1}" shift 1 + if [ ! -z "${vm_config}" ]; then + if [ ! -e "${vm_config}" ]; then + panic "Configuration file ${vm_config} not found." + fi + . ${vm_config} + fi + case ${cmd} in vm-base) eval vm_create_baseimage "$@" || return 0 diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index 657fa52d37fa..05c103a1f4a7 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -188,9 +188,28 @@ vm_create_vmdisk() { } main() { + vm_config= + while getopts "c:" arg; do + case ${arg} in + c) + vm_config="${OPTARG}" + ;; + *) + ;; + esac + done + shift $(( ${OPTIND} - 1 )) + cmd="${1}" shift 1 + if [ ! -z "${vm_config}" ]; then + if [ ! -e "${vm_config}" ]; then + panic "Configuration file ${vm_config} not found." + fi + . ${vm_config} + fi + case ${cmd} in vm-base) eval vm_create_baseimage "$@" || return 0 From 648326ad2d336d041c6d8163cb095c7b0a7efb20 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Thu, 9 Oct 2014 04:10:58 +0000 Subject: [PATCH 11/13] Reference vm_prebuild_setup(), vm_setup(), and vm_postbuild_setup() functions. Sponsored by: The FreeBSD Foundation --- release/amd64/mk-vmimage.sh | 9 +++++++++ release/i386/mk-vmimage.sh | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index 05c103a1f4a7..de233163d6de 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -92,6 +92,9 @@ panic() { } vm_create_baseimage() { + # Run anything that is needed before the virtual machine disk image + # is created. + vm_prebuild_setup # Creates the UFS root filesystem for the virtual machine disk, # written to the formatted disk image with mkimg(1). # @@ -125,6 +128,9 @@ vm_create_baseimage() { >> ${DESTDIR}/etc/fstab echo '/dev/gpt/swapfs none swap sw 0 0' \ >> ${DESTDIR}/etc/fstab + # Run anything that is needed while the virtual machine disk image + # userland filesystem is still mounted as a md(4) device. + vm_setup sync while ! umount ${DESTDIR}; do i=$(( $i + 1 )) @@ -184,6 +190,9 @@ vm_create_vmdisk() { -p freebsd-ufs/rootfs:=${VMBASE} \ -o ${VMIMAGE} + # Run anything that is needed for the virtual machine disk image + # after it has been created. + vm_postbuild_setup return 0 } diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index 05c103a1f4a7..de233163d6de 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -92,6 +92,9 @@ panic() { } vm_create_baseimage() { + # Run anything that is needed before the virtual machine disk image + # is created. + vm_prebuild_setup # Creates the UFS root filesystem for the virtual machine disk, # written to the formatted disk image with mkimg(1). # @@ -125,6 +128,9 @@ vm_create_baseimage() { >> ${DESTDIR}/etc/fstab echo '/dev/gpt/swapfs none swap sw 0 0' \ >> ${DESTDIR}/etc/fstab + # Run anything that is needed while the virtual machine disk image + # userland filesystem is still mounted as a md(4) device. + vm_setup sync while ! umount ${DESTDIR}; do i=$(( $i + 1 )) @@ -184,6 +190,9 @@ vm_create_vmdisk() { -p freebsd-ufs/rootfs:=${VMBASE} \ -o ${VMIMAGE} + # Run anything that is needed for the virtual machine disk image + # after it has been created. + vm_postbuild_setup return 0 } From 41a29c1ab299473db6127dbe4abb352c1fd40afb Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Tue, 14 Oct 2014 11:20:08 +0000 Subject: [PATCH 12/13] Revert r272793, r272794, r272795: Remove implementation of vm_prebuild_setup(), vm_setup(), and vm_postbuild_setup(). It does not scale well, and I am not happy with their implementation. Sponsored by: The FreeBSD Foundation --- release/amd64/mk-vmimage.sh | 40 ------------------------------------- release/i386/mk-vmimage.sh | 40 ------------------------------------- 2 files changed, 80 deletions(-) diff --git a/release/amd64/mk-vmimage.sh b/release/amd64/mk-vmimage.sh index de233163d6de..b3ffd232f848 100755 --- a/release/amd64/mk-vmimage.sh +++ b/release/amd64/mk-vmimage.sh @@ -35,18 +35,6 @@ PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" export PATH -vm_prebuild_setup() { - return 0 -} - -vm_setup() { - return 0 -} - -vm_postbuild_setup() { - return 0 -} - usage_vm_base() { echo -n "$(basename ${0}) vm-base " echo " " @@ -92,9 +80,6 @@ panic() { } vm_create_baseimage() { - # Run anything that is needed before the virtual machine disk image - # is created. - vm_prebuild_setup # Creates the UFS root filesystem for the virtual machine disk, # written to the formatted disk image with mkimg(1). # @@ -128,9 +113,6 @@ vm_create_baseimage() { >> ${DESTDIR}/etc/fstab echo '/dev/gpt/swapfs none swap sw 0 0' \ >> ${DESTDIR}/etc/fstab - # Run anything that is needed while the virtual machine disk image - # userland filesystem is still mounted as a md(4) device. - vm_setup sync while ! umount ${DESTDIR}; do i=$(( $i + 1 )) @@ -190,35 +172,13 @@ vm_create_vmdisk() { -p freebsd-ufs/rootfs:=${VMBASE} \ -o ${VMIMAGE} - # Run anything that is needed for the virtual machine disk image - # after it has been created. - vm_postbuild_setup return 0 } main() { - vm_config= - while getopts "c:" arg; do - case ${arg} in - c) - vm_config="${OPTARG}" - ;; - *) - ;; - esac - done - shift $(( ${OPTIND} - 1 )) - cmd="${1}" shift 1 - if [ ! -z "${vm_config}" ]; then - if [ ! -e "${vm_config}" ]; then - panic "Configuration file ${vm_config} not found." - fi - . ${vm_config} - fi - case ${cmd} in vm-base) eval vm_create_baseimage "$@" || return 0 diff --git a/release/i386/mk-vmimage.sh b/release/i386/mk-vmimage.sh index de233163d6de..b3ffd232f848 100755 --- a/release/i386/mk-vmimage.sh +++ b/release/i386/mk-vmimage.sh @@ -35,18 +35,6 @@ PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin" export PATH -vm_prebuild_setup() { - return 0 -} - -vm_setup() { - return 0 -} - -vm_postbuild_setup() { - return 0 -} - usage_vm_base() { echo -n "$(basename ${0}) vm-base " echo " " @@ -92,9 +80,6 @@ panic() { } vm_create_baseimage() { - # Run anything that is needed before the virtual machine disk image - # is created. - vm_prebuild_setup # Creates the UFS root filesystem for the virtual machine disk, # written to the formatted disk image with mkimg(1). # @@ -128,9 +113,6 @@ vm_create_baseimage() { >> ${DESTDIR}/etc/fstab echo '/dev/gpt/swapfs none swap sw 0 0' \ >> ${DESTDIR}/etc/fstab - # Run anything that is needed while the virtual machine disk image - # userland filesystem is still mounted as a md(4) device. - vm_setup sync while ! umount ${DESTDIR}; do i=$(( $i + 1 )) @@ -190,35 +172,13 @@ vm_create_vmdisk() { -p freebsd-ufs/rootfs:=${VMBASE} \ -o ${VMIMAGE} - # Run anything that is needed for the virtual machine disk image - # after it has been created. - vm_postbuild_setup return 0 } main() { - vm_config= - while getopts "c:" arg; do - case ${arg} in - c) - vm_config="${OPTARG}" - ;; - *) - ;; - esac - done - shift $(( ${OPTIND} - 1 )) - cmd="${1}" shift 1 - if [ ! -z "${vm_config}" ]; then - if [ ! -e "${vm_config}" ]; then - panic "Configuration file ${vm_config} not found." - fi - . ${vm_config} - fi - case ${cmd} in vm-base) eval vm_create_baseimage "$@" || return 0 From 3991f309ede76bfe7493b8d9bf5c6d622fa05851 Mon Sep 17 00:00:00 2001 From: Glen Barber Date: Tue, 14 Oct 2014 11:48:28 +0000 Subject: [PATCH 13/13] Add a separate make(1) target to release/Makefile to build FreeBSD virtual machine disk images for use on the Microsoft Azure service. For now, this target is not directly connected to the build, however can be manually invoked. The 'vm-azure' target invokes {amd64,i386}/mk-azure.sh, which does the heavy lifting to produce proper VHDs. mk-azure.sh uses a configuration file, defaulting to tools/azure.conf if otherwise unset. Sponsored by: The FreeBSD Foundation --- release/Makefile | 15 ++++ release/amd64/mk-azure.sh | 171 ++++++++++++++++++++++++++++++++++++++ release/i386/mk-azure.sh | 171 ++++++++++++++++++++++++++++++++++++++ release/tools/azure.conf | 14 ++++ 4 files changed, 371 insertions(+) create mode 100755 release/amd64/mk-azure.sh create mode 100755 release/i386/mk-azure.sh create mode 100644 release/tools/azure.conf diff --git a/release/Makefile b/release/Makefile index 8a6ece7d1b58..8a517f810243 100644 --- a/release/Makefile +++ b/release/Makefile @@ -101,6 +101,7 @@ VMTARGETS= vm-base vm-image VMFORMATS?= vhd vmdk qcow2 raw VMSIZE?= 20G VMBASE?= vm +AZURECONF?= ${.CURDIR}/tools/azure.conf CLEANFILES= packagesystem *.txz MANIFEST system ${IMAGES} .if defined(WITH_COMPRESSED_IMAGES) && !empty(WITH_COMPRESSED_IMAGES) @@ -121,6 +122,12 @@ CLEANDIRS= dist ftp release bootonly dvd .if defined(WITH_VMIMAGES) && !empty(WITH_VMIMAGES) CLEANDIRS+= ${VMTARGETS} .endif +.if exists(${.CURDIR}/${TARGET}/mk-azure.sh) +CLEANFILES+= ${OSRELEASE}.vhd \ + ${OSRELEASE}.vhd.raw \ + azure.img +CLEANDIRS+= vm-azure +.endif beforeclean: chflags -R noschg . .include @@ -338,3 +345,11 @@ vm-image: vm-base . endif .endif touch ${.TARGET} + +vm-azure: +.if exists(${.CURDIR}/${TARGET}/mk-azure.sh) + env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} AZURECONF=${AZURECONF} \ + ${.CURDIR}/${TARGET}/mk-azure.sh ${.TARGET} azure.img \ + ${WORLDDIR} ${.TARGET} ${VMSIZE} ${OSRELEASE}.vhd +.endif + touch ${.TARGET} diff --git a/release/amd64/mk-azure.sh b/release/amd64/mk-azure.sh new file mode 100755 index 000000000000..b691d54f6a69 --- /dev/null +++ b/release/amd64/mk-azure.sh @@ -0,0 +1,171 @@ +#!/bin/sh +#- +# Copyright (c) 2014 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-azure.sh: Create virtual machine disk images for Microsoft Azure +# +# $FreeBSD$ +# + +export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin" + +usage() { + echo "Usage:" + echo -n "$(basename ${0}) vm-azure " + echo " " + exit 1 +} + +panic() { + msg="${@}" + printf "${msg}\n" + if [ ! -z "${mddev}" ]; then + mdconfig -d -u ${mddev} + fi + # Do not allow one failure case to chain through any remaining image + # builds. + exit 0 +} + +vm_create_azure() { + # Arguments: + # vm-azure + + VMBASE="${1}" + WORLDDIR="${2}" + DESTDIR="${3}" + VMSIZE="${4}" + VMIMAGE="${5}" + + if [ -z "${VMBASE}" -o -z "${WORLDDIR}" -o -z "${DESTDIR}" \ + -o -z "${VMSIZE}" -o -z "${VMIMAGE}" ]; then + usage + fi + + trap "umount ${DESTDIR}/dev ${DESTDIR}" EXIT + + i=0 + mkdir -p ${DESTDIR} + truncate -s ${VMSIZE} ${VMBASE} + mddev=$(mdconfig -f ${VMBASE}) + newfs -j /dev/${mddev} + mkdir -p ${DESTDIR} + mount /dev/${mddev} ${DESTDIR} + make -C ${WORLDDIR} DESTDIR=$(realpath ${DESTDIR}) \ + installworld installkernel distribution || \ + panic 1 "\n\nCannot install the base system to ${DESTDIR}." + mount -t devfs devfs ${DESTDIR}/dev + chroot ${DESTDIR} /usr/bin/newaliases + echo '# Custom /etc/fstab for FreeBSD VM images' \ + > ${DESTDIR}/etc/fstab + echo '/dev/gpt/rootfs / ufs rw 2 2' \ + >> ${DESTDIR}/etc/fstab + # Although a swap partition is created, it is not used in Azure. + echo '#/dev/gpt/swapfs none swap sw 0 0' \ + >> ${DESTDIR}/etc/fstab + + chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart + chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg bootstrap -y + chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg install -y \ + python python2 python27 py27-asn1 sudo bash + if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then + chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg install -y \ + ${VM_EXTRA_PACKAGES} + fi + + fetch -o ${DESTDIR}/usr/sbin/waagent \ + http://people.freebsd.org/~gjb/waagent + chmod +x ${DESTDIR}/usr/sbin/waagent + rm -f ${DESTDIR}/etc/resolv.conf + chroot ${DESTDIR} /usr/sbin/waagent -verbose -install + yes | chroot ${DESTDIR} /usr/sbin/waagent -deprovision + echo 'sshd_enable="YES"' > ${DESTDIR}/etc/rc.conf + echo 'ifconfig_hn0="SYNCDHCP"' >> ${DESTDIR}/etc/rc.conf + echo 'waagent_enable="YES"' >> ${DESTDIR}/etc/rc.conf + + echo 'console="comconsole vidconsole"' >> ${DESTDIR}/boot/loader.conf + echo 'comconsole_speed="115200"' >> ${DESTDIR}/boot/loader.conf + + if [ ! -z "${VM_RC_LIST}" ]; then + for _rcvar in ${VM_RC_LIST}; do + echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf + done + fi + + 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." + panic 1 "${msg}" + fi + sleep 1 + done + + mkimg -f vhdf -s gpt \ + -b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \ + -p freebsd-swap/swapfs::1G \ + -p freebsd-ufs/rootfs:=${VMBASE} \ + -o ${VMIMAGE}.raw + + if [ ! -x "/usr/local/bin/qemu-img" ]; then + env ASSUME_ALWAYS_YES=yes pkg install -y emulators/qemu-devel + fi + + size=$(qemu-img info -f raw --output json ${VMIMAGE}.raw | awk '/virtual-size/ {print $2}' | tr -d ',') + size=$(( ( ${size} / ( 1024 * 1024 ) + 1 ) * ( 1024 * 1024 ) )) + qemu-img resize ${VMIMAGE}.raw ${size} + qemu-img convert -f raw -o subformat=fixed -O vpc ${VMIMAGE}.raw ${VMIMAGE} + + return 0 +} + +main() { + cmd="${1}" + shift 1 + + if [ -e "${AZURECONF}" -a ! -c "${AZURECONF}" ]; then + . ${AZURECONF} + fi + + case ${cmd} in + vm-azure) + eval vm_create_azure "$@" || return 0 + ;; + *|\?) + usage + ;; + esac + + return 0 +} + +main "$@" diff --git a/release/i386/mk-azure.sh b/release/i386/mk-azure.sh new file mode 100755 index 000000000000..b691d54f6a69 --- /dev/null +++ b/release/i386/mk-azure.sh @@ -0,0 +1,171 @@ +#!/bin/sh +#- +# Copyright (c) 2014 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-azure.sh: Create virtual machine disk images for Microsoft Azure +# +# $FreeBSD$ +# + +export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin" + +usage() { + echo "Usage:" + echo -n "$(basename ${0}) vm-azure " + echo " " + exit 1 +} + +panic() { + msg="${@}" + printf "${msg}\n" + if [ ! -z "${mddev}" ]; then + mdconfig -d -u ${mddev} + fi + # Do not allow one failure case to chain through any remaining image + # builds. + exit 0 +} + +vm_create_azure() { + # Arguments: + # vm-azure + + VMBASE="${1}" + WORLDDIR="${2}" + DESTDIR="${3}" + VMSIZE="${4}" + VMIMAGE="${5}" + + if [ -z "${VMBASE}" -o -z "${WORLDDIR}" -o -z "${DESTDIR}" \ + -o -z "${VMSIZE}" -o -z "${VMIMAGE}" ]; then + usage + fi + + trap "umount ${DESTDIR}/dev ${DESTDIR}" EXIT + + i=0 + mkdir -p ${DESTDIR} + truncate -s ${VMSIZE} ${VMBASE} + mddev=$(mdconfig -f ${VMBASE}) + newfs -j /dev/${mddev} + mkdir -p ${DESTDIR} + mount /dev/${mddev} ${DESTDIR} + make -C ${WORLDDIR} DESTDIR=$(realpath ${DESTDIR}) \ + installworld installkernel distribution || \ + panic 1 "\n\nCannot install the base system to ${DESTDIR}." + mount -t devfs devfs ${DESTDIR}/dev + chroot ${DESTDIR} /usr/bin/newaliases + echo '# Custom /etc/fstab for FreeBSD VM images' \ + > ${DESTDIR}/etc/fstab + echo '/dev/gpt/rootfs / ufs rw 2 2' \ + >> ${DESTDIR}/etc/fstab + # Although a swap partition is created, it is not used in Azure. + echo '#/dev/gpt/swapfs none swap sw 0 0' \ + >> ${DESTDIR}/etc/fstab + + chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart + chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg bootstrap -y + chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg install -y \ + python python2 python27 py27-asn1 sudo bash + if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then + chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg install -y \ + ${VM_EXTRA_PACKAGES} + fi + + fetch -o ${DESTDIR}/usr/sbin/waagent \ + http://people.freebsd.org/~gjb/waagent + chmod +x ${DESTDIR}/usr/sbin/waagent + rm -f ${DESTDIR}/etc/resolv.conf + chroot ${DESTDIR} /usr/sbin/waagent -verbose -install + yes | chroot ${DESTDIR} /usr/sbin/waagent -deprovision + echo 'sshd_enable="YES"' > ${DESTDIR}/etc/rc.conf + echo 'ifconfig_hn0="SYNCDHCP"' >> ${DESTDIR}/etc/rc.conf + echo 'waagent_enable="YES"' >> ${DESTDIR}/etc/rc.conf + + echo 'console="comconsole vidconsole"' >> ${DESTDIR}/boot/loader.conf + echo 'comconsole_speed="115200"' >> ${DESTDIR}/boot/loader.conf + + if [ ! -z "${VM_RC_LIST}" ]; then + for _rcvar in ${VM_RC_LIST}; do + echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf + done + fi + + 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." + panic 1 "${msg}" + fi + sleep 1 + done + + mkimg -f vhdf -s gpt \ + -b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \ + -p freebsd-swap/swapfs::1G \ + -p freebsd-ufs/rootfs:=${VMBASE} \ + -o ${VMIMAGE}.raw + + if [ ! -x "/usr/local/bin/qemu-img" ]; then + env ASSUME_ALWAYS_YES=yes pkg install -y emulators/qemu-devel + fi + + size=$(qemu-img info -f raw --output json ${VMIMAGE}.raw | awk '/virtual-size/ {print $2}' | tr -d ',') + size=$(( ( ${size} / ( 1024 * 1024 ) + 1 ) * ( 1024 * 1024 ) )) + qemu-img resize ${VMIMAGE}.raw ${size} + qemu-img convert -f raw -o subformat=fixed -O vpc ${VMIMAGE}.raw ${VMIMAGE} + + return 0 +} + +main() { + cmd="${1}" + shift 1 + + if [ -e "${AZURECONF}" -a ! -c "${AZURECONF}" ]; then + . ${AZURECONF} + fi + + case ${cmd} in + vm-azure) + eval vm_create_azure "$@" || return 0 + ;; + *|\?) + usage + ;; + esac + + return 0 +} + +main "$@" diff --git a/release/tools/azure.conf b/release/tools/azure.conf new file mode 100644 index 000000000000..da205bb1d307 --- /dev/null +++ b/release/tools/azure.conf @@ -0,0 +1,14 @@ +#!/bin/sh +# +# $FreeBSD$ +# + +# Set to a list of packages to install. +# Example: +#export VM_EXTRA_PACKAGES="www/apache24" +export VM_EXTRA_PACKAGES= + +# Set to a list of third-party software to enable in rc.conf(5). +# Example: +#export VM_RC_LIST="apache24" +export VM_RC_LIST="apache24"