Tiny Core Linux

Tiny Core Base => Corepure64 => Topic started by: pek on September 16, 2021, 08:33:12 PM

Title: Auto mount USB drive from .X.d Not Working
Post by: pek on September 16, 2021, 08:33:12 PM
Hi All,
I need another help again today :)

My aim:
1. To auto mount a USB drive with specific label name after X started.
2. run a script from from the USB.


What I did:
1. create a script to mount the USB /home/tc/mountUSB.sh
Code: [Select]
sudo mkdir /tmp/theUSB
sudo mount -L theLABEL /tmp/theUSB
sh /tmp/theUSB/theSCRIPT.sh

2. create a file to load it in /home/tc/.X.d/loadit
Code: [Select]
exec /home/tc/mountUSB.sh &

The problem:
**After boots up to X, the USB was not mounted.
But the /tmp/theUSB directory was successfully created.

**If I manually type exec "/home/tc/mountUSB.sh" in aterm, everything works perfectly and the script within the USB was launched successfully.

Can anyone help me on this please?
Thank you.
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: Rich on September 16, 2021, 09:43:56 PM
Hi pek
Try it like this:
Code: [Select]
#!/bin/sh
Code: [Select]
sudo mkdir /tmp/theUSB
Code: [Select]
drive="`/sbin/blkid -lt LABEL=theLABEL -o device`"
Code: [Select]
sudo mount "$drive" /tmp/theUSB
Code: [Select]
/tmp/theUSB/theSCRIPT.sh
Make  /home/tc/.X.d/loadit  look like this:
Code: [Select]
/home/tc/mountUSB.sh &
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 16, 2021, 10:15:58 PM
Hi Rich, thanks for your suggestion.
However it behaves exactly the same like the original.

**After boots up to X, the USB was not mounted.
But the /tmp/theUSB directory was successfully created.

**when I type exec ./.X.d/loadit in aterm, everything works perfectly and the script within the USB was launched successfully.
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: Rich on September 16, 2021, 10:20:19 PM
Hi pek
Try adding a  sync  command after the  mkdir  line.
Also, make sure your script is executable:
Code: [Select]
chmod 775 /home/tc/mountUSB.sh
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: curaga on September 16, 2021, 10:36:49 PM
Remove the exec, it may block anything after it from running.
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 02:36:53 AM
Hi pek
Try adding a  sync  command after the  mkdir  line.
Also, make sure your script is executable:
Code: [Select]
chmod 775 /home/tc/mountUSB.sh

Hi Rich,
still the same.

This is what i did
Code: [Select]
sudo mkdir /tmp/theUSB
drive="`/sbin/blkid -lt LABEL=theLABEL -o device`"
sudo mount "$drive" /tmp/theUSB
sudo sync -f /tmp/theUSB/theSCRIPT.sh
/tmp/theUSB/theSCRIPT.sh

I'm not sure if that the correct sync command and position.
I think it should be invoked after mount instead of mkdir.
But no joy...
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 02:38:37 AM
Remove the exec, it may block anything after it from running.

Hi curaga,
yes I did as Rich pointed out above.. But still same result.
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: Rich on September 17, 2021, 05:22:38 AM
Hi pek
... I'm not sure if that the correct sync command and position. ...
No, like this:
Code: [Select]
sudo mkdir /tmp/theUSB
sync
drive="`/sbin/blkid -lt LABEL=theLABEL -o device`"
sudo mount "$drive" /tmp/theUSB
/tmp/theUSB/theSCRIPT.sh
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: Rich on September 17, 2021, 05:57:11 AM
Hi pek
If the  sync  command does not help, change the first line of  /home/tc/mountUSB.sh to this:
Code: [Select]
#!/bin/sh -xAnd add these 2 lines immediately after the first line:
Code: [Select]
exec 1>/home/tc/debug.txt
Code: [Select]
exec 2>&1
Restart your machine and attach the the  /home/tc/debug.txt  file to your next post.
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 07:35:37 AM
Hi Rich, it still failed.

Here's the debug.txt
Code: [Select]
+ sudo mkdir /tmp/theUSB
+ sync
++ /sbin/blkid -lt LABEL=theLABEL -o device
+ drive=
+ sudo mount '' /tmp/theUSB
mount: /tmp/theUSB: wrong fs type, bad option, bad superblock on , missing codepage or helper program, or other error.
+ /tmp/theUSB/xyz/theSCRIPT.sh
cd: line 4: can't cd to /tmp/theUSB/xyz: No such file or directory

