LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-08-2019, 04:27 AM   #1
Sharvin26
LQ Newbie
 
Registered: Feb 2019
Posts: 21

Rep: Reputation: Disabled
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 am a pretty newbie to Linux.
 
Old 05-08-2019, 04:39 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
Old 05-08-2019, 04:46 AM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
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.
 
Old 05-08-2019, 05:14 AM   #4
Sharvin26
LQ Newbie
 
Registered: Feb 2019
Posts: 21

Original Poster
Rep: Reputation: Disabled
I saw autofs but it won't be helpful in my case. Thanks for the response.
Quote:
Originally Posted by pan64 View Post
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 View Post
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.
 
Old 05-08-2019, 05:17 AM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Sharvin26 View Post
Yes, that's my last option to write a custom script.
You may find that it is your first option.
 
Old 05-08-2019, 05:42 AM   #6
Sharvin26
LQ Newbie
 
Registered: Feb 2019
Posts: 21

Original Poster
Rep: Reputation: Disabled
Yes, I have started to think that this is only the option. Thank you for the response.

Quote:
Originally Posted by hydrurga View Post
You may find that it is your first option.
 
Old 05-08-2019, 08:51 PM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Sharvin26 View Post

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.
 
Old 05-08-2019, 09:48 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,701

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I've never played with autofs dynamic mounting but it is possible. This is untested code.

In the auto.master file
Code:
/mount_directory    program:/usr/local/bin/autofs-auto-mapper.sh
In the autofs-auto-mapper.sh script
Code:
#!/bin/bash

#Pick partition device ID code goes here
if whatever
then
  devid=/dev/sdc2
else
  devid=/dev/sdc3
fi
echo "-fstype=ext4		:$devid"
And change permissions to make the script executable.
 
Old 05-09-2019, 08:37 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
1 members found this post helpful.
Old 05-09-2019, 08:49 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,701

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
In addition to the above suggestion using the user option allows a regular user to mount a filesystem.
 
1 members found this post helpful.
  


Reply

Tags
debian, fstab, linux, mount, partition



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can we convert a dynamic library (filename.so) to a static library (filename.a). hanumaan Programming 4 11-20-2009 12:07 AM
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 09:29 PM
fstab problem: mount: can't find dvd in /etc/fstab or /etc/mtab Nikon01 Slackware 5 11-17-2006 06:15 AM
mount/fstab issues, vfat filename conventions: 8.3 vs long tisource Linux - Software 2 03-23-2005 09:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration