Home | History | Annotate | Line # | Download | only in gpt
resize.c revision 1.22
      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.22 2015/12/04 16:54:28 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 int cmd_resize(gpt_t, int, char *[]);
     53 
     54 static const char *resizehelp[] = {
     55 	"-i index [-a alignment] [-s size]",
     56 };
     57 
     58 struct gpt_cmd c_resize = {
     59 	"resize",
     60 	cmd_resize,
     61 	resizehelp, __arraycount(resizehelp),
     62 	0,
     63 };
     64 
     65 #define usage() gpt_usage(NULL, &c_resize)
     66 
     67 static int
     68 resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size)
     69 {
     70 	map_t map;
     71 	struct gpt_hdr *hdr;
     72 	struct gpt_ent *ent;
     73 	unsigned int i;
     74 	off_t alignsecs, newsize;
     75 	uint64_t end;
     76 
     77 
     78 	if ((hdr = gpt_hdr(gpt)) == NULL)
     79 		return -1;
     80 
     81 	i = entry - 1;
     82 	ent = gpt_ent_primary(gpt, i);
     83 	if (gpt_uuid_is_nil(ent->ent_type)) {
     84 		gpt_warnx(gpt, "Entry at index %u is unused", entry);
     85 		return -1;
     86 	}
     87 
     88 	alignsecs = alignment / gpt->secsz;
     89 
     90 	for (map = map_first(gpt); map != NULL; map = map->map_next) {
     91 		if (entry == map->map_index)
     92 			break;
     93 	}
     94 	if (map == NULL) {
     95 		gpt_warnx(gpt, "Could not find map entry corresponding "
     96 		    "to index");
     97 		return -1;
     98 	}
     99 
    100 	if (sectors > 0 && sectors == map->map_size)
    101 		if (alignment == 0 ||
    102 		    (alignment > 0 && sectors % alignsecs == 0)) {
    103 			/* nothing to do */
    104 			gpt_warnx(gpt, "partition does not need resizing");
    105 			return 0;
    106 		}
    107 
    108 	newsize = map_resize(gpt, map, sectors, alignsecs);
    109 	if (newsize == -1)
    110 		return -1;
    111 
    112 	end = htole64((uint64_t)(map->map_start + newsize - 1LL));
    113 	ent->ent_lba_end = end;
    114 
    115 	if (gpt_write_primary(gpt) == -1)
    116 		return -1;
    117 
    118 	ent = gpt_ent(gpt->gpt, gpt->lbt, i);
    119 	ent->ent_lba_end = end;
    120 
    121 	if (gpt_write_backup(gpt) == -1)
    122 		return -1;
    123 
    124 	gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64, entry,
    125 	    map->map_start, newsize);
    126 
    127 	return 0;
    128 }
    129 
    130 static int
    131 cmd_resize(gpt_t gpt, int argc, char *argv[])
    132 {
    133 	int ch;
    134 	off_t alignment = 0, sectors, size = 0;
    135 	unsigned int entry = 0;
    136 
    137 	while ((ch = getopt(argc, argv, GPT_AIS)) != -1) {
    138 		if (gpt_add_ais(gpt, &alignment, &entry, &size, ch) == -1)
    139 			return usage();
    140 	}
    141 
    142 	if (argc != optind)
    143 		return usage();
    144 
    145 	if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1)
    146 		return -1;
    147 
    148 	return resize(gpt, entry, alignment, sectors, size);
    149 }
    150