Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/gpt Add two new subcommands, "set" and "unset". These ...



details:   https://anonhg.NetBSD.org/src/rev/cce0e3ef12f3
branches:  trunk
changeset: 325151:cce0e3ef12f3
user:      jnemeth <jnemeth%NetBSD.org@localhost>
date:      Mon Dec 09 08:03:17 2013 +0000

description:
Add two new subcommands, "set" and "unset".  These were inspired by
FreeBSD's gpart(8), but the code is all mine.  The purpose of these
is to set and unset partition attributes.

diffstat:

 sbin/gpt/Makefile |    4 +-
 sbin/gpt/gpt.8    |   62 +++++++++++++++++-
 sbin/gpt/gpt.c    |   13 ++-
 sbin/gpt/gpt.h    |    2 +
 sbin/gpt/set.c    |  188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sbin/gpt/unset.c  |  188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 450 insertions(+), 7 deletions(-)

diffs (truncated from 553 to 300 lines):

diff -r 23ab29424b79 -r cce0e3ef12f3 sbin/gpt/Makefile
--- a/sbin/gpt/Makefile Mon Dec 09 01:35:02 2013 +0000
+++ b/sbin/gpt/Makefile Mon Dec 09 08:03:17 2013 +0000
@@ -1,9 +1,9 @@
-# $NetBSD: Makefile,v 1.5 2013/11/20 08:08:47 jnemeth Exp $
+# $NetBSD: Makefile,v 1.6 2013/12/09 08:03:17 jnemeth Exp $
 # $FreeBSD: src/sbin/gpt/Makefile,v 1.7 2005/09/01 02:49:20 marcel Exp $
 
 PROG=  gpt
 SRCS=  add.c biosboot.c create.c destroy.c gpt.c label.c map.c migrate.c \
-       recover.c remove.c resize.c show.c
+       recover.c remove.c resize.c set.c show.c unset.c
 MAN=   gpt.8
 
 LDADD+=        -lprop -lutil
diff -r 23ab29424b79 -r cce0e3ef12f3 sbin/gpt/gpt.8
--- a/sbin/gpt/gpt.8    Mon Dec 09 01:35:02 2013 +0000
+++ b/sbin/gpt/gpt.8    Mon Dec 09 08:03:17 2013 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.24 2013/12/09 01:35:02 jnemeth Exp $
+.\" $NetBSD: gpt.8,v 1.25 2013/12/09 08:03:17 jnemeth Exp $
 .\"
 .\" Copyright (c) 2002 Marcel Moolenaar
 .\" All rights reserved.
@@ -26,7 +26,7 @@
 .\"
 .\" $FreeBSD: src/sbin/gpt/gpt.8,v 1.17 2006/06/22 22:22:32 marcel Exp $
 .\"
-.Dd December 8, 2013
+.Dd December 9, 2013
 .Dt GPT 8
 .Os
 .Sh NAME
@@ -344,6 +344,35 @@
 .Fl a
 option is specified then the size will be adjusted to be a multiple of
 alignment if possible.
+.\" ==== set ====
+.It Nm Ic set Fl a Ar attribute Fl i Ar index Ar device ...
+The
+.Ic set
+command sets various partition attributes.
+The
+.Fl a
+option specifies which attributes to set and may be specified more then once.
+The
+.Fl i
+option specifies which entry to update.
+The possible attributes are
+.Do biosboot Dc ,
+.Do bootme Dc ,
+.Do bootonce Dc , and
+.Do bootfailed Dc .
+The biosboot flag is used to indicate which partition should be booted
+by legacy BIOS boot code.
+See the
+.Ic biosboot
+command for more information.
+The other three attributes are for compatibility with
+.Fx
+and are not currently used by any
+.Nx
+code.
+They may be used by
+.Nx
+code in the future.
 .\" ==== show ====
 .It Nm Ic show Oo Fl glu Oc Oo Fl i Ar index Oc Ar device ...
 The
@@ -372,6 +401,35 @@
 .Fl l ,
 .Fl g ,
 .Fl u .
+.\" ==== unset ====
+.It Nm Ic unset Fl a Ar attribute Fl i Ar index Ar device ...
+The
+.Ic unset
+command unsets various partition attributes.
+The
+.Fl a
+option specifies which attributes to unset and may be specified more then once.
+The
+.Fl i
+option specifies which entry to update.
+The possible attributes are
+.Do biosboot Dc ,
+.Do bootme Dc ,
+.Do bootonce Dc , and
+.Do bootfailed Dc .
+The biosboot flag is used to indicate which partition should be booted
+by legacy BIOS boot code.
+See the
+.Ic biosboot
+command for more information.
+The other three attributes are for compatibility with
+.Fx
+and are not currently used by any
+.Nx
+code.
+They may be used by
+.Nx
+code in the future.
 .El
 .Sh EXAMPLES
 .Bd -literal
