From 279da39389d49983cad1503ecc6f9494d2cd81c6 Mon Sep 17 00:00:00 2001 From: christos Date: Sat, 4 Oct 2014 01:00:42 +0000 Subject: [PATCH] Allow specifying sector and media size on the command line. --- sbin/gpt/gpt.8 | 17 ++++++- sbin/gpt/gpt.c | 132 +++++++++++++++++++++++++++---------------------- 2 files changed, 89 insertions(+), 60 deletions(-) diff --git a/sbin/gpt/gpt.8 b/sbin/gpt/gpt.8 index 2cb7f0dfc59e..b18c218db385 100644 --- a/sbin/gpt/gpt.8 +++ b/sbin/gpt/gpt.8 @@ -1,4 +1,4 @@ -.\" $NetBSD: gpt.8,v 1.32 2014/09/29 05:56:43 jnemeth Exp $ +.\" $NetBSD: gpt.8,v 1.33 2014/10/04 01:00:42 christos 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 September 29, 2014 +.Dd October 3, 2014 .Dt GPT 8 .Os .Sh NAME @@ -68,6 +68,12 @@ Not all commands use all default settings, so some general options may not have an effect on all commands. .Pp The +.Fl m Ar mediasize +option overrides the default media size for the device (obtained +from the kernel if possible) or defaulting to the file size for +plain files. +.Pp +The .Fl p Ar partitions option allows the user to change the number of partitions the GPT can accommodate. @@ -86,6 +92,13 @@ Currently this option is primarily useful for the command, but the intent is to use it to implement dry-run behaviour. .Pp +The +.Fl s Ar sectorsize +option overrides the default sector size for the device (obtained +from the kernel if possible) or +.Dv 512 +for plain files. +.Pp The .Fl v option controls the verbosity level. diff --git a/sbin/gpt/gpt.c b/sbin/gpt/gpt.c index 3cbbe1289bb6..fc70005f9375 100644 --- a/sbin/gpt/gpt.c +++ b/sbin/gpt/gpt.c @@ -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.35 2014/10/02 19:15:21 joerg Exp $"); +__RCSID("$NetBSD: gpt.c,v 1.36 2014/10/04 01:00:42 christos Exp $"); #endif #include @@ -375,6 +375,9 @@ drvctl(const char *name, u_int *sector_size, off_t *media_size) int dfd, res; char *dname, *p; + if (*sector_size && *media_size) + return 0; + if ((dfd = open("/dev/drvctl", O_RDONLY)) == -1) { warn("%s: /dev/drvctl", __func__); return -1; @@ -434,13 +437,15 @@ drvctl(const char *name, u_int *sector_size, off_t *media_size) if (number == NULL) goto out; - *sector_size = prop_number_integer_value(number); + if (*sector_size == 0) + *sector_size = prop_number_integer_value(number); number = prop_dictionary_get(geometry, "sectors-per-unit"); if (number == NULL) goto out; - *media_size = prop_number_integer_value(number) * *sector_size; + if (*media_size == 0) + *media_size = prop_number_integer_value(number) * *sector_size; return 0; out: @@ -572,21 +577,26 @@ gpt_open(const char *dev) if ((sb.st_mode & S_IFMT) != S_IFREG) { #ifdef DIOCGSECTORSIZE - if (ioctl(fd, DIOCGSECTORSIZE, &secsz) == -1 || - ioctl(fd, DIOCGMEDIASIZE, &mediasz) == -1) + if ((secsz == 0 && ioctl(fd, DIOCGSECTORSIZE, &secsz) == -1) || + (mediasz == 0 && ioctl(fd, DIOCGMEDIASIZE, &mediasz) == -1)) goto close; #endif #ifndef HAVE_NBTOOL_CONFIG_H if (drvctl(device_name, &secsz, &mediasz) == -1) goto close; #endif + if (secsz == 0 || mediasz == 0) + errx(1, "Please specify sector/media size"); } else { - secsz = 512; /* Fixed size for files. */ - if (sb.st_size % secsz) { - errno = EINVAL; - goto close; + if (secsz == 0) + secsz = 512; /* Fixed size for files. */ + if (mediasz == 0) { + if (sb.st_size % secsz) { + errno = EINVAL; + goto close; + } + mediasz = sb.st_size; } - mediasz = sb.st_size; } /* @@ -670,61 +680,53 @@ usage(void) #ifndef HAVE_NBTOOL_CONFIG_H extern const char backupmsg[], restoremsg[]; #endif - + const char *p = getprogname(); + const char *f = + "[-rv] [-m ] [-p ] [-s ]"; fprintf(stderr, - "usage: %s %s\n" - " %s %s\n" + "Usage: %s %s []\n", p, f); + fprintf(stderr, + "Commands:\n" #ifndef HAVE_NBTOOL_CONFIG_H - " %s %s\n" + " %s\n" + " %s\n" #endif - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %*s %s\n" - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %s %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n" + " %s\n", + addmsg1, addmsg2, #ifndef HAVE_NBTOOL_CONFIG_H - " %s %s\n" + backupmsg, #endif - " %s %s\n" - " %s %s\n" - " %s %s\n" - " %*s %s\n" - " %s %s\n", - getprogname(), addmsg1, - getprogname(), addmsg2, + biosbootmsg, createmsg, destroymsg, + labelmsg1, labelmsg2, labelmsg3, + migratemsg, recovermsg, + removemsg1, removemsg2, + resizemsg, resizediskmsg, #ifndef HAVE_NBTOOL_CONFIG_H - getprogname(), backupmsg, + restoremsg, #endif - getprogname(), biosbootmsg, - getprogname(), createmsg, - getprogname(), destroymsg, - getprogname(), labelmsg1, - getprogname(), labelmsg2, - (int)strlen(getprogname()), "", labelmsg3, - getprogname(), migratemsg, - getprogname(), recovermsg, - getprogname(), removemsg1, - getprogname(), removemsg2, - getprogname(), resizemsg, - getprogname(), resizediskmsg, -#ifndef HAVE_NBTOOL_CONFIG_H - getprogname(), restoremsg, -#endif - getprogname(), setmsg, - getprogname(), showmsg, - getprogname(), typemsg1, - getprogname(), typemsg2, - (int)strlen(getprogname()), "", typemsg3, - getprogname(), unsetmsg); + setmsg, showmsg, + typemsg1, typemsg2, typemsg3, + unsetmsg); exit(1); } @@ -751,8 +753,15 @@ main(int argc, char *argv[]) int ch, i; /* Get the generic options */ - while ((ch = getopt(argc, argv, "p:rv")) != -1) { + while ((ch = getopt(argc, argv, "m:p:rs:v")) != -1) { switch(ch) { + case 'm': + if (mediasz > 0) + usage(); + mediasz = strtoul(optarg, &p, 10); + if (*p != 0 || mediasz < 1) + usage(); + break; case 'p': if (parts > 0) usage(); @@ -763,6 +772,13 @@ main(int argc, char *argv[]) case 'r': readonly = 1; break; + case 's': + if (secsz > 0) + usage(); + secsz = strtoul(optarg, &p, 10); + if (*p != 0 || secsz < 1) + usage(); + break; case 'v': verbose++; break;