]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/growfs.c
repart: log fixes
[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/btrfs.h>
7 #include <linux/magic.h>
8 #include <sys/ioctl.h>
9 #include <sys/mount.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 "main-func.h"
22 #include "mountpoint-util.h"
23 #include "parse-util.h"
24 #include "pretty-print.h"
25 #include "resize-fs.h"
26
27 static const char *arg_target = NULL;
28 static bool arg_dry_run = false;
29
30 #if HAVE_LIBCRYPTSETUP
31 static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
32 _cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
33 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
34 _cleanup_close_ int main_devfd = -1;
35 uint64_t size;
36 int r;
37
38 r = device_path_make_major_minor(S_IFBLK, main_devno, &main_devpath);
39 if (r < 0)
40 return log_error_errno(r, "Failed to format device major/minor path: %m");
41
42 main_devfd = open(main_devpath, O_RDONLY|O_CLOEXEC);
43 if (main_devfd < 0)
44 return log_error_errno(errno, "Failed to open \"%s\": %m", main_devpath);
45
46 if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0)
47 return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m",
48 main_devpath);
49
50 log_debug("%s is %"PRIu64" bytes", main_devpath, size);
51 r = device_path_make_major_minor(S_IFBLK, devno, &devpath);
52 if (r < 0)
53 return log_error_errno(r, "Failed to format major/minor path: %m");
54
55 r = crypt_init(&cd, devpath);
56 if (r < 0)
57 return log_error_errno(r, "crypt_init(\"%s\") failed: %m", devpath);
58
59 crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
60
61 r = crypt_load(cd, CRYPT_LUKS, NULL);
62 if (r < 0)
63 return log_debug_errno(r, "Failed to load LUKS metadata for %s: %m", devpath);
64
65 if (arg_dry_run)
66 return 0;
67
68 r = crypt_resize(cd, main_devpath, 0);
69 if (r < 0)
70 return log_error_errno(r, "crypt_resize() of %s failed: %m", devpath);
71
72 if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0)
73 log_warning_errno(errno, "Failed to query size of \"%s\" (after resize): %m",
74 devpath);
75 else
76 log_debug("%s is now %"PRIu64" bytes", main_devpath, size);
77
78 return 1;
79 }
80 #endif
81
82 static int maybe_resize_underlying_device(const char *mountpath, dev_t main_devno) {
83 _cleanup_free_ char *fstype = NULL, *devpath = NULL;
84 dev_t devno;
85 int r;
86
87 #if HAVE_LIBCRYPTSETUP
88 crypt_set_log_callback(NULL, cryptsetup_log_glue, NULL);
89 if (DEBUG_LOGGING)
90 crypt_set_debug_level(CRYPT_DEBUG_ALL);
91 #endif
92
93 r = get_block_device_harder(mountpath, &devno);
94 if (r < 0)
95 return log_error_errno(r, "Failed to determine underlying block device of \"%s\": %m",
96 mountpath);
97
98 log_debug("Underlying device %d:%d, main dev %d:%d, %s",
99 major(devno), minor(devno),
100 major(main_devno), minor(main_devno),
101 devno == main_devno ? "same" : "different");
102 if (devno == main_devno)
103 return 0;
104
105 r = device_path_make_major_minor(S_IFBLK, devno, &devpath);
106 if (r < 0)
107 return log_error_errno(r, "Failed to format device major/minor path: %m");
108
109 r = probe_filesystem(devpath, &fstype);
110 if (r == -EUCLEAN)
111 return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath);
112 if (r < 0)
113 return log_warning_errno(r, "Failed to probe \"%s\": %m", devpath);
114
115 #if HAVE_LIBCRYPTSETUP
116 if (streq_ptr(fstype, "crypto_LUKS"))
117 return resize_crypt_luks_device(devno, fstype, main_devno);
118 #endif
119
120 log_debug("Don't know how to resize %s of type %s, ignoring.", devpath, strnull(fstype));
121 return 0;
122 }
123
124 static int help(void) {
125 _cleanup_free_ char *link = NULL;
126 int r;
127
128 r = terminal_urlify_man("systemd-growfs@.service", "8", &link);
129 if (r < 0)
130 return log_oom();
131
132 printf("%s [OPTIONS...] /path/to/mountpoint\n\n"
133 "Grow filesystem or encrypted payload to device size.\n\n"
134 "Options:\n"
135 " -h --help Show this help and exit\n"
136 " --version Print version string and exit\n"
137 " -n --dry-run Just print what would be done\n"
138 "\nSee the %s for details.\n"
139 , program_invocation_short_name
140 , link
141 );
142
143 return 0;
144 }
145
146 static int parse_argv(int argc, char *argv[]) {
147 enum {
148 ARG_VERSION = 0x100,
149 };
150
151 int c;
152
153 static const struct option options[] = {
154 { "help", no_argument, NULL, 'h' },
155 { "version" , no_argument, NULL, ARG_VERSION },
156 { "dry-run", no_argument, NULL, 'n' },
157 {}
158 };
159
160 assert(argc >= 0);
161 assert(argv);
162
163 while ((c = getopt_long(argc, argv, "hn", options, NULL)) >= 0)
164 switch(c) {
165 case 'h':
166 return help();
167
168 case ARG_VERSION:
169 return version();
170
171 case 'n':
172 arg_dry_run = true;
173 break;
174
175 case '?':
176 return -EINVAL;
177
178 default:
179 assert_not_reached("Unhandled option");
180 }
181
182 if (optind + 1 != argc)
183 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
184 "%s excepts exactly one argument (the mount point).",
185 program_invocation_short_name);
186
187 arg_target = argv[optind];
188
189 return 1;
190 }
191
192 static int run(int argc, char *argv[]) {
193 _cleanup_close_ int mountfd = -1, devfd = -1;
194 _cleanup_free_ char *devpath = NULL;
195 uint64_t size, newsize;
196 char fb[FORMAT_BYTES_MAX];
197 dev_t devno;
198 int r;
199
200 log_setup_service();
201
202 r = parse_argv(argc, argv);
203 if (r <= 0)
204 return r;
205
206 r = path_is_mount_point(arg_target, NULL, 0);
207 if (r < 0)
208 return log_error_errno(r, "Failed to check if \"%s\" is a mount point: %m", arg_target);
209 if (r == 0)
210 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "\"%s\" is not a mount point: %m", arg_target);
211
212 r = get_block_device(arg_target, &devno);
213 if (r < 0)
214 return log_error_errno(r, "Failed to determine block device of \"%s\": %m", arg_target);
215
216 r = maybe_resize_underlying_device(arg_target, devno);
217 if (r < 0)
218 return r;
219
220 mountfd = open(arg_target, O_RDONLY|O_CLOEXEC);
221 if (mountfd < 0)
222 return log_error_errno(errno, "Failed to open \"%s\": %m", arg_target);
223
224 r = device_path_make_major_minor(S_IFBLK, devno, &devpath);
225 if (r < 0)
226 return log_error_errno(r, "Failed to format device major/minor path: %m");
227
228 devfd = open(devpath, O_RDONLY|O_CLOEXEC);
229 if (devfd < 0)
230 return log_error_errno(errno, "Failed to open \"%s\": %m", devpath);
231
232 if (ioctl(devfd, BLKGETSIZE64, &size) != 0)
233 return log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath);
234
235 log_debug("Resizing \"%s\" to %"PRIu64" bytes...", arg_target, size);
236 r = resize_fs(mountfd, size, &newsize);
237 if (r < 0)
238 return log_error_errno(r, "Failed to resize \"%s\" to %"PRIu64" bytes: %m",
239 arg_target, size);
240 if (newsize == size)
241 log_info("Successfully resized \"%s\" to %s bytes.",
242 arg_target,
243 format_bytes(fb, sizeof fb, newsize));
244 else
245 log_info("Successfully resized \"%s\" to %s bytes (%"PRIu64" bytes lost due to blocksize).",
246 arg_target,
247 format_bytes(fb, sizeof fb, newsize),
248 size - newsize);
249 return 0;
250 }
251
252 DEFINE_MAIN_FUNCTION(run);