HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/mount/mount-tool.c

56,896 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 37732ec19fc67ecb38f39e1a59172aa8a2118c13

Lines 1001–1577 of 1,577

        return umount_by_device(bus, dev);
}
 
static int action_umount(sd_bus *bus, char **args) {
        int r, ret = 0;
 
        assert(bus);
        assert(args);
        assert(!strv_isempty(args));
 
        if (arg_transport != BUS_TRANSPORT_LOCAL || !arg_canonicalize) {
                STRV_FOREACH(arg, args) {
                        _cleanup_free_ char *p = NULL;
 
                        r = path_simplify_alloc(*arg, &p);
                        if (r < 0)
                                return r;
 
                        RET_GATHER(ret, stop_mounts(bus, p));
                }
                return ret;
        }
 
        STRV_FOREACH(arg, args) {
                _cleanup_free_ char *u = NULL, *p = NULL;
 
                u = fstab_node_to_udev_node(*arg);
                if (!u)
                        return log_oom();
 
                _cleanup_close_ int fd = -EBADF;
                r = chase(u, /* root= */ NULL, CHASE_TRIGGER_AUTOFS, &p, &fd);
                if (r < 0) {
                        RET_GATHER(ret, log_error_errno(r, "Failed to chase path '%s': %m", u));
                        continue;
                }
 
                struct stat st;
                if (fstat(fd, &st) < 0)
                        return log_error_errno(errno, "Can't stat '%s' (from %s): %m", p, *arg);
 
                r = is_mount_point_at(fd, /* path= */ NULL, /* flags= */ 0);
                fd = safe_close(fd); /* before continuing make sure the dir is not keeping anything busy */
                if (r > 0)
                        RET_GATHER(ret, stop_mounts(bus, p));
                else {
                        /* This can realistically fail on pre-5.8 kernels that do not tell us via statx() if
                         * something is a mount point, hence handle this gracefully, and go by type as we did
                         * in pre-v258 times. */
                        if (r < 0)
                                log_warning_errno(r, "Failed to determine if '%s' is a mount point, ignoring: %m", u);
 
                        if (S_ISDIR(st.st_mode))
                                r = stop_mounts(bus, p);
                        else if (S_ISBLK(st.st_mode))
                                r = umount_by_device_node(bus, p);
                        else if (S_ISREG(st.st_mode))
                                r = umount_loop(bus, p);
                        else
                                r = log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                    "Unknown file type for unmounting: %s (from %s)",
                                                    p, *arg);
                        RET_GATHER(ret, r);
                }
        }
 
        return ret;
}
 
static int acquire_mount_type(sd_device *d) {
        const char *v;
 
        assert(d);
 
        if (arg_mount_type)
                return 0;
 
        if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
                return 0;
 
        arg_mount_type = strdup(v);
        if (!arg_mount_type)
                return log_oom();
 
        log_debug("Discovered type=%s", arg_mount_type);
        return 1;
}
 
static int acquire_mount_options(sd_device *d) {
        const char *v;
 
        assert(d);
 
        if (arg_mount_options)
                return 0;
 
        if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
                return 0;
 
        arg_mount_options = strdup(v);
        if (!arg_mount_options)
                return log_oom();
 
        log_debug("Discovered options=%s", arg_mount_options);
        return 1;
}
 
static const char* get_label(sd_device *d) {
        const char *label;
 
        assert(d);
 
        if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0)
                return label;
 
        if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0)
                return label;
 
        return NULL;
}
 
static int acquire_mount_where(sd_device *d) {
        const char *v;
        int r;
 
        if (arg_mount_where)
                return 0;
 
        if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) {
                _cleanup_free_ char *escaped = NULL, *devname_bn = NULL;
                const char *name;
 
                name = get_label(d);
                if (!name)
                        (void) device_get_model_string(d, &name);
                if (!name) {
                        const char *dn;
 
                        if (sd_device_get_devname(d, &dn) < 0)
                                return 0;
 
                        r = path_extract_filename(dn, &devname_bn);
                        if (r < 0)
                                return log_error_errno(r, "Failed to extract file name from '%s': %m", dn);
 
                        name = devname_bn;
                }
 
                escaped = xescape(name, "\\");
                if (!escaped)
                        return log_oom();
                if (!filename_is_valid(escaped))
                        return 0;
 
                arg_mount_where = path_join("/run/media/system", escaped);
        } else
                arg_mount_where = strdup(v);
 
        if (!arg_mount_where)
                return log_oom();
 
        log_debug("Discovered where=%s", arg_mount_where);
        return 1;
}
 
static int acquire_mount_where_for_loop_dev(sd_device *dev) {
        _cleanup_strv_free_ char **list = NULL;
        const char *node;
        int r;
 
        assert(dev);
 
        if (arg_mount_where)
                return 0;
 
        r = sd_device_get_devname(dev, &node);
        if (r < 0)
                return r;
 
        r = find_mount_points_by_source(node, &list);
        if (r < 0)
                return r;
        if (r == 0)
                return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL),
                                              "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
                                              node, node);
        if (r >= 2)
                return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL),
                                              "%s is mounted on %d places. It is expected that %s is mounted on a place.",
                                              node, r, node);
 
        arg_mount_where = strdup(list[0]);
        if (!arg_mount_where)
                return log_oom();
 
        log_debug("Discovered where=%s", arg_mount_where);
        return 1;
}
 
