Source-Changes-HG archive

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

[src/trunk]: src/sbin/gpt Add a resize command. This command was inspired by...



details:   https://anonhg.NetBSD.org/src/rev/55433a9f68da
branches:  trunk
changeset: 791474:55433a9f68da
user:      jnemeth <jnemeth%NetBSD.org@localhost>
date:      Wed Nov 20 08:08:47 2013 +0000

description:
Add a resize command.  This command was inspired by FreeBSD's gpart(8),
but the code was written by myself.

diffstat:

 sbin/gpt/Makefile |    4 +-
 sbin/gpt/gpt.8    |   20 ++++-
 sbin/gpt/gpt.c    |    7 +-
 sbin/gpt/gpt.h    |    1 +
 sbin/gpt/map.c    |   92 ++++++++++++++++++++-
 sbin/gpt/map.h    |    1 +
 sbin/gpt/resize.c |  241 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 359 insertions(+), 7 deletions(-)

diffs (truncated from 477 to 300 lines):

diff -r 4fabc2efb070 -r 55433a9f68da sbin/gpt/Makefile
--- a/sbin/gpt/Makefile Wed Nov 20 07:18:23 2013 +0000
+++ b/sbin/gpt/Makefile Wed Nov 20 08:08:47 2013 +0000
@@ -1,9 +1,9 @@
-# $NetBSD: Makefile,v 1.4 2011/01/06 01:08:48 jakllsch Exp $
+# $NetBSD: Makefile,v 1.5 2013/11/20 08:08:47 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 show.c
+       recover.c remove.c resize.c show.c
 MAN=   gpt.8
 
 LDADD+=        -lprop -lutil
diff -r 4fabc2efb070 -r 55433a9f68da sbin/gpt/gpt.8
--- a/sbin/gpt/gpt.8    Wed Nov 20 07:18:23 2013 +0000
+++ b/sbin/gpt/gpt.8    Wed Nov 20 08:08:47 2013 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.16 2013/11/19 05:07:40 jnemeth Exp $
+.\" $NetBSD: gpt.8,v 1.17 2013/11/20 08:08:47 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 November 18, 2013
+.Dd November 20, 2013
 .Dt GPT 8
 .Os
 .Sh NAME
@@ -316,6 +316,22 @@
 See above for a description of these options.
 Partitions are removed by clearing the partition type.
 No other information is changed.
+.\" ==== resize ====
+.It Nm Ic resize Fl i Ar index Oo Fl a Ar alignment Oc \
+Oo Fl s Ar count Oc Ar device ...
+The
+.Ic resize
+command allows the user to resize a partition.
+The partition may be shrunked and if there is sufficient free space
+immediately after it then it may be expanded.
+The
+.Fl s
+option allows the new size to be specified, otherwise the partition will
+be increased to the maximum available size.
+If the
+.Fl a
+option is specified then the size will be adjusted to be a multiple of
+alignment if possible.
 .\" ==== show ====
 .It Nm Ic show Oo Fl lu Oc Ar device ...
 The
diff -r 4fabc2efb070 -r 55433a9f68da sbin/gpt/gpt.c
--- a/sbin/gpt/gpt.c    Wed Nov 20 07:18:23 2013 +0000
+++ b/sbin/gpt/gpt.c    Wed Nov 20 08:08:47 2013 +0000
@@ -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.21 2013/11/19 05:03:41 jnemeth Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.22 2013/11/20 08:08:47 jnemeth Exp $");
 #endif
 
 #include <sys/param.h>
@@ -776,6 +776,7 @@
        { cmd_recover, "recover" },
        { cmd_remove, "remove" },
        { NULL, "rename" },
+       { cmd_resize, "resize" },
        { cmd_show, "show" },
        { NULL, "verify" },
        { NULL, NULL }
@@ -787,7 +788,7 @@
        extern const char addmsg1[], addmsg2[], biosbootmsg[], createmsg[];
        extern const char destroymsg[], labelmsg1[], labelmsg2[], labelmsg3[];
        extern const char migratemsg[], recovermsg[], removemsg1[];
-       extern const char removemsg2[], showmsg[];
+       extern const char removemsg2[], resizemsg[], showmsg[];
 
        fprintf(stderr,
            "usage: %s %s\n"
@@ -802,6 +803,7 @@
            "       %s %s\n"
            "       %s %s\n"
            "       %s %s\n"
+           "       %s %s\n"
            "       %s %s\n",
            getprogname(), addmsg1,
            getprogname(), addmsg2,
@@ -815,6 +817,7 @@
            getprogname(), recovermsg,
            getprogname(), removemsg1,
            getprogname(), removemsg2,
+           getprogname(), resizemsg,
            getprogname(), showmsg);
        exit(1);
 }
diff -r 4fabc2efb070 -r 55433a9f68da sbin/gpt/gpt.h
--- a/sbin/gpt/gpt.h    Wed Nov 20 07:18:23 2013 +0000
+++ b/sbin/gpt/gpt.h    Wed Nov 20 08:08:47 2013 +0000
@@ -102,6 +102,7 @@
 int    cmd_migrate(int, char *[]);
 int    cmd_recover(int, char *[]);
 int    cmd_remove(int, char *[]);
