Featured image of post Automount with systemd o openRC

Automount with systemd o openRC

Automatically mount a hard disk with systemd or openRC

Mount Hard Disk

Create mount service with systemd

Create a file home-user-data.mount with the following content:

[Unit]
Description=Mount USB disk
[Mount]
What=/dev/disk/by-uuid/00000000-0000-0000-0000-00000000
Where=/home/gallochri/data
Type=ext4
Options=defaults,rw,noatime
TimeoutSec=10
[Install]
WantedBy=multi-user.targe

Copy to /etc/systemd/system/ and rename to the correct path.

$ cp home-user-data.mount_template /etc/systemd/system/home-gallochri-data.mount

Edit .mount service file

[Unit]                  
Description=                        # Unit description

[Mount]                             
What=                               # Disk by UUID
Where=                              # Where you want the device mounted
#Type=                              # FS type (optional)
#Options=                           # Options (fstab option, optional)
#TimeoutSec=seconds                 # How long systemd should wait for the mount command to finish (optional)

[Install]
WantedBy=multi-user.target

What=

List disk

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   2.7T  0 disk 
└─sda1   8:1    0   2.7T  0 part 
sdb      8:16   0 232.9G  0 disk 
├─sdb1   8:17   0   512M  0 part /boot/firmware
└─sdb2   8:18   0 232.4G  0 part /

Get UUID

$ lsblk -no UUID /dev/sda1
0776c720-2fed-434d-b537-025ba4293f62

Where=

Path where you want the device mounted, if the path do not exist it will be created with 755 owned by root.

Automount disk with Systemd

Create a home-user-data.automount file with the following content:

[Unit]
Description=Automount backup partition

[Automount]
Where=/home/gallochri/data
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

copy it to /etc/systemd/system/ and rename to the correct path.

$ cp home-user-data.automount /etc/systemd/system/home-gallochri-data.automount

Mount and enable automount:

$ sudo systemctl enable home-gallochri-data.mount --now
$ sudo systemctl enable home-gallochri-data.automount --now

Create a mount service with OpenRC

On Alpine Linux:

~ # blkid
/dev/sdb1: UUID="205e1362-81ba-4798-aff7-83aa88a96ea6" TYPE="ext4"
/dev/sda3: UUID="f8714de2-ab41-49b5-ad20-93dfbcc703dd" TYPE="ext4"
/dev/sda2: UUID="fe7e53b7-c543-44b8-97f1-28fc29680457" TYPE="swap"
/dev/sda1: UUID="C837-1EC5" TYPE="vfat"

Create a openRC script /etc/init.d/mount-usb:

#!/sbin/openrc-run

description="Mount USB disk"

start() {
    ebegin "Mounting USB disk"
    mkdir -p /home/gallochri/data
    mount /dev/sdb1 /home/gallochri/data
    eend $?
}

stop() {
    ebegin "Unmounting USB disk"
    umount /home/gallochri/data
    eend $?
}

make it executable and test

# chmod +x /etc/init.d/mount-usb
# rc-update add mount-usb boot
# rc-service mount-usb start
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy