LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-08-2020, 03:35 AM   #1
dr_berta
Member
 
Registered: Sep 2005
Location: Carpi (Modena) - Italy
Distribution: Ubuntu 20.04_x86_64 LTS; Slackware 13 64bit; Gentoo, Fedora, Yocto
Posts: 77

Rep: Reputation: 1
Mount issue in bash script?


Hi,
I'm writing a script to save automatically data from a SSD having three primary partitions using a USB pendrive with a live distribution.

The pendrive has in its /mnt folder two subfolder: /mnt/gentoo and /mnt/key

I want to mount in the /mnt/gentoo folder the partiton /dev/sda3 of the SSD where I have the data and I want to mount in the /mnt/key the second partition of the pendrive where I want to save the data.

The script is lunched automatically from the .bashrc script of root user.
The code is the following:
Code:
tput clear
echo "Detecting system..."
case "$(uname -p)" in
*G3900*)
	DEVICE="/dev/sda"
	PLATFORM="64bit"
	;;
*N270*)
	DEVICE="/dev/sda"
	;;
*)
	DEVICE="/dev/hda"
	;;
esac
echo "$PLATFORM platform"

mount

DEV="${DEVICE}3"
mount -t ext4 $DEV /mnt/gentoo
if [ "$DEVICE" = "/dev/sda" ]; then
	DISK2="/dev/sdb2" 
else
	DISK2="/dev/sda2"
fi
mount -t vfat $DISK2 /mnt/key
The issue is that, when the script executes the two mount command I see a message saying that the disk is already mounted on the folder.

The mount command shows me that only the pendrive is mounted before the two commands.
Why I receive those messages?
Are they error messages or information messages saying me that the command has properly executed?

Thanks
Claudio
 
Old 10-08-2020, 05:09 AM   #2
lvm_
Member
 
Registered: Jul 2020
Posts: 912

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
.bashrc runs for each interactive shell and so can be executed multiple times, it is a bad place for commands which should be executed only once like mount. Proper place for specifying mountpoints is /etc/fstab. On the other hand, running mount extra time has no adverse effects apart from the error message you are seeing.
 
Old 10-08-2020, 09:32 AM   #3
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
Like lvm says, don't put this in .bashrc.

In case you need to do this in a script to figure out which device should be mounted, perhaps run it at system startup using localrc. How this is done precisely depends on the distro and version.

You say that /mnt/key is not mounted, yet the very last line fails? Can you post the exact error messages?
 
1 members found this post helpful.
Old 10-08-2020, 10:00 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
In addition to the above what live distribution/version are you running on the flash drive?

Is the OS/desktop automatically mounting the second partition on the flash drive?
 
Old 10-11-2020, 07:22 AM   #5
dr_berta
Member
 
Registered: Sep 2005
Location: Carpi (Modena) - Italy
Distribution: Ubuntu 20.04_x86_64 LTS; Slackware 13 64bit; Gentoo, Fedora, Yocto
Posts: 77

Original Poster
Rep: Reputation: 1
HI,
thank you for the clarifications and the suggestions.

About the message from the mount command I would only understand if it is an information message saying that the folder has been mounted or an error message saying that the folder was already mounted. This is not clear from the message.

This code is a part of a more complex script that should backup data from a device, clean the SSD, repartition it and install a new software.
To do this I'm using a live distro based on gentoo kernel 4.14 on a usb pendrive.

I tried to call the script from .bashrc because I have inserted the call of the script in the /etc/local.start file, but the script doesn't start.

I know that .bashrc is executed for any interactive shell, but since I'm using a command line interface I was expecting that only one shell was used.
To avoid multiple esecutions I tried the following solutions:
- I have created a dummy file in /tmp and checking for its presence to avoid to restart the script, but for strange reasons this doesn't work.
- I tried to start it directly from /etc/init.d, bit I'm not able to create the link from the script in /etc/init.d to /etc/runlevel/default during the squash filesystem generation in my development device.

The next tentative I'm planning to do is to start the script from /etc/inittab.

Do you have any suggestion also about the tentatives I have already done?

Thanks
Claudio
 
Old 10-11-2020, 09:36 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Several options:
  1. Check the output from "df -P /mnt/gentoo /mnt/key" to see if those directories are on the own devices or are still part of the root filesystem.
  2. Run the "mount" command (with no arguments) and see if those mount points are listed.
  3. (quickest) Grep /proc/mounts to see if the expected mount points are in use.
 
Old 10-11-2020, 09:37 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
The output of the mount command from your script only shows what filesystems are currently mounted. Not enough information posted to know but in some cases how drives are discovered may not always be the same i.e you can assume that your flash drive will always be /dev/sdb and your internal drive as /dev/sda. That is why typically /etc/fstab use UUIDs or filesystem labels versus absolute devices IDs now days.

Using /etc/rc.local might be an alternative to .bashrc or creating a startup script.
 
