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