Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: How to automatically mount a specific drive on startup, which is always there? (Read 1352 times) previous topic - next topic
0 Members and 2 Guests are viewing this topic.

How to automatically mount a specific drive on startup, which is always there?

Hello!

Everytime I boot my pc, I have to
Code: [Select]
lsblk
Code: [Select]
sudo mount /dev/sdX /mnt

The alternative is to press Super+F9 and wait a bit then press the drives, via dwm's config.h which runs dmenumount
Contents of dmenumount:
Code: [Select]
#!/bin/sh

# A dmenu prompt to unmount drives.
# Provides you with mounted partitions, select one to unmount.
# Drives mounted at /, /boot and /home will not be options to unmount.

unmountusb() {
[ -z "$drives" ] && exit
chosen="$(echo "$drives" | dmenu -i -p "Unmount which drive?")" || exit 1
chosen="$(echo "$chosen" | awk '{print $1}')"
[ -z "$chosen" ] && exit
sudo -A umount "$chosen" && notify-send "💻 USB unmounting" "$chosen unmounted."
}

unmountandroid() { \
chosen="$(awk '/simple-mtpfs/ {print $2}' /etc/mtab | dmenu -i -p "Unmount which device?")" || exit 1
[ -z "$chosen" ] && exit
sudo -A umount -l "$chosen" && notify-send "🤖 Android unmounting" "$chosen unmounted."
}

asktype() { \
choice="$(printf "USB\\nAndroid" | dmenu -i -p "Unmount a USB drive or Android device?")" || exit 1
case "$choice" in
USB) unmountusb ;;
Android) unmountandroid ;;
esac
}

drives=$(lsblk -nrpo "name,type,size,mountpoint,label" | awk -F':' '{gsub(/ /,":")}$4!~/\/boot|\/efi|\/home$|SWAP/&&length($4)>1{printf "%s (%s) %s\n",$4,$3,$5}')

if ! grep simple-mtpfs /etc/mtab; then
[ -z "$drives" ] && echo "No drives to unmount." &&  exit
echo "Unmountable USB drive detected."
unmountusb
else
if [ -z "$drives" ]
then
echo "Unmountable Android device detected."
       unmountandroid
else
echo "Unmountable USB drive(s) and Android device(s) detected."
asktype
fi
fi