Old 10-11-2020, 09:41 AM   #8
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 dr_berta View Post
About the message from the mount command I would only understand if it is an information message saying that the folder has been mounted or an error message saying that the folder was already mounted. This is not clear from the message.
It means that the storage device is already mounted. It can only be mounted once at a time. So it is an error message, indeed.
Quote:
I tried to call the script from .bashrc because I have inserted the call of the script in the /etc/local.start file, but the script doesn't start.
While I don't know Gentoo, the local.d wiki page says that the script must be named something.start and must be in directory /etc/local.d.
Quote:
I know that .bashrc is executed for any interactive shell, but since I'm using a command line interface I was expecting that only one shell was used.
You log in once, the partition will be mounted. You log out, and log in again. This attempts to mount the device a second time. Since it is already mounted, the command issues the error message.
Quote:
Do you have any suggestion also about the tentatives I have already done?
I don't know your code of checking for the presence of a file in /tmp, so I have no opinion about it (except that it looks overly complex and not very robust). Not being versed in Gentoo's startup method OpenRC, I can't comment on the second tentative.

Try putting the mount script in /etc/local.d/mymount.start or designing an initscript.

Last edited by berndbausch; 10-11-2020 at 09:42 AM.
 
Old 10-11-2020, 06:21 PM   #9
dr_berta
Member
 
Registered: Sep 2005
Location: Carpi (Modena) - Italy
Distribution: Ubuntu 20.04_x86_64 LTS; Slackware 13 64bit; Gentoo, Fedora, Yocto
Posts: 77

Original Poster
Rep: Reputation: 1
Hi,
I have already tried to create a file with extension .start calling my script and located in the folder /etc/local.d. Even in this case my script has not been executed.

about the local.start file in /etc/conf.d, it is executed by the "local" script present in /etc/init.d. I simply added a row in that file to execute my script.

About the /tmp issue the code was similar to this one:
Code:
if [ ! -e /tmp/myfile ]; then
     >/tmp/myfile
     myscript
fi
I tried also to write an initscript but I don't understand how to enable it in the default runlevel of the squash filesystem that is not under execution.

An alternative could be to create a pid file in /var/run when my script is launched and check if that pid file is present or not. If it is present the launch is skipped.
Do you think that could it work?

Thanks
Claudio
 
Old 10-11-2020, 06:29 PM   #10
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 dr_berta View Post
About the /tmp issue the code was similar to this one:
Code:
if [ ! -e /tmp/myfile ]; then
     >/tmp/myfile
     myscript
fi
And you still got the error? Somebody/something removed myfile, I guess. Or somebody/something mounted the device before this code was executed.
Quote:
An alternative could be to create a pid file in /var/run when my script is launched and check if that pid file is present or not. If it is present the launch is skipped.
Do you think that could it work?
Only if it's the same PID file for all invocations. Which it is not, since PID files are by definition unique.

The real solution is to use initscripts. Sorry for not being able to help there.
 
Old 10-12-2020, 09:43 AM   #11
dr_berta
Member
 
Registered: Sep 2005
Location: Carpi (Modena) - Italy
Distribution: Ubuntu 20.04_x86_64 LTS; Slackware 13 64bit; Gentoo, Fedora, Yocto
Posts: 77

Original Poster
Rep: Reputation: 1
Hi,
I checked again about the solution using a file created in /tmp and I found that the solution could work. The issue I found is that the script is excuted in a virtual terminal different from the tty1 that is shown on the screen. This is the reason why it seemed to me that it was not working. Checking with the command
Code:
ps -ax
I found that the script was running in the virtual terminal tty5

I'm curious to understand why that virtual terminal.

Do you have any suggestion about how to force the script to be excuted in the virtual terminal tty1?

Thanks
Claudio
 
Old 10-12-2020, 09:47 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by dr_berta View Post
Do you have any suggestion about how to force the script to be excuted in the virtual terminal tty1?
This works differently. Every app inherits the terminal from its own parent (where it was started from).
 
Old 10-12-2020, 10:54 AM   #13
dr_berta
Member
 
Registered: Sep 2005
Location: Carpi (Modena) - Italy
Distribution: Ubuntu 20.04_x86_64 LTS; Slackware 13 64bit; Gentoo, Fedora, Yocto
Posts: 77

Original Poster
Rep: Reputation: 1
Hi,
I think I found an acceptable solution (not perfect but good).

The script is launched from the .bash_profile script located in /root folder at the startup of the live distribution. It starts only once because I check the presence of the file /tmp/myfile. If it is present the launch of the script is skipped otherwise the /tmp/myfile file is created and the script is lauched.

I have verified that the script starts and can be seen every time in a different virtual terminal (I will investigate why).

I solved the issue to see the result of the script redirecting its output on /dev/tty1. The informations I can see are not complete but are adequate to let understand to the user what is happening.

Do you see some possible issues with this solution?

Thanks
Claudio
 
Old 10-12-2020, 11:09 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
You ought to redirect the output into a file.
 
  


Reply



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
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
how to mount drive in bash script if mount requires su? babag Mandriva 8 09-08-2008 07:58 PM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
Mount. Umount. Mount. Umount. Mount. Umount. Mount.. cwizardone Slackware 10 03-22-2007 09:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:27 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