git: 0b569fe6717a - stable/13 - kboot: Add hostdisk override

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:14:33 UTC
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=0b569fe6717a155c6f46e63d9de75ab009fcf5d1

commit 0b569fe6717a155c6f46e63d9de75ab009fcf5d1
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-01-13 21:21:07 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:47 +0000

    kboot: Add hostdisk override
    
    When hostdisk_override is set, all the /dev devices are hidden, and only
    the files in that directory are used. This will allow filesystem testing
    on FreeBSD without root, for example. Adjust the parse routine to not
    require devices start with /dev (plus fix a leak for an error
    condition). Add a match routine to allow the device name to be something
    like "/home/user/testing/zfsfoo:" instead of strictly in /dev. Note:
    since we need to look at all the devices in the system to probe for ZFS
    zpools, you can't generally use a full path to get a 'virtual disk' at
    this time.
    
    Sponsored by:           Netflix
    Reviewed by:            kevans
    Differential Revision:  https://reviews.freebsd.org/D38011
    
    (cherry picked from commit b7ecfa195f46da4a08a0cb5b399c8bc69bff225a)
---
 stand/kboot/hostdisk.c | 79 ++++++++++++++++++++++++++++++++++++++++++++------
 stand/kboot/main.c     |  7 +++--
 2 files changed, 75 insertions(+), 11 deletions(-)

diff --git a/stand/kboot/hostdisk.c b/stand/kboot/hostdisk.c
index a96c38c21182..fab4ee04bb6b 100644
--- a/stand/kboot/hostdisk.c
+++ b/stand/kboot/hostdisk.c
@@ -47,6 +47,7 @@ static int hostdisk_close(struct open_file *f);
 static int hostdisk_ioctl(struct open_file *f, u_long cmd, void *data);
 static int hostdisk_print(int verbose);
 static char *hostdisk_fmtdev(struct devdesc *vdev);
+static bool hostdisk_match(struct devsw *devsw, const char *devspec);
 static int hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path);
 
 struct devsw hostdisk = {
@@ -60,6 +61,7 @@ struct devsw hostdisk = {
 	.dv_print = hostdisk_print,
 	.dv_cleanup = nullsys,
 	.dv_fmtdev = hostdisk_fmtdev,
+	.dv_match = hostdisk_match,
 	.dv_parsedev = hostdisk_parsedev,
 };
 
@@ -247,15 +249,59 @@ hostdisk_one_disk(struct host_dirent64 *dent, void *argp __unused)
 	return (true);
 }
 
-static void
-hostdisk_find_block_devices(void)
+static bool
+hostdisk_fake_one_disk(struct host_dirent64 *dent, void *argp)
 {
+	char *override_dir = argp;
+	char *fn = NULL;
+	hdinfo_t *hd = NULL;
+	struct host_kstat sb;
+
 	/*
-	 * Start here XXX open SYSBLK, walk through all directories, keep the
-	 * ones that return a size and a 'block' device when we 'stat' it. Try
-	 * to avoid partitions and only do raw devices.
+	 * We only do regular files. Each one is treated as a disk image
+	 * accessible via /dev/${dent->d_name}.
 	 */
-	foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
+	if (dent->d_type != HOST_DT_REG && dent->d_type != HOST_DT_LNK)
+		return (true);
+	if (asprintf(&fn, "%s/%s", override_dir, dent->d_name) == -1)
+		return (true);
+	if (host_stat(fn, &sb) != 0)
+		goto err;
+	if (!HOST_S_ISREG(sb.st_mode))
+		return (true);
+	if (sb.st_size == 0)
+		goto err;
+	if ((hd = calloc(1, sizeof(*hd))) == NULL)
+		goto err;
+	hd->hd_dev = fn;
+	hd->hd_size = sb.st_size;
+	hd->hd_sectorsize = 512;	/* XXX configurable? */
+	hd->hd_sectors = hd->hd_size / hd->hd_sectorsize;
+	if (hd->hd_size < HOSTDISK_MIN_SIZE)
+		goto err;
+	hd->hd_flags = 0;
+	STAILQ_INIT(&hd->hd_children);
+	printf("%s: %ju %ju %ju\n",
+	    hd->hd_dev, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
+	STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
+	/* XXX no partiions? -- is that OK? */
+	return (true);
+err:
+	free(hd);
+	free(fn);
+	return (true);
+}
+
+static void
+hostdisk_find_block_devices(void)
+{
+	char *override;
+
+	override=getenv("hostdisk_override");
+	if (override != NULL)
+		foreach_file(override, hostdisk_fake_one_disk, override, 0);
+	else
+		foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
 }
 
 static int
@@ -387,6 +433,23 @@ hostdisk_fmtdev(struct devdesc *vdev)
 	return ((char *)hd_name(dev2hd(vdev)));
 }
 
+static bool
+hostdisk_match(struct devsw *devsw, const char *devspec)
+{
+	hdinfo_t *hd;
+	const char *colon;
+	char *cp;
+
+	colon = strchr(devspec, ':');
+	if (colon == NULL)
+		return false;
+	cp = strdup(devspec);
+	cp[colon - devspec] = '\0';
+	hd = hostdisk_find(cp);
+	free(cp);
+	return (hd != NULL);
+}
+
 static int
 hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
 {
@@ -396,9 +459,6 @@ hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
 	int len;
 	char *fn;
 
-	/* Must start with /dev */
-	if (strncmp(devspec, "/dev", 4) != 0)
-		return (EINVAL);
 	/* Must have a : in it */
 	cp = strchr(devspec, ':');
 	if (cp == NULL)
@@ -412,6 +472,7 @@ hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
 	hd = hostdisk_find(fn);
 	if (hd == NULL) {
 		printf("Can't find hdinfo for %s\n", fn);
+		free(fn);
 		return (EINVAL);
 	}
 	free(fn);
diff --git a/stand/kboot/main.c b/stand/kboot/main.c
index 9a0f8b8baf69..91bc4c06e452 100644
--- a/stand/kboot/main.c
+++ b/stand/kboot/main.c
@@ -192,9 +192,12 @@ main(int argc, const char **argv)
 #if defined(LOADER_ZFS_SUPPORT)
 	if (strcmp(bootdev, "zfs:") == 0) {
 		/*
-		 * Pseudo device that says go find the right ZFS pool.
+		 * Pseudo device that says go find the right ZFS pool. This will be
+		 * the first pool that we find that passes the sanity checks (eg looks
+		 * like it might be vbootable) and sets currdev to the right thing based
+		 * on active BEs, etc
 		 */
-		printf("WARNING: bare 'zfs:' for boot device not yet implemented\n");
+		hostdisk_zfs_find_default();
 	}
 #endif