WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Auto mount USB drive from .X.d Not Working  (Read 3244 times)

Offline pek

  • Jr. Member
  • **
  • Posts: 61
Auto mount USB drive from .X.d Not Working
« 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.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 10291
Re: Auto mount USB drive from .X.d Not Working
« Reply #1 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 &
« Last Edit: September 16, 2021, 09:46:12 PM by Rich »

Offline pek

  • Jr. Member
  • **
  • Posts: 61
Re: Auto mount USB drive from .X.d Not Working
« Reply #2 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.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 10291
Re: Auto mount USB drive from .X.d Not Working
« Reply #3 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

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 10692
Re: Auto mount USB drive from .X.d Not Working
« Reply #4 on: September 16, 2021, 10:36:49 PM »
Remove the exec, it may block anything after it from running.
The only barriers that can stop you are the ones you create yourself.

Offline pek

  • Jr. Member
  • **
  • Posts: 61
Re: Auto mount USB drive from .X.d Not Working
« Reply #5 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...

Offline pek

  • Jr. Member
  • **
  • Posts: 61
Re: Auto mount USB drive from .X.d Not Working
« Reply #6 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.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 10291
Re: Auto mount USB drive from .X.d Not Working
« Reply #7 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
« Last Edit: September 17, 2021, 05:59:01 AM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 10291
Re: Auto mount USB drive from .X.d Not Working
« Reply #8 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.

Offline pek

  • Jr. Member
  • **
  • Posts: 61
Re: Auto mount USB drive from .X.d Not Working
« Reply #9 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".
« Last Edit: September 17, 2021, 07:47:28 AM by pek »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 10291
Re: Auto mount USB drive from .X.d Not Working
« Reply #10 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

Offline pek

  • Jr. Member
  • **
  • Posts: 61
Re: Auto mount USB drive from .X.d Not Working
« Reply #11 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.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 10692
Re: Auto mount USB drive from .X.d Not Working
« Reply #12 on: September 17, 2021, 08:16:34 AM »
Could be timing then? Your device's label is not yet known at that time?
The only barriers that can stop you are the ones you create yourself.

Offline pek

  • Jr. Member
  • **
  • Posts: 61
Re: Auto mount USB drive from .X.d Not Working
« Reply #13 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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 10291
Re: Auto mount USB drive from .X.d Not Working
« Reply #14 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.