static int acquire_description(sd_device *d) {
        const char *model = NULL, *label;
 
        if (arg_description)
                return 0;
 
        (void) device_get_model_string(d, &model);
 
        label = get_label(d);
        if (!label)
                (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
 
        if (model && label)
                arg_description = strjoin(model, " ", label);
        else if (label)
                arg_description = strdup(label);
        else if (model)
                arg_description = strdup(model);
        else
                return 0;
 
        if (!arg_description)
                return log_oom();
 
        log_debug("Discovered description=%s", arg_description);
        return 1;
}
 
static int acquire_removable(sd_device *d) {
        int r;
 
        assert(d);
 
        /* Shortcut this if there's no reason to check it */
        if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0)
                return 0;
 
        for (;;) {
                r = device_get_sysattr_bool(d, "removable");
                if (r == 0)
                        return 0; /* not a removable device */
                if (r > 0)
                        break;
                if (r != -ENOENT)
                        return log_device_debug_errno(d, r, "Failed to read 'removable' sysattr: %m");
 
                r = sd_device_get_parent(d, &d);
                if (r == -ENODEV)
                        return 0;
                if (r < 0)
                        return r;
 
                r = device_in_subsystem(d, "block");
                if (r <= 0)
                        return r;
        }
 
        log_debug("Discovered removable device.");
 
        if (arg_action == ACTION_DEFAULT) {
                log_debug("Automatically turning on automount.");
                arg_action = ACTION_AUTOMOUNT;
        }
 
        if (!arg_timeout_idle_set) {
                log_debug("Setting idle timeout to 1s.");
                arg_timeout_idle = USEC_PER_SEC;
        }
 
        if (arg_bind_device < 0) {
                log_debug("Binding automount unit to device.");
                arg_bind_device = true;
        }
 
        return 1;
}
 
