Home | History | Annotate | Line # | Download | only in gpt
resize.c revision 1.18
      1 /*-
      2  * Copyright (c) 2002 Marcel Moolenaar
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #if HAVE_NBTOOL_CONFIG_H
     28 #include "nbtool_config.h"
     29 #endif
     30 
     31 #include <sys/cdefs.h>
     32 #ifdef __FBSDID
     33 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
     34 #endif
     35 #ifdef __RCSID
     36 __RCSID("$NetBSD: resize.c,v 1.18 2015/12/02 20:01:44 christos Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 
     41 #include <err.h>
     42 #include <stddef.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "map.h"
     49 #include "gpt.h"
     50 #include "gpt_private.h"
     51 
     52 static off_t alignment, sectors, size;
     53 static unsigned int entry;
     54 
     55 static int cmd_resize(gpt_t, int, char *[]);
     56 
     57 static const char *resizehelp[] = {
     58     "-i index [-a alignment] [-s size]",
     59 };
     60 
     61 struct gpt_cmd c_resize = {
     62 	"resize",
     63 	cmd_resize,
     64 	resizehelp, __arraycount(resizehelp),
     65 	0,
     66 };
     67 
     68 #define usage() gpt_usage(NULL, &c_resize)
     69 
     70 static int
     71 resize(gpt_t gpt)
     72 {
     73 	map_t map;
     74 	struct gpt_hdr *hdr;
     75 	struct gpt_ent *ent;
     76 	unsigned int i;
     77 	off_t alignsecs, newsize;
     78 
     79 
     80 	if ((hdr = gpt_hdr(gpt)) == NULL)
     81 		return -1;
     82 
     83 	ent = NULL;
     84 
     85 	i = entry - 1;
     86 	ent = gpt_ent_primary(gpt, i);
     87 	if (gpt_uuid_is_nil(ent->ent_type)) {
     88 		gpt_warnx(gpt, "Entry at index %u is unused", entry);
     89 		return -1;
     90 	}
     91 
     92 	alignsecs = alignment / gpt->secsz;
     93 
     94 	for (map = map_first(gpt); map != NULL; map = map->map_next) {
     95 		if (entry == map->map_index)
     96 			break;
     97 	}
     98 	if (map == NULL) {
     99 		gpt_warnx(gpt, "Could not find map entry corresponding "
    100 		    "to index");
    101 		return -1;
    102 	}
    103 
    104 	if (sectors > 0 && sectors == map->map_size)
    105 		if (alignment == 0 ||
    106 		    (alignment > 0 && sectors % alignsecs == 0)) {
    107 			/* nothing to do */
    108 			gpt_warnx(gpt, "partition does not need resizing");
    109 			return 0;
    110 		}
    111 
    112 	newsize = map_resize(gpt, map, sectors, alignsecs);
    113 	if (newsize == -1)
    114 		return -1;
    115 
    116 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    117 
    118 	if (gpt_write_primary(gpt) == -1)
    119 		return -1;
    120 
    121 	ent = gpt_ent(gpt->gpt, gpt->lbt, i);
    122 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    123 
    124 	if (gpt_write_backup(gpt) == -1)
    125 		return -1;
    126 
    127 	gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64 "\n", entry,
    128 	    map->map_start, newsize);
    129 
    130 	return 0;
    131 }
    132 
    133 static int
    134 cmd_resize(gpt_t gpt, int argc, char *argv[])
    135 {
    136 	int ch;
    137 
    138 	while ((ch = getopt(argc, argv, GPT_AIS)) != -1) {
    139 		if (gpt_add_ais(gpt, &alignment, &entry, &size, ch) == -1)
    140 			return usage();
    141 	}
    142 
    143 	if (argc != optind)
    144 		return usage();
    145 
    146 	if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1)
    147 		return -1;
    148 
    149 	return resize(gpt);
    150 }
    151