diff options
author | Franck Bui <fbui@suse.com> | 2016-12-16 17:13:58 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2016-12-16 17:13:58 +0100 |
commit | ebc8968bc0b6fc460099041f5ae1262ca17eeb6e (patch) | |
tree | a1c3593e691fae26283c8bf2f38649c1e33b15d8 /src/core/device.c | |
parent | d6ccb4f9428102ac784f8ebd5d937d5363146c1d (diff) |
core: make mount units from /proc/self/mountinfo possibly bind to a device (#4515)
Since commit 9d06297, mount units from mountinfo are not bound to their devices
anymore (they use the "Requires" dependency instead).
This has the following drawback: if a media is mounted and the eject button is
pressed then the media is unconditionally ejected leaving some inconsistent
states.
Since udev is the component that is reacting (no matter if the device is used
or not) to the eject button, users expect that udev at least try to unmount the
media properly.
This patch introduces a new property "SYSTEMD_MOUNT_DEVICE_BOUND". When set on
a block device, all units that requires this device will see their "Requires"
dependency upgraded to a "BindTo" one. This is currently only used by cdrom
devices.
This patch also gives the possibility to the user to restore the previous
behavior that is bind a mount unit to a device. This is achieved by passing the
"x-systemd.device-bound" option to mount(8). Please note that currently this is
not working because libmount treats the x-* options has comments therefore
they're not available in utab for later application retrievals.
Diffstat (limited to 'src/core/device.c')
-rw-r--r-- | src/core/device.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/core/device.c b/src/core/device.c index e345552f24..bd481c8050 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -285,6 +285,37 @@ static int device_add_udev_wants(Unit *u, struct udev_device *dev) { } } +static bool device_is_bound_by_mounts(Unit *d, struct udev_device *dev) { + const char *bound_by; + int r = false; + + assert(d); + assert(dev); + + bound_by = udev_device_get_property_value(dev, "SYSTEMD_MOUNT_DEVICE_BOUND"); + if (bound_by) + r = parse_boolean(bound_by) > 0; + + DEVICE(d)->bind_mounts = r; + return r; +} + +static int device_upgrade_mount_deps(Unit *u) { + Unit *other; + Iterator i; + int r; + + SET_FOREACH(other, u->dependencies[UNIT_REQUIRED_BY], i) { + if (other->type != UNIT_MOUNT) + continue; + + r = unit_add_dependency(other, UNIT_BINDS_TO, u, true); + if (r < 0) + return r; + } + return 0; +} + static int device_setup_unit(Manager *m, struct udev_device *dev, const char *path, bool main) { _cleanup_free_ char *e = NULL; const char *sysfs = NULL; @@ -349,6 +380,13 @@ static int device_setup_unit(Manager *m, struct udev_device *dev, const char *pa (void) device_add_udev_wants(u, dev); } + /* So the user wants the mount units to be bound to the device but a + * mount unit might has been seen by systemd before the device appears + * on its radar. In this case the device unit is partially initialized + * and includes the deps on the mount unit but at that time the "bind + * mounts" flag wasn't not present. Fix this up now. */ + if (device_is_bound_by_mounts(u, dev)) + device_upgrade_mount_deps(u); /* Note that this won't dispatch the load queue, the caller * has to do that if needed and appropriate */ @@ -824,6 +862,14 @@ int device_found_node(Manager *m, const char *node, bool add, DeviceFound found, return device_update_found_by_name(m, node, add, found, now); } +bool device_shall_be_bound_by(Unit *device, Unit *u) { + + if (u->type != UNIT_MOUNT) + return false; + + return DEVICE(device)->bind_mounts; +} + const UnitVTable device_vtable = { .object_size = sizeof(Device), .sections = |