diff -r 23ab29424b79 -r cce0e3ef12f3 sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c    Mon Dec 09 01:35:02 2013 +0000
+++ b/sbin/gpt/gpt.c    Mon Dec 09 08:03:17 2013 +0000
@@ -31,7 +31,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.25 2013/12/04 20:15:51 jakllsch Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.26 2013/12/09 08:03:17 jnemeth Exp $");
 #endif
 
 #include <sys/param.h>
@@ -727,7 +727,9 @@
        { cmd_remove, "remove" },
        { NULL, "rename" },
        { cmd_resize, "resize" },
+       { cmd_set, "set" },
        { cmd_show, "show" },
+       { cmd_unset, "unset" },
        { NULL, "verify" },
        { NULL, NULL }
 };
@@ -738,7 +740,8 @@
        extern const char addmsg1[], addmsg2[], biosbootmsg[], createmsg[];
        extern const char destroymsg[], labelmsg1[], labelmsg2[], labelmsg3[];
        extern const char migratemsg[], recovermsg[], removemsg1[];
-       extern const char removemsg2[], resizemsg[], showmsg[];
+       extern const char removemsg2[], resizemsg[], setmsg[], showmsg[];
+       extern const char unsetmsg[];
 
        fprintf(stderr,
            "usage: %s %s\n"
@@ -754,6 +757,8 @@
            "       %s %s\n"
            "       %s %s\n"
            "       %s %s\n"
+           "       %s %s\n"
+           "       %s %s\n"
            "       %s %s\n",
            getprogname(), addmsg1,
            getprogname(), addmsg2,
@@ -768,7 +773,9 @@
            getprogname(), removemsg1,
            getprogname(), removemsg2,
            getprogname(), resizemsg,
-           getprogname(), showmsg);
+           getprogname(), setmsg,
+           getprogname(), showmsg,
+           getprogname(), unsetmsg);
        exit(1);
 }
 
diff -r 23ab29424b79 -r cce0e3ef12f3 sbin/gpt/gpt.h
--- a/sbin/gpt/gpt.h    Mon Dec 09 01:35:02 2013 +0000
+++ b/sbin/gpt/gpt.h    Mon Dec 09 08:03:17 2013 +0000
@@ -88,6 +88,8 @@
 int    cmd_recover(int, char *[]);
 int    cmd_remove(int, char *[]);
 int    cmd_resize(int, char *[]);
+int    cmd_set(int, char *[]);
 int    cmd_show(int, char *[]);
+int    cmd_unset(int, char *[]);
 
 #endif /* _GPT_H_ */
diff -r 23ab29424b79 -r cce0e3ef12f3 sbin/gpt/set.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sbin/gpt/set.c    Mon Dec 09 08:03:17 2013 +0000
@@ -0,0 +1,188 @@
+/*-
+ * Copyright (c) 2002 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifdef __FBSDID
+__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
+#endif
+#ifdef __RCSID
+__RCSID("$NetBSD: set.c,v 1.1 2013/12/09 08:03:17 jnemeth Exp $");
+#endif
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#include "map.h"
+#include "gpt.h"
+
+static unsigned int entry;
+static uint64_t attributes;
+
+const char setmsg[] = "set -a attribute -i index device ...";
+
+__dead static void
+usage_set(void)
+{
+
+       fprintf(stderr,
+           "usage: %s %s\n", getprogname(), setmsg);
+       exit(1);
+}
+
+static void
+set(int fd)
+{
+       uuid_t uuid;
+       map_t *gpt, *tpg;
+       map_t *tbl, *lbt;
+       struct gpt_hdr *hdr;
+       struct gpt_ent *ent;
+       unsigned int i;
+       
+
+       gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
+       ent = NULL;
+       if (gpt == NULL) {
+               warnx("%s: error: no primary GPT header; run create or recover",
+                   device_name);
+               return;
+       }
+
+       tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
+       if (tpg == NULL) {
+               warnx("%s: error: no secondary GPT header; run recover",
+                   device_name);
+               return;
+       }
+
+       tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
+       lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
+       if (tbl == NULL || lbt == NULL) {
+               warnx("%s: error: run recover -- trust me", device_name);
+               return;
+       }
+
+       hdr = gpt->map_data;
+       if (entry > le32toh(hdr->hdr_entries)) {
+               warnx("%s: error: index %u out of range (%u max)", device_name,
+                   entry, le32toh(hdr->hdr_entries));
+               return;
+       }
+
+       i = entry - 1;
+       ent = (void*)((char*)tbl->map_data + i *
+           le32toh(hdr->hdr_entsz));
+       le_uuid_dec(ent->ent_type, &uuid);
+       if (uuid_is_nil(&uuid, NULL)) {
+               warnx("%s: error: entry at index %u is unused",
+                   device_name, entry);
+               return;
+       }
+
+       ent->ent_attr |= attributes;
+
+       hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
+           le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
+       hdr->hdr_crc_self = 0;
+       hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
+
+       gpt_write(fd, gpt);
+       gpt_write(fd, tbl);
+
+       hdr = tpg->map_data;
+       ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
+       ent->ent_attr |= attributes;
+
+       hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
+           le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));



Home | Main Index | Thread Index | Old Index