Module Name:    src
Committed By:   jnemeth
Date:           Tue Nov  3 02:19:24 UTC 2015

Modified Files:
        src/sbin/gpt: Makefile gpt.8 gpt.c gpt.h
Added Files:
        src/sbin/gpt: header.c

Log Message:
Add a "header" subcommand that displays information about the size of
the disk along with information from the GPT header if it exists.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sbin/gpt/Makefile
cvs rdiff -u -r1.36 -r1.37 src/sbin/gpt/gpt.8
cvs rdiff -u -r1.40 -r1.41 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.18 -r1.19 src/sbin/gpt/gpt.h
cvs rdiff -u -r0 -r1.1 src/sbin/gpt/header.c

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

Modified files:

Index: src/sbin/gpt/Makefile
diff -u src/sbin/gpt/Makefile:1.13 src/sbin/gpt/Makefile:1.14
--- src/sbin/gpt/Makefile:1.13	Mon Dec 29 16:27:06 2014
+++ src/sbin/gpt/Makefile	Tue Nov  3 02:19:24 2015
@@ -1,8 +1,8 @@
-# $NetBSD: Makefile,v 1.13 2014/12/29 16:27:06 christos Exp $
+# $NetBSD: Makefile,v 1.14 2015/11/03 02:19:24 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 \
+SRCS=	add.c biosboot.c create.c destroy.c gpt.c header.c label.c map.c \
 	migrate.c recover.c remove.c resize.c resizedisk.c \
 	set.c show.c type.c unset.c gpt_uuid.c
 MAN=	gpt.8

Index: src/sbin/gpt/gpt.8
diff -u src/sbin/gpt/gpt.8:1.36 src/sbin/gpt/gpt.8:1.37
--- src/sbin/gpt/gpt.8:1.36	Sat Dec  6 21:53:55 2014
+++ src/sbin/gpt/gpt.8	Tue Nov  3 02:19:24 2015
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.36 2014/12/06 21:53:55 wiz Exp $
+.\" $NetBSD: gpt.8,v 1.37 2015/11/03 02:19:24 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 6, 2014
+.Dd November 2, 2015
 .Dt GPT 8
 .Os
 .Sh NAME
@@ -271,6 +271,12 @@ The
 option instructs
 .Nm
 to destroy the table in a way that it can be recovered.
+.\" ==== header ====
+.It Nm Ic header Ar device ...
+The
+.Ic header
+command displays size information about the media and information from the
+GPT header if it exists.
 .\" ==== label ====
 .It Nm Ic label Oo Fl a Oc Ao Fl f Ar file | Fl l Ar label Ac Ar device ...
 .It Nm Ic label Oo Fl b Ar blocknr Oc Oo Fl i Ar index Oc \

Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.40 src/sbin/gpt/gpt.c:1.41
--- src/sbin/gpt/gpt.c:1.40	Mon Dec 29 16:27:06 2014
+++ src/sbin/gpt/gpt.c	Tue Nov  3 02:19:24 2015
@@ -35,7 +35,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.40 2014/12/29 16:27:06 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.41 2015/11/03 02:19:24 jnemeth Exp $");
 #endif
 
 #include <sys/param.h>
