mount service: use uevent-watch

This commit is contained in:
Daniel Barlow 2024-04-17 12:59:13 +01:00
parent 721e7499f3
commit 00076c7b81
4 changed files with 39 additions and 47 deletions

View file

@ -83,7 +83,7 @@ in rec {
}; };
services.mount_external_disk = svc.mount.build { services.mount_external_disk = svc.mount.build {
device = "LABEL=backup-disk"; partlabel = "backup-disk";
mountpoint = "/srv"; mountpoint = "/srv";
fstype = "ext4"; fstype = "ext4";
}; };

View file

@ -19,28 +19,36 @@ in {
type = liminix.lib.types.serviceDefn; type = liminix.lib.types.serviceDefn;
}; };
}; };
config.system.service = { imports = [ ../mdevd.nix ];
mount = liminix.callService ./service.nix { config.system.service.mount =
device = mkOption { let svc = liminix.callService ./service.nix {
type = types.str; partlabel = mkOption {
example = "/dev/sda1"; type = types.str;
}; example = "my-usb-stick";
mountpoint = mkOption { };
type = types.str; mountpoint = mkOption {
example = "/mnt/media"; type = types.str;
}; example = "/mnt/media";
options = mkOption { };
type = types.listOf types.str; options = mkOption {
default = []; type = types.listOf types.str;
example = ["noatime" "ro" "sync"]; default = [];
}; example = ["noatime" "ro" "sync"];
fstype = mkOption { };
type = types.str; fstype = mkOption {
default = "auto"; type = types.str;
example = "vfat"; default = "auto";
}; example = "vfat";
};
};
in svc // {
build = args:
let args' = args // {
dependencies = (args.dependencies or []) ++ [config.services.mdevd];
};
in svc.build args' ;
}; };
};
config.programs.busybox = { config.programs.busybox = {
applets = ["blkid" "findfs"]; applets = ["blkid" "findfs"];
options = { options = {

View file

@ -1,10 +1,12 @@
{ {
liminix liminix
, uevent-watch
, lib , lib
}: }:
{ device, mountpoint, options, fstype }: { partlabel, mountpoint, options, fstype }:
let let
inherit (liminix.services) longrun oneshot; inherit (liminix.services) longrun oneshot;
device = "/dev/disk/by-partlabel/${partlabel}";
options_string = options_string =
if options == [] then "" else "-o ${lib.concatStringsSep "," options}"; if options == [] then "" else "-o ${lib.concatStringsSep "," options}";
mount_service = oneshot { mount_service = oneshot {
@ -18,14 +20,7 @@ in longrun {
isTrigger = true; isTrigger = true;
buildInputs = [ mount_service ]; buildInputs = [ mount_service ];
# This accommodates bringing the service up when the device appears.
# It doesn't bring it down on unplug because unmount will probably
# fail anyway (so don't do that)
run = '' run = ''
while ! findfs ${device}; do ${uevent-watch}/bin/uevent-watch -s ${mount_service.name} -n ${device} partname=${partlabel} devtype=partition
echo waiting for device ${device}
sleep 1
done
s6-rc -b -u change ${mount_service.name}
''; '';
} }

View file

@ -47,20 +47,9 @@ in rec {
rootfsType = "jffs2"; rootfsType = "jffs2";
hostname = "inout"; hostname = "inout";
services.watch_mount_srv = services.mount_backup_disk = svc.mount.build {
let partlabel = "backup-disk";
node = "/dev/disk/by-partlabel/backup-disk"; mountpoint = "/srv";
mount = oneshot { fstype = "ext4";
name = "mount-srv"; };
up = "mount -t ext2 ${node} /srv";
down = "umount /srv";
};
in longrun {
name = "mount_srv";
run = ''
${pkgs.uevent-watch}/bin/uevent-watch -s ${mount.name} -n ${node} partname=backup-disk devtype=partition
'';
dependencies = [ config.services.mdevd ];
buildInputs = [ mount ];
};
} }