+int    cmd_resize(int, char *[]);
 int    cmd_show(int, char *[]);
 
 #endif /* _GPT_H_ */
diff -r 4fabc2efb070 -r 55433a9f68da sbin/gpt/map.c
--- a/sbin/gpt/map.c    Wed Nov 20 07:18:23 2013 +0000
+++ b/sbin/gpt/map.c    Wed Nov 20 08:08:47 2013 +0000
@@ -29,7 +29,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.4 2013/11/19 05:03:41 jnemeth Exp $");
+__RCSID("$NetBSD: map.c,v 1.5 2013/11/20 08:08:47 jnemeth Exp $");
 #endif
 
 #include <sys/types.h>
@@ -186,6 +186,96 @@
        return NULL;
 }
 
+off_t
+map_resize(map_t *m, off_t size, off_t alignment)
+{
+       map_t *n, *o;
+       off_t alignsize, prevsize;
+
+       n = m->map_next;
+
+       if (size == 0 && alignment == 0) {
+               if (n == NULL || n->map_type != MAP_TYPE_UNUSED)
+                       return 0;
+               else {
+                       size = m->map_size + n->map_size;
+                       m->map_size = size;
+                       m->map_next = n->map_next;
+                       if (n->map_next != NULL)
+                               n->map_next->map_prev = m;
+                       if (n->map_data != NULL)
+                               free(n->map_data);
+                       free(n);
+                       return size;
+               }
+       }
+
+       if (size == 0 && alignment > 0) {
+               if (n == NULL || n->map_type != MAP_TYPE_UNUSED)
+                       return 0;
+               else {
+                       prevsize = m->map_size;
+                       size = (m->map_size + n->map_size) /
+                              alignment * alignment;
+                       if (size <= prevsize)
+                               return 0;
+                       m->map_size = size;
+                       n->map_start += size - prevsize;
+                       n->map_size -= size - prevsize;
+                       if (n->map_size == 0) {
+                               m->map_next = n->map_next;
+                               if (n->map_next != NULL)
+                                       n->map_next->map_prev = m;
+                               if (n->map_data != NULL)
+                                       free(n->map_data);
+                               free(n);
+                       }
+                       return size;
+               }
+       }
+                       
+       alignsize = size;
+       if (alignment % size != 0)
+               alignsize = (size + alignment) / alignment * alignment;
+
+       if (alignsize < m->map_size) {          /* shrinking */
+               prevsize = m->map_size;
+               m->map_size = alignsize;
+               if (n == NULL || n->map_type != MAP_TYPE_UNUSED) {
+                       o = mkmap(m->map_start + alignsize,
+                                 prevsize - alignsize, MAP_TYPE_UNUSED);
+                       m->map_next = o;
+                       o->map_prev = m;
+                       o->map_next = n;
+                       if (n != NULL)
+                               n->map_prev = o;
+                       return alignsize;
+               } else {
+                       n->map_start -= alignsize;
+                       n->map_size += alignsize;
+                       return alignsize;
+               }
+       } else if (alignsize > m->map_size) {           /* expanding */
+               if (n == NULL || n->map_type != MAP_TYPE_UNUSED ||
+                   n->map_size < alignsize - m->map_size) {
+                       return 0;
+               }
+               n->map_size -= alignsize - m->map_size;
+               n->map_start += alignsize - m->map_size;
+               if (n->map_size == 0) {
+                       m->map_next = n->map_next;
+                       if (n->map_next != NULL)
+                               n->map_next->map_prev = m;
+                       if (n->map_data != NULL)
+                               free(n->map_data);
+                       free(n);
+               }
+               m->map_size = alignsize;
+               return alignsize;
+       } else                                          /* correct size */
+               return alignsize;
+}
+
 map_t *
 map_find(int type)
 {
diff -r 4fabc2efb070 -r 55433a9f68da sbin/gpt/map.h
--- a/sbin/gpt/map.h    Wed Nov 20 07:18:23 2013 +0000
+++ b/sbin/gpt/map.h    Wed Nov 20 08:08:47 2013 +0000
@@ -55,6 +55,7 @@
 map_t *map_find(int);
 map_t *map_first(void);
 map_t *map_last(void);
+off_t map_resize(map_t *, off_t, off_t);
 
 off_t map_free(off_t, off_t);
 
diff -r 4fabc2efb070 -r 55433a9f68da sbin/gpt/resize.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sbin/gpt/resize.c Wed Nov 20 08:08:47 2013 +0000
@@ -0,0 +1,241 @@
+/*-
+ * 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/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
+#endif
+#ifdef __RCSID
+__RCSID("$NetBSD: resize.c,v 1.1 2013/11/20 08:08:47 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 <inttypes.h>
+
+#include "map.h"
+#include "gpt.h"
+
+static off_t alignment, size;
+static unsigned int entry;
+
+const char resizemsg[] = "resize -i index [-a alignment] [-s lba] device ...";
+
+__dead static void
+usage_resize(void)
+{
+
+       fprintf(stderr,
+           "usage: %s %s\n", getprogname(), resizemsg);
+       exit(1);
+}
+
+static void
+resize(int fd)
+{



Home | Main Index | Thread Index | Old Index