Tiny Core Linux

Tiny Core Base => TCB Q&A Forum => Topic started by: coreplayer2 on January 14, 2012, 03:03:25 PM

Title: Any method to reliably mount removable drives or all drives on core boot??
Post by: coreplayer2 on January 14, 2012, 03:03:25 PM
This is the closest reference I can find in the forum to answering my dilemma, like I said close but not exactly what I had in mind although I think the OP's question is very similar to mine.
http://forum.tinycorelinux.net/index.php/topic,3600.msg18920.html#msg18920 (http://forum.tinycorelinux.net/index.php/topic,3600.msg18920.html#msg18920)

First an overview, the task is to copy a directory from a drive/cd/usb/iso whose uuid or label or identifier is dynamic to a directory in Home on each boot.  easy right :p

Injecting the directory/files into the iso/usb or having them located on a separate drive etc, check done that.
mounting a specific drive and directory on boot, ok check

Since I don't know which drive the required files are on (but I am aware of the location of the directory relative to the drive's structure), can't exactly run a search for the dir and then mount it which is a chicken before the egg syndrome.

Main question then,  is there a reliable method to mount all attached drives using core 4.2 on each boot?   

A static one off task is no problem.  reliably automating the mount process is the current issue.  I think it's safe to say that only removable devices are necessary to be mounted.  I say that in referrence to a utility called pmount which I've read of.

anyone's thoughts for a solution is appreciated. 

thanks

Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: gerald_clark on January 14, 2012, 03:21:04 PM
cd /mnt
for DIR in *
do
  mount $DIR
done
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: Rich on January 14, 2012, 03:24:46 PM
Hi coreplayer2
While the obvious answer might seem to be  mount -a, this won't work since the  mount options
in  /etc/fstab  (fourth column) are set to  noauto. Try running the following:
Code: [Select]
#!/bin/sh
for i in `grep -o "/mnt/[h,s]d[a-d][1-8, ]" /etc/fstab`
do
j=`grep -o $i /etc/mtab`
[ -n "$j" ] && continue
echo "$i"
done
This should list all available drives that are not mounted not including CDs or floppies.
If this works for your purpose, replace the word  echo  with  mount  and those drives will get
mounted when you run this script.
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: coreplayer2 on January 14, 2012, 05:54:26 PM
Thanks guys, both scrips work flawlessly  thanks :)

will keep you posted.
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: coreplayer2 on January 14, 2012, 09:09:01 PM
Ok Gerald,  I really like this scrip, for it's simplicity and effectiveness.   to take it one step further I figured after performing the file copy I think it's best to unmount the unused devices and seeing how it's not possible to unmount sda1 i figured simply use umount to reverse the last command. But this is not so because umount is so efficient at unmounting all drives in the variable $DIR   including sda1

