Add an option to bhyveload(8) that allows setting a loader environment variable from the command line. The option syntax is "-e " Reviewed by: grehan Requested by: alfred Index: share/examples/bhyve/vmrun.sh =================================================================== --- share/examples/bhyve/vmrun.sh (revision 256645) +++ share/examples/bhyve/vmrun.sh (working copy) @@ -39,11 +39,12 @@ DEFAULT_ISOFILE="./release.iso" usage() { - echo "Usage: vmrun.sh [-hai][-g ][-m ][-d ][-I ][-t ] " + echo "Usage: vmrun.sh [-hai][-g ][-m ][-d ][-e ][-I ][-t ] " echo " -h: display this help message" echo " -a: force memory mapped local apic access" echo " -c: number of virtual cpus (default is ${DEFAULT_CPUS})" echo " -d: virtio diskdev file (default is ${DEFAULT_VIRTIO_DISK})" + echo " -e: set FreeBSD loader environment variable" echo " -g: listen for connection from kgdb at " echo " -i: force boot of the Installation CDROM image" echo " -I: Installation CDROM image location (default is ${DEFAULT_ISOFILE})" @@ -73,8 +74,9 @@ tapdev=${DEFAULT_TAPDEV} apic_opt="" gdbport=0 +env_opt="" -while getopts haic:g:I:m:d:t: c ; do +while getopts haic:e:g:I:m:d:t: c ; do case $c in h) usage @@ -85,6 +87,9 @@ d) virtio_diskdev=${OPTARG} ;; + e) + env_opt="${env_opt} -e ${OPTARG}" + ;; g) gdbport=${OPTARG} ;; i) @@ -163,7 +168,7 @@ installer_opt="" fi - ${LOADER} -m ${memsize} -d ${BOOTDISK} ${vmname} + ${LOADER} -m ${memsize} -d ${BOOTDISK} ${env_opt} ${vmname} if [ $? -ne 0 ]; then break fi Index: usr.sbin/bhyveload/bhyveload.8 =================================================================== --- usr.sbin/bhyveload/bhyveload.8 (revision 256645) +++ usr.sbin/bhyveload/bhyveload.8 (working copy) @@ -38,6 +38,7 @@ .Op Fl m Ar mem-size .Op Fl d Ar disk-path .Op Fl h Ar host-path +.Op Fl e Ar name=value .Ar vmname .Sh DESCRIPTION .Nm @@ -91,6 +92,11 @@ The .Ar host-path is the directory at the top of the guest's boot filesystem. +.It Fl e Ar name=value +Set the FreeBSD loader environment variable +.Ar name +to +.Ar value . .El .Sh EXAMPLES To create a virtual machine named Index: usr.sbin/bhyveload/bhyveload.c =================================================================== --- usr.sbin/bhyveload/bhyveload.c (revision 256645) +++ usr.sbin/bhyveload/bhyveload.c (working copy) @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -498,23 +499,37 @@ vm_get_memory_seg(ctx, 4 * GB, ret_highmem, NULL); } +struct env { + const char *str; /* name=value */ + SLIST_ENTRY(env) next; +}; + +static SLIST_HEAD(envhead, env) envhead; + +static void +addenv(const char *str) +{ + struct env *env; + + env = malloc(sizeof(struct env)); + env->str = str; + SLIST_INSERT_HEAD(&envhead, env, next); +} + static const char * cb_getenv(void *arg, int num) { - int max; + int i; + struct env *env; - static const char * var[] = { - "smbios.bios.vendor=BHYVE", - "boot_serial=1", - NULL - }; + i = 0; + SLIST_FOREACH(env, &envhead, next) { + if (i == num) + return (env->str); + i++; + } - max = sizeof(var) / sizeof(var[0]); - - if (num < max) - return (var[num]); - else - return (NULL); + return (NULL); } static struct loader_callbacks cb = { @@ -553,8 +568,8 @@ { fprintf(stderr, - "usage: %s [-m mem-size][-d ] [-h ] " - "\n", progname); + "usage: %s [-m mem-size][-d ] [-h ] " + "[-e ] \n", progname); exit(1); } @@ -572,12 +587,16 @@ mem_size = 256 * MB; disk_image = NULL; - while ((opt = getopt(argc, argv, "d:h:m:")) != -1) { + while ((opt = getopt(argc, argv, "d:e:h:m:")) != -1) { switch (opt) { case 'd': disk_image = optarg; break; + case 'e': + addenv(optarg); + break; + case 'h': host_base = optarg; break; @@ -638,5 +657,9 @@ if (disk_image) { disk_fd = open(disk_image, O_RDONLY); } + + addenv("smbios.bios.vendor=BHYVE"); + addenv("boot_serial=1"); + func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0); }