@@ -553,6 +553,7 @@ static struct {
 	{ cmd_biosboot, "biosboot" },
 	{ cmd_create, "create" },
 	{ cmd_destroy, "destroy" },
+	{ cmd_header, "header" },
 	{ NULL, "help" },
 	{ cmd_label, "label" },
 	{ cmd_migrate, "migrate" },
@@ -576,10 +577,10 @@ __dead static void
 usage(void)
 {
 	extern const char addmsg1[], addmsg2[], biosbootmsg[];
-	extern const char createmsg[], destroymsg[], labelmsg1[], labelmsg2[];
-	extern const char labelmsg3[], migratemsg[], recovermsg[], removemsg1[];
-	extern const char removemsg2[], resizemsg[], resizediskmsg[];
-	extern const char setmsg[], showmsg[], typemsg1[];
+	extern const char createmsg[], destroymsg[], headermsg[], labelmsg1[];
+	extern const char labelmsg2[], labelmsg3[], migratemsg[], recovermsg[];
+	extern const char removemsg1[], removemsg2[], resizemsg[];
+	extern const char resizediskmsg[], setmsg[], showmsg[], typemsg1[];
 	extern const char typemsg2[], typemsg3[], unsetmsg[];
 #ifndef HAVE_NBTOOL_CONFIG_H
 	extern const char backupmsg[], restoremsg[];
@@ -615,13 +616,14 @@ usage(void)
 	    "       %s\n"
 	    "       %s\n"
 	    "       %s\n"
+	    "       %s\n"
 	    "       %s\n",
 	    addmsg1, addmsg2,
 #ifndef HAVE_NBTOOL_CONFIG_H
 	    backupmsg,
 #endif
 	    biosbootmsg, createmsg, destroymsg,
-	    labelmsg1, labelmsg2, labelmsg3,
+	    headermsg, labelmsg1, labelmsg2, labelmsg3,
 	    migratemsg, recovermsg,
 	    removemsg1, removemsg2,
 	    resizemsg, resizediskmsg,

Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.18 src/sbin/gpt/gpt.h:1.19
--- src/sbin/gpt/gpt.h:1.18	Mon Dec 29 16:27:06 2014
+++ src/sbin/gpt/gpt.h	Tue Nov  3 02:19:24 2015
@@ -82,6 +82,7 @@ int	cmd_backup(int, char *[]);
 int	cmd_biosboot(int, char *[]);
 int	cmd_create(int, char *[]);
 int	cmd_destroy(int, char *[]);
+int	cmd_header(int, char *[]);
 int	cmd_label(int, char *[]);
 int	cmd_migrate(int, char *[]);
 int	cmd_recover(int, char *[]);

Added files:

Index: src/sbin/gpt/header.c
diff -u /dev/null src/sbin/gpt/header.c:1.1
--- /dev/null	Tue Nov  3 02:19:24 2015
+++ src/sbin/gpt/header.c	Tue Nov  3 02:19:24 2015
@@ -0,0 +1,145 @@
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by John Nemeth
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION OR CONTRIBUTORS
+ * 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.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/cdefs.h>
+#ifdef __RCSID
+__RCSID("$NetBSD: header.c,v 1.1 2015/11/03 02:19:24 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 "map.h"
+#include "gpt.h"
+
+const char headermsg[] = "header device ...";
+
+__dead static void
+usage_header(void)
+{
+
+	fprintf(stderr,
+	    "usage: %s %s\n", getprogname(), headermsg);
+	exit(1);
+}
+
+static void
+header(void)
+{
+	map_t *gpt;
+	struct gpt_hdr *hdr;
+	char buf[128];
+#ifdef HN_AUTOSCALE
+	char human_num[5];
+#endif
+
+#ifdef HN_AUTOSCALE
+	if (humanize_number(human_num, 5, mediasz,
+	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
+		human_num[0] = '\0';
+	if (human_num[0] != '\0')
+		printf("Media Size: %llu (%s)\n", (long long unsigned)mediasz,
+		    human_num);
+	else
+#endif
+		printf("Media Size: %llu\n", (long long unsigned)mediasz);
+
+	printf("Sector Size: %u\n", secsz);
+
+#ifdef HN_AUTOSCALE
+	if (humanize_number(human_num, 5, mediasz / secsz,
+	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
+		human_num[0] = '\0';
+	if (human_num[0] != '\0')
+		printf("Number of Sectors: %llu (%s)\n",
+		    (long long unsigned)(mediasz / secsz), human_num);
+	else
+#endif
+		printf("Number of Sectors: %llu\n",
+		    (long long unsigned)(mediasz / secsz));
+
+	printf("\nHeader Information:\n");
+
+	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
+	if (gpt == NULL) {
+		printf("- GPT Header not found\n");
+		return;
+	}
+
+	hdr = gpt->map_data;
+	printf("- GPT Revision: %u\n", hdr->hdr_revision);
+	printf("- First Data Sector: %llu\n",
+	    (long long unsigned)hdr->hdr_lba_start);
+#ifdef HN_AUTOSCALE
+	if (humanize_number(human_num, 5, hdr->hdr_lba_end,
+	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
+		human_num[0] = '\0';
+	if (human_num[0] != '\0')
+		printf("- Last Data Sector: %llu (%s)\n",
+		    (long long unsigned)hdr->hdr_lba_end, human_num);
+	else
+#endif
+	printf("- Last Data Sector: %llu\n",
+	    (long long unsigned)hdr->hdr_lba_end);
+	gpt_uuid_snprintf(buf, sizeof(buf), "%d", hdr->hdr_guid);
+	printf("- Media GUID: %s\n", buf);
+	printf("- Number of GPT Entries: %u\n", hdr->hdr_entries);
+}
+
+int
+cmd_header(int argc, char *argv[])
+{
+	int fd;
+
+	if (argc == optind)
+		usage_header();
+
+	while (optind < argc) {
+		fd = gpt_open(argv[optind++]);
+		if (fd == -1) {
+			warn("unable to open device '%s'", device_name);
+			continue;
+		}
+		header();
+
+		gpt_close(fd);
+	}
+
+	return (0);
+}

Reply via email to