How I tested
Code: [Select]
#!/bin/sh -x
cd /mnt
#mount all devices in mnt
for DIR in *; do mount $DIR; done 2>//dev/null
#
read -p "waiting.." *  #a break to be replaced by find and copy function after test
#
#exclude sda1 then umount remaining devices
for DIR in *; do umount -l ${DIR//sda1}; done #2>//dev/null
must have tried a dozen or so options to exclude sda1 from umount but  this method functions flawlessly through 20-30 repeated tests  so am very grateful for this guidance  thanks..
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: Rich on January 14, 2012, 10:08:53 PM
Hi coreplayer2
You might want to use a somewhat more controlled approach.
To mount all unmounted drives:
Code: [Select]
#!/bin/sh
DRIVESMOUNTED="/tmp/newmounts"
for i in `grep -o "/mnt/[h,s]d[a-d][1-8, ]" /etc/fstab`
do
j=`grep -o $i /etc/mtab`
[ -n "$j" ] && continue
mount "$i"
echo "$i" >> $DRIVESMOUNTED
done
To unmount any drives mounted by the above script:
Code: [Select]
#!/bin/sh
DRIVESMOUNTED="/tmp/newmounts"
while read -r i
do
umount "$i"
done < $DRIVESMOUNTED
rm -f $DRIVESMOUNTED
sync
The first script will only mount drives that are not already mounted, so it will ignore any drives that
are normally mounted when booting. It creates a log file for the second script so it unmounts only
those drives. If you plug in a USB thumb drive after running the first script, you can safely run it
again, it will append the new drive to the log. Just remember to allow at least 5 seconds for the
system to detect the drive first. It's also probably a good idea to sync the file system. I don't do
scripting, so I'm sure there are better ways of doing this.
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: coreplayer2 on January 15, 2012, 12:42:21 AM
Awesome flow of ideas, various methods; luv it  thanks

I have enough systems around here that I can use all these ideas, all scripts work great (added sr0 options for cd) to the controlled approach and will be used on my 24/7 systems thanks Rich.  because it always bothered me that I had to manually mount drives that were already connected to the system at boot., I mean really?.

Meanwhile I thought I was finally finished as this script really satisfies the requirements for the current short term task.  that was until Rich mentioned sync the file system!!    what?  ok I think I get the idea, makes sense  thanks

As for the short term use systems we've taken this script from the VM to the real deal.  it's working great too

Code: [Select]
#!/bin/sh
#mounts devices finds and copies MyDir to home/tc then unmounts
cd /mnt
#mount all devices in mnt
for DIR in *; do mount $DIR; done 2>//dev/null
#
#find files, copy and set owner
while [ ! -d /home/tc/MyDir ]; do sudo find / -iname "MyDir" -exec cp -r {} /home/tc/ \;; done 2>//dev/null
sudo chown -R tc:staff /home/tc/MyDir 2>//dev/null
#
#exclude sda1 then unmount remaining devices
for DIR in *; do umount -l ${DIR//sda1}; done 2>//dev/null
sync
exit 0

 find and copy command took much reading of the man pages but got there in the end.

Thanks Rich and thanks Gerald really appreciate both your help here.




Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: gerald_clark on January 15, 2012, 01:27:46 AM
umount syncs the drive before unmounting.
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: coreplayer2 on January 15, 2012, 03:42:59 AM
Interesting thanks,  I hope it wont matter is the sync is performed twice as the not all drives are effected by the umount command due to " umount -l ${DIR//sda1} "  or does this not matter?
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: Rich on January 15, 2012, 08:03:41 AM
Hi coreplayer2
You can run  sync  as often as you like, it won't hurt anything. Just because you get a command
prompt back after copying a file, doesn't mean it's been completed, it just means the system is
ready for another command, meanwhile your file might still be copying in the background. The
sync  command won't return until everything that should be written to discs, has been written, so
don't unplug that thumb drive until sync returns.
Quote
sudo find / -iname "MyDir"
You are doing a case insensitive search by using  -iname, this will also return Mydir, mydir, etc...
If you want it to be case sensitive, use -name.
Also, until you finalize this script, you might want to comment out those  2>//dev/null  statements.
This way you can see the error messages to make sure they are all acceptable.
Title: Re: Any method to reliably mount removable drives or all drives on core boot??
Post by: coreplayer2 on January 16, 2012, 01:05:51 AM
Thanks for the continued advise, much appreciated  thanks.

the script was desensitized and MyDir was a substitute, while it's very unlikely the word would have more than one variation, that is a good point especially when redirecting the result of a search to a variable. interesting and noted thanks :)

yes my 3rd post indicates how I tested with -x  and commenting out the error redirects into black holes and also found the read -p  command ideal to pause at defined intervals, and although I didn't show it there I've been using $? occasionally.  All helps to understand what what is happening at that point in time.  thanks

While both scripts are in use now thanks, the  script indicated above works nice on one line with only one redirect, but is almost unreadable :(

I really need to work on understanding the for in command better, I notice it's used heavily in core scripts.   back to the books..


I must say big thanks to both of you, for both scripts run consistently flawless.