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