Can fstab dynamically mount a partition by reading from a file or filename?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can fstab dynamically mount a partition by reading from a file or filename?
I have a partition structure as follows:
Code:
/dev/sdc1 => Partition 1 ( My OS. [ Linux flavour ] )
/dev/sdc2 => Partition 2 ( This contents some data. )
/dev/sdc3 => Partition 3 ( This also contents some data. )
/dev/sdc4 => Partition 4 ( I want this as a deciding partition. )
I am trying to mount partition 2 or partition 3 dynamically depending on the file present in partition 4.
For example:- Partition 2 will be mounted if partition 4 consists of a file named two. Partition 3 will be mounted if partition 4 consists of a file named three.
Note:- This partition will never be mounted together i.e. if Partition 2 is mounted partition 3 will be not be mounted until partition 2 is unmounted. Thus I can use a common directory for both partitions.
As I have systemd available on my os I can write a startup script which can read from partition 4 and mount the appropriate partition at boot and write the partition record into /etc/fstab.
But according to my understanding fstab is critical file and if any failure happens or fstab get's corrupt it's going to stop the system from booting.
Question:
Now what I am trying to achieve is can I add an entry in fstab which will read dynamically partition 4 and add the entry for partition 2 or partition 3 depending on the file that exists in the partition 4.
I think you do not need to modify fstab. You need to use some kind of automounter (see autofs), but I think there is no such tool available, so you need to implement something.
I saw autofs but it won't be helpful in my case. Thanks for the response.
Quote:
Originally Posted by pan64
I think you do not need to modify fstab. You need to use some kind of automounter (see autofs), but I think there is no such tool available, so you need to implement something.
Yes, that's my last option to write a custom script.
Quote:
Originally Posted by hydrurga
I don't think that fstab will be able to do so. Of course, you could always write your own script, to be executed at startup, to do that.
As I have systemd available on my os I can write a startup script which can read from partition 4 and mount the appropriate partition at boot and write the partition record into /etc/fstab.
You don’t need fstab to mount a filesystem. You need the mount command.
Quote:
Now what I am trying to achieve is can I add an entry in fstab which will read dynamically partition 4 and add the entry for partition 2 or partition 3 depending on the file that exists in the partition 4.
Perhaps. You can use the device files to identify partitions, e.g. /dev/sdc3, but you can also use the filesystem’s UUID or its label, e.g. LABEL=mylabel. Your script could manipulate the labels of the two “dynamic” filesystems. But I think it’s much simpler to just mount one filesystem or the other, without playing around with fstab entries or filesystem labels.
You could even replace the mount command executable with a script that conditionally mounts one of the “dynamic” partitions, then calls the original mount command.
However fstab is just a list of filesystems to be mounted and has no mechanism for doing so conditionally.
Last edited by berndbausch; 05-08-2019 at 08:56 PM.
You can USE the fstab to define how to mount - but include the option "noauto".
Now you can use a script to pick what to mount without having to specify the full list.
Without that, I prefer the "LABEL=volname" to mount filesystems post boot. It is easier than using the UUID for the device, and you can't depend on the /dev/sd... name.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.