I'm working on some home automation via a Raspberry Pi 3 B, I've got an external drive attached to it that I want to mount at startup without using fstab because I'll likely remove it at some point and this needs to be adaptable. I'm mounting by label in this scenario.
Currently the service fails at startup, and reports that the connection to the drive timed out. However, once the service is run by a user, the drive mounts without error. I'm beginning to think this is due to a misunderstanding about systemd but I can't think of an alternative, so I'd like some assistance.
Here's some relevant information:
/sbin/mount-media.sh
#!/bin/bash #This is a mounting operation for use at startup. ## Variables #Replace this with the label of target drive DRIVE_LABEL='pidrive' #The Rest LABEL_FOUND=$(ls /dev/disk/by-label | grep -c $DRIVE_LABEL) IS_MOUNTED=$(mount | grep -c /media) CONNECTION_ATTEMPTS=0 ## Function for checking if mounted. is_mountable() { if [ $LABEL_FOUND = 1 ]; then if [ $IS_MOUNTED = 0 ]; then return 0 else echo "Drive is already mounted" return 1 fi else return 1 fi } ## Attempt to connect for 120 seconds connect() { until [ $CONNECTION_ATTEMPTS -gt 119 ]; do if is_mountable; then mount /dev/disk/by-label/$DRIVE_LABEL /media return 0 else ((CONNECTION_ATTEMPTS=CONNECTION_ATTEMPTS+1)) echo $CONNECTION_ATTEMPTS sleep 1 fi done if [ $IS_MOUNTED = 1 ]; then echo "Mounted $DRIVE_LABEL" exit 0 else echo "Timed out, failed to mount." exit 1 fi } ## Main connect /etc/systemd/service/mount-media.service
[Unit] Description=Mount our local drive to /media [Service] ExecStart=/sbin/mount-media.sh [Install] WantedBy=multi-user.target On startup...
sudo journalctl -alf
-- Logs begin at Fri 2020-03-27 13:43:24 PDT. -- Mar 27 13:45:33 QTPi mount-media.sh[310]: 116 Mar 27 13:45:34 QTPi mount-media.sh[310]: 117 Mar 27 13:45:35 QTPi mount-media.sh[310]: 118 Mar 27 13:45:36 QTPi mount-media.sh[310]: 119 Mar 27 13:45:37 QTPi mount-media.sh[310]: 120 Mar 27 13:45:38 QTPi mount-media.sh[310]: Timed out, failed to mount. Mar 27 13:45:38 QTPi systemd[1]: mount-media.service: Main process exited, code=exited, status=1/FAILURE Mar 27 13:45:38 QTPi systemd[1]: mount-media.service: Failed with result 'exit-code'. however, when the device is booted and a user runs
sudo systemctl start mount-media.service
Mar 27 13:49:04 QTPi sudo[2467]: pi : TTY=pts/1 ; PWD=/home/pi ; USER=root ; COMMAND=/bin/systemctl start mount-media.service Mar 27 13:49:04 QTPi sudo[2467]: pam_unix(sudo:session): session opened for user root by pi(uid=0) Mar 27 13:49:04 QTPi systemd[1]: Started Mount our local drive to /media. Mar 27 13:49:04 QTPi sudo[2467]: pam_unix(sudo:session): session closed for user root Mar 27 13:49:05 QTPi kernel: EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null) Mar 27 13:49:05 QTPi systemd[1]: mount-media.service: Succeeded. pi@QTPi:~$ mount | grep /media
/dev/sda1 on /media type ext4 (rw,relatime) pi@QTPi:~$ sudo blkid | grep pidrive
/dev/sda1: LABEL="pidrive" UUID="8249a0f7-f020-4c8d-866d-d8728df3b1e7" TYPE="ext4" PARTLABEL="primary" PARTUUID="9504d415-2649-48c1-a294-6869a275164b" Should note that this UUID shifts every startup.
uname -a
Linux QTPi 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l GNU/Linux