]>
Commit | Line | Data |
---|---|---|
80750adb | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
80750adb ZJS |
2 | |
3 | #include <errno.h> | |
4 | #include <fcntl.h> | |
385de88a | 5 | #include <getopt.h> |
01234e1f | 6 | #include <linux/btrfs.h> |
80750adb ZJS |
7 | #include <linux/magic.h> |
8 | #include <sys/ioctl.h> | |
9 | #include <sys/mount.h> | |
10 | #include <sys/stat.h> | |
11 | #include <sys/types.h> | |
12 | #include <sys/vfs.h> | |
13 | ||
18c528e9 | 14 | #include "blockdev-util.h" |
c34b75a1 | 15 | #include "crypt-util.h" |
80750adb | 16 | #include "device-nodes.h" |
c34b75a1 | 17 | #include "dissect-image.h" |
80750adb ZJS |
18 | #include "escape.h" |
19 | #include "fd-util.h" | |
20 | #include "format-util.h" | |
21 | #include "log.h" | |
22 | #include "missing.h" | |
049af8ad | 23 | #include "mountpoint-util.h" |
80750adb ZJS |
24 | #include "parse-util.h" |
25 | #include "path-util.h" | |
294bf0c3 | 26 | #include "pretty-print.h" |
54b22b26 | 27 | #include "stat-util.h" |
80750adb | 28 | #include "strv.h" |
ca78ad1d | 29 | #include "util.h" |
80750adb | 30 | |
088c49c3 LP |
31 | static const char *arg_target = NULL; |
32 | static bool arg_dry_run = false; | |
385de88a | 33 | |
76d3e083 | 34 | static int resize_ext4(const char *path, int mountfd, int devfd, uint64_t numblocks, uint64_t blocksize) { |
80750adb ZJS |
35 | assert((uint64_t) (int) blocksize == blocksize); |
36 | ||
385de88a ZJS |
37 | if (arg_dry_run) |
38 | return 0; | |
39 | ||
80750adb | 40 | if (ioctl(mountfd, EXT4_IOC_RESIZE_FS, &numblocks) != 0) |
76d3e083 ZJS |
41 | return log_error_errno(errno, "Failed to resize \"%s\" to %"PRIu64" blocks (ext4): %m", |
42 | path, numblocks); | |
80750adb ZJS |
43 | |
44 | return 0; | |
45 | } | |
46 | ||
76d3e083 | 47 | static int resize_btrfs(const char *path, int mountfd, int devfd, uint64_t numblocks, uint64_t blocksize) { |
80750adb ZJS |
48 | struct btrfs_ioctl_vol_args args = {}; |
49 | int r; | |
50 | ||
51 | assert((uint64_t) (int) blocksize == blocksize); | |
52 | ||
76d3e083 ZJS |
53 | /* https://bugzilla.kernel.org/show_bug.cgi?id=118111 */ |
54 | if (numblocks * blocksize < 256*1024*1024) { | |
55 | log_warning("%s: resizing of btrfs volumes smaller than 256M is not supported", path); | |
56 | return -EOPNOTSUPP; | |
57 | } | |
58 | ||
80750adb ZJS |
59 | r = snprintf(args.name, sizeof(args.name), "%"PRIu64, numblocks * blocksize); |
60 | /* The buffer is large enough for any number to fit... */ | |
61 | assert((size_t) r < sizeof(args.name)); | |
62 | ||
385de88a ZJS |
63 | if (arg_dry_run) |
64 | return 0; | |
65 | ||
80750adb | 66 | if (ioctl(mountfd, BTRFS_IOC_RESIZE, &args) != 0) |
76d3e083 ZJS |
67 | return log_error_errno(errno, "Failed to resize \"%s\" to %"PRIu64" blocks (btrfs): %m", |
68 | path, numblocks); | |
80750adb ZJS |
69 | |
70 | return 0; | |
71 | } | |
72 | ||
80088c7e | 73 | #if HAVE_LIBCRYPTSETUP |
c34b75a1 | 74 | static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) { |
54b22b26 | 75 | _cleanup_free_ char *devpath = NULL, *main_devpath = NULL; |
c34b75a1 | 76 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
54b22b26 | 77 | _cleanup_close_ int main_devfd = -1; |
c34b75a1 ZJS |
78 | uint64_t size; |
79 | int r; | |
80 | ||
54b22b26 LP |
81 | r = device_path_make_major_minor(S_IFBLK, main_devno, &main_devpath); |
82 | if (r < 0) | |
83 | return log_error_errno(r, "Failed to format device major/minor path: %m"); | |
84 | ||
c34b75a1 ZJS |
85 | main_devfd = open(main_devpath, O_RDONLY|O_CLOEXEC); |
86 | if (main_devfd < 0) | |
87 | return log_error_errno(errno, "Failed to open \"%s\": %m", main_devpath); | |
88 | ||
89 | if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0) | |
90 | return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m", | |
91 | main_devpath); | |
92 | ||
93 | log_debug("%s is %"PRIu64" bytes", main_devpath, size); | |
54b22b26 LP |
94 | r = device_path_make_major_minor(S_IFBLK, devno, &devpath); |
95 | if (r < 0) | |
96 | return log_error_errno(r, "Failed to format major/minor path: %m"); | |
c34b75a1 | 97 | |
c34b75a1 ZJS |
98 | r = crypt_init(&cd, devpath); |
99 | if (r < 0) | |
100 | return log_error_errno(r, "crypt_init(\"%s\") failed: %m", devpath); | |
101 | ||
102 | crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); | |
103 | ||
104 | r = crypt_load(cd, CRYPT_LUKS, NULL); | |
105 | if (r < 0) | |
106 | return log_debug_errno(r, "Failed to load LUKS metadata for %s: %m", devpath); | |
107 | ||
385de88a ZJS |
108 | if (arg_dry_run) |
109 | return 0; | |
110 | ||
c34b75a1 ZJS |
111 | r = crypt_resize(cd, main_devpath, 0); |
112 | if (r < 0) | |
113 | return log_error_errno(r, "crypt_resize() of %s failed: %m", devpath); | |
114 | ||
115 | if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0) | |
116 | log_warning_errno(errno, "Failed to query size of \"%s\" (after resize): %m", | |
117 | devpath); | |
118 | else | |
119 | log_debug("%s is now %"PRIu64" bytes", main_devpath, size); | |
120 | ||
121 | return 1; | |
122 | } | |
80088c7e | 123 | #endif |
c34b75a1 ZJS |
124 | |
125 | static int maybe_resize_slave_device(const char *mountpath, dev_t main_devno) { | |
54b22b26 | 126 | _cleanup_free_ char *fstype = NULL, *devpath = NULL; |
c34b75a1 | 127 | dev_t devno; |
c34b75a1 ZJS |
128 | int r; |
129 | ||
80088c7e | 130 | #if HAVE_LIBCRYPTSETUP |
c34b75a1 ZJS |
131 | crypt_set_log_callback(NULL, cryptsetup_log_glue, NULL); |
132 | crypt_set_debug_level(1); | |
80088c7e | 133 | #endif |
c34b75a1 ZJS |
134 | |
135 | r = get_block_device_harder(mountpath, &devno); | |
136 | if (r < 0) | |
137 | return log_error_errno(r, "Failed to determine underlying block device of \"%s\": %m", | |
138 | mountpath); | |
139 | ||
140 | log_debug("Underlying device %d:%d, main dev %d:%d, %s", | |
141 | major(devno), minor(devno), | |
142 | major(main_devno), minor(main_devno), | |
143 | devno == main_devno ? "same" : "different"); | |
144 | if (devno == main_devno) | |
145 | return 0; | |
146 | ||
54b22b26 LP |
147 | r = device_path_make_major_minor(S_IFBLK, devno, &devpath); |
148 | if (r < 0) | |
149 | return log_error_errno(r, "Failed to format device major/minor path: %m"); | |
150 | ||
c34b75a1 | 151 | r = probe_filesystem(devpath, &fstype); |
7cc84b2c ZJS |
152 | if (r == -EUCLEAN) |
153 | return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath); | |
c34b75a1 ZJS |
154 | if (r < 0) |
155 | return log_warning_errno(r, "Failed to probe \"%s\": %m", devpath); | |
156 | ||
80088c7e | 157 | #if HAVE_LIBCRYPTSETUP |
c34b75a1 ZJS |
158 | if (streq_ptr(fstype, "crypto_LUKS")) |
159 | return resize_crypt_luks_device(devno, fstype, main_devno); | |
80088c7e | 160 | #endif |
c34b75a1 ZJS |
161 | |
162 | log_debug("Don't know how to resize %s of type %s, ignoring", devpath, strnull(fstype)); | |
163 | return 0; | |
164 | } | |
165 | ||
37ec0fdd LP |
166 | static int help(void) { |
167 | _cleanup_free_ char *link = NULL; | |
168 | int r; | |
169 | ||
170 | r = terminal_urlify_man("systemd-growfs@.service", "8", &link); | |
171 | if (r < 0) | |
172 | return log_oom(); | |
173 | ||
385de88a ZJS |
174 | printf("%s [OPTIONS...] /path/to/mountpoint\n\n" |
175 | "Grow filesystem or encrypted payload to device size.\n\n" | |
176 | "Options:\n" | |
177 | " -h --help Show this help and exit\n" | |
178 | " --version Print version string and exit\n" | |
179 | " -n --dry-run Just print what would be done\n" | |
37ec0fdd LP |
180 | "\nSee the %s for details.\n" |
181 | , program_invocation_short_name | |
182 | , link | |
183 | ); | |
184 | ||
185 | return 0; | |
385de88a ZJS |
186 | } |
187 | ||
188 | static int parse_argv(int argc, char *argv[]) { | |
189 | enum { | |
190 | ARG_VERSION = 0x100, | |
191 | }; | |
192 | ||
193 | int c; | |
194 | ||
195 | static const struct option options[] = { | |
196 | { "help", no_argument, NULL, 'h' }, | |
197 | { "version" , no_argument, NULL, ARG_VERSION }, | |
198 | { "dry-run", no_argument, NULL, 'n' }, | |
199 | {} | |
200 | }; | |
201 | ||
202 | assert(argc >= 0); | |
203 | assert(argv); | |
204 | ||
205 | while ((c = getopt_long(argc, argv, "hn", options, NULL)) >= 0) | |
206 | switch(c) { | |
207 | case 'h': | |
37ec0fdd | 208 | return help(); |
385de88a ZJS |
209 | |
210 | case ARG_VERSION: | |
37ec0fdd | 211 | return version(); |
385de88a ZJS |
212 | |
213 | case 'n': | |
214 | arg_dry_run = true; | |
215 | break; | |
216 | ||
217 | case '?': | |
218 | return -EINVAL; | |
219 | ||
220 | default: | |
221 | assert_not_reached("Unhandled option"); | |
222 | } | |
223 | ||
baaa35ad ZJS |
224 | if (optind + 1 != argc) |
225 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
226 | "%s excepts exactly one argument (the mount point).", | |
227 | program_invocation_short_name); | |
385de88a ZJS |
228 | |
229 | arg_target = argv[optind]; | |
230 | ||
231 | return 1; | |
232 | } | |
233 | ||
80750adb | 234 | int main(int argc, char *argv[]) { |
80750adb | 235 | _cleanup_close_ int mountfd = -1, devfd = -1; |
54b22b26 | 236 | _cleanup_free_ char *devpath = NULL; |
80750adb | 237 | uint64_t size, numblocks; |
54b22b26 | 238 | char fb[FORMAT_BYTES_MAX]; |
80750adb | 239 | struct statfs sfs; |
54b22b26 LP |
240 | dev_t devno; |
241 | int blocksize; | |
80750adb ZJS |
242 | int r; |
243 | ||
6bf3c61c | 244 | log_setup_service(); |
80750adb | 245 | |
385de88a ZJS |
246 | r = parse_argv(argc, argv); |
247 | if (r < 0) | |
248 | return EXIT_FAILURE; | |
249 | if (r == 0) | |
250 | return EXIT_SUCCESS; | |
251 | ||
252 | r = path_is_mount_point(arg_target, NULL, 0); | |
80750adb | 253 | if (r < 0) { |
385de88a | 254 | log_error_errno(r, "Failed to check if \"%s\" is a mount point: %m", arg_target); |
80750adb ZJS |
255 | return EXIT_FAILURE; |
256 | } | |
257 | if (r == 0) { | |
385de88a | 258 | log_error_errno(r, "\"%s\" is not a mount point: %m", arg_target); |
80750adb ZJS |
259 | return EXIT_FAILURE; |
260 | } | |
261 | ||
385de88a | 262 | r = get_block_device(arg_target, &devno); |
80750adb | 263 | if (r < 0) { |
385de88a | 264 | log_error_errno(r, "Failed to determine block device of \"%s\": %m", arg_target); |
80750adb ZJS |
265 | return EXIT_FAILURE; |
266 | } | |
267 | ||
385de88a | 268 | r = maybe_resize_slave_device(arg_target, devno); |
c34b75a1 ZJS |
269 | if (r < 0) |
270 | return EXIT_FAILURE; | |
271 | ||
385de88a | 272 | mountfd = open(arg_target, O_RDONLY|O_CLOEXEC); |
80750adb | 273 | if (mountfd < 0) { |
385de88a | 274 | log_error_errno(errno, "Failed to open \"%s\": %m", arg_target); |
80750adb ZJS |
275 | return EXIT_FAILURE; |
276 | } | |
277 | ||
54b22b26 LP |
278 | r = device_path_make_major_minor(S_IFBLK, devno, &devpath); |
279 | if (r < 0) { | |
280 | log_error_errno(r, "Failed to format device major/minor path: %m"); | |
281 | return EXIT_FAILURE; | |
282 | } | |
283 | ||
80750adb ZJS |
284 | devfd = open(devpath, O_RDONLY|O_CLOEXEC); |
285 | if (devfd < 0) { | |
286 | log_error_errno(errno, "Failed to open \"%s\": %m", devpath); | |
287 | return EXIT_FAILURE; | |
288 | } | |
289 | ||
290 | if (ioctl(devfd, BLKBSZGET, &blocksize) != 0) { | |
291 | log_error_errno(errno, "Failed to query block size of \"%s\": %m", devpath); | |
292 | return EXIT_FAILURE; | |
293 | } | |
294 | ||
295 | if (ioctl(devfd, BLKGETSIZE64, &size) != 0) { | |
296 | log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath); | |
297 | return EXIT_FAILURE; | |
298 | } | |
299 | ||
300 | if (size % blocksize != 0) | |
301 | log_notice("Partition size %"PRIu64" is not a multiple of the blocksize %d," | |
302 | " ignoring %"PRIu64" bytes", size, blocksize, size % blocksize); | |
303 | ||
304 | numblocks = size / blocksize; | |
305 | ||
306 | if (fstatfs(mountfd, &sfs) < 0) { | |
385de88a | 307 | log_error_errno(errno, "Failed to stat file system \"%s\": %m", arg_target); |
80750adb ZJS |
308 | return EXIT_FAILURE; |
309 | } | |
310 | ||
311 | switch(sfs.f_type) { | |
312 | case EXT4_SUPER_MAGIC: | |
385de88a | 313 | r = resize_ext4(arg_target, mountfd, devfd, numblocks, blocksize); |
80750adb ZJS |
314 | break; |
315 | case BTRFS_SUPER_MAGIC: | |
385de88a | 316 | r = resize_btrfs(arg_target, mountfd, devfd, numblocks, blocksize); |
80750adb ZJS |
317 | break; |
318 | default: | |
319 | log_error("Don't know how to resize fs %llx on \"%s\"", | |
385de88a | 320 | (long long unsigned) sfs.f_type, arg_target); |
80750adb ZJS |
321 | return EXIT_FAILURE; |
322 | } | |
323 | ||
76d3e083 | 324 | if (r < 0) |
80750adb | 325 | return EXIT_FAILURE; |
80750adb ZJS |
326 | |
327 | log_info("Successfully resized \"%s\" to %s bytes (%"PRIu64" blocks of %d bytes).", | |
385de88a | 328 | arg_target, format_bytes(fb, sizeof fb, size), numblocks, blocksize); |
80750adb ZJS |
329 | return EXIT_SUCCESS; |
330 | } |