View previous topic :: View next topic |
Author |
Message |
Niteriver n00b
Joined: 26 May 2024 Posts: 6
|
Posted: Mon Jun 03, 2024 6:43 pm Post subject: Actual automount |
|
|
I want my system to automatically recognise ALL of my drives, and by automatically i mean that i wouldn't have to touch fstab or manually mount drives even once. If you ever used Windows you know that it mounts all drives no matter if you mounted them before or they are completely new to your system, and you don't have to do anything when you install new drives. How do I do that?
P.S. Obviously i want to automount all types of drives, not only USBs. |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22686
|
Posted: Mon Jun 03, 2024 7:28 pm Post subject: |
|
|
Automount has a specific, and slightly different, commonly understood meaning. For your situation, where would you like these drives to be mounted? My first attempt would probably be a udev-rule to react to the block device appearing and handle it by running mount. |
|
Back to top |
|
|
Niteriver n00b
Joined: 26 May 2024 Posts: 6
|
Posted: Mon Jun 03, 2024 7:39 pm Post subject: |
|
|
Does it matter where to mount them? Well, ideally, as in Windows so that there would be root partition and everything else, but I would be satisfied if it was in /home/ or in /mnt/. And I'm not sure about device "appearing", because I would like it if system recognised drives that are already plugged, but aren't present in fstab. |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22686
|
Posted: Mon Jun 03, 2024 10:05 pm Post subject: |
|
|
A mount needs to appear somewhere in order for anything to use it. Windows could cheat by just assigning arbitrary unused drive letters to these devices. Under Linux, it is the administrator's decision where these should appear. Using directories under /mnt seems like a reasonable choice. |
|
Back to top |
|
|
Niteriver n00b
Joined: 26 May 2024 Posts: 6
|
Posted: Tue Jun 04, 2024 5:45 pm Post subject: |
|
|
I wrote a script which should do the thing I want. It assumes that currently used system is on /dev/sda. Also it requires sudo and udisks2.
Code: | #!/usr/bin/bash
number=$(sudo blkid | grep -v sda -c)
for (( i=1; (i<number+1); i++ ))
do
list[i-1]=$(sudo blkid | grep -v sda | cut -d':' -f1 | head -n $i | taile -n 1)
done
for (( i=1; (i<number+1); i++ ))
do
udisksctl mount -b ${list[i-1]}
done
initstate=$(sudo blkid | cut -d':' -f1)
while [true]
do
newstate=$(sudo blkid | cut -d':' -f1)
if [[ $newstate != $initstate ]]
then
number=$(sudo blkid | grep -v sda -c)
for (( i=1; (i<number+1); i++ ))
do
list[i-1]=$(sudo blkid | grep -v sda | cut -d':' -f1 | head -n $i | taile -n 1)
done
for (( i=1; (i<number+1); i++ ))
do
udisksctl mount -b ${list[i-1]}
done
initstate=$(sudo blkid | cut -d':' -f1)
fi
sleep 1
done |
This is my first bash script so everyone is welcome to correct and improve it.
Unfortunately, I didn't test it, because I'm afraid that mounting drives reserved by Windows system will break my Windows. Should I worry about it or is it actually safe? |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22686
|
Posted: Tue Jun 04, 2024 6:18 pm Post subject: |
|
|
Niteriver wrote: | Also it requires sudo and udisks2. | Why does it require sudo? A script run from udev will naturally have root privilege.Missing set -eu as a first line. Given the extensive use of pipelines, set -o pipefail would probably be good too. Niteriver wrote: | Code: | number=$(sudo blkid | grep -v sda -c)
for (( i=1; (i<number+1); i++ ))
do
list[i-1]=$(sudo blkid | grep -v sda | cut -d':' -f1 | head -n $i | taile -n 1)
done |
| I think this could be done much more simply by asking blkid to format the output as desired. Based on the output of blkid --help, it looks like it can produce the desired output natively. Look at blkid --output device. Also, there is no command taile. Perhaps you meant tail? I would use: Code: | list=( $( blkid --output device ) ) |
Niteriver wrote: | Code: | for (( i=1; (i<number+1); i++ ))
do
udisksctl mount -b ${list[i-1]}
done |
| I think this could be simplified to: Code: | for device in "${list[@]}"; do
udisksctl mount -b "$device"
done |
Niteriver wrote: | Code: | initstate=$(sudo blkid | cut -d':' -f1) |
| This can be initialized as a copy of list.This is ill-formed. Use while true instead. Better yet, do not poll for anything, and instead arrange for a command to run when a device appears. Niteriver wrote: | Code: | newstate=$(sudo blkid | cut -d':' -f1) |
| This can be converted to: Code: | newstate=( $( blkid --output device ) ) |
Niteriver wrote: | Code: | if [[ $newstate != $initstate ]] |
| I don't think this will do what you want. Consider: Code: | $ cat a.sh
declare -a a b
a[0]=x
a[1]=y
a[2]=z
b[0]=x
b[1]=z
b[2]=y
if [[ $a != $b ]]; then
echo Mismatch
else
echo Match
fi
$ bash a.sh
Match | The next block looks like a repeat of the starting condition. Why not just start with list blank and let the first pass through the loop always trigger the mismatch case? Niteriver wrote: | Unfortunately, I didn't test it | You could at least test that it reaches the lines where it should run udisksctl, but have those calls commented out and replaced with a print statement. Niteriver wrote: | because I'm afraid that mounting drives reserved by Windows system will break my Windows. Should I worry about it or is it actually safe? | If you think this will break Windows, what is the value of the script? Do you have non-Windows drives that the script can mount safely? |
|
Back to top |
|
|
Niteriver n00b
Joined: 26 May 2024 Posts: 6
|
Posted: Tue Jun 04, 2024 7:14 pm Post subject: |
|
|
Quote: | A script run from udev will naturally have root privilege. |
What do you mean udev? I planned to enable it through rc-update or in .xinitrc .
Quote: | Missing set -eu as a first line. Given the extensive use of pipelines, set -o pipefail would probably be good too. |
I don't quite understand what it means.
Quote: | Perhaps you meant tail? |
Yes, this is a typo.
Quote: | This can be initialized as a copy of list. |
You mean like this?
But they are not meant to be the same.
Quote: | Better yet, do not poll for anything, and instead arrange for a command to run when a device appears. |
I don't know how to do this.
Quote: | I don't think this will do what you want. Consider:
Код:
$ cat a.sh
declare -a a b
a[0]=x
a[1]=y
a[2]=z
b[0]=x
b[1]=z
b[2]=y
if [[ $a != $b ]]; then
echo Mismatch
else
echo Match
fi
$ bash a.sh
Match
|
Sorry, I don't understand what this does.
Quote: | You could at least test that it reaches the lines where it should run udisksctl, but have those calls commented out and replaced with a print statement. |
I actually did this, script itself runs ok (though i didn't run it with while true), my concern is about udisksctl itself.
Quote: | Do you have non-Windows drives that the script can mount safely? |
I don't have non-Windows drives beside the drive on which i run gentoo. It can actually safely mount one of partitions, which is the only one i tested, because it's just a partition. But I need to be absolutely sure about partitions Reserved by system. Is mounting them generally safe?
Thanks for the advice. |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22686
|
Posted: Tue Jun 04, 2024 8:21 pm Post subject: |
|
|
Generally, it's better to use attributed quotes where practical. I will fix up the quotes here, since they are all of me. Niteriver wrote: | Hu wrote: | A script run from udev will naturally have root privilege. | What do you mean udev? I planned to enable it through rc-update or in .xinitrc . | I meant from systemd-udevd, the daemon that monitors for devices to be cold-plugged or hot-plugged, and handles them according to administrator-defined rules. Running from rc-update would also grant you innate root privilege. Running from xinitrc would not. Niteriver wrote: | Hu wrote: | Missing set -eu as a first line. Given the extensive use of pipelines, set -o pipefail would probably be good too. | I don't quite understand what it means. | I mean you should enable those shell options, because they can convert subtle bugs into obvious error reports. Niteriver wrote: | Hu wrote: | This can be initialized as a copy of list. | You mean like this? | list should be an array, not a scalar, so no, you need initstate=( "${list[@]}" ). Niteriver wrote: | But they are not meant to be the same. | As I read your code, they start out the same, for the purpose of not immediately executing any udisksctl calls in the loop, but instead waiting for something to change. Niteriver wrote: | Hu wrote: | I don't think this will do what you want. Consider: | Sorry, I don't understand what this does. | It is a simple bash script, replicating the logic of yours, and demonstrating that your logic produces incorrect results. Niteriver wrote: | script itself runs ok (though i didn't run it with while true), my concern is about udisksctl itself. | Sorry, I do not use udisksctl. I mount my fixed devices via fstab, and my removable ones through udev rules (if used regularly) or ad-hoc via a root shell (if one-off). Niteriver wrote: | I don't have non-Windows drives beside the drive on which i run gentoo. It can actually safely mount one of partitions, which is the only one i tested, because it's just a partition. But I need to be absolutely sure about partitions Reserved by system. Is mounting them generally safe? | I don't know why the system reserved them. Presumably they were reserved for a good reason, so modifying them is not a good idea. |
|
Back to top |
|
|
Niteriver n00b
Joined: 26 May 2024 Posts: 6
|
Posted: Tue Jun 04, 2024 9:07 pm Post subject: |
|
|
Hu wrote: | It is a simple bash script, replicating the logic of yours, and demonstrating that your logic produces incorrect results. |
If you mean that comparing whole arrays is incorrect, you're probably right, but in this case I compare strings.
Hu wrote: | As I read your code, they start out the same, for the purpose of not immediately executing any udisksctl calls in the loop, but instead waiting for something to change. |
initstate contains all drives whereas list doesn't contain sda. But maybe it's redundant.
Hu wrote: | I don't know why the system reserved them. Presumably they were reserved for a good reason, so modifying them is not a good idea. |
You say modifying, but is mounting modifying? |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|