Module Name:    src
Committed By:   jnemeth
Date:           Thu Dec 19 06:46:51 UTC 2013

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

Log Message:
Add the backup subcommand.  It dumps the contents of the partition
tables as a plist, which is readable by the restore subcommand.

XXX restore subcommand forthcoming


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sbin/gpt/Makefile
cvs rdiff -u -r0 -r1.1 src/sbin/gpt/backup.c
cvs rdiff -u -r1.26 -r1.27 src/sbin/gpt/gpt.8 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.9 -r1.10 src/sbin/gpt/gpt.h

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.6 src/sbin/gpt/Makefile:1.7
--- src/sbin/gpt/Makefile:1.6	Mon Dec  9 08:03:17 2013
+++ src/sbin/gpt/Makefile	Thu Dec 19 06:46:51 2013
@@ -1,9 +1,9 @@
-# $NetBSD: Makefile,v 1.6 2013/12/09 08:03:17 jnemeth Exp $
+# $NetBSD: Makefile,v 1.7 2013/12/19 06:46:51 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 set.c show.c unset.c
+SRCS=	add.c backup.c biosboot.c create.c destroy.c gpt.c label.c map.c \
+	migrate.c recover.c remove.c resize.c set.c show.c unset.c
 MAN=	gpt.8
 
 LDADD+=	-lprop -lutil

Index: src/sbin/gpt/gpt.8
diff -u src/sbin/gpt/gpt.8:1.26 src/sbin/gpt/gpt.8:1.27
--- src/sbin/gpt/gpt.8:1.26	Mon Dec  9 09:22:44 2013
+++ src/sbin/gpt/gpt.8	Thu Dec 19 06:46:51 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.26 2013/12/09 09:22:44 wiz Exp $
+.\" $NetBSD: gpt.8,v 1.27 2013/12/19 06:46:51 jnemeth Exp $
 .\"
 .\" Copyright (c) 2002 Marcel Moolenaar
 .\" All rights reserved.
@@ -151,6 +151,16 @@ accepts
 and
 .Cm windows
 as aliases for the most commonly used partition types.
+.\" ==== backup ====
+.It Nm Ic backup Ar device ...
+The
+.Ic backup
+command dumps the MBR or (PMBR) and GPT partition tables to standard
+output in a format to be used by the
+.Ic restore
+command.
+The format is a plist.
+It should not be modified.
 .\" ==== biosboot ====
 .It Nm Ic biosboot Oo Fl c Ar bootcode Oc Oo Fl i Ar index Oc Ar device ...
 The
Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.26 src/sbin/gpt/gpt.c:1.27
--- src/sbin/gpt/gpt.c:1.26	Mon Dec  9 08:03:17 2013
+++ src/sbin/gpt/gpt.c	Thu Dec 19 06:46:51 2013
@@ -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.26 2013/12/09 08:03:17 jnemeth Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.27 2013/12/19 06:46:51 jnemeth Exp $");
 #endif
 
 #include <sys/param.h>
