git: 2f47321e3f96 - stable/13 - bsdinstall: Filter out devices that cannot be opened

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Fri, 05 Jan 2024 00:23:12 UTC
The branch stable/13 has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=2f47321e3f96d80bf4fbfad8c2482b93197e03cb

commit 2f47321e3f96d80bf4fbfad8c2482b93197e03cb
Author:     Brad Davis <brd@FreeBSD.org>
AuthorDate: 2022-05-13 14:33:21 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-01-05 00:18:53 +0000

    bsdinstall: Filter out devices that cannot be opened
    
    Devices that cannot be opened are most likely the install media and
    should not be listed as destinations.
    
    Reviewed by:            allanjude
    Approved by:            allanjude
    Differential Revision:  https://reviews.freebsd.org/D34879
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    
    (cherry picked from commit 964ad27f1e48ec28247142cfe6afdfa1f8bd3182)
---
 usr.sbin/bsdinstall/partedit/part_wizard.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/usr.sbin/bsdinstall/partedit/part_wizard.c b/usr.sbin/bsdinstall/partedit/part_wizard.c
index 90dba58383e2..b790e8b3c4d4 100644
--- a/usr.sbin/bsdinstall/partedit/part_wizard.c
+++ b/usr.sbin/bsdinstall/partedit/part_wizard.c
@@ -131,6 +131,16 @@ boot_disk_select(struct gmesh *mesh)
 					continue;
 				if (strncmp(pp->lg_name, "cd", 2) == 0)
 					continue;
+				/*
+				 * Check if the disk is available to be opened for
+				 * write operations, it helps prevent the USB
+				 * stick used to boot from being listed as an option
+				 */
+				fd = g_open(pp->lg_name, 1);
+				if (fd == -1) {
+					continue;
+				}
+				g_close(fd);
 
 				disks = realloc(disks, (++n)*sizeof(disks[0]));
 				disks[n-1].name = pp->lg_name;