Debian Codegraph @

src/growfs/growfs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/vfs.h>
5
6 #include "alloc-util.h"
7 #include "blockdev-util.h"
8 #include "btrfs-util.h"
9 #include "build.h"
10 #include "cryptsetup-util.h"
11 #include "device-util.h"
12 #include "devnum-util.h"
13 #include "dissect-image.h"
14 #include "fd-util.h"
15 #include "format-table.h"
16 #include "format-util.h"
17 #include "log.h"
18 #include "main-func.h"
19 #include "mountpoint-util.h"
20 #include "options.h"
21 #include "pretty-print.h"
22 #include "resize-fs.h"
23 #include "string-util.h"
24
25 static const char *arg_target = NULL;
26 static bool arg_dry_run = false;
27
28 #if HAVE_LIBCRYPTSETUP
29 static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
30 _cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
31 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
32 _cleanup_close_ int main_devfd = -EBADF;
33 uint64_t size;
34 int r;
35
36 r = DLOPEN_CRYPTSETUP(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED);
37 if (r < 0)
38 return r;
39
40 main_devfd = r = device_open_from_devnum(S_IFBLK, main_devno, O_RDONLY|O_CLOEXEC, &main_devpath);
41 if (r < 0)
42 return log_error_errno(r, "Failed to open main block device " DEVNUM_FORMAT_STR ": %m",
43 DEVNUM_FORMAT_VAL(main_devno));
44
45 r = blockdev_get_device_size(main_devfd, &size);
46 if (r < 0)
47 return log_error_errno(r, "Failed to query size of \"%s\" (before resize): %m",
48 main_devpath);
49
50 log_debug("%s is %"PRIu64" bytes", main_devpath, size);
51
52 r = devname_from_devnum(S_IFBLK, devno, &devpath);
53 if (r < 0)
54 return log_error_errno(r, "Failed to get devpath of " DEVNUM_FORMAT_STR ": %m",
55 DEVNUM_FORMAT_VAL(devno));
56
57 r = sym_crypt_init(&cd, devpath);
58 if (r < 0)
59 return log_error_errno(r, "crypt_init(\"%s\") failed: %m", devpath);
60
61 cryptsetup_enable_logging(cd);
62
63 r = sym_crypt_load(cd, CRYPT_LUKS, NULL);
64 if (r < 0)
65 return log_debug_errno(r, "Failed to load LUKS metadata for %s: %m", devpath);
66
67 if (arg_dry_run)
68 return 0;
69
70 r = sym_crypt_resize(cd, main_devpath, 0);
71 if (r < 0)
72 return log_error_errno(r, "crypt_resize() of %s failed: %m", devpath);
73
74 r = blockdev_get_device_size(main_devfd, &size);
75 if (r < 0)
76 log_warning_errno(r, "Failed to query size of \"%s\" (after resize): %m", devpath);
77 else
78 log_debug("%s is now %"PRIu64" bytes", main_devpath, size);
79
80 return 1;
81 }
82 #endif
83
84 static int maybe_resize_underlying_device(
85 int mountfd,
86 const char *mountpath,
87 dev_t main_devno) {
88
89 _cleanup_free_ char *devpath = NULL, *fstype = NULL;
90 dev_t devno;
91 int r;
92
93 assert(mountfd >= 0);
94 assert(mountpath);
95
96 r = get_block_device_harder_fd(mountfd, &devno);
97 if (r < 0)
98 return log_error_errno(r, "Failed to determine underlying block device of \"%s\": %m",
99 mountpath);
100 if (devno == 0)
101 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" not backed by block device.", arg_target);
102
103 log_debug("Underlying device " DEVNUM_FORMAT_STR ", main dev " DEVNUM_FORMAT_STR ", %s",
104 DEVNUM_FORMAT_VAL(devno),
105 DEVNUM_FORMAT_VAL(main_devno),
106 devno == main_devno ? "same" : "different");
107 if (devno == main_devno)
108 return 0;
109
110 r = devname_from_devnum(S_IFBLK, devno, &devpath);
111 if (r < 0)
112 return log_error_errno(r, "Failed to get devpath for block device " DEVNUM_FORMAT_STR ": %m",
113 DEVNUM_FORMAT_VAL(devno));
114
115 r = probe_filesystem(devpath, &fstype);
116 if (r == -EUCLEAN)
117 return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath);
118 if (r < 0)
119 return log_warning_errno(r, "Failed to probe \"%s\": %m", devpath);
120
121 #if HAVE_LIBCRYPTSETUP
122 if (streq_ptr(fstype, "crypto_LUKS"))
123 return resize_crypt_luks_device(devno, fstype, main_devno);
124 #endif
125
126 log_debug("Don't know how to resize %s of type %s, ignoring.", devpath, strnull(fstype));
127 return 0;
128 }
129
130 static int help(void) {
131 _cleanup_free_ char *link = NULL;
132 _cleanup_(table_unrefp) Table *options = NULL;
133 int r;
134
135 r = terminal_urlify_man("[email protected]", "8", &link);
136 if (r < 0)
137 return log_oom();
138
139 r = option_parser_get_help_table(&options);
140 if (r < 0)
141 return r;
142
143 printf("%s [OPTIONS...] /path/to/mountpoint\n\n"
144 "Grow filesystem or encrypted payload to device size.\n",
145 program_invocation_short_name);
146
147 printf("\n%sOptions:%s\n",
148 ansi_underline(),
149 ansi_normal());
150
151 r = table_print_or_warn(options);
152 if (r < 0)
153 return r;
154
155 printf("\nSee the %s for details.\n", link);
156 return 0;
157 }
158
159 static int parse_argv(int argc, char *argv[]) {
160 assert(argc >= 0);
161 assert(argv);
162
163 OptionParser opts = { argc, argv };
164
165 FOREACH_OPTION_OR_RETURN(c, &opts)
166 switch (c) {
167
168 OPTION_COMMON_HELP:
169 return help();
170
171 OPTION_COMMON_VERSION:
172 return version();
173
174 OPTION('n', "dry-run", NULL, "Just print what would be done"):
175 arg_dry_run = true;
176 break;
177 }
178
179 if (option_parser_get_n_args(&opts) != 1)
180 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
181 "%s expects exactly one argument (the mount point).",
182 program_invocation_short_name);
183
184 arg_target = option_parser_get_arg(&opts, 0);
185 return 1;
186 }
187
188 static int run(int argc, char *argv[]) {
189 _cleanup_close_ int mountfd = -EBADF, devfd = -EBADF;
190 _cleanup_free_ char *devpath = NULL;
191 uint64_t size, newsize;
192 dev_t devno;
193 int r;
194
195 log_setup();
196
197 r = parse_argv(argc, argv);
198 if (r <= 0)
199 return r;
200
201 r = path_is_mount_point(arg_target);
202 if (r < 0)
203 return log_error_errno(r, "Failed to check if \"%s\" is a mount point: %m", arg_target);
204 if (r == 0)
205 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "\"%s\" is not a mount point.", arg_target);
206
207 mountfd = open(arg_target, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
208 if (mountfd < 0)
209 return log_error_errno(errno, "Failed to open \"%s\": %m", arg_target);
210
211 r = get_block_device_fd(mountfd, &devno);
212 if (r == -EUCLEAN)
213 return btrfs_log_dev_root(LOG_ERR, r, arg_target);
214 if (r < 0)
215 return log_error_errno(r, "Failed to determine block device of \"%s\": %m", arg_target);
216 if (devno == 0)
217 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "File system \"%s\" not backed by block device.", arg_target);
218
219 r = maybe_resize_underlying_device(mountfd, arg_target, devno);
220 if (r < 0)
221 log_warning_errno(r, "Unable to resize underlying device of \"%s\", proceeding anyway: %m", arg_target);
222
223 devfd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC, &devpath);
224 if (r < 0)
225 return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
226 DEVNUM_FORMAT_VAL(devno));
227
228 r = blockdev_get_device_size(devfd, &size);
229 if (r < 0)
230 return log_error_errno(r, "Failed to query size of \"%s\": %m", devpath);
231
232 log_debug("Resizing \"%s\" to %"PRIu64" bytes...", arg_target, size);
233
234 if (arg_dry_run)
235 return 0;
236
237 r = resize_fs(mountfd, size, &newsize);
238 if (r < 0)
239 return log_error_errno(r, "Failed to resize \"%s\" to %"PRIu64" bytes: %m",
240 arg_target, size);
241 if (newsize == size)
242 log_info("Successfully resized \"%s\" to %s bytes.",
243 arg_target,
244 FORMAT_BYTES(newsize));
245 else
246 log_info("Successfully resized \"%s\" to %s bytes (%"PRIu64" bytes lost due to blocksize).",
247 arg_target,
248 FORMAT_BYTES(newsize),
249 size - newsize);
250 return 0;
251 }
252
253 DEFINE_MAIN_FUNCTION(run);