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