Module Name:    src
Committed By:   martin
Date:           Wed Nov 14 02:30:00 UTC 2018

Modified Files:
        src/usr.sbin/sysinst: defs.h disks.c

Log Message:
Slightly enhance previous: create two utility functions checking for
partitionability and boot code requirements, use a generic match helper
function for both and fully check the device name.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/usr.sbin/sysinst/defs.h
cvs rdiff -u -r1.26 -r1.27 src/usr.sbin/sysinst/disks.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/sysinst/defs.h
diff -u src/usr.sbin/sysinst/defs.h:1.24 src/usr.sbin/sysinst/defs.h:1.25
--- src/usr.sbin/sysinst/defs.h:1.24	Sun Nov 11 10:06:09 2018
+++ src/usr.sbin/sysinst/defs.h	Wed Nov 14 02:30:00 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: defs.h,v 1.24 2018/11/11 10:06:09 martin Exp $	*/
+/*	$NetBSD: defs.h,v 1.25 2018/11/14 02:30:00 martin Exp $	*/
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -483,6 +483,8 @@ bool	get_default_cdrom(char *, size_t);
 int	find_disks(const char *);
 bool enumerate_disks(void *state,bool (*func)(void *state, const char *dev));
 bool is_cdrom_device(const char *dev, bool as_target);
+bool is_bootable_device(const char *dev);
+bool is_partitionable_device(const char *dev);
 
 struct menudesc;
 void	fmt_fspart(struct menudesc *, int, void *);

Index: src/usr.sbin/sysinst/disks.c
diff -u src/usr.sbin/sysinst/disks.c:1.26 src/usr.sbin/sysinst/disks.c:1.27
--- src/usr.sbin/sysinst/disks.c:1.26	Tue Nov 13 17:22:04 2018
+++ src/usr.sbin/sysinst/disks.c	Wed Nov 14 02:30:00 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: disks.c,v 1.26 2018/11/13 17:22:04 bouyer Exp $ */
+/*	$NetBSD: disks.c,v 1.27 2018/11/14 02:30:00 martin Exp $ */
 
 /*
  * Copyright 1997 Piermont Information Systems Inc.
@@ -546,6 +546,53 @@ is_cdrom_device(const char *dev, bool as
 	return false;
 }
 
+/* does this device match any entry in the driver list? */
+static bool
+dev_in_list(const char *dev, const char **list)
+{
+
+	for ( ; *list; list++) {
+
+		size_t len = strlen(*list);
+
+		/* start of name matches? */
+		if (strncmp(dev, *list, len) == 0) {
+			char *endp;
+			int e;
+
+			/* remainder of name is a decimal number? */
+			strtou(dev+len, &endp, 10, 0, INT_MAX, &e);
+			if (endp && *endp == 0 && e == 0)
+				return true;
+		}
+	}
+
+	return false;
+}
+
+bool
+is_bootable_device(const char *dev)
+{
+	static const char *non_bootable_devs[] = {
+		"raid",	/* bootcode lives outside of raid */
+		"xbd",	/* xen virtual device, can not boot from that */
+		NULL
+	};
+
+	return !dev_in_list(dev, non_bootable_devs);
+}
+
+bool
+is_partitionable_device(const char *dev)
+{
+	static const char *non_partitionable_devs[] = {
+		"dk",	/* this is alreay a partioned slice */
+		NULL
+	};
+
+	return !dev_in_list(dev, non_partitionable_devs);
+}
+
 /*
  * Multi-purpose helper function:
  * iterate all known disks, invoke a callback for each.
@@ -604,27 +651,14 @@ get_disks_helper(void *arg, const char *
 	if (is_cdrom_device(dev, true))
 		return true;
 
+	memset(state->dd, 0, sizeof(*state->dd));
 	strlcpy(state->dd->dd_name, dev, sizeof state->dd->dd_name - 2);
-	state->dd->dd_no_mbr = false;
-	state->dd->dd_no_part = false;
+	state->dd->dd_no_mbr = !is_bootable_device(dev);
+	state->dd->dd_no_part = !is_partitionable_device(dev);
 
-	if (strncmp(dev, "dk", 2) == 0) {
-		char *endp;
-		int e;
-
-		/* if this device is dkNNNN, no partitioning is possible */
-		strtou(dev+2, &endp, 10, 0, INT_MAX, &e);
-		if (endp && *endp == 0 && e == 0)
-			state->dd->dd_no_part = true;
-	}
 	if (state->dd->dd_no_part && !state->with_non_partitionable)
 		return true;
 
-	if (strncmp(dev, "xbd", 3) == 0 || strncmp(dev, "raid", 4) == 0) {
-		/* if this device is xbd or raid, don't set up an MBR */
-		state->dd->dd_no_mbr = true;
-	}
-
 	if (!get_geom(state->dd->dd_name, &l)) {
 		if (errno == ENOENT)
 			return true;

Reply via email to