]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e3478379 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2010 ProFUSION embedded systems |
e3478379 FF |
4 | ***/ |
5 | ||
6 | #include <errno.h> | |
7 | #include <fcntl.h> | |
4f5dd394 | 8 | #include <linux/loop.h> |
e3478379 FF |
9 | #include <string.h> |
10 | #include <sys/mount.h> | |
11 | #include <sys/swap.h> | |
e3478379 | 12 | |
95b862b0 ZJS |
13 | /* This needs to be after sys/mount.h :( */ |
14 | #include <libmount.h> | |
15 | ||
6bcf00ed | 16 | #include "sd-device.h" |
b4bbcaa9 | 17 | |
b5efdb8a | 18 | #include "alloc-util.h" |
18c528e9 | 19 | #include "blockdev-util.h" |
d5641e0d | 20 | #include "def.h" |
8437c059 | 21 | #include "device-util.h" |
07630cea | 22 | #include "escape.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
471b48ed | 24 | #include "fstab-util.h" |
dcce98a4 | 25 | #include "linux-3.13/dm-ioctl.h" |
e3478379 | 26 | #include "mount-setup.h" |
18c528e9 | 27 | #include "mount-util.h" |
049af8ad | 28 | #include "mountpoint-util.h" |
9eb977db | 29 | #include "path-util.h" |
dccca82b | 30 | #include "process-util.h" |
d5641e0d | 31 | #include "signal-util.h" |
07630cea | 32 | #include "string-util.h" |
da9fc98d | 33 | #include "strv.h" |
3ffd4af2 | 34 | #include "umount.h" |
e3478379 | 35 | #include "util.h" |
024f268d | 36 | #include "virt.h" |
e3478379 | 37 | |
95b862b0 ZJS |
38 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table); |
39 | DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter); | |
40 | ||
12aad1d0 LP |
41 | static void mount_point_free(MountPoint **head, MountPoint *m) { |
42 | assert(head); | |
43 | assert(m); | |
e3478379 | 44 | |
71fda00f | 45 | LIST_REMOVE(mount_point, *head, m); |
12aad1d0 LP |
46 | |
47 | free(m->path); | |
3bc341be | 48 | free(m->remount_options); |
12aad1d0 | 49 | free(m); |
e3478379 FF |
50 | } |
51 | ||
6fa392bf | 52 | void mount_points_list_free(MountPoint **head) { |
12aad1d0 LP |
53 | assert(head); |
54 | ||
55 | while (*head) | |
56 | mount_point_free(head, *head); | |
e3478379 FF |
57 | } |
58 | ||
6fa392bf | 59 | int mount_points_list_get(const char *mountinfo, MountPoint **head) { |
95b862b0 ZJS |
60 | _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL; |
61 | _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL; | |
527b7a42 | 62 | int r; |
e3478379 | 63 | |
12aad1d0 LP |
64 | assert(head); |
65 | ||
95b862b0 ZJS |
66 | t = mnt_new_table(); |
67 | i = mnt_new_iter(MNT_ITER_FORWARD); | |
68 | if (!t || !i) | |
69 | return log_oom(); | |
70 | ||
6fa392bf | 71 | r = mnt_table_parse_mtab(t, mountinfo); |
95b862b0 | 72 | if (r < 0) |
6fa392bf | 73 | return log_error_errno(r, "Failed to parse %s: %m", mountinfo); |
95b862b0 ZJS |
74 | |
75 | for (;;) { | |
76 | struct libmnt_fs *fs; | |
66c91c3a | 77 | const char *path, *fstype; |
78 | _cleanup_free_ char *options = NULL; | |
c7543606 | 79 | _cleanup_free_ char *p = NULL; |
95b862b0 ZJS |
80 | unsigned long remount_flags = 0u; |
81 | _cleanup_free_ char *remount_options = NULL; | |
82 | bool try_remount_ro; | |
12aad1d0 | 83 | MountPoint *m; |
e3478379 | 84 | |
95b862b0 ZJS |
85 | r = mnt_table_next_fs(t, i, &fs); |
86 | if (r == 1) | |
87 | break; | |
527b7a42 | 88 | if (r < 0) |
6fa392bf | 89 | return log_error_errno(r, "Failed to get next entry from %s: %m", mountinfo); |
95b862b0 ZJS |
90 | |
91 | path = mnt_fs_get_target(fs); | |
92 | if (!path) | |
93 | continue; | |
94 | ||
95 | if (cunescape(path, UNESCAPE_RELAX, &p) < 0) | |
96 | return log_oom(); | |
97 | ||
95b862b0 | 98 | fstype = mnt_fs_get_fstype(fs); |
e3478379 | 99 | |
66c91c3a | 100 | /* Combine the generic VFS options with the FS-specific |
101 | * options. Duplicates are not a problem here, because the only | |
102 | * options that should come up twice are typically ro/rw, which | |
103 | * are turned into MS_RDONLY or the invertion of it. | |
104 | * | |
105 | * Even if there are duplicates later in mount_option_mangle() | |
106 | * it shouldn't hurt anyways as they override each other. | |
107 | */ | |
108 | if (!strextend_with_separator(&options, ",", | |
109 | mnt_fs_get_vfs_options(fs), | |
110 | NULL)) | |
111 | return log_oom(); | |
112 | if (!strextend_with_separator(&options, ",", | |
113 | mnt_fs_get_fs_options(fs), | |
114 | NULL)) | |
115 | return log_oom(); | |
116 | ||
46108b3b LP |
117 | /* Ignore mount points we can't unmount because they |
118 | * are API or because we are keeping them open (like | |
874d3404 LP |
119 | * /dev/console). Also, ignore all mounts below API |
120 | * file systems, since they are likely virtual too, | |
121 | * and hence not worth spending time on. Also, in | |
122 | * unprivileged containers we might lack the rights to | |
123 | * unmount these things, hence don't bother. */ | |
46108b3b LP |
124 | if (mount_point_is_api(p) || |
125 | mount_point_ignore(p) || | |
e5e0a796 | 126 | PATH_STARTSWITH_SET(p, "/dev", "/sys", "/proc")) |
2054a5b8 | 127 | continue; |
2054a5b8 | 128 | |
1d62d22d JJ |
129 | /* If we are in a container, don't attempt to |
130 | * read-only mount anything as that brings no real | |
131 | * benefits, but might confuse the host, as we remount | |
132 | * the superblock here, not the bind mount. | |
133 | * | |
134 | * If the filesystem is a network fs, also skip the | |
135 | * remount. It brings no value (we cannot leave | |
136 | * a "dirty fs") and could hang if the network is down. | |
137 | * Note that umount2() is more careful and will not | |
138 | * hang because of the network being down. */ | |
95b862b0 ZJS |
139 | try_remount_ro = detect_container() <= 0 && |
140 | !fstype_is_network(fstype) && | |
141 | !fstype_is_api_vfs(fstype) && | |
142 | !fstype_is_ro(fstype) && | |
143 | !fstab_test_yes_no_option(options, "ro\0rw\0"); | |
3bc341be | 144 | |
95b862b0 | 145 | if (try_remount_ro) { |
3bc341be JJ |
146 | /* mount(2) states that mount flags and options need to be exactly the same |
147 | * as they were when the filesystem was mounted, except for the desired | |
148 | * changes. So we reconstruct both here and adjust them for the later | |
149 | * remount call too. */ | |
150 | ||
95b862b0 ZJS |
151 | r = mnt_fs_get_propagation(fs, &remount_flags); |
152 | if (r < 0) { | |
153 | log_warning_errno(r, "mnt_fs_get_propagation() failed for %s, ignoring: %m", path); | |
154 | continue; | |
155 | } | |
3bc341be | 156 | |
95b862b0 ZJS |
157 | r = mount_option_mangle(options, remount_flags, &remount_flags, &remount_options); |
158 | if (r < 0) { | |
159 | log_warning_errno(r, "mount_option_mangle failed for %s, ignoring: %m", path); | |
160 | continue; | |
161 | } | |
3bc341be JJ |
162 | |
163 | /* MS_BIND is special. If it is provided it will only make the mount-point | |
164 | * read-only. If left out, the super block itself is remounted, which we want. */ | |
95b862b0 | 165 | remount_flags = (remount_flags|MS_REMOUNT|MS_RDONLY) & ~MS_BIND; |
3bc341be | 166 | } |
471b48ed | 167 | |
95b862b0 ZJS |
168 | m = new0(MountPoint, 1); |
169 | if (!m) | |
170 | return log_oom(); | |
171 | ||
172 | free_and_replace(m->path, p); | |
173 | free_and_replace(m->remount_options, remount_options); | |
174 | m->remount_flags = remount_flags; | |
175 | m->try_remount_ro = try_remount_ro; | |
176 | ||
71fda00f | 177 | LIST_PREPEND(mount_point, *head, m); |
e3478379 FF |
178 | } |
179 | ||
c3544e8d | 180 | return 0; |
e3478379 FF |
181 | } |
182 | ||
1fd8edb5 | 183 | int swap_list_get(const char *swaps, MountPoint **head) { |
71ae04c4 ZJS |
184 | _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL; |
185 | _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL; | |
527b7a42 | 186 | int r; |
e3478379 | 187 | |
12aad1d0 LP |
188 | assert(head); |
189 | ||
71ae04c4 ZJS |
190 | t = mnt_new_table(); |
191 | i = mnt_new_iter(MNT_ITER_FORWARD); | |
192 | if (!t || !i) | |
193 | return log_oom(); | |
e3478379 | 194 | |
71ae04c4 ZJS |
195 | r = mnt_table_parse_swaps(t, swaps); |
196 | if (r < 0) | |
197 | return log_error_errno(r, "Failed to parse %s: %m", swaps); | |
198 | ||
199 | for (;;) { | |
200 | struct libmnt_fs *fs; | |
e3478379 | 201 | |
e3478379 | 202 | MountPoint *swap; |
71ae04c4 ZJS |
203 | const char *source; |
204 | _cleanup_free_ char *d = NULL; | |
205 | ||
206 | r = mnt_table_next_fs(t, i, &fs); | |
207 | if (r == 1) | |
208 | break; | |
209 | if (r < 0) | |
210 | return log_error_errno(r, "Failed to get next entry from %s: %m", swaps); | |
e3478379 | 211 | |
71ae04c4 ZJS |
212 | source = mnt_fs_get_source(fs); |
213 | if (!source) | |
e3478379 | 214 | continue; |
e3478379 | 215 | |
71ae04c4 | 216 | r = cunescape(source, UNESCAPE_RELAX, &d); |
527b7a42 LP |
217 | if (r < 0) |
218 | return r; | |
e3478379 | 219 | |
527b7a42 | 220 | swap = new0(MountPoint, 1); |
595c66a3 | 221 | if (!swap) |
e1d75803 | 222 | return -ENOMEM; |
e3478379 | 223 | |
595c66a3 | 224 | free_and_replace(swap->path, d); |
71fda00f | 225 | LIST_PREPEND(mount_point, *head, swap); |
e3478379 FF |
226 | } |
227 | ||
e1d75803 | 228 | return 0; |
e3478379 FF |
229 | } |
230 | ||
12aad1d0 | 231 | static int loopback_list_get(MountPoint **head) { |
6bcf00ed YW |
232 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
233 | sd_device *d; | |
06acf2d4 | 234 | int r; |
e3478379 | 235 | |
12aad1d0 LP |
236 | assert(head); |
237 | ||
6bcf00ed YW |
238 | r = sd_device_enumerator_new(&e); |
239 | if (r < 0) | |
240 | return r; | |
e3478379 | 241 | |
6bcf00ed YW |
242 | r = sd_device_enumerator_allow_uninitialized(e); |
243 | if (r < 0) | |
244 | return r; | |
e3478379 | 245 | |
6bcf00ed | 246 | r = sd_device_enumerator_add_match_subsystem(e, "block", true); |
06acf2d4 LP |
247 | if (r < 0) |
248 | return r; | |
249 | ||
6bcf00ed | 250 | r = sd_device_enumerator_add_match_sysname(e, "loop*"); |
06acf2d4 LP |
251 | if (r < 0) |
252 | return r; | |
253 | ||
6bcf00ed | 254 | r = sd_device_enumerator_add_match_sysattr(e, "loop/backing_file", NULL, true); |
06acf2d4 LP |
255 | if (r < 0) |
256 | return r; | |
e3478379 | 257 | |
8437c059 | 258 | FOREACH_DEVICE(e, d) { |
209c6f03 | 259 | _cleanup_free_ char *p = NULL; |
6bcf00ed | 260 | const char *dn; |
209c6f03 | 261 | MountPoint *lb; |
e3478379 | 262 | |
6bcf00ed | 263 | if (sd_device_get_devname(d, &dn) < 0) |
2d9a3397 | 264 | continue; |
b854a7e7 | 265 | |
209c6f03 YW |
266 | p = strdup(dn); |
267 | if (!p) | |
268 | return -ENOMEM; | |
269 | ||
270 | lb = new(MountPoint, 1); | |
a6dcd229 | 271 | if (!lb) |
1ca208fb | 272 | return -ENOMEM; |
e3478379 | 273 | |
209c6f03 YW |
274 | *lb = (MountPoint) { |
275 | .path = TAKE_PTR(p), | |
276 | }; | |
a6dcd229 | 277 | |
71fda00f | 278 | LIST_PREPEND(mount_point, *head, lb); |
e3478379 FF |
279 | } |
280 | ||
1ca208fb | 281 | return 0; |
e3478379 FF |
282 | } |
283 | ||
12aad1d0 | 284 | static int dm_list_get(MountPoint **head) { |
6bcf00ed YW |
285 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
286 | sd_device *d; | |
06acf2d4 | 287 | int r; |
d48141ba | 288 | |
12aad1d0 LP |
289 | assert(head); |
290 | ||
6bcf00ed YW |
291 | r = sd_device_enumerator_new(&e); |
292 | if (r < 0) | |
293 | return r; | |
d48141ba | 294 | |
6bcf00ed YW |
295 | r = sd_device_enumerator_allow_uninitialized(e); |
296 | if (r < 0) | |
297 | return r; | |
d48141ba | 298 | |
6bcf00ed | 299 | r = sd_device_enumerator_add_match_subsystem(e, "block", true); |
06acf2d4 LP |
300 | if (r < 0) |
301 | return r; | |
d48141ba | 302 | |
6bcf00ed | 303 | r = sd_device_enumerator_add_match_sysname(e, "dm-*"); |
06acf2d4 LP |
304 | if (r < 0) |
305 | return r; | |
d48141ba | 306 | |
8437c059 | 307 | FOREACH_DEVICE(e, d) { |
209c6f03 | 308 | _cleanup_free_ char *p = NULL; |
6bcf00ed | 309 | const char *dn; |
209c6f03 | 310 | MountPoint *m; |
6bcf00ed | 311 | dev_t devnum; |
d48141ba | 312 | |
209c6f03 YW |
313 | if (sd_device_get_devnum(d, &devnum) < 0 || |
314 | sd_device_get_devname(d, &dn) < 0) | |
6bcf00ed | 315 | continue; |
d48141ba | 316 | |
209c6f03 YW |
317 | p = strdup(dn); |
318 | if (!p) | |
319 | return -ENOMEM; | |
d48141ba | 320 | |
209c6f03 | 321 | m = new(MountPoint, 1); |
a6dcd229 | 322 | if (!m) |
1ca208fb | 323 | return -ENOMEM; |
d48141ba | 324 | |
209c6f03 YW |
325 | *m = (MountPoint) { |
326 | .path = TAKE_PTR(p), | |
327 | .devnum = devnum, | |
328 | }; | |
a6dcd229 | 329 | |
71fda00f | 330 | LIST_PREPEND(mount_point, *head, m); |
d48141ba LP |
331 | } |
332 | ||
1ca208fb | 333 | return 0; |
d48141ba LP |
334 | } |
335 | ||
e3478379 | 336 | static int delete_loopback(const char *device) { |
03e334a1 LP |
337 | _cleanup_close_ int fd = -1; |
338 | int r; | |
e3478379 | 339 | |
0494cae0 JJ |
340 | assert(device); |
341 | ||
03e334a1 LP |
342 | fd = open(device, O_RDONLY|O_CLOEXEC); |
343 | if (fd < 0) | |
c4f8bd1a | 344 | return errno == ENOENT ? 0 : -errno; |
e3478379 | 345 | |
ce726252 | 346 | r = ioctl(fd, LOOP_CLR_FD, 0); |
12aad1d0 LP |
347 | if (r >= 0) |
348 | return 1; | |
349 | ||
ce726252 | 350 | /* ENXIO: not bound, so no error */ |
12aad1d0 LP |
351 | if (errno == ENXIO) |
352 | return 0; | |
353 | ||
354 | return -errno; | |
e3478379 FF |
355 | } |
356 | ||
2d9a3397 | 357 | static int delete_dm(dev_t devnum) { |
cf139e60 | 358 | |
b92bea5d | 359 | struct dm_ioctl dm = { |
cf139e60 LP |
360 | .version = { |
361 | DM_VERSION_MAJOR, | |
362 | DM_VERSION_MINOR, | |
363 | DM_VERSION_PATCHLEVEL | |
364 | }, | |
b92bea5d ZJS |
365 | .data_size = sizeof(dm), |
366 | .dev = devnum, | |
367 | }; | |
d48141ba | 368 | |
cf139e60 LP |
369 | _cleanup_close_ int fd = -1; |
370 | ||
2d9a3397 | 371 | assert(major(devnum) != 0); |
d48141ba | 372 | |
e62d8c39 ZJS |
373 | fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC); |
374 | if (fd < 0) | |
d48141ba LP |
375 | return -errno; |
376 | ||
cf139e60 LP |
377 | if (ioctl(fd, DM_DEV_REMOVE, &dm) < 0) |
378 | return -errno; | |
379 | ||
380 | return 0; | |
d48141ba LP |
381 | } |
382 | ||
c826cd3f ZJS |
383 | static bool nonunmountable_path(const char *path) { |
384 | return path_equal(path, "/") | |
349cc4a5 | 385 | #if ! HAVE_SPLIT_USR |
c826cd3f ZJS |
386 | || path_equal(path, "/usr") |
387 | #endif | |
388 | || path_startswith(path, "/run/initramfs"); | |
389 | } | |
390 | ||
456b2199 | 391 | static int remount_with_timeout(MountPoint *m, int umount_log_level) { |
d5641e0d KW |
392 | pid_t pid; |
393 | int r; | |
394 | ||
395 | BLOCK_SIGNALS(SIGCHLD); | |
396 | ||
0494cae0 | 397 | assert(m); |
0494cae0 | 398 | |
d5641e0d KW |
399 | /* Due to the possiblity of a remount operation hanging, we |
400 | * fork a child process and set a timeout. If the timeout | |
401 | * lapses, the assumption is that that particular remount | |
402 | * failed. */ | |
0b1f3c76 | 403 | r = safe_fork("(sd-remount)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_REOPEN_LOG, &pid); |
4c253ed1 | 404 | if (r < 0) |
b6e1fff1 | 405 | return r; |
4c253ed1 | 406 | if (r == 0) { |
3bc341be | 407 | log_info("Remounting '%s' read-only in with options '%s'.", m->path, m->remount_options); |
d5641e0d KW |
408 | |
409 | /* Start the mount operation here in the child */ | |
3bc341be | 410 | r = mount(NULL, m->path, NULL, m->remount_flags, m->remount_options); |
d5641e0d | 411 | if (r < 0) |
456b2199 | 412 | log_full_errno(umount_log_level, errno, "Failed to remount '%s' read-only: %m", m->path); |
d5641e0d KW |
413 | |
414 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); | |
415 | } | |
416 | ||
417 | r = wait_for_terminate_with_timeout(pid, DEFAULT_TIMEOUT_USEC); | |
418 | if (r == -ETIMEDOUT) { | |
00adeed9 | 419 | log_error_errno(r, "Remounting '%s' timed out, issuing SIGKILL to PID " PID_FMT ".", m->path, pid); |
d5641e0d | 420 | (void) kill(pid, SIGKILL); |
00adeed9 | 421 | } else if (r == -EPROTO) |
456b2199 | 422 | log_debug_errno(r, "Remounting '%s' failed abnormally, child process " PID_FMT " aborted or exited non-zero.", m->path, pid); |
00adeed9 LP |
423 | else if (r < 0) |
424 | log_error_errno(r, "Remounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT ": %m", m->path, pid); | |
d5641e0d KW |
425 | |
426 | return r; | |
427 | } | |
428 | ||
456b2199 | 429 | static int umount_with_timeout(MountPoint *m, int umount_log_level) { |
d5641e0d KW |
430 | pid_t pid; |
431 | int r; | |
432 | ||
433 | BLOCK_SIGNALS(SIGCHLD); | |
434 | ||
0494cae0 JJ |
435 | assert(m); |
436 | ||
d5641e0d KW |
437 | /* Due to the possiblity of a umount operation hanging, we |
438 | * fork a child process and set a timeout. If the timeout | |
439 | * lapses, the assumption is that that particular umount | |
440 | * failed. */ | |
0b1f3c76 | 441 | r = safe_fork("(sd-umount)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_REOPEN_LOG, &pid); |
4c253ed1 | 442 | if (r < 0) |
b6e1fff1 | 443 | return r; |
4c253ed1 | 444 | if (r == 0) { |
d5641e0d KW |
445 | log_info("Unmounting '%s'.", m->path); |
446 | ||
447 | /* Start the mount operation here in the child Using MNT_FORCE | |
448 | * causes some filesystems (e.g. FUSE and NFS and other network | |
449 | * filesystems) to abort any pending requests and return -EIO | |
450 | * rather than blocking indefinitely. If the filesysten is | |
451 | * "busy", this may allow processes to die, thus making the | |
452 | * filesystem less busy so the unmount might succeed (rather | |
453 | * then return EBUSY).*/ | |
454 | r = umount2(m->path, MNT_FORCE); | |
455 | if (r < 0) | |
456b2199 | 456 | log_full_errno(umount_log_level, errno, "Failed to unmount %s: %m", m->path); |
d5641e0d KW |
457 | |
458 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); | |
459 | } | |
460 | ||
461 | r = wait_for_terminate_with_timeout(pid, DEFAULT_TIMEOUT_USEC); | |
462 | if (r == -ETIMEDOUT) { | |
00adeed9 | 463 | log_error_errno(r, "Unmounting '%s' timed out, issuing SIGKILL to PID " PID_FMT ".", m->path, pid); |
d5641e0d | 464 | (void) kill(pid, SIGKILL); |
00adeed9 | 465 | } else if (r == -EPROTO) |
456b2199 | 466 | log_debug_errno(r, "Unmounting '%s' failed abnormally, child process " PID_FMT " aborted or exited non-zero.", m->path, pid); |
00adeed9 LP |
467 | else if (r < 0) |
468 | log_error_errno(r, "Unmounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT ": %m", m->path, pid); | |
d5641e0d KW |
469 | |
470 | return r; | |
471 | } | |
472 | ||
116e6d96 AJ |
473 | /* This includes remounting readonly, which changes the kernel mount options. |
474 | * Therefore the list passed to this function is invalidated, and should not be reused. */ | |
456b2199 | 475 | static int mount_points_list_umount(MountPoint **head, bool *changed, int umount_log_level) { |
116e6d96 | 476 | MountPoint *m; |
12aad1d0 | 477 | int n_failed = 0; |
e3478379 | 478 | |
12aad1d0 | 479 | assert(head); |
0494cae0 | 480 | assert(changed); |
12aad1d0 | 481 | |
116e6d96 | 482 | LIST_FOREACH(mount_point, m, *head) { |
1d62d22d | 483 | if (m->try_remount_ro) { |
93bd1577 LP |
484 | /* We always try to remount directories |
485 | * read-only first, before we go on and umount | |
486 | * them. | |
487 | * | |
488 | * Mount points can be stacked. If a mount | |
489 | * point is stacked below / or /usr, we | |
ab06eef8 | 490 | * cannot umount or remount it directly, |
93bd1577 LP |
491 | * since there is no way to refer to the |
492 | * underlying mount. There's nothing we can do | |
493 | * about it for the general case, but we can | |
494 | * do something about it if it is aliased | |
495 | * somehwere else via a bind mount. If we | |
496 | * explicitly remount the super block of that | |
497 | * alias read-only we hence should be | |
8645ffd1 | 498 | * relatively safe regarding keeping a dirty fs |
d5641e0d KW |
499 | * we cannot otherwise see. |
500 | * | |
501 | * Since the remount can hang in the instance of | |
502 | * remote filesystems, we remount asynchronously | |
8645ffd1 | 503 | * and skip the subsequent umount if it fails. */ |
456b2199 | 504 | if (remount_with_timeout(m, umount_log_level) < 0) { |
8645ffd1 JJ |
505 | /* Remount failed, but try unmounting anyway, |
506 | * unless this is a mount point we want to skip. */ | |
507 | if (nonunmountable_path(m->path)) { | |
c826cd3f | 508 | n_failed++; |
8645ffd1 JJ |
509 | continue; |
510 | } | |
c826cd3f | 511 | } |
93bd1577 LP |
512 | } |
513 | ||
514 | /* Skip / and /usr since we cannot unmount that | |
5a6f9d23 HG |
515 | * anyway, since we are running from it. They have |
516 | * already been remounted ro. */ | |
c826cd3f | 517 | if (nonunmountable_path(m->path)) |
e3478379 FF |
518 | continue; |
519 | ||
d5641e0d | 520 | /* Trying to umount */ |
456b2199 | 521 | if (umount_with_timeout(m, umount_log_level) < 0) |
d5641e0d | 522 | n_failed++; |
0494cae0 JJ |
523 | else |
524 | *changed = true; | |
e3478379 FF |
525 | } |
526 | ||
12aad1d0 | 527 | return n_failed; |
e3478379 FF |
528 | } |
529 | ||
12aad1d0 LP |
530 | static int swap_points_list_off(MountPoint **head, bool *changed) { |
531 | MountPoint *m, *n; | |
532 | int n_failed = 0; | |
533 | ||
534 | assert(head); | |
0494cae0 | 535 | assert(changed); |
e3478379 | 536 | |
12aad1d0 | 537 | LIST_FOREACH_SAFE(mount_point, m, n, *head) { |
735e0712 | 538 | log_info("Deactivating swap %s.", m->path); |
12aad1d0 | 539 | if (swapoff(m->path) == 0) { |
0494cae0 | 540 | *changed = true; |
12aad1d0 LP |
541 | mount_point_free(head, m); |
542 | } else { | |
56f64d95 | 543 | log_warning_errno(errno, "Could not deactivate swap %s: %m", m->path); |
12aad1d0 | 544 | n_failed++; |
e3478379 FF |
545 | } |
546 | } | |
547 | ||
12aad1d0 | 548 | return n_failed; |
e3478379 FF |
549 | } |
550 | ||
456b2199 | 551 | static int loopback_points_list_detach(MountPoint **head, bool *changed, int umount_log_level) { |
12aad1d0 | 552 | MountPoint *m, *n; |
7fc942b2 LP |
553 | int n_failed = 0, k; |
554 | struct stat root_st; | |
12aad1d0 LP |
555 | |
556 | assert(head); | |
0494cae0 | 557 | assert(changed); |
12aad1d0 | 558 | |
7fc942b2 LP |
559 | k = lstat("/", &root_st); |
560 | ||
12aad1d0 LP |
561 | LIST_FOREACH_SAFE(mount_point, m, n, *head) { |
562 | int r; | |
7fc942b2 LP |
563 | struct stat loopback_st; |
564 | ||
565 | if (k >= 0 && | |
566 | major(root_st.st_dev) != 0 && | |
567 | lstat(m->path, &loopback_st) >= 0 && | |
568 | root_st.st_dev == loopback_st.st_rdev) { | |
313cefa1 | 569 | n_failed++; |
7fc942b2 LP |
570 | continue; |
571 | } | |
e3478379 | 572 | |
735e0712 | 573 | log_info("Detaching loopback %s.", m->path); |
bce93b7a MS |
574 | r = delete_loopback(m->path); |
575 | if (r >= 0) { | |
0494cae0 | 576 | if (r > 0) |
12aad1d0 LP |
577 | *changed = true; |
578 | ||
579 | mount_point_free(head, m); | |
580 | } else { | |
456b2199 | 581 | log_full_errno(umount_log_level, errno, "Could not detach loopback %s: %m", m->path); |
12aad1d0 | 582 | n_failed++; |
e3478379 FF |
583 | } |
584 | } | |
585 | ||
12aad1d0 | 586 | return n_failed; |
e3478379 FF |
587 | } |
588 | ||
456b2199 | 589 | static int dm_points_list_detach(MountPoint **head, bool *changed, int umount_log_level) { |
12aad1d0 | 590 | MountPoint *m, *n; |
33e8d8af FB |
591 | int n_failed = 0, r; |
592 | dev_t rootdev; | |
12aad1d0 LP |
593 | |
594 | assert(head); | |
0494cae0 | 595 | assert(changed); |
12aad1d0 | 596 | |
33e8d8af FB |
597 | r = get_block_device("/", &rootdev); |
598 | if (r <= 0) | |
599 | rootdev = 0; | |
7fc942b2 | 600 | |
12aad1d0 | 601 | LIST_FOREACH_SAFE(mount_point, m, n, *head) { |
12aad1d0 | 602 | |
0494cae0 JJ |
603 | if (major(rootdev) != 0 && rootdev == m->devnum) { |
604 | n_failed ++; | |
605 | continue; | |
606 | } | |
7fc942b2 | 607 | |
735e0712 | 608 | log_info("Detaching DM %u:%u.", major(m->devnum), minor(m->devnum)); |
bce93b7a MS |
609 | r = delete_dm(m->devnum); |
610 | if (r >= 0) { | |
0494cae0 | 611 | *changed = true; |
12aad1d0 LP |
612 | mount_point_free(head, m); |
613 | } else { | |
456b2199 | 614 | log_full_errno(umount_log_level, errno, "Could not detach DM %s: %m", m->path); |
12aad1d0 | 615 | n_failed++; |
d48141ba LP |
616 | } |
617 | } | |
618 | ||
12aad1d0 | 619 | return n_failed; |
d48141ba LP |
620 | } |
621 | ||
456b2199 | 622 | static int umount_all_once(bool *changed, int umount_log_level) { |
e3478379 | 623 | int r; |
a6dcd229 | 624 | _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head); |
e3478379 | 625 | |
0494cae0 JJ |
626 | assert(changed); |
627 | ||
71fda00f | 628 | LIST_HEAD_INIT(mp_list_head); |
6fa392bf | 629 | r = mount_points_list_get(NULL, &mp_list_head); |
e3478379 | 630 | if (r < 0) |
a6dcd229 | 631 | return r; |
116e6d96 | 632 | |
a6dcd229 | 633 | return mount_points_list_umount(&mp_list_head, changed, umount_log_level); |
116e6d96 AJ |
634 | } |
635 | ||
456b2199 | 636 | int umount_all(bool *changed, int umount_log_level) { |
116e6d96 AJ |
637 | bool umount_changed; |
638 | int r; | |
639 | ||
0494cae0 JJ |
640 | assert(changed); |
641 | ||
d5641e0d KW |
642 | /* Retry umount, until nothing can be umounted anymore. Mounts are |
643 | * processed in order, newest first. The retries are needed when | |
644 | * an old mount has been moved, to a path inside a newer mount. */ | |
6f7f51f7 HH |
645 | do { |
646 | umount_changed = false; | |
3e085b6c | 647 | |
456b2199 | 648 | r = umount_all_once(&umount_changed, umount_log_level); |
6f7f51f7 HH |
649 | if (umount_changed) |
650 | *changed = true; | |
3e085b6c LP |
651 | } while (umount_changed); |
652 | ||
e3478379 FF |
653 | return r; |
654 | } | |
655 | ||
12aad1d0 | 656 | int swapoff_all(bool *changed) { |
a6dcd229 | 657 | _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, swap_list_head); |
e3478379 | 658 | int r; |
e3478379 | 659 | |
0494cae0 JJ |
660 | assert(changed); |
661 | ||
71fda00f | 662 | LIST_HEAD_INIT(swap_list_head); |
e3478379 | 663 | |
1fd8edb5 | 664 | r = swap_list_get(NULL, &swap_list_head); |
e3478379 | 665 | if (r < 0) |
a6dcd229 | 666 | return r; |
e3478379 | 667 | |
a6dcd229 | 668 | return swap_points_list_off(&swap_list_head, changed); |
e3478379 FF |
669 | } |
670 | ||
456b2199 | 671 | int loopback_detach_all(bool *changed, int umount_log_level) { |
a6dcd229 | 672 | _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, loopback_list_head); |
e3478379 | 673 | int r; |
e3478379 | 674 | |
0494cae0 JJ |
675 | assert(changed); |
676 | ||
71fda00f | 677 | LIST_HEAD_INIT(loopback_list_head); |
e3478379 FF |
678 | |
679 | r = loopback_list_get(&loopback_list_head); | |
680 | if (r < 0) | |
a6dcd229 | 681 | return r; |
e3478379 | 682 | |
a6dcd229 | 683 | return loopback_points_list_detach(&loopback_list_head, changed, umount_log_level); |
e3478379 | 684 | } |
d48141ba | 685 | |
456b2199 | 686 | int dm_detach_all(bool *changed, int umount_log_level) { |
a6dcd229 | 687 | _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, dm_list_head); |
d48141ba | 688 | int r; |
d48141ba | 689 | |
0494cae0 JJ |
690 | assert(changed); |
691 | ||
71fda00f | 692 | LIST_HEAD_INIT(dm_list_head); |
d48141ba LP |
693 | |
694 | r = dm_list_get(&dm_list_head); | |
695 | if (r < 0) | |
a6dcd229 | 696 | return r; |
d48141ba | 697 | |
a6dcd229 | 698 | return dm_points_list_detach(&dm_list_head, changed, umount_log_level); |
d48141ba | 699 | } |