static int discover_loop_backing_file(void) {
        _cleanup_(sd_device_unrefp) sd_device *d = NULL;
        int r;
 
        r = find_loop_device(arg_mount_what, &d);
        if (r == -ENXIO) {
                _cleanup_free_ char *escaped = NULL, *bn = NULL;
 
                if (arg_mount_where)
                        return 0;
 
                r = path_extract_filename(arg_mount_what, &bn);
                if (r < 0)
                        return log_error_errno(r, "Failed to extract file name from backing file path '%s': %m", arg_mount_what);
 
                escaped = xescape(bn, "\\");
                if (!escaped)
                        return log_oom();
                if (!filename_is_valid(escaped))
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "Escaped name %s is not a valid filename.",
                                               escaped);
 
                arg_mount_where = path_join("/run/media/system", escaped);
                if (!arg_mount_where)
                        return log_oom();
 
                log_debug("Discovered where=%s", arg_mount_where);
                return 0;
 
        } else if (r < 0)
                return log_error_errno(r, "Can't get loop device for %s: %m", arg_mount_what);
 
        r = acquire_mount_type(d);
        if (r < 0)
                return r;
 
        r = acquire_mount_options(d);
        if (r < 0)
                return r;
 
        r = acquire_mount_where_for_loop_dev(d);
        if (r < 0)
                return r;
 
        r = acquire_description(d);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int discover_device(void) {
        _cleanup_(sd_device_unrefp) sd_device *d = NULL;
        struct stat st;
        const char *v;
        int r;
 
        if (stat(arg_mount_what, &st) < 0)
                return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what);
 
        if (S_ISREG(st.st_mode))
                return discover_loop_backing_file();
 
        r = stat_verify_block(&st);
        if (r < 0)
                return log_error_errno(r, "Unsupported mount source type for --discover: %s", arg_mount_what);
 
        r = sd_device_new_from_stat_rdev(&d, &st);
        if (r < 0)
                return log_error_errno(r, "Failed to get device from device number: %m");
 
        if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem"))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "%s does not contain a known file system.",
                                       arg_mount_what);
 
        r = acquire_mount_type(d);
        if (r < 0)
                return r;
 
        r = acquire_mount_options(d);
        if (r < 0)
                return r;
 
        r = acquire_mount_where(d);
        if (r < 0)
                return r;
 
        r = acquire_description(d);
        if (r < 0)
                return r;
 
        r = acquire_removable(d);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int list_devices(void) {
        enum {
                COLUMN_NODE,
                COLUMN_DISKSEQ,
                COLUMN_PATH,
                COLUMN_MODEL,
                COLUMN_WWN,
                COLUMN_FSTYPE,
                COLUMN_LABEL,
                COLUMN_UUID,
                _COLUMN_MAX,
        };
 
        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
        _cleanup_(table_unrefp) Table *table = NULL;
        int r;
 
        r = sd_device_enumerator_new(&e);
        if (r < 0)
                return log_oom();
 
        r = sd_device_enumerator_add_match_subsystem(e, "block", true);
        if (r < 0)
                return log_error_errno(r, "Failed to add block match: %m");
 
        r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
        if (r < 0)
                return log_error_errno(r, "Failed to add property match: %m");
 
        table = table_new("node", "diskseq", "path", "model", "wwn", "fstype", "label", "uuid");
        if (!table)
                return log_oom();
 
        if (arg_full)
                table_set_width(table, 0);
 
        r = table_set_sort(table, (size_t) 0);
        if (r < 0)
                return log_error_errno(r, "Failed to set sort index: %m");
 
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
 
        FOREACH_DEVICE(e, d) {
                for (unsigned c = 0; c < _COLUMN_MAX; c++) {
                        const char *x = NULL;
 
                        switch (c) {
 
                        case COLUMN_NODE:
                                (void) sd_device_get_devname(d, &x);
                                break;
 
                        case COLUMN_DISKSEQ: {
                                uint64_t ds;
 
                                r = sd_device_get_diskseq(d, &ds);
                                if (r < 0) {
                                        log_debug_errno(r, "Failed to get diskseq of block device, ignoring: %m");
                                        r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
                                } else
                                        r = table_add_cell(table, NULL, TABLE_UINT64, &ds);
                                if (r < 0)
                                        return table_log_add_error(r);
 
                                continue;
                        }
 
                        case COLUMN_PATH:
                                (void) sd_device_get_property_value(d, "ID_PATH", &x);
                                break;
 
                        case COLUMN_MODEL:
                                (void) device_get_model_string(d, &x);
                                break;
 
                        case COLUMN_WWN:
                                (void) sd_device_get_property_value(d, "ID_WWN", &x);
                                break;
 
                        case COLUMN_FSTYPE:
                                (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
                                break;
 
                        case COLUMN_LABEL:
                                x = get_label(d);
                                break;
 
                        case COLUMN_UUID:
                                (void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
                                break;
                        }
 
                        r = table_add_cell(table, NULL, c == COLUMN_NODE ? TABLE_PATH : TABLE_STRING, x);
                        if (r < 0)
                                return table_log_add_error(r);
                }
        }
 
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
}
 
static int run(int argc, char* argv[]) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        char **args = NULL;
        int r;
 
        LIBMOUNT_NOTE(recommended);
 
        log_setup();
 
        r = parse_argv(argc, argv, &args);
        if (r <= 0)
                return r;
 
        if (arg_action == ACTION_LIST)
                return list_devices();
 
        r = bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, &bus);
        if (r < 0)
                return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
 
        (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
 
        if (arg_action == ACTION_UMOUNT)
                return action_umount(bus, args);
 
        if ((!arg_mount_type || fstype_is_blockdev_backed(arg_mount_type))
            && !path_is_normalized(arg_mount_what))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Path contains non-normalized components: %s",
                                       arg_mount_what);
 
        if (arg_discover) {
                r = discover_device();
                if (r < 0)
                        return r;
        }
 
        if (!arg_mount_where)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Can't figure out where to mount %s.",
                                       arg_mount_what);
 
        if (path_equal(arg_mount_where, "/"))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Refusing to operate on root directory.");
 
        if (!path_is_normalized(arg_mount_where))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Path contains non-normalized components: %s",
                                       arg_mount_where);
 
        if (streq_ptr(arg_mount_type, "auto"))
                arg_mount_type = mfree(arg_mount_type);
        if (streq_ptr(arg_mount_options, "defaults"))
                arg_mount_options = mfree(arg_mount_options);
 
        if (!is_device_path(arg_mount_what))
                arg_fsck = false;
 
        if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) {
                r = fsck_exists_for_fstype(arg_mount_type);
                if (r < 0)
                        log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type);
                else if (r == 0) {
                        log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type);
                        arg_fsck = false; /* fsck doesn't exist, let's not attempt it */
                }
        }
 
        /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
         * but not for all filesystem types. Let's try to catch the cases where the option
         * would be used if the file system does not support it. It is also possible to
         * autodetect the file system, but that's only possible with disk-based file systems
         * which incidentally seem to be implemented more carefully and reject unknown options,
         * so it's probably OK that we do the check only when the type is specified.
         */
        if (arg_mount_type &&
            !streq(arg_mount_type, "auto") &&
            arg_uid != UID_INVALID &&
            !fstype_can_uid_gid(arg_mount_type))
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                                       "File system type %s is not known to support uid=/gid=, refusing.",
                                       arg_mount_type);
 
        switch (arg_action) {
 
        case ACTION_MOUNT:
        case ACTION_DEFAULT:
                return start_transient_mount(bus);
 
        case ACTION_AUTOMOUNT:
                return start_transient_automount(bus);
 
        default:
                assert_not_reached();
        }
}
 
DEFINE_MAIN_FUNCTION(run);