@@ -717,6 +717,7 @@ static struct {
 	const char *name;
 } cmdsw[] = {
 	{ cmd_add, "add" },
+	{ cmd_backup, "backup" },
 	{ cmd_biosboot, "biosboot" },
 	{ cmd_create, "create" },
 	{ cmd_destroy, "destroy" },
@@ -737,9 +738,9 @@ static struct {
 __dead static void
 usage(void)
 {
-	extern const char addmsg1[], addmsg2[], biosbootmsg[], createmsg[];
-	extern const char destroymsg[], labelmsg1[], labelmsg2[], labelmsg3[];
-	extern const char migratemsg[], recovermsg[], removemsg1[];
+	extern const char addmsg1[], addmsg2[], backupmsg[], biosbootmsg[];
+	extern const char createmsg[], destroymsg[], labelmsg1[], labelmsg2[];
+	extern const char labelmsg3[], migratemsg[], recovermsg[], removemsg1[];
 	extern const char removemsg2[], resizemsg[], setmsg[], showmsg[];
 	extern const char unsetmsg[];
 
@@ -751,6 +752,7 @@ usage(void)
 	    "       %s %s\n"
 	    "       %s %s\n"
 	    "       %s %s\n"
+	    "       %s %s\n"
 	    "       %*s %s\n"
 	    "       %s %s\n"
 	    "       %s %s\n"
@@ -762,6 +764,7 @@ usage(void)
 	    "       %s %s\n",
 	    getprogname(), addmsg1,
 	    getprogname(), addmsg2,
+	    getprogname(), backupmsg,
 	    getprogname(), biosbootmsg,
 	    getprogname(), createmsg,
 	    getprogname(), destroymsg,

Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.9 src/sbin/gpt/gpt.h:1.10
--- src/sbin/gpt/gpt.h:1.9	Mon Dec  9 08:03:17 2013
+++ src/sbin/gpt/gpt.h	Thu Dec 19 06:46:51 2013
@@ -80,6 +80,7 @@ uint8_t *utf16_to_utf8(uint16_t *);
 void	utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
 
 int	cmd_add(int, char *[]);
+int	cmd_backup(int, char *[]);
 int	cmd_biosboot(int, char *[]);
 int	cmd_create(int, char *[]);
 int	cmd_destroy(int, char *[]);

Added files:

Index: src/sbin/gpt/backup.c
diff -u /dev/null src/sbin/gpt/backup.c:1.1
--- /dev/null	Thu Dec 19 06:46:51 2013
+++ src/sbin/gpt/backup.c	Thu Dec 19 06:46:51 2013
@@ -0,0 +1,308 @@
+/*-
+ * 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/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
+#endif
+#ifdef __RCSID
+__RCSID("$NetBSD: backup.c,v 1.1 2013/12/19 06:46:51 jnemeth Exp $");
+#endif
+
+#include <sys/bootblock.h>
+#include <sys/types.h>
+
+#include <err.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <prop/proplib.h>
+
+#include "map.h"
+#include "gpt.h"
+
+
+const char backupmsg[] = "backup device ...";
+
+__dead static void
+usage_backup(void)
+{
+
+	fprintf(stderr,
+	    "usage: %s %s\n", getprogname(), backupmsg);
+	exit(1);
+}
+
+#define PROP_ERR(x)	if (!(x)) {		\
+		warn("proplib failure");	\
+		return;				\
+	}
+
+static void
+backup(void)
+{
+	uuid_t u;
+	map_t *m;
+	struct mbr *mbr;
+	struct gpt_ent *ent;
+	struct gpt_hdr *hdr;
+	unsigned int i;
+	prop_dictionary_t props, mbr_dict, gpt_dict, type_dict;
+	prop_array_t mbr_array, gpt_array;
+	prop_data_t propdata;
+	prop_number_t propnum;
+	prop_string_t propstr;
+	char *propext, *s;
+	bool rc;
+
+	props = prop_dictionary_create();
+	PROP_ERR(props);
+	m = map_first();
+	while (m != NULL) {
+		switch (m->map_type) {
+		case MAP_TYPE_MBR:
+		case MAP_TYPE_PMBR:
+			type_dict = prop_dictionary_create();
+			PROP_ERR(type_dict);
+			mbr = m->map_data;
+			propdata = prop_data_create_data_nocopy(mbr->mbr_code,
+			    sizeof(mbr->mbr_code));
+			PROP_ERR(propdata);
+			rc = prop_dictionary_set(type_dict, "code", propdata);
+			PROP_ERR(rc);
+			mbr_array = NULL;
+			for (i = 0; i < 4; i++) {
+				if (mbr->mbr_part[i].part_typ !=
+				    MBR_PTYPE_UNUSED) {
+					mbr_dict = prop_dictionary_create();
+					PROP_ERR(mbr_dict);
+					propnum = prop_number_create_integer(i);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "index", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_flag);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "flag", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_shd);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "start_head", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ssect);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "start_sector", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_scyl);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "start_cylinder", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_typ);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "type", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_ehd);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "end_head", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(mbr->mbr_part[i].part_esect);
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "end_cylinder", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_start_lo));
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "lba_start_low", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_start_hi));
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "lba_start_high", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_size_lo));
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "lba_size_low", propnum);
+					PROP_ERR(rc);
+					propnum = prop_number_create_unsigned_integer(le16toh(mbr->mbr_part[i].part_size_hi));
+					PROP_ERR(propnum);
+					rc = prop_dictionary_set(mbr_dict,
+					    "lba_size_high", propnum);
+					if (mbr_array == NULL) {
+						mbr_array = prop_array_create();
+						PROP_ERR(mbr_array);
+					}
+					rc = prop_array_add(mbr_array,
+					    mbr_dict);
+					PROP_ERR(rc);
+				}
+			}
+			if (mbr_array != NULL) {
+				rc = prop_dictionary_set(type_dict,
+				    "mbr_array", mbr_array);
+				PROP_ERR(rc);
+				prop_object_release(mbr_array);
+			}
+			rc = prop_dictionary_set(props, "MBR", type_dict);
+			PROP_ERR(rc);
+			prop_object_release(type_dict);
+			break;
+		case MAP_TYPE_PRI_GPT_HDR:
+			type_dict = prop_dictionary_create();
+			PROP_ERR(type_dict);
+			hdr = m->map_data;
+			propnum = prop_number_create_unsigned_integer(le32toh(hdr->hdr_revision));
+			PROP_ERR(propnum);
+			rc = prop_dictionary_set(type_dict, "revision",
+			    propnum);
+			PROP_ERR(rc);
+			le_uuid_dec(hdr->hdr_guid, &u);
+			uuid_to_string(&u, &s, NULL);
+			propstr = prop_string_create_cstring(s);
+			free(s);
+			PROP_ERR(propstr);
+			rc = prop_dictionary_set(type_dict, "guid", propstr);
+			PROP_ERR(rc);
+			propnum = prop_number_create_integer(le32toh(hdr->hdr_entries));
+			PROP_ERR(propnum);
+			rc = prop_dictionary_set(type_dict, "entries", propnum);
+			PROP_ERR(rc);
+			rc = prop_dictionary_set(props, "GPT_HDR", type_dict);
+			PROP_ERR(rc);
+			prop_object_release(type_dict);
+			break;
+		case MAP_TYPE_PRI_GPT_TBL:
+			type_dict = prop_dictionary_create();
+			PROP_ERR(type_dict);
+			ent = m->map_data;
+			gpt_array = NULL;
+			for (i = 1, ent = m->map_data;
+			    (char *)ent < (char *)(m->map_data) +
+			    m->map_size * secsz; i++, ent++) {
+				if (uuid_is_nil((uuid_t *)&ent->ent_type, NULL))
+					continue;
+				gpt_dict = prop_dictionary_create();
+				PROP_ERR(gpt_dict);
+				propnum = prop_number_create_integer(i);
+				PROP_ERR(propnum);
+				rc = prop_dictionary_set(gpt_dict, "index",
+				    propnum);
+				PROP_ERR(propnum);
+				le_uuid_dec(ent->ent_type, &u);
+				uuid_to_string(&u, &s, NULL);
+				propstr = prop_string_create_cstring(s);
+				free(s);
+				PROP_ERR(propstr);
+				rc = prop_dictionary_set(gpt_dict, "type",
+				    propstr);
+				le_uuid_dec(ent->ent_guid, &u);
+				uuid_to_string(&u, &s, NULL);
+				propstr = prop_string_create_cstring(s);
+				free(s);
+				PROP_ERR(propstr);
+				rc = prop_dictionary_set(gpt_dict, "guid",
+				    propstr);
+				PROP_ERR(propstr);
+				propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_lba_start));
+				PROP_ERR(propnum);
+				rc = prop_dictionary_set(gpt_dict, "start",
+				    propnum);
+				PROP_ERR(rc);
+				propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_lba_end));
+				PROP_ERR(rc);
+				rc = prop_dictionary_set(gpt_dict, "end",
+				    propnum);
+				PROP_ERR(rc);
+				propnum = prop_number_create_unsigned_integer(le64toh(ent->ent_attr));
+				PROP_ERR(propnum);
+				rc = prop_dictionary_set(gpt_dict,
+				    "attributes", propnum);
+				PROP_ERR(rc);
+				s = (char *)utf16_to_utf8(ent->ent_name);
+				if (*s != '\0') {
+					propstr = prop_string_create_cstring(s);
+					PROP_ERR(propstr);
+					rc = prop_dictionary_set(gpt_dict,
+					    "name", propstr);
+					PROP_ERR(rc);
+				}
+				if (gpt_array == NULL) {
+					gpt_array = prop_array_create();
+					PROP_ERR(gpt_array);
+				}
+				rc = prop_array_add(gpt_array, gpt_dict);
+				PROP_ERR(rc);
+			}
+			if (gpt_array != NULL) {
+				rc = prop_dictionary_set(type_dict,
+				    "gpt_array", gpt_array);
+				PROP_ERR(rc);
+				prop_object_release(gpt_array);
+			}
+			rc = prop_dictionary_set(props, "GPT_TBL", type_dict);
+			PROP_ERR(rc);
+			prop_object_release(type_dict);
+			break;
+		}
+		m = m->map_next;
+	}
+	propext = prop_dictionary_externalize(props);
+	PROP_ERR(propext);
+	prop_object_release(props);
+	fputs(propext, stdout);
+}
+
+int
+cmd_backup(int argc, char *argv[])
+{
+	int fd;
+
+	if (argc == optind)
+		usage_backup();
+
+	while (optind < argc) {
+		fd = gpt_open(argv[optind++]);
+		if (fd == -1) {
+			warn("unable to open device '%s'", device_name);
+			continue;
+		}
+
+		backup();
+
+		gpt_close(fd);
+	}
+
+	return (0);
+}

Reply via email to