From 463390984eb0aed0f709cffa963f0c0c58a8ecf4 Mon Sep 17 00:00:00 2001 From: jsing Date: Sat, 18 Jan 2014 02:47:27 +0000 Subject: [PATCH] Add a -r flag that allows for the mount point of the root filesystem to be specified. This is primarily for use by the installer and defaults to /. --- usr.sbin/installboot/Makefile | 4 +-- usr.sbin/installboot/installboot.8 | 10 +++++-- usr.sbin/installboot/installboot.c | 21 ++++++++++++--- usr.sbin/installboot/installboot.h | 5 +++- usr.sbin/installboot/util.c | 43 ++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 usr.sbin/installboot/util.c diff --git a/usr.sbin/installboot/Makefile b/usr.sbin/installboot/Makefile index c455c9ee4c2..5cf07fc7d27 100644 --- a/usr.sbin/installboot/Makefile +++ b/usr.sbin/installboot/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.9 2014/01/18 02:45:38 jsing Exp $ +# $OpenBSD: Makefile,v 1.10 2014/01/18 02:47:27 jsing Exp $ PROG= installboot -SRCS= installboot.c +SRCS= installboot.c util.c MAN= installboot.8 CPPFLAGS= -I${.CURDIR} diff --git a/usr.sbin/installboot/installboot.8 b/usr.sbin/installboot/installboot.8 index 676b74ee439..0f1a362024d 100644 --- a/usr.sbin/installboot/installboot.8 +++ b/usr.sbin/installboot/installboot.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: installboot.8,v 1.1 2014/01/05 16:02:40 jsing Exp $ +.\" $OpenBSD: installboot.8,v 1.2 2014/01/18 02:47:27 jsing Exp $ .\" .\" Copyright (c) 2013, 2014 Joel Sing .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: January 5 2014 $ +.Dd $Mdocdate: January 18 2014 $ .Dt INSTALLBOOT 8 .Os .Sh NAME @@ -23,6 +23,7 @@ .Sh SYNOPSIS .Nm installboot .Op Fl nv +.Op Fl r Ar root .Ar disk .Op Ar stage1 Op Ar stage2 .Sh DESCRIPTION @@ -40,6 +41,11 @@ The options are as follows: Perform a dry run - do not actually write any bootstrap to the disk. .It Fl v Increase verbosity during operation. +.It Fl r Ar root +Specify the mount point of the +.Ar root +filesystem to operate on, defaulting to +.Ar / . .It Ar disk Specify the .Ar disk diff --git a/usr.sbin/installboot/installboot.c b/usr.sbin/installboot/installboot.c index 48148da9464..30b7d5a17af 100644 --- a/usr.sbin/installboot/installboot.c +++ b/usr.sbin/installboot/installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.c,v 1.1 2013/12/27 13:52:40 jsing Exp $ */ +/* $OpenBSD: installboot.c,v 1.2 2014/01/18 02:47:27 jsing Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,7 @@ int nowrite; int stages; int verbose; +char *root = "/"; char *stage1; char *stage2; @@ -37,7 +39,7 @@ usage(void) { extern char *__progname; - fprintf(stderr, "usage: %s [-nv] disk [stage1%s]\n", + fprintf(stderr, "usage: %s [-nv] [-r root] disk [stage1%s]\n", __progname, (stages >= 2) ? " [stage2]" : ""); exit(1); @@ -52,11 +54,16 @@ main(int argc, char **argv) md_init(); - while ((opt = getopt(argc, argv, "nv")) != -1) { + while ((opt = getopt(argc, argv, "nr:v")) != -1) { switch (opt) { case 'n': nowrite = 1; break; + case 'r': + root = strdup(optarg); + if (root == NULL) + err(1, "strdup"); + break; case 'v': verbose = 1; break; @@ -77,6 +84,14 @@ main(int argc, char **argv) if (argc > 2) stage2 = argv[2]; + /* Prefix stages with root. */ + if (verbose) + fprintf(stderr, "Using %s as root\n", root); + if (stage1 != NULL) + stage1 = fileprefix(root, stage1); + if (stage2 != NULL) + stage2 = fileprefix(root, stage2); + if ((devfd = opendev(dev, (nowrite ? O_RDONLY : O_RDWR), OPENDEV_PART, &realdev)) < 0) err(1, "open: %s", realdev); diff --git a/usr.sbin/installboot/installboot.h b/usr.sbin/installboot/installboot.h index 1b0a79efe48..69967e8c754 100644 --- a/usr.sbin/installboot/installboot.h +++ b/usr.sbin/installboot/installboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.h,v 1.2 2013/12/28 11:26:57 jsing Exp $ */ +/* $OpenBSD: installboot.h,v 1.3 2014/01/18 02:47:27 jsing Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing * @@ -19,6 +19,7 @@ extern int nowrite; extern int stages; extern int verbose; +extern char *root; extern char *stage1; extern char *stage2; @@ -26,6 +27,8 @@ extern char *stage2; void bootstrap(int, char *, char *); #endif +char *fileprefix(const char *, const char *); + void md_init(void); void md_loadboot(void); void md_installboot(int, char *); diff --git a/usr.sbin/installboot/util.c b/usr.sbin/installboot/util.c new file mode 100644 index 00000000000..77cdc048180 --- /dev/null +++ b/usr.sbin/installboot/util.c @@ -0,0 +1,43 @@ +/* $OpenBSD: util.c,v 1.1 2014/01/18 02:47:27 jsing Exp $ */ + +/* + * Copyright (c) 2014 Joel Sing + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "installboot.h" + +char * +fileprefix(const char *base, const char *path) +{ + char *r, *s; + int n; + + if ((s = malloc(PATH_MAX)) == NULL) + err(1, "malloc"); + n = snprintf(s, PATH_MAX, "%s/%s", base, path); + if (n < 1 || n >= PATH_MAX) + err(1, "snprintf"); + if ((r = realpath(s, NULL)) == NULL) + err(1, "realpath"); + free(s); + + return r; +} -- 2.20.1