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