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