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.27 2025/12/17 15:56:06 nia Exp $"); 37 #endif 38 39 #include <sys/types.h> 40 #if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H) 41 #include <sys/endian.h> 42 #endif 43 44 #include <err.h> 45 #include <stdbool.h> 46 #include <stddef.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #include "map.h" 53 #include "gpt.h" 54 #include "gpt_private.h" 55 56 static int cmd_resize(gpt_t, int, char *[]); 57 58 static const char *resizehelp[] = { 59 "[-i index | -b blocknr] [-a alignment] [-s size] [-q]", 60 }; 61 62 const struct gpt_cmd c_resize = { 63 "resize", 64 cmd_resize, 65 resizehelp, __arraycount(resizehelp), 66 GPT_SYNC, 67 }; 68 69 #define usage() gpt_usage(NULL, &c_resize) 70 71 static int 72 resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, 73 off_t size __unused, bool quiet) 74 { 75 map_t map; 76 struct gpt_ent *ent; 77 unsigned int i; 78 off_t alignsecs, newsize, oldsize; 79 uint64_t end; 80 81 82 if (gpt_hdr(gpt) == NULL) 83 return -1; 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 if (!quiet) 109 gpt_warnx(gpt, 110 "partition does not need resizing"); 111 return 0; 112 } 113 114 oldsize = map->map_size; 115 newsize = map_resize(gpt, map, sectors, alignsecs); 116 if (newsize == -1) 117 return -1; 118 119 if (oldsize == newsize) { 120 /* Nothing to do */ 121 if (!quiet) 122 gpt_warnx(gpt, 123 "partition does not need resizing"); 124 return 0; 125 } 126 127 end = htole64((uint64_t)(map->map_start + newsize - 1LL)); 128 ent->ent_lba_end = end; 129 130 if (gpt_write_primary(gpt) == -1) 131 return -1; 132 133 ent = gpt_ent(gpt->gpt, gpt->lbt, i); 134 ent->ent_lba_end = end; 135 136 if (gpt_write_backup(gpt) == -1) 137 return -1; 138 139 gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64, entry, 140 map->map_start, newsize); 141 142 return 0; 143 } 144 145 static int 146 cmd_resize(gpt_t gpt, int argc, char *argv[]) 147 { 148 int ch; 149 off_t alignment = 0, sectors, start = 0, size = 0; 150 unsigned int entry = 0; 151 map_t m; 152 bool quiet = false; 153 154 while ((ch = getopt(argc, argv, GPT_AIS "b:q")) != -1) { 155 if (ch == 'b') 156 gpt_human_get(gpt, &start); 157 else if (ch == 'q') 158 quiet = true; 159 else if (gpt_add_ais(gpt, &alignment, &entry, &size, ch) == -1) 160 return usage(); 161 } 162 163 if (argc != optind) 164 return usage(); 165 166 if (start > 0) { 167 for (m = map_first(gpt); m != NULL; m = m->map_next) { 168 if (m->map_type != MAP_TYPE_GPT_PART || 169 m->map_index < 1) 170 continue; 171 if (start != m->map_start) 172 continue; 173 entry = m->map_index; 174 break; 175 } 176 } 177 178 if ((sectors = gpt_check_ais(gpt, alignment, entry, size)) == -1) 179 return -1; 180 181 return resize(gpt, entry, alignment, sectors, size, quiet); 182 } 183