]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/partition/growfs.c
tree-wide: avoid some loaded terms
[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>
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"
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"
2b82a99f 21#include "main-func.h"
049af8ad 22#include "mountpoint-util.h"
80750adb 23#include "parse-util.h"
294bf0c3 24#include "pretty-print.h"
d6f1e660 25#include "resize-fs.h"
80750adb 26
088c49c3
LP
27static const char *arg_target = NULL;
28static bool arg_dry_run = false;
385de88a 29
80088c7e 30#if HAVE_LIBCRYPTSETUP
c34b75a1 31static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
54b22b26 32 _cleanup_free_ char *devpath = NULL, *main_devpath = NULL;
c34b75a1 33 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
54b22b26 34 _cleanup_close_ int main_devfd = -1;
c34b75a1
ZJS
35 uint64_t size;
36 int r;
37
54b22b26
LP
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
c34b75a1
ZJS
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);
54b22b26
LP
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");
c34b75a1 54
c34b75a1
ZJS
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
385de88a
ZJS
65 if (arg_dry_run)
66 return 0;
67
c34b75a1
ZJS
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}
80088c7e 80#endif
c34b75a1 81
6b000af4 82static int maybe_resize_underlying_device(const char *mountpath, dev_t main_devno) {
54b22b26 83 _cleanup_free_ char *fstype = NULL, *devpath = NULL;
c34b75a1 84 dev_t devno;
c34b75a1
ZJS
85 int r;
86
80088c7e 87#if HAVE_LIBCRYPTSETUP
c34b75a1 88 crypt_set_log_callback(NULL, cryptsetup_log_glue, NULL);
ac8bb308
IS
89 if (DEBUG_LOGGING)
90 crypt_set_debug_level(CRYPT_DEBUG_ALL);
80088c7e 91#endif
c34b75a1
ZJS
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
54b22b26
LP
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
c34b75a1 109 r = probe_filesystem(devpath, &fstype);
7cc84b2c
ZJS
110 if (r == -EUCLEAN)
111 return log_warning_errno(r, "Cannot reliably determine probe \"%s\", refusing to proceed.", devpath);
c34b75a1
ZJS
112 if (r < 0)
113 return log_warning_errno(r, "Failed to probe \"%s\": %m", devpath);
114
80088c7e 115#if HAVE_LIBCRYPTSETUP
c34b75a1
ZJS
116 if (streq_ptr(fstype, "crypto_LUKS"))
117 return resize_crypt_luks_device(devno, fstype, main_devno);
80088c7e 118#endif
c34b75a1 119
d6f1e660 120 log_debug("Don't know how to resize %s of type %s, ignoring.", devpath, strnull(fstype));
c34b75a1
ZJS
121 return 0;
122}
123
37ec0fdd
LP
124static 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
385de88a
ZJS
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"
37ec0fdd
LP
138 "\nSee the %s for details.\n"
139 , program_invocation_short_name
140 , link
141 );
142
143 return 0;
385de88a
ZJS
144}
145
146static 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':
37ec0fdd 166 return help();
385de88a
ZJS
167
168 case ARG_VERSION:
37ec0fdd 169 return version();
385de88a
ZJS
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
baaa35ad
ZJS
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);
385de88a
ZJS
186
187 arg_target = argv[optind];
188
189 return 1;
190}
191
2b82a99f 192static int run(int argc, char *argv[]) {
80750adb 193 _cleanup_close_ int mountfd = -1, devfd = -1;
54b22b26 194 _cleanup_free_ char *devpath = NULL;
d6f1e660 195 uint64_t size, newsize;
54b22b26 196 char fb[FORMAT_BYTES_MAX];
54b22b26 197 dev_t devno;
80750adb
ZJS
198 int r;
199
6bf3c61c 200 log_setup_service();
80750adb 201
385de88a 202 r = parse_argv(argc, argv);
2b82a99f
ZJS
203 if (r <= 0)
204 return r;
385de88a
ZJS
205
206 r = path_is_mount_point(arg_target, NULL, 0);
2b82a99f
ZJS
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);
80750adb 211
385de88a 212 r = get_block_device(arg_target, &devno);
2b82a99f
ZJS
213 if (r < 0)
214 return log_error_errno(r, "Failed to determine block device of \"%s\": %m", arg_target);
80750adb 215
6b000af4 216 r = maybe_resize_underlying_device(arg_target, devno);
c34b75a1 217 if (r < 0)
2b82a99f 218 return r;
c34b75a1 219
385de88a 220 mountfd = open(arg_target, O_RDONLY|O_CLOEXEC);
2b82a99f
ZJS
221 if (mountfd < 0)
222 return log_error_errno(errno, "Failed to open \"%s\": %m", arg_target);
80750adb 223
54b22b26 224 r = device_path_make_major_minor(S_IFBLK, devno, &devpath);
2b82a99f
ZJS
225 if (r < 0)
226 return log_error_errno(r, "Failed to format device major/minor path: %m");
54b22b26 227
80750adb 228 devfd = open(devpath, O_RDONLY|O_CLOEXEC);
2b82a99f
ZJS
229 if (devfd < 0)
230 return log_error_errno(errno, "Failed to open \"%s\": %m", devpath);
80750adb 231
2b82a99f
ZJS
232 if (ioctl(devfd, BLKGETSIZE64, &size) != 0)
233 return log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath);
80750adb 234
d6f1e660
ZJS
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;
80750adb 250}
2b82a99f
ZJS
251
252DEFINE_MAIN_FUNCTION(run);