Exit gracefully when auto-expanding a partition and it is already the

correct size. Add a -q flag to "resize" and "resizedisk" commands to skip
printing warnings in the already resize paths.
This commit is contained in:
jmcneill 2020-05-24 14:42:44 +00:00
parent 2cf653d8b7
commit 084befafcb
4 changed files with 53 additions and 20 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: gpt.8,v 1.70 2019/07/26 07:22:05 martin Exp $
.\" $NetBSD: gpt.8,v 1.71 2020/05/24 14:42:44 jmcneill 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 July 26, 2019
.Dd May 24, 2020
.Dt GPT 8
.Os
.Sh NAME
@ -470,7 +470,7 @@ Partitions are removed by clearing the partition type.
No other information is changed.
.\" ==== resize ====
.It Nm Ic resize Oo Fl i Ar index Oc Oo Fl b Ar startsec Oc Oo Fl a Ar alignment Oc \
Oo Fl s Ar size Oc
Oo Fl s Ar size Oc Oo Fl q Oc
The
.Ic resize
command allows the user to resize a partition.
@ -500,8 +500,12 @@ If the
.Fl a
option is specified then the size will be adjusted to be a multiple of
alignment if possible.
If the
.Fl q
option is specified then the utility will not print output when a
resize is not required.
.\" ==== resizedisk ====
.It Nm Ic resizedisk Oo Fl s Ar size Oc
.It Nm Ic resizedisk Oo Fl s Ar size Oc Oo Fl q Oc
The
.Ic resizedisk
command allows the user to resize a disk.
@ -537,6 +541,10 @@ Using the
.Fl s
option allows you to move the backup copy prior to resizing the medium.
This is primarily useful when shrinking the medium.
If the
.Fl q
option is specified then the utility will not print output when a
resize is not required.
.\" ==== restore ====
.It Nm Ic restore Oo Fl F Oc Oo Fl i Ar infile Oc
The

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/map.c,v 1.6 2005/08/31 01:47:19 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: map.c,v 1.14 2018/04/11 07:14:23 mrg Exp $");
__RCSID("$NetBSD: map.c,v 1.15 2020/05/24 14:42:44 jmcneill Exp $");
#endif
#include <sys/types.h>
@ -280,7 +280,10 @@ map_resize(gpt_t gpt, map_t m, off_t size, off_t alignment)
prevsize = m->map_size;
size = ((m->map_size + n->map_size) / alignment)
* alignment;
if (size <= prevsize) {
if (size == prevsize) {
m->map_size = size;
return size;
} else if (size < prevsize) {
gpt_warnx(gpt, "Can't coalesce %ju <= %ju",
(uintmax_t)prevsize, (uintmax_t)size);
return -1;

View File

@ -33,12 +33,13 @@
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: resize.c,v 1.24 2019/03/24 13:31:00 martin Exp $");
__RCSID("$NetBSD: resize.c,v 1.25 2020/05/24 14:42:44 jmcneill Exp $");
#endif
#include <sys/types.h>
#include <err.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@ -52,7 +53,7 @@ __RCSID("$NetBSD: resize.c,v 1.24 2019/03/24 13:31:00 martin Exp $");
static int cmd_resize(gpt_t, int, char *[]);
static const char *resizehelp[] = {
"[-i index | -b blocknr] [-a alignment] [-s size]",
"[-i index | -b blocknr] [-a alignment] [-s size] [-q]",
};
struct gpt_cmd c_resize = {
@ -65,13 +66,13 @@ struct gpt_cmd c_resize = {
#define usage() gpt_usage(NULL, &c_resize)
static int
resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size)
resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size, bool quiet)
{
map_t map;
struct gpt_hdr *hdr;
struct gpt_ent *ent;
unsigned int i;
off_t alignsecs, newsize;
off_t alignsecs, newsize, oldsize;
uint64_t end;
@ -101,14 +102,25 @@ resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size)
if (alignment == 0 ||
(alignment > 0 && sectors % alignsecs == 0)) {
/* nothing to do */
gpt_warnx(gpt, "partition does not need resizing");
if (!quiet)
gpt_warnx(gpt,
"partition does not need resizing");
return 0;
}
oldsize = map->map_size;
newsize = map_resize(gpt, map, sectors, alignsecs);
if (newsize == -1)
return -1;
if (oldsize == newsize) {
/* Nothing to do */
if (!quiet)
gpt_warnx(gpt,
"partition does not need resizing");
return 0;
}
end = htole64((uint64_t)(map->map_start + newsize - 1LL));
ent->ent_lba_end = end;
@ -134,10 +146,13 @@ cmd_resize(gpt_t gpt, int argc, char *argv[])
off_t alignment = 0, sectors, start = 0, size = 0;
unsigned int entry = 0;
map_t m;
bool quiet = false;
while ((ch = getopt(argc, argv, GPT_AIS "b:")) != -1) {
while ((ch = getopt(argc, argv, GPT_AIS "b:q")) != -1) {
if (ch == 'b')
gpt_human_get(gpt, &start);
else if (ch == 'q')
quiet = true;
else if (gpt_add_ais(gpt, &alignment, &entry, &size, ch) == -1)
return usage();
}
@ -160,5 +175,5 @@ cmd_resize(gpt_t gpt, int argc, char *argv[])
if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1)
return -1;
return resize(gpt, entry, alignment, sectors, size);
return resize(gpt, entry, alignment, sectors, size, quiet);
}

View File

@ -33,13 +33,14 @@
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: resizedisk.c,v 1.17 2015/12/04 21:39:18 christos Exp $");
__RCSID("$NetBSD: resizedisk.c,v 1.18 2020/05/24 14:42:44 jmcneill Exp $");
#endif
#include <sys/bootblock.h>
#include <sys/types.h>
#include <err.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@ -54,7 +55,7 @@ __RCSID("$NetBSD: resizedisk.c,v 1.17 2015/12/04 21:39:18 christos Exp $");
static int cmd_resizedisk(gpt_t, int, char *[]);
static const char *resizediskhelp[] = {
"[-s size]",
"[-s size] [-q]",
};
struct gpt_cmd c_resizedisk = {
@ -78,7 +79,7 @@ struct gpt_cmd c_resizedisk = {
* - when shrinking, verify that table fits
*/
static int
resizedisk(gpt_t gpt, off_t sector, off_t size)
resizedisk(gpt_t gpt, off_t sector, off_t size, bool quiet)
{
map_t mbrmap;
struct gpt_hdr *hdr;
@ -144,12 +145,14 @@ resizedisk(gpt_t gpt, off_t sector, off_t size)
gpt_size = gpt->tbl->map_size;
if (sector == oldloc) {
gpt_warnx(gpt, "Device is already the specified size");
if (!quiet)
gpt_warnx(gpt, "Device is already the specified size");
return 0;
}
if (sector == 0 && last == oldloc) {
gpt_warnx(gpt, "Device hasn't changed size");
if (!quiet)
gpt_warnx(gpt, "Device hasn't changed size");
return 0;
}
@ -253,13 +256,17 @@ cmd_resizedisk(gpt_t gpt, int argc, char *argv[])
{
int ch;
off_t sector, size = gpt->mediasz;
bool quiet = false;
while ((ch = getopt(argc, argv, "s:")) != -1) {
while ((ch = getopt(argc, argv, "s:q")) != -1) {
switch(ch) {
case 's':
if (gpt_add_ais(gpt, NULL, NULL, &size, ch) == -1)
return -1;
break;
case 'q':
quiet = true;
break;
default:
return usage();
}
@ -276,5 +283,5 @@ cmd_resizedisk(gpt_t gpt, int argc, char *argv[])
return -1;
}
return resizedisk(gpt, sector, size);
return resizedisk(gpt, sector, size, quiet);
}