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