Source-Changes-HG archive

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

[src/trunk]: src/sbin/gpt Exit gracefully when auto-expanding a partition and...



details:   https://anonhg.NetBSD.org/src/rev/0c36e84fd8f1
branches:  trunk
changeset: 933310:0c36e84fd8f1
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sun May 24 14:42:44 2020 +0000

description:
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.

diffstat:

 sbin/gpt/gpt.8        |  16 ++++++++++++----
 sbin/gpt/map.c        |   7 +++++--
 sbin/gpt/resize.c     |  29 ++++++++++++++++++++++-------
 sbin/gpt/resizedisk.c |  21 ++++++++++++++-------
 4 files changed, 53 insertions(+), 20 deletions(-)

diffs (248 lines):

diff -r 9371b7c90194 -r 0c36e84fd8f1 sbin/gpt/gpt.8
--- a/sbin/gpt/gpt.8    Sun May 24 14:41:26 2020 +0000
+++ b/sbin/gpt/gpt.8    Sun May 24 14:42:44 2020 +0000
@@ -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 @@
 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 @@
 .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 @@
 .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
diff -r 9371b7c90194 -r 0c36e84fd8f1 sbin/gpt/map.c
--- a/sbin/gpt/map.c    Sun May 24 14:41:26 2020 +0000
+++ b/sbin/gpt/map.c    Sun May 24 14:42:44 2020 +0000
@@ -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 @@
                        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;
diff -r 9371b7c90194 -r 0c36e84fd8f1 sbin/gpt/resize.c
--- a/sbin/gpt/resize.c Sun May 24 14:41:26 2020 +0000
+++ b/sbin/gpt/resize.c Sun May 24 14:42:44 2020 +0000
@@ -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 @@
 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 @@
 #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 @@
                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 @@
        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 @@
        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);
 }
diff -r 9371b7c90194 -r 0c36e84fd8f1 sbin/gpt/resizedisk.c
--- a/sbin/gpt/resizedisk.c     Sun May 24 14:41:26 2020 +0000
+++ b/sbin/gpt/resizedisk.c     Sun May 24 14:42:44 2020 +0000
@@ -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 @@
 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 @@
  * - 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 @@
 
        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 @@
 {
        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 @@
                return -1;
        }
 
-       return resizedisk(gpt, sector, size);
+       return resizedisk(gpt, sector, size, quiet);
 }



Home | Main Index | Thread Index | Old Index