Linux - SoftwareThis 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
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.
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?
.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.
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?
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?
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.
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.
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?
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.
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?
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.