Index | Thread | Search

From:
Luis Henriques <henrix@camandro.org>
Subject:
Don't detach volumes that have mounted partitions
To:
tech@openbsd.org
Date:
Wed, 06 Mar 2024 21:30:38 +0000

Download raw body.

Thread
Hi!

I've been told I'm an idiot but I like to believe that my idiocy level
isn't much above the average.  Unfortunately, I've done the same stupid
thing more than once, which may indicate that I'm wrong.  And that's why I
think the patch below may help me not loosing data by detaching an
encrypted volume that has partitions mounted.  Which has happen.  Several
times.  (Not say how many.)

Cheers,
-- 
Luís

diff --git sbin/bioctl/bioctl.8 sbin/bioctl/bioctl.8
index a187daf36edd..6dc06b8503d4 100644
--- sbin/bioctl/bioctl.8
+++ sbin/bioctl/bioctl.8
@@ -245,6 +245,9 @@ they become part of the array again.
 .It Fl d
 Detach volume specified by
 .Ar device .
+It will fail if
+.Ar device
+has a mounted partition.
 .It Fl k Ar keydisk
 Use special device
 .Ar keydisk
diff --git sbin/bioctl/bioctl.c sbin/bioctl/bioctl.c
index 2e25d5801d9e..22281e2cce2d 100644
--- sbin/bioctl/bioctl.c
+++ sbin/bioctl/bioctl.c
@@ -31,6 +31,8 @@
 #include <sys/ioctl.h>
 #include <sys/dkio.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/mount.h>
 #include <dev/softraidvar.h>
 #include <dev/biovar.h>
 
@@ -39,6 +41,7 @@
 #include <fcntl.h>
 #include <util.h>
 #include <ctype.h>
+#include <paths.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1080,6 +1083,23 @@ void
 bio_deleteraid(char *dev)
 {
 	struct bioc_deleteraid	bd;
+	struct statfs *fs;
+	int len, n;
+	char *s;
+
+	n = getmntinfo(&fs, MNT_NOWAIT);
+	if (n == 0)
+		errx(1, "getmntinfo");
+	len = sizeof(_PATH_DEV) - 1;
+	while (--n >= 0) {
+		s = fs[n].f_mntfromname;
+		if (strncmp(_PATH_DEV, s, len) == 0)
+			s += len;
+		if (strncmp(dev, s, strlen(dev)) == 0)
+			errx(1, "%s is still mounted on %s",
+			     fs[n].f_mntfromname, fs[n].f_mntonname);
+	}
+
 	memset(&bd, 0, sizeof(bd));
 
 	bd.bd_bio.bio_cookie = bio_cookie;