Hi,
This is a quick and dirty implementation of: http://mail-index.NetBSD.org/current-users/2009/05/10/msg009372.html to use: $ echo KERNEL_DIR=yes >> /etc/mk.conf # apply the enclosed patch $ mv /netbsd{,.old} $ mkdir -p /netbsd/modules # build a new kernel and put it in /netbsd/kernel $ make && make install in src/sys/modules $ make && make install sys/arch/i386/stand/boot $ cp /usr/mdec/boot / There are quite a few open issues: - All ports need to provide get_booted_kernel() Are there ports where we can't get the booted kernel name? What do we do there? - Make install in modules always overwrites the modules in /netbsd/ - Does each kernel.tgz need to pack all the modules in the sets or do we keep the modules.tgz that unpacks in /netbsd/modules? - How about the other kernels? do they unpack in /KERNEL-NAME/kernel? - Right now there is fallback code in the boot blocks for just the kernel but not the old module path (this is intentional to keep things simple). You can always boot a kernel by name from a file. christos Index: share/mk/bsd.kmodule.mk =================================================================== RCS file: /cvsroot/src/share/mk/bsd.kmodule.mk,v retrieving revision 1.63 diff -u -p -u -r1.63 bsd.kmodule.mk --- share/mk/bsd.kmodule.mk 1 Dec 2019 20:24:47 -0000 1.63 +++ share/mk/bsd.kmodule.mk 7 Dec 2019 02:26:01 -0000 @@ -172,7 +172,13 @@ ${PROG}: ${OBJS} ${DPADD} ${KMODSCRIPT} ##### Install rules .if !target(kmodinstall) .if !defined(KMODULEDIR) +.if ${KERNEL_DIR:Uno} == "yes" +KMODULEDIR= ${DESTDIR}/netbsd/modules/${KMOD} +_INST_DIRS= ${DESTDIR}/netbsd +_INST_DIRS+= ${DESTDIR}/netbsd/modules +_INST_DIRS+= ${DESTDIR}/netbsd/modules/${KMOD} _OSRELEASE!= ${HOST_SH} $S/conf/osrelease.sh -k +.else # Ensure these are recorded properly in METALOG on unprived installes: KMODULEARCHDIR?= ${MACHINE} _INST_DIRS= ${DESTDIR}/stand/${KMODULEARCHDIR} @@ -180,6 +186,7 @@ _INST_DIRS+= ${DESTDIR}/stand/${KMODULEA _INST_DIRS+= ${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules KMODULEDIR= ${DESTDIR}/stand/${KMODULEARCHDIR}/${_OSRELEASE}/modules/${KMOD} .endif +.endif _PROG:= ${KMODULEDIR}/${PROG} # installed path .if ${MKUPDATE} == "no" Index: sys/arch/i386/stand/boot/Makefile.boot =================================================================== RCS file: /cvsroot/src/sys/arch/i386/stand/boot/Makefile.boot,v retrieving revision 1.73 diff -u -p -u -r1.73 Makefile.boot --- sys/arch/i386/stand/boot/Makefile.boot 13 Sep 2019 02:19:45 -0000 1.73 +++ sys/arch/i386/stand/boot/Makefile.boot 7 Dec 2019 02:26:01 -0000 @@ -53,6 +53,10 @@ CFLAGS+= -Wall -Wmissing-prototypes -Wst CPPFLAGS+= -nostdinc -D_STANDALONE CPPFLAGS+= -I$S +.if ${KERNEL_DIR:Uno} == "yes" +CPPFLAGS+= -DKERNEL_DIR +.endif + CPPFLAGS+= -DSUPPORT_PS2 CPPFLAGS+= -DDIRECT_SERIAL CPPFLAGS+= -DSUPPORT_SERIAL=boot_params.bp_consdev Index: sys/arch/i386/stand/boot/boot2.c =================================================================== RCS file: /cvsroot/src/sys/arch/i386/stand/boot/boot2.c,v retrieving revision 1.72 diff -u -p -u -r1.72 boot2.c --- sys/arch/i386/stand/boot/boot2.c 2 Sep 2019 06:10:24 -0000 1.72 +++ sys/arch/i386/stand/boot/boot2.c 7 Dec 2019 02:26:01 -0000 @@ -279,6 +279,12 @@ bootit(const char *filename, int howto) if (howto & AB_VERBOSE) printf("booting %s (howto 0x%x)\n", sprint_bootsel(filename), howto); +#ifdef KERNEL_DIR + char path[512]; + strcpy(path, filename); + strcat(path, "/kernel"); + (void)exec_netbsd(path, 0, howto, boot_biosdev < 0x80, clearit); +#endif if (exec_netbsd(filename, 0, howto, boot_biosdev < 0x80, clearit) < 0) printf("boot: %s: %s\n", sprint_bootsel(filename), Index: sys/arch/i386/stand/lib/exec.c =================================================================== RCS file: /cvsroot/src/sys/arch/i386/stand/lib/exec.c,v retrieving revision 1.74 diff -u -p -u -r1.74 exec.c --- sys/arch/i386/stand/lib/exec.c 13 Sep 2019 02:19:46 -0000 1.74 +++ sys/arch/i386/stand/lib/exec.c 7 Dec 2019 02:26:01 -0000 @@ -151,7 +151,7 @@ static void module_add_common(const char static void userconf_init(void); static void extract_device(const char *, char *, size_t); -static void module_base_path(char *, size_t); +static void module_base_path(char *, size_t, const char *); static int module_open(boot_module_t *, int, const char *, const char *, bool); @@ -653,8 +653,18 @@ module_open(boot_module_t *bm, int mode, } static void -module_base_path(char *buf, size_t bufsize) +module_base_path(char *buf, size_t bufsize, const char *kernel_path) { +#ifdef KERNEL_DIR + /* we cheat here, because %.* does not work with the mini printf */ + char *ptr = strrchr(kernel_path, '/'); + if (*ptr) + *ptr = '\0'; + + snprintf(buf, bufsize, "%s/modules", kernel_path); + if (*ptr) + *ptr = '/'; +#else const char *machine; switch (netbsd_elf_class) { @@ -682,6 +692,7 @@ module_base_path(char *buf, size_t bufsi netbsd_version / 100000000, netbsd_version / 1000000 % 100); } +#endif } static void @@ -697,7 +708,7 @@ module_init(const char *kernel_path) int err, fd, nfail = 0; extract_device(kernel_path, kdev, sizeof(kdev)); - module_base_path(module_base, sizeof(module_base)); + module_base_path(module_base, sizeof(module_base), kernel_path); /* First, see which modules are valid and calculate btinfo size */ len = sizeof(struct btinfo_modulelist); Index: sys/arch/x86/x86/x86_machdep.c =================================================================== RCS file: /cvsroot/src/sys/arch/x86/x86/x86_machdep.c,v retrieving revision 1.133 diff -u -p -u -r1.133 x86_machdep.c --- sys/arch/x86/x86/x86_machdep.c 3 Dec 2019 15:20:59 -0000 1.133 +++ sys/arch/x86/x86/x86_machdep.c 7 Dec 2019 02:26:01 -0000 @@ -1092,6 +1092,13 @@ x86_startup(void) #endif } +const char * +get_booted_kernel(void) +{ + const struct btinfo_bootpath *bibp = lookup_bootinfo(BTINFO_BOOTPATH); + return bibp ? bibp->bootpath : NULL; +} + /* * machine dependent system variables. */ Index: sys/conf/Makefile.kern.inc =================================================================== RCS file: /cvsroot/src/sys/conf/Makefile.kern.inc,v retrieving revision 1.268 diff -u -p -u -r1.268 Makefile.kern.inc --- sys/conf/Makefile.kern.inc 5 Feb 2019 08:33:25 -0000 1.268 +++ sys/conf/Makefile.kern.inc 7 Dec 2019 02:26:01 -0000 @@ -66,6 +66,9 @@ CPPFLAGS+= ${INCLUDES} ${IDENT} -D_KERNE .if !defined(COVERITY_TOP_CONFIG) CPPFLAGS+= -std=gnu99 .endif +.if ${KERNEL_DIR:Uno} == "yes" +CPPFLAGS+= -DKERNEL_DIR +.endif DEFCOPTS?= -O2 COPTS?= ${DEFCOPTS} DBG= # might contain unwanted -Ofoo @@ -413,10 +416,17 @@ install: install-kernel-${MACHINE_NAME} .if !target(install-kernel-${MACHINE_NAME}) install-kernel-${MACHINE_NAME}: .for _K in ${KERNIMAGES} +.if ${KERNEL_DIR:Uno} == "yes" + rm -fr ${DESTDIR}/o${_K} + mv ${DESTDIR}/${_K} ${DESTDIR}/o${_K} + mkdir -p ${DESTDIR}/${_K} + cp ${_K} ${DESTDIR}/${_K}/kernel +.else rm -f ${DESTDIR}/o${_K} ln ${DESTDIR}/${_K} ${DESTDIR}/o${_K} cp ${_K} ${DESTDIR}/n${_K} mv ${DESTDIR}/n${_K} ${DESTDIR}/${_K} +.endif .endfor .endif .endif Index: sys/kern/kern_module.c =================================================================== RCS file: /cvsroot/src/sys/kern/kern_module.c,v retrieving revision 1.138 diff -u -p -u -r1.138 kern_module.c --- sys/kern/kern_module.c 8 Aug 2019 18:08:41 -0000 1.138 +++ sys/kern/kern_module.c 7 Dec 2019 02:31:15 -0000 @@ -418,6 +418,12 @@ module_init(void) module_init_md(); #endif +#ifdef KERNEL_DIR + const char *booted_kernel = get_booted_kernel(); + char *ptr = strrchr(booted_kernel, '/'); + snprintf(module_base, sizeof(module_base), "/%.*s/modules", + (int)(ptr - booted_kernel), booted_kernel); +#else if (!module_machine) module_machine = machine; #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */ @@ -428,6 +434,7 @@ module_init(void) module_machine, __NetBSD_Version__ / 100000000, __NetBSD_Version__ / 1000000 % 100); #endif +#endif module_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM, module_listener_cb, NULL); Index: sys/sys/systm.h =================================================================== RCS file: /cvsroot/src/sys/sys/systm.h,v retrieving revision 1.291 diff -u -p -u -r1.291 systm.h --- sys/sys/systm.h 15 Nov 2019 12:18:46 -0000 1.291 +++ sys/sys/systm.h 7 Dec 2019 02:26:02 -0000 @@ -167,6 +167,8 @@ extern int boothowto; /* reboot flags, #define bootverbose (boothowto & AB_VERBOSE) #define bootquiet (boothowto & AB_QUIET) +extern const char *get_booted_kernel(void); + extern void (*v_putc)(int); /* Virtual console putc routine */ /*