Home | History | Annotate | Line # | Download | only in gpt
resize.c revision 1.16
      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.16 2015/12/01 16:32:19 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 == 0 && alignment > 0) {
    114 		gpt_warnx(gpt, "Could not resize partition with alignment "
    115 		      "constraint");
    116 		return -1;
    117 	} else if (newsize == 0) {
    118 		gpt_warnx(gpt, "Could not resize partition");
    119 		return -1;
    120 	}
    121 
    122 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    123 
    124 	if (gpt_write_primary(gpt) == -1)
    125 		return -1;
    126 
    127 	ent = gpt_ent(gpt->gpt, gpt->lbt, i);
    128 	ent->ent_lba_end = htole64(map->map_start + newsize - 1LL);
    129 
    130 	if (gpt_write_backup(gpt) == -1)
    131 		return -1;
    132 
    133 	gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64 "\n", entry,
    134 	    map->map_start, newsize);
    135 
    136 	return 0;
    137 }
    138 
    139 static int
    140 cmd_resize(gpt_t gpt, int argc, char *argv[])
    141 {
    142 	char *p;
    143 	int ch;
    144 	int64_t human_num;
    145 
    146 	while ((ch = getopt(argc, argv, "a:i:s:")) != -1) {
    147 		switch(ch) {
    148 		case 'a':
    149 			if (alignment > 0)
    150 				return usage();
    151 			if (dehumanize_number(optarg, &human_num) < 0)
    152 				return usage();
    153 			alignment = human_num;
    154 			if (alignment < 1)
    155 				return usage();
    156 			break;
    157 		case 'i':
    158 			if (entry > 0)
    159 				return usage();
    160 			entry = strtoul(optarg, &p, 10);
    161 			if (*p != 0 || entry < 1)
    162 				return usage();
    163 			break;
    164 		case 's':
    165 			if (sectors > 0 || size > 0)
    166 				return usage();
    167 			sectors = strtoll(optarg, &p, 10);
    168 			if (sectors < 1)
    169 				return usage();
    170 			if (*p == '\0')
    171 				break;
    172 			if (*p == 's' || *p == 'S') {
    173 				if (*(p + 1) == '\0')
    174 					break;
    175 				else
    176 					return usage();
    177 			}
    178 			if (*p == 'b' || *p == 'B') {
    179 				if (*(p + 1) == '\0') {
    180 					size = sectors;
    181 					sectors = 0;
    182 					break;
    183 				} else
    184 					return usage();
    185 			}
    186 			if (dehumanize_number(optarg, &human_num) < 0)
    187 				return usage();
    188 			size = human_num;
    189 			sectors = 0;
    190 			break;
    191 		default:
    192 			return usage();
    193 		}
    194 	}
    195 
    196 	if (argc != optind)
    197 		return usage();
    198 
    199 	if (entry == 0)
    200 		return usage();
    201 
    202 	if ((sectors = gpt_check(gpt, alignment, size)) == -1)
    203 		return -1;
    204 
    205 	return resize(gpt);
    206 }
    207