when I change the mount command into my original one, the debug gives
Code: [Select]
+ sudo mount -L theLABEL /tmp/theUSB
mount: /tmp/theUSB: can't find LABEL="theLABEL".
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: Rich on September 17, 2021, 07:45:07 AM
Hi pek
It's not finding the drive. Is the drives label actually  theLABEL ?

If you run this command manually does it find the device:
Code: [Select]
/sbin/blkid -lt LABEL=theLABEL -o device
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 07:54:28 AM
Hi pek
It's not finding the drive. Is the drives label actually  theLABEL ?

If you run this command manually does it find the device:
Code: [Select]
/sbin/blkid -lt LABEL=theLABEL -o device

yes, it returned
Code: [Select]
/dev/sdb1
And the fact that everything runs properly when invoked "./.X.d/loadit" means all is named correctly.

I edited my reply before this.. i tried mount -L command and gives similar result.
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: curaga on September 17, 2021, 08:16:34 AM
Could be timing then? Your device's label is not yet known at that time?
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 08:21:00 AM
Could be timing then? Your device's label is not yet known at that time?

Possibly.. but i tried "sleep 9".. still nothing
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: Rich on September 17, 2021, 08:26:50 AM
Hi pek
Change this:
Code: [Select]
drive="`/sbin/blkid -lt LABEL=theLABEL -o device`"
To this:
Code: [Select]
drive=""
while [ -z "$drive" ]
do
drive="`/sbin/blkid -lt LABEL=theLABEL -o device`"
s l e e p 1
done

Remove the extra spaces in the  sleep  command.
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 09:08:25 AM
Good news.. It works this time..!!  ;D
Thank you Rich and curaga.

It took about 2 or 3 seconds to get mounted as can be seen in below debug.txt
Code: [Select]
+ sudo mkdir /tmp/theUSB
+ sync
+ drive=
+ '[' -z '' ']'
++ /sbin/blkid -lt LABEL=theLABEL -o device
+ drive=
+ sleep 1
+ '[' -z '' ']'
++ /sbin/blkid -lt LABEL=theLABEL -o device
+ drive=
+ sleep 1
+ '[' -z '' ']'
++ /sbin/blkid -lt LABEL=theLABEL -o device
+ drive=
+ sleep 1
+ '[' -z '' ']'
++ /sbin/blkid -lt LABEL=theLABEL -o device
+ drive=
+ sleep 1
+ '[' -z '' ']'
++ /sbin/blkid -lt LABEL=theLABEL -o device
+ drive=/dev/sdb1
+ sleep 1
+ '[' -z /dev/sdb1 ']'
+ sudo mount /dev/sdb1 /tmp/theUSB

However when I did "sleep 9" without the loop as above, it did not recognized.
Probably the USB drive needs to be probed a few times. Thus the loop "wakes" it up only in few seconds.

I'm not sure how how this works... But it does.
Thank you again guys...!!  :)
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 09:47:11 AM
Sorry.. my bad.
Actually "sleep 9" is also working.
I put it at the wrong line... That's why it did not work.
Code: [Select]
sudo mkdir /tmp/theUSB
sudo mount -L theLABEL /tmp/theUSB
s l e e p 9

the correct one is
Code: [Select]
sudo mkdir /tmp/theUSB
s l e e p 9
sudo mount -L theLABEL /tmp/theUSB

even so.. I prefer Rich's loop code. That way I don't need to guess how long the "sleep" should be.

All good.. Mistery solved.
Sorry for the hassles guys..
And best of all, I learn more new tricks... Thank you!!  ;D ;D
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: Rich on September 17, 2021, 09:58:44 AM
Hi pek
... I prefer Rich's loop code. That way I don't need to guess how long the "sleep" should be. ...
It's also faster since it waits only until the label becomes visible.

The  sleep  command also can handle decimal numbers.
If you want to reduce the wait time a little more, try  sleep 0.25
Title: Re: Auto mount USB drive from .X.d Not Working
Post by: pek on September 17, 2021, 10:47:47 PM
The  sleep  command also can handle decimal numbers.
If you want to reduce the wait time a little more, try  sleep 0.25

Oh.. that's good to know. Thank you.