It is kinda annoying, and linux is all about automating stuff and making you faster/more efficient (at least that's how I feel about it)

I have no idea about startup services or daemons (I use runit)
I don't know if the drives are different  each boot at `lsblk`, I always just compare their sizes to ensure I don't mount the wrong drive lol
Also on boot/setup, I remember I had setup UUID somewhere.

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #1
I don't know about runit, but openrc has a feature where you put your scripts in /etc/local.d and they will get run every boot after all the services have been started, might want to see if runit has an equivalent.

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #2
The traditional way to automount drives is to add them in fstab.
This is not dependent on the init.

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #3
Did you try adding it to /etc/fstab (like you did for your / partition)?

If it's recognized too late in the boot process for fstab/mount -a, you could try a udev rule (but writing a "correct" one requires knowing more the device).

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #4
Quote
The traditional way to automount drives is to add them in fstab.
How to do this? I did the artix installation 1:1 without understanding all of it, so idk how to use fstab

Quote
Did you try adding it to /etc/fstab (like you did for your / partition)?
No, how to do this?
Assume that the drive I want to mount is sdb, and it has sdb1, sdb2, sdb3
I want to mount sdb3 on /mnt
(But then again, how to ensure sdb3 won't be sda3 on next boot?)

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #5
Quote
How to do this? I did the artix installation 1:1 without understanding all of it, so idk how to use fstab
https://wiki.archlinux.org/title/Fstab#Usage

Quote
Assume that the drive I want to mount is sdb, and it has sdb1, sdb2, sdb3
I want to mount sdb3 on /mnt
(But then again, how to ensure sdb3 won't be sda3 on next boot?)

https://wiki.archlinux.org/title/Fstab#Identifying_file_systems

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #6
Great links. I want to ask and make sure, before typing anything I cannot reverse.

So, the drive I want to mount has UUID=4C44027F44026C50

Should I just
Code: [Select]
vim /etc/fstab

And just append it there, like the other lines , in the same typing format pretty much?
So, /etc/fstab:
Code: [Select]
# Static information about the filesystems.
# See fstab(5) for details.

# <file system> <dir> <type> <options> <dump> <pass>
# /dev/sda2
UUID=dd74678b-a072-4fcc-83aa-af7e226c1ac5 /         ext4      rw,relatime 0 1

# /dev/sda1
UUID=5BD7-6DD0      /boot     vfat      rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2

# /dev/sda3
UUID=3982a440-caea-4f41-a30a-80bdd863463d /home     ext4      rw,relatime 0 2

Should become the following?

Code: [Select]
# Static information about the filesystems.
# See fstab(5) for details.

# <file system> <dir> <type> <options> <dump> <pass>
# /dev/sda2
UUID=dd74678b-a072-4fcc-83aa-af7e226c1ac5 /         ext4      rw,relatime 0 1

# /dev/sda1
UUID=5BD7-6DD0      /boot     vfat      rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2

# /dev/sda3
UUID=3982a440-caea-4f41-a30a-80bdd863463d /home     ext4      rw,relatime 0 2

#/dev/sdb3
UUID=4C44027F44026C50                            /mnt               ext4               rw,relatime 0 2

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #7
yes

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #8
/mnt is often used by various software and OS functions to mount things, so for your own use it might be better to mount stuff on some dir you made yourself, e.g. /tya-mnt or anything like that, but make sure to create the dir of course.

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #9
My distro crashed, cmon, I cannot afford the time to reformat it, I was in the middle of setting up an email server!

So, I changed fstab into what I wrote above, but before that I did chmod 666 because I was lazy.
Then wrote
Code: [Select]
sudo reboot

Linux became extremely sluggish. Took like 1 minute to reach "Loading Linux linux..."and 1 line below that "Loading initial ramdisk"(? not sure that was the line, but probably)
It stayed there for like 5 minutes, pressed powerbutton and it instantly shut down.

Note that the drive I mounted (onto /mnt) was of another OS. So, I plugged it out, and restarted. No lag/delay, and this is the boot sequence
Code: [Select]
Starting version 251.4-1-artix

:: Triggering udev uevents
:: Loading user-specified modules
:: Waiting for udev uevents to be processed
:: Starting net-lo
:: Checking filesystems
fsck.fat 4.2 (2021-01-31)
/dev/sda1: 367 files, 15604/261627 clusters
/dev/sda3: clean, 8101/12623872 files, 1455658/50480895 blocks
fsck.ext4: Unable to resolve 'UUID=4C44027F44026C50'

************************************ FILESYSTEM CHECK FAILED *********************************
*
*    Please repair manually and reboot. Note that the root file system is currently mounted read-only. To remount it read-write, type: mount - o remount,rw /
*    When you exit the maintenance shell, the system will reboot automatically
*
****************************************************************************************************

Give root password for maintenance (or press Control-D to continue): <input caret>
sth about UUID being unable to resolve (lol how)
Anyway, I now have only read-only access. I can open /etc/fstab but only read-only, cannot even sudo chmod!!!

How to get write access onto fstab file? Just going back to mounting by hand each boot works, at least I can work with that :(

Edit: Cannot even reboot/poweroff. Neither `exit`
Code: [Select]
init: fatal: unable to create /etc/runit/stopit: file does not exist
Edit2: For some reason, after 2 minutes, command `exit` worked, and I managed to shut down the computer.

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #10
Ok, so I did what it said (enter with root -> type `mount - o remount,rw /` -> edit `/etc/fstab` by commenting out what I entered)
I can boot linux normally.

Could some1 tell me what permissions fstab has by default? (since I maxed them out via chmod and that may be to blame)

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #11
Is the external drive really formatted as ext4?

/etc/fstab is 644 by default (as are most files in /etc).

Re: How to automatically mount a specific drive on startup, which is alwayed there?

Reply #12
Quote
/etc/fstab is 644 by default (as are most files in /etc).
Thanks, now I can rest assured it's exactly the same as before

Quote
/mnt is often used by various software and OS functions to mount things, so for your own use it might be better to mount stuff on some dir you made yourself, e.g. /tya-mnt or anything like that, but make sure to create the dir of course.
I have to admit I didn't think of that, what you suggest is good. It may bug at boot as /mnt may be occupied (default mount location), but its definitely not clean to have /mnt always occupied. I just created an appropriate folder, to exclusively mount it.

Quote
Is the external drive really formatted as ext4?
It's actually not an external drive, but a windows 7 drive
idk if its ext4, but its definitely UEFI

Edit: Its NTFS lol, let me edit /etc/fstab properly now, will report results soon
Edit2: Since its not ext4 but NTFS, should I change anything further in the /etc/fstab? I see that vfat has different parameters after it, and is not like ext4, for example. Or do I just replace what I had written far above from `ext4` to `ntfs` (or `NTFS`)?

tl;dr: Will this work?
Code: [Select]
# Static information about the filesystems.
# See fstab(5) for details.

# <file system> <dir> <type> <options> <dump> <pass>
# /dev/sda2
UUID=dd74678b-a072-4fcc-83aa-af7e226c1ac5 /         ext4      rw,relatime 0 1

# /dev/sda1
UUID=5BD7-6DD0      /boot     vfat      rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2

# /dev/sda3
UUID=3982a440-caea-4f41-a30a-80bdd863463d /home     ext4      rw,relatime 0 2

#/dev/sdb3
UUID=4C44027F44026C50                            /windows               ntfs               rw,relatime 0 2

Re: How to automatically mount a specific drive on startup, which is always there?

Reply #13
The reason I haven't tried the above is because I'm afraid of a crash or something even worse I can't predict (e.g. the windows drive mounted becomes corrupted idk)
On the previous crash, I was lucky the error was detected and commands on how to restore my system appeared on grub rescue, but in linux that is very rare, next time I may have to wait a full day until some1 replies how to save my system.
I haven't backed up my linux files either

So, can any1 confirm if the above fstab script will work? I cannot find any documentation on how to fstab mount ntfs drives

Re: How to automatically mount a specific drive on startup, which is always there?

Reply #14
Just run mount -a and see if it works. It's not like anything bad is going to happen if there's a mistake. The drive just won't mount.