2016-05-20 16:12:11 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2022-02-17 09:17:56 +01:00
|
|
|
# Copyright (c) 2016-2022 Franco Fichtner <franco@opnsense.org>
|
2016-05-20 16:12:11 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
SELF=vm
|
|
|
|
|
2017-04-27 19:14:36 +02:00
|
|
|
. ./common.sh
|
2016-05-20 16:12:11 +02:00
|
|
|
|
2017-07-24 09:07:23 +02:00
|
|
|
check_image ${SELF} ${@}
|
2016-05-21 11:54:42 +02:00
|
|
|
|
2016-05-20 16:12:11 +02:00
|
|
|
VMFORMAT="vmdk"
|
|
|
|
VMSIZE="20G"
|
|
|
|
VMSWAP="1G"
|
2022-02-17 15:32:21 +01:00
|
|
|
VMEXTRAS=${SELF}
|
2016-05-20 16:12:11 +02:00
|
|
|
|
|
|
|
if [ -n "${1}" ]; then
|
|
|
|
VMFORMAT=${1}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${2}" ]; then
|
|
|
|
VMSIZE=${2}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${3}" ]; then
|
2022-02-23 13:13:26 +01:00
|
|
|
if [ "${3}" != "off" -a "${3}" != "never" ]; then
|
2016-05-20 16:12:11 +02:00
|
|
|
VMSWAP=${3}
|
|
|
|
else
|
|
|
|
VMSWAP=
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-02-17 15:32:21 +01:00
|
|
|
if [ -n "${4}" ]; then
|
|
|
|
VMEXTRAS=${4}
|
|
|
|
fi
|
|
|
|
|
2016-07-17 15:38:10 +02:00
|
|
|
VMIMG="${IMAGESDIR}/${PRODUCT_RELEASE}-vm-${PRODUCT_ARCH}.${VMFORMAT}"
|
2021-08-20 12:08:12 +02:00
|
|
|
VMBASE="vmbase"
|
2016-05-20 16:12:11 +02:00
|
|
|
|
|
|
|
sh ./clean.sh ${SELF}
|
|
|
|
|
2016-05-21 10:53:24 +02:00
|
|
|
setup_stage ${STAGEDIR} mnt
|
2016-05-20 16:12:11 +02:00
|
|
|
|
2021-08-20 12:08:12 +02:00
|
|
|
truncate -s ${VMSIZE} ${STAGEDIR}/${VMBASE}
|
|
|
|
DEV=$(mdconfig -t vnode -f ${STAGEDIR}/${VMBASE})
|
2022-02-17 09:17:56 +01:00
|
|
|
|
|
|
|
if [ -n "${PRODUCT_ZFS}" ]; then
|
|
|
|
ZPOOL=${PRODUCT_ZFS}
|
|
|
|
|
|
|
|
# avoid clobbering existing pools
|
|
|
|
for IMPORT in $(zpool import 2> /dev/null | awk '$1 == "pool:" { print $2}'); do
|
|
|
|
if [ ${IMPORT} = ${ZPOOL} ]; then
|
|
|
|
echo ">>> ZFS pool '${ZPOOL}' already exists"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# 4k sector alignment
|
|
|
|
gnop create -S 4096 ${DEV}
|
|
|
|
|
|
|
|
# create ZFS pool like installer would
|
|
|
|
zpool create -R ${STAGEDIR}/mnt ${ZPOOL} ${DEV}.nop
|
|
|
|
zfs create -o mountpoint=none ${ZPOOL}/ROOT
|
|
|
|
zfs create -o mountpoint=/ ${ZPOOL}/ROOT/default
|
|
|
|
zfs create -o mountpoint=/tmp -o exec=on -o setuid=off ${ZPOOL}/tmp
|
|
|
|
zfs create -o mountpoint=/usr -o canmount=off ${ZPOOL}/usr
|
|
|
|
zfs create ${ZPOOL}/usr/home
|
|
|
|
zfs create -o setuid=off ${ZPOOL}/usr/ports
|
|
|
|
zfs create ${ZPOOL}/usr/src
|
|
|
|
zfs create -o mountpoint=/var -o canmount=off ${ZPOOL}/var
|
|
|
|
zfs create -o exec=off -o setuid=off ${ZPOOL}/var/audit
|
|
|
|
zfs create -o exec=off -o setuid=off ${ZPOOL}/var/crash
|
|
|
|
zfs create -o exec=off -o setuid=off ${ZPOOL}/var/log
|
|
|
|
zfs create -o atime=on ${ZPOOL}/var/mail
|
|
|
|
zfs create -o setuid=off ${ZPOOL}/var/tmp
|
|
|
|
zpool set bootfs=${ZPOOL}/ROOT/default ${ZPOOL}
|
|
|
|
|
2022-05-19 15:53:34 +02:00
|
|
|
GPTNAME=gptzfsboot
|
2022-02-17 09:17:56 +01:00
|
|
|
ROOTFS=zfs
|
|
|
|
else
|
|
|
|
newfs /dev/${DEV}
|
|
|
|
mount /dev/${DEV} ${STAGEDIR}/mnt
|
|
|
|
|
|
|
|
GPTNAME=gptboot
|
|
|
|
ROOTFS=ufs/rootfs
|
|
|
|
fi
|
2016-05-20 16:12:11 +02:00
|
|
|
|
|
|
|
setup_base ${STAGEDIR}/mnt
|
|
|
|
|
|
|
|
# need these again later
|
2019-05-19 13:31:33 +02:00
|
|
|
cp -R ${STAGEDIR}/mnt/boot ${STAGEDIR}
|
2016-05-20 16:12:11 +02:00
|
|
|
|
|
|
|
setup_kernel ${STAGEDIR}/mnt
|
|
|
|
setup_packages ${STAGEDIR}/mnt
|
2022-02-17 15:32:21 +01:00
|
|
|
setup_extras ${STAGEDIR}/mnt ${VMEXTRAS}
|
2016-05-20 22:38:10 +02:00
|
|
|
setup_entropy ${STAGEDIR}/mnt
|
2016-05-20 16:12:11 +02:00
|
|
|
|
|
|
|
cat > ${STAGEDIR}/mnt/etc/fstab << EOF
|
|
|
|
# Device Mountpoint FStype Options Dump Pass#
|
2022-02-17 09:17:56 +01:00
|
|
|
EOF
|
|
|
|
|
|
|
|
if [ -z "${PRODUCT_ZFS}" ]; then
|
|
|
|
cat >> ${STAGEDIR}/mnt/etc/fstab << EOF
|
2016-05-20 16:12:11 +02:00
|
|
|
/dev/gpt/rootfs / ufs rw 1 1
|
|
|
|
EOF
|
2022-02-17 09:17:56 +01:00
|
|
|
fi
|
2016-05-20 16:12:11 +02:00
|
|
|
|
2022-02-17 09:17:56 +01:00
|
|
|
GPTBOOT="-p freebsd-boot/bootfs:=boot/${GPTNAME}"
|
2017-05-24 17:22:46 +02:00
|
|
|
GPTDUMMY="-p freebsd-swap::512k"
|
2021-08-20 12:08:12 +02:00
|
|
|
MBRBOOT="-b boot/pmbr"
|
2016-05-20 16:12:11 +02:00
|
|
|
SWAPARGS=
|
2017-05-24 17:22:46 +02:00
|
|
|
UEFIBOOT=
|
|
|
|
|
2016-05-20 16:12:11 +02:00
|
|
|
if [ -n "${VMSWAP}" ]; then
|
|
|
|
SWAPARGS="-p freebsd-swap/swapfs::${VMSWAP}"
|
2016-05-20 18:24:06 +02:00
|
|
|
cat >> ${STAGEDIR}/mnt/etc/fstab << EOF
|
2016-05-20 16:12:11 +02:00
|
|
|
/dev/gpt/swapfs none swap sw 0 0
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
2021-08-20 12:08:12 +02:00
|
|
|
if [ -n "${PRODUCT_UEFI}" -a -z "${PRODUCT_UEFI%%*"${SELF}"*}" ]; then
|
2022-02-23 13:49:16 +01:00
|
|
|
UEFIBOOT="-p efi/efifs:=efiboot.img"
|
2021-08-20 12:08:12 +02:00
|
|
|
|
|
|
|
setup_efiboot ${STAGEDIR}/efiboot.img \
|
2022-02-23 13:49:16 +01:00
|
|
|
${STAGEDIR}/boot/loader.efi $((260 * 1024))
|
|
|
|
|
|
|
|
cat >> ${STAGEDIR}/mnt/etc/fstab << EOF
|
|
|
|
/dev/gpt/efifs /boot/efi msdosfs rw 2 2
|
|
|
|
EOF
|
2021-08-20 12:08:12 +02:00
|
|
|
fi
|
2016-05-21 11:54:42 +02:00
|
|
|
|
2021-07-26 13:04:14 +02:00
|
|
|
if [ ${PRODUCT_ARCH} = "aarch64" ]; then
|
2021-07-26 13:51:23 +02:00
|
|
|
GPTBOOT=
|
|
|
|
MBRBOOT=
|
|
|
|
|
|
|
|
# produce even number of partitions
|
|
|
|
if [ -n "${VMSWAP}" -a -z "${UEFIBOOT}" ]; then
|
|
|
|
GPTDUMMY=
|
|
|
|
elif [ -z "${VMSWAP}" -a -n "${UEFIBOOT}" ]; then
|
|
|
|
GPTDUMMY=
|
|
|
|
fi
|
2021-07-26 13:04:14 +02:00
|
|
|
else
|
2021-07-26 13:51:23 +02:00
|
|
|
# produce even number of partitions
|
|
|
|
if [ -z "${VMSWAP}" -a -z "${UEFIBOOT}" ]; then
|
|
|
|
GPTDUMMY=
|
|
|
|
elif [ -n "${VMSWAP}" -a -n "${UEFIBOOT}" ]; then
|
|
|
|
GPTDUMMY=
|
|
|
|
fi
|
2021-07-26 13:04:14 +02:00
|
|
|
fi
|
2016-05-21 11:54:42 +02:00
|
|
|
|
2022-02-23 13:13:26 +01:00
|
|
|
if [ "${3}" == "never" ]; then
|
|
|
|
GPTDUMMY=
|
|
|
|
fi
|
|
|
|
|
2022-02-23 13:49:16 +01:00
|
|
|
if [ -n "${PRODUCT_ZFS}" ]; then
|
|
|
|
zpool export ${ZPOOL}
|
|
|
|
else
|
|
|
|
umount ${STAGEDIR}/mnt
|
|
|
|
fi
|
|
|
|
mdconfig -d -u ${DEV}
|
|
|
|
|
2021-08-20 12:08:12 +02:00
|
|
|
echo -n ">>> Building vm image... "
|
|
|
|
|
|
|
|
(cd ${STAGEDIR}; mkimg -s gpt -f ${VMFORMAT} -o ${VMIMG} \
|
2022-02-17 09:17:56 +01:00
|
|
|
${MBRBOOT} ${UEFIBOOT} ${GPTBOOT} ${GPTDUMMY} ${SWAPARGS} \
|
|
|
|
-p freebsd-${ROOTFS}:=${VMBASE})
|
2021-07-26 13:51:23 +02:00
|
|
|
|
2016-05-21 11:54:42 +02:00
|
|
|
echo "done"
|
2023-11-20 11:08:37 +01:00
|
|
|
|
|
|
|
sign_image ${VMIMG}
|