1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 ProFUSION embedded systems
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/loop.h>
25 #include <sys/mount.h>
30 #include "alloc-util.h"
31 #include "blockdev-util.h"
35 #include "fstab-util.h"
36 #include "linux-3.13/dm-ioctl.h"
38 #include "mount-setup.h"
39 #include "mount-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
42 #include "signal-util.h"
43 #include "string-util.h"
44 #include "udev-util.h"
49 typedef struct MountPoint
{
54 LIST_FIELDS(struct MountPoint
, mount_point
);
57 static void mount_point_free(MountPoint
**head
, MountPoint
*m
) {
61 LIST_REMOVE(mount_point
, *head
, m
);
67 static void mount_points_list_free(MountPoint
**head
) {
71 mount_point_free(head
, *head
);
74 static int mount_points_list_get(MountPoint
**head
) {
75 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
81 proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
82 if (!proc_self_mountinfo
)
86 _cleanup_free_
char *path
= NULL
, *options
= NULL
, *type
= NULL
;
91 k
= fscanf(proc_self_mountinfo
,
92 "%*s " /* (1) mount id */
93 "%*s " /* (2) parent id */
94 "%*s " /* (3) major:minor */
96 "%ms " /* (5) mount point */
97 "%*s" /* (6) mount flags */
98 "%*[^-]" /* (7) optional fields */
99 "- " /* (8) separator */
100 "%ms " /* (9) file system type */
101 "%*s" /* (10) mount source */
102 "%ms" /* (11) mount options */
103 "%*[^\n]", /* some rubbish at the end */
104 &path
, &type
, &options
);
109 log_warning("Failed to parse /proc/self/mountinfo:%u.", i
);
113 r
= cunescape(path
, UNESCAPE_RELAX
, &p
);
117 /* Ignore mount points we can't unmount because they
118 * are API or because we are keeping them open (like
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. */
124 if (mount_point_is_api(p
) ||
125 mount_point_ignore(p
) ||
126 path_startswith(p
, "/dev") ||
127 path_startswith(p
, "/sys") ||
128 path_startswith(p
, "/proc")) {
133 m
= new0(MountPoint
, 1);
140 m
->options
= options
;
145 LIST_PREPEND(mount_point
, *head
, m
);
151 static int swap_list_get(MountPoint
**head
) {
152 _cleanup_fclose_
FILE *proc_swaps
= NULL
;
158 proc_swaps
= fopen("/proc/swaps", "re");
160 return (errno
== ENOENT
) ? 0 : -errno
;
162 (void) fscanf(proc_swaps
, "%*s %*s %*s %*s %*s\n");
166 _cleanup_free_
char *dev
= NULL
, *d
= NULL
;
169 k
= fscanf(proc_swaps
,
170 "%ms " /* device/file */
171 "%*s " /* type of swap */
172 "%*s " /* swap size */
174 "%*s\n", /* priority */
181 log_warning("Failed to parse /proc/swaps:%u.", i
);
185 if (endswith(dev
, " (deleted)"))
188 r
= cunescape(dev
, UNESCAPE_RELAX
, &d
);
192 swap
= new0(MountPoint
, 1);
196 free_and_replace(swap
->path
, d
);
197 LIST_PREPEND(mount_point
, *head
, swap
);
203 static int loopback_list_get(MountPoint
**head
) {
204 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
205 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
206 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
215 e
= udev_enumerate_new(udev
);
219 r
= udev_enumerate_add_match_subsystem(e
, "block");
223 r
= udev_enumerate_add_match_sysname(e
, "loop*");
227 r
= udev_enumerate_add_match_sysattr(e
, "loop/backing_file", NULL
);
231 r
= udev_enumerate_scan_devices(e
);
235 first
= udev_enumerate_get_list_entry(e
);
236 udev_list_entry_foreach(item
, first
) {
238 _cleanup_udev_device_unref_
struct udev_device
*d
;
242 d
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
246 dn
= udev_device_get_devnode(d
);
254 lb
= new0(MountPoint
, 1);
261 LIST_PREPEND(mount_point
, *head
, lb
);
267 static int dm_list_get(MountPoint
**head
) {
268 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
269 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
270 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
279 e
= udev_enumerate_new(udev
);
283 r
= udev_enumerate_add_match_subsystem(e
, "block");
287 r
= udev_enumerate_add_match_sysname(e
, "dm-*");
291 r
= udev_enumerate_scan_devices(e
);
295 first
= udev_enumerate_get_list_entry(e
);
296 udev_list_entry_foreach(item
, first
) {
298 _cleanup_udev_device_unref_
struct udev_device
*d
;
303 d
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
307 devnum
= udev_device_get_devnum(d
);
308 dn
= udev_device_get_devnode(d
);
309 if (major(devnum
) == 0 || !dn
)
316 m
= new(MountPoint
, 1);
324 LIST_PREPEND(mount_point
, *head
, m
);
330 static int delete_loopback(const char *device
) {
331 _cleanup_close_
int fd
= -1;
334 fd
= open(device
, O_RDONLY
|O_CLOEXEC
);
336 return errno
== ENOENT
? 0 : -errno
;
338 r
= ioctl(fd
, LOOP_CLR_FD
, 0);
342 /* ENXIO: not bound, so no error */
349 static int delete_dm(dev_t devnum
) {
351 struct dm_ioctl dm
= {
355 DM_VERSION_PATCHLEVEL
357 .data_size
= sizeof(dm
),
361 _cleanup_close_
int fd
= -1;
363 assert(major(devnum
) != 0);
365 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
369 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
) < 0)
375 static bool nonunmountable_path(const char *path
) {
376 return path_equal(path
, "/")
378 || path_equal(path
, "/usr")
380 || path_startswith(path
, "/run/initramfs");
383 static int remount_with_timeout(MountPoint
*m
, char *options
, int *n_failed
) {
387 BLOCK_SIGNALS(SIGCHLD
);
389 /* Due to the possiblity of a remount operation hanging, we
390 * fork a child process and set a timeout. If the timeout
391 * lapses, the assumption is that that particular remount
393 r
= safe_fork("(sd-remount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
397 log_info("Remounting '%s' read-only in with options '%s'.", m
->path
, options
);
399 /* Start the mount operation here in the child */
400 r
= mount(NULL
, m
->path
, NULL
, MS_REMOUNT
|MS_RDONLY
, options
);
402 log_error_errno(errno
, "Failed to remount '%s' read-only: %m", m
->path
);
404 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
407 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
408 if (r
== -ETIMEDOUT
) {
409 log_error_errno(errno
, "Remounting '%s' - timed out, issuing SIGKILL to PID "PID_FMT
".", m
->path
, pid
);
410 (void) kill(pid
, SIGKILL
);
412 log_error_errno(r
, "Failed to wait for process: %m");
417 static int umount_with_timeout(MountPoint
*m
, bool *changed
) {
421 BLOCK_SIGNALS(SIGCHLD
);
423 /* Due to the possiblity of a umount operation hanging, we
424 * fork a child process and set a timeout. If the timeout
425 * lapses, the assumption is that that particular umount
427 r
= safe_fork("(sd-umount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
, &pid
);
431 log_info("Unmounting '%s'.", m
->path
);
433 /* Start the mount operation here in the child Using MNT_FORCE
434 * causes some filesystems (e.g. FUSE and NFS and other network
435 * filesystems) to abort any pending requests and return -EIO
436 * rather than blocking indefinitely. If the filesysten is
437 * "busy", this may allow processes to die, thus making the
438 * filesystem less busy so the unmount might succeed (rather
439 * then return EBUSY).*/
440 r
= umount2(m
->path
, MNT_FORCE
);
442 log_error_errno(errno
, "Failed to unmount %s: %m", m
->path
);
444 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
447 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
448 if (r
== -ETIMEDOUT
) {
449 log_error_errno(errno
, "Unmounting '%s' - timed out, issuing SIGKILL to PID "PID_FMT
".", m
->path
, pid
);
450 (void) kill(pid
, SIGKILL
);
452 log_error_errno(r
, "Failed to wait for process: %m");
457 /* This includes remounting readonly, which changes the kernel mount options.
458 * Therefore the list passed to this function is invalidated, and should not be reused. */
460 static int mount_points_list_umount(MountPoint
**head
, bool *changed
) {
466 LIST_FOREACH(mount_point
, m
, *head
) {
467 bool mount_is_readonly
;
469 mount_is_readonly
= fstab_test_yes_no_option(m
->options
, "ro\0rw\0");
471 /* If we are in a container, don't attempt to
472 read-only mount anything as that brings no real
473 benefits, but might confuse the host, as we remount
474 the superblock here, not the bind mount.
475 If the filesystem is a network fs, also skip the
476 remount. It brings no value (we cannot leave
477 a "dirty fs") and could hang if the network is down.
478 Note that umount2() is more careful and will not
479 hang because of the network being down. */
480 if (detect_container() <= 0 &&
481 !fstype_is_network(m
->type
) &&
482 !mount_is_readonly
) {
483 _cleanup_free_
char *options
= NULL
;
484 /* MS_REMOUNT requires that the data parameter
485 * should be the same from the original mount
486 * except for the desired changes. Since we want
487 * to remount read-only, we should filter out
488 * rw (and ro too, because it confuses the kernel) */
489 (void) fstab_filter_options(m
->options
, "rw\0ro\0", NULL
, NULL
, &options
);
491 /* We always try to remount directories
492 * read-only first, before we go on and umount
495 * Mount points can be stacked. If a mount
496 * point is stacked below / or /usr, we
497 * cannot umount or remount it directly,
498 * since there is no way to refer to the
499 * underlying mount. There's nothing we can do
500 * about it for the general case, but we can
501 * do something about it if it is aliased
502 * somehwere else via a bind mount. If we
503 * explicitly remount the super block of that
504 * alias read-only we hence should be
505 * relatively safe regarding keeping dirty an fs
506 * we cannot otherwise see.
508 * Since the remount can hang in the instance of
509 * remote filesystems, we remount asynchronously
510 * and skip the subsequent umount if it fails */
511 if (remount_with_timeout(m
, options
, &n_failed
) < 0) {
512 if (nonunmountable_path(m
->path
))
518 /* Skip / and /usr since we cannot unmount that
519 * anyway, since we are running from it. They have
520 * already been remounted ro. */
521 if (nonunmountable_path(m
->path
))
524 /* Trying to umount */
525 if (umount_with_timeout(m
, changed
) < 0)
536 static int swap_points_list_off(MountPoint
**head
, bool *changed
) {
542 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
543 log_info("Deactivating swap %s.", m
->path
);
544 if (swapoff(m
->path
) == 0) {
548 mount_point_free(head
, m
);
550 log_warning_errno(errno
, "Could not deactivate swap %s: %m", m
->path
);
558 static int loopback_points_list_detach(MountPoint
**head
, bool *changed
) {
565 k
= lstat("/", &root_st
);
567 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
569 struct stat loopback_st
;
572 major(root_st
.st_dev
) != 0 &&
573 lstat(m
->path
, &loopback_st
) >= 0 &&
574 root_st
.st_dev
== loopback_st
.st_rdev
) {
579 log_info("Detaching loopback %s.", m
->path
);
580 r
= delete_loopback(m
->path
);
582 if (r
> 0 && changed
)
585 mount_point_free(head
, m
);
587 log_warning_errno(errno
, "Could not detach loopback %s: %m", m
->path
);
595 static int dm_points_list_detach(MountPoint
**head
, bool *changed
) {
602 r
= get_block_device("/", &rootdev
);
606 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
608 if (major(rootdev
) != 0)
609 if (rootdev
== m
->devnum
) {
614 log_info("Detaching DM %u:%u.", major(m
->devnum
), minor(m
->devnum
));
615 r
= delete_dm(m
->devnum
);
620 mount_point_free(head
, m
);
622 log_warning_errno(errno
, "Could not detach DM %s: %m", m
->path
);
630 static int umount_all_once(bool *changed
) {
632 LIST_HEAD(MountPoint
, mp_list_head
);
634 LIST_HEAD_INIT(mp_list_head
);
635 r
= mount_points_list_get(&mp_list_head
);
639 r
= mount_points_list_umount(&mp_list_head
, changed
);
642 mount_points_list_free(&mp_list_head
);
647 int umount_all(bool *changed
) {
651 /* Retry umount, until nothing can be umounted anymore. Mounts are
652 * processed in order, newest first. The retries are needed when
653 * an old mount has been moved, to a path inside a newer mount. */
655 umount_changed
= false;
657 r
= umount_all_once(&umount_changed
);
660 } while (umount_changed
);
665 int swapoff_all(bool *changed
) {
667 LIST_HEAD(MountPoint
, swap_list_head
);
669 LIST_HEAD_INIT(swap_list_head
);
671 r
= swap_list_get(&swap_list_head
);
675 r
= swap_points_list_off(&swap_list_head
, changed
);
678 mount_points_list_free(&swap_list_head
);
683 int loopback_detach_all(bool *changed
) {
685 LIST_HEAD(MountPoint
, loopback_list_head
);
687 LIST_HEAD_INIT(loopback_list_head
);
689 r
= loopback_list_get(&loopback_list_head
);
693 r
= loopback_points_list_detach(&loopback_list_head
, changed
);
696 mount_points_list_free(&loopback_list_head
);
701 int dm_detach_all(bool *changed
) {
703 LIST_HEAD(MountPoint
, dm_list_head
);
705 LIST_HEAD_INIT(dm_list_head
);
707 r
= dm_list_get(&dm_list_head
);
711 r
= dm_points_list_detach(&dm_list_head
, changed
);
714 mount_points_list_free(&dm_list_head
);