LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script and proof if sd card is available (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-and-proof-if-sd-card-is-available-4175611865/)

fohnbit 08-13-2017 03:25 AM

bash script and proof if sd card is available
 
Hello,

I`m mounting a sd card at boot.
Then I have the partition "/dev/mmcblk0p1"

When the sd card is not inserted, of course this partition will not shown.

Now I need a bash script to do some actions, when the partition is available.

But my test with:
Code:

if [ -d "/dev/mmcblk0p1/" ]
then
      echo "exists."
else
      echo "does not exist."
fi

failt.

Guess it is because it is a partition and not a directory?

How can I check if the partition or the sd card available?

Thank you!

business_kid 08-13-2017 03:50 AM

/dev/mmcblk0p1/ is actually a node, a special type of thing.

Try
Code:

if [ -b "/dev/mmcblk0p1/" ]

AwesomeMachine 08-13-2017 04:38 AM

The script appears correct, but if you're using /dev/mmcblk0p1 as a mount point, then it will always be there. It would be a directory. /dev/mmcblk0p1 cannot be a partition itself. It can only be a mount point.

Separate partitions can be mounted to any mount point (directory off of /). But the partition cannot mount if the mount point is not there. So, I think your terminology is a bit off when you say /dev/mmcblk0p1 is a partition, because I think it's actually a mount point for a partition.

Edit: No, it's probably a device file. business_kid is correct.

ondoho 08-13-2017 10:16 AM

i just went through the process of setting up a udev rule when a usb device is connected.
i'm sure it would apply to your situation also.
this tutorial helped me the most:
http://hackaday.com/2009/09/18/how-to-write-udev-rules/
and
https://wiki.archlinux.org/index.php/Udev

business_kid 08-13-2017 01:00 PM

I have an sdcard; it's device node is /dev/mmcblk0, (equivalent to /dev/sda) and the first partition is /dev/mmcblk0p1 (equivalent to /dev/sda1). I'm pretty sure it's a device node made in udev.
Code:

bash-4.3$ ls -l /dev/mmcblk0p1
brw-rw---- 1 root disk 179, 1 Aug 10 16:26 /dev/mmcblk0p1

The 179,1 is major & minor numbers - see man mknod.

I mount it in /mnt/zip/ which is a directory.

RockDoctor 08-13-2017 04:05 PM

I'm probably not being very efficient about it, but
Code:

mount | grep mmcblk0p1
will return something if the partition is mounted, and nothing if it isn't

fohnbit 08-14-2017 12:52 AM

Goo morning to all,

you are great :-)
With udev it work fine.

So it is better to mount the sd-card not in fstab. I can do this in the sh script, which the udev rule start.

But the Warning:

Warning: To mount removable drives, do not call mount from udev rules. In case of FUSE filesystems, you will get Transport endpoint not connected errors. Instead, you could use udisks that handles automount correctly or to make mount work inside udev rules, copy /usr/lib/systemd/system/systemd-udevd.service to /etc/systemd/system/systemd-udevd.service and replace MountFlags=slave to MountFlags=shared.[3] Keep in mind though that udev is not intended to invoke long-running processes.

did bnot help. The folders are not existing at my Linux and udisks2 seems not working?

here my settings:
udev rule for checking sd-card is inserted:
Code:

/etc/udev/rules.d# cat 81-sdcard.rules
KERNEL=="mmcblk0" SYMLINK+="dlc_sdcard", RUN+="/home/ged/prepare_sd_card.sh"

Code:

/home/ged/prepare_sd_card.sh
#! /bin/sh

/bin/mkdir /home/ged/ok
/bin/mount /dev/mmcblk0p1 /media/card/

Code:

/etc/udev/rules.d/99-udisks2.rules
# UDISKS_FILESYSTEM_SHARED
# ==1: mount filesystem to a shared directory (/media/VolumeName)
# ==0: mount filesystem to a private directory (/run/media/$USER/VolumeName)
# See udisks(8)
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"

The folder "OK" are written ... so my udev rule are working, but the mount are not working.
And udisks2 seems also not mounting

Thank you!

ondoho 08-14-2017 01:28 AM

why do you need to mount the sd card with a custom script?
doesn't it automount on insertion?
or, can't you configure udev (or udisks or udisks2 or gvfs...) to automount it?

my udev rule example was not meant for mounting!
it was meant for what you wrote in post #1: device is present, now do things.

speaking of which, what things do you need to do with the sd card? what scripts/actions?

fohnbit 08-14-2017 02:15 AM

Quote:

Originally Posted by ondoho (Post 5748005)
why do you need to mount the sd card with a custom script?
doesn't it automount on insertion?
or, can't you configure udev (or udisks or udisks2 or gvfs...) to automount it?

my udev rule example was not meant for mounting!
it was meant for what you wrote in post #1: device is present, now do things.

speaking of which, what things do you need to do with the sd card? what scripts/actions?

Hello,

I must not need to mount the sd-card itself with an own scrpit.
But after mounting, I have to do some tasks. Made some, folders, ...

And:
I move the /var/log and other folders to the sd-card with:
/dev/mmcblk0p1 /var/log vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,nofail 0 0
(example from fstab)

So when the sd-card on insertion, I need runnig some tasks.

No, the sd-card will not be automoundet :-(
I use a baglebone black.

And yes, unfortunately I can't configure udev (or udisks or udisks2 or gvfs...) to automount

fohnbit 08-14-2017 02:25 AM

1 Attachment(s)
I try to reach this logic.

Move folders, where often are written, to the SD-Card ... when the SD card is insertion or removing it sync with the temporary backup folder to keep all 3 folders up-2-date

business_kid 08-14-2017 03:45 AM

From the vague picture that's developing, it sounds like networking rather than sdcards might be a better way to go if you're trying to sync folders.

fohnbit 08-14-2017 06:23 AM

Hello,

Im not sure if I unterstand your answer correct, but here are the reason:
I Dont want to write too often in the beaglebone black flash memory. The logfiles write very often and the config folder of an application same.

So I want to move this 2 sources to the sd-card. Neither the logger application or the other application knows about this. They write in the default folder .. but moved with mount to the sd card.

But when the sd card is missing or broken, the system should use the original folder

A cronjob copy each 6h the datas from the sd in a backup folder of the flash memory.
When the beaglebone start or the sd-card will be replaced, it sync all 3 destinations:
back folder
original folder
sd card fold

to get the latest version of each file

Hope I could explain clearly?

fohnbit 08-14-2017 07:16 AM

Hello again,

Im near finished all tasks ... but automount on a beaglebone black with Debian seems not working.

Can someone support me for the automount part?

Thank you!

business_kid 08-14-2017 09:26 AM

Code:

/dev/sdb1        /mnt/hd  auto relatime,diratime,user,exec,dev,suid,nofail  0  0
That6's a good set of options(in /etc/fstab) for usb keys & that automounts. It mounts as root during boot, otherwise it mounts as a luser. I think /mnt/hd is owned by a user.

Frankly, sd cards are not the thing for regular writes either - ramdisks are. You need to choose your most writable source or consider an nfs mount over your network, or such like. Com,pare how many writes you can expect from your beaglebone flash versus an sd card. The flash will probably come out on top. The other consideration is atime. How are you handling that?

fohnbit 08-15-2017 01:42 AM

Hello,

yes, you are right. But if the sd card failed, I can replace without to buy a new beagle and reinstall the software. And later also a Database should be working and therefore I need the sd card.

But Im not able to realize the automount. Tried different ways :-(

business_kid 08-15-2017 11:02 AM

If you're sticjking with the sdcard, 'noatime' would be better than 'relatime.' Atime writes every 5 seconds; relatime writes every 15 seconds; noatime is best.

ondoho 08-16-2017 01:26 AM

i think you should delete your custom udev rules and stick with the shell script, and the advice given in post #2.

or, you should reformulate your problem from scratch.
here's a simple rule:
you never say "it doesn't work" without providing full disclosure about commands issued, output received (errors or not), general system information etc.

fohnbit 08-20-2017 03:39 AM

Hello,

ok, I removed all and try this:
Code:

cat /etc/udev/rules.d/69-sdcard.rules
KERNEL=="mmcblk0", SYMLINK+="sdcard"

ACTION=="add", RUN+="/home/domo/sdcard_insertion.sh"
ACTION=="remove", RUN+="/home/domo/sdcard_removed.sh"

Code:

cat sdcard_insertion.sh
#! /bin/sh

/bin/mkdir /home/domo/sdcard_present
/bin/mount /dev/mmcblk0p1 /media/card/

Code:

cat sdcard_removed.sh
#! /bin/sh

/bin/rm -R /home/domo/sdcard_present

When I now put the sd_card in the beaglebone, the folder are created, when I remove the sdcard, it will be delete.

Now I want to mount over with the sh.script the sdcard. When I put the sdcard in the beaglebone, I get in the syslog:
Code:

Aug 20 10:35:23 foxc0a800bd kernel: [320476.390036] mmc0: host does not support reading read-only switch, assuming write-enable
Aug 20 10:35:23 c0a800bd kernel: [320476.395067] mmc0: new high speed SDHC card at address 0007
Aug 20 10:35:23 c0a800bd kernel: [320476.411492] mmcblk0: mmc0:0007 SD16G 14.4 GiB
Aug 20 10:35:23 c0a800bd kernel: [320476.419344]  mmcblk0: p1

but the folder in /media/card is empty

When I run after in den commandline:
/bin/mount /dev/mmcblk0p1 /media/card/

then it works.

But why not in the bash script?

fohnbit 08-20-2017 03:58 AM

Hello,

I see now in the syslog:
Aug 20 10:53:49 c0a800bd systemd-udevd[20525]: Process '/home/domo/sdcard_insertion.sh' failed with exit code 32.
Aug 20 10:53:53 c0a800bd systemd-udevd[20525]: Process '/home/domo/sdcard_insertion.sh' failed with exit code 32.
Aug 20 10:53:56 c0a800bd systemd-udevd[20525]: Process '/home/domo/sdcard_insertion.sh' failed with exit code 32.

Error 32 means:
mount failure


Same when I remove the sdcard:
Aug 20 10:56:45 c0a800bd kernel: [321758.605831] mmc0: card 0007 removed
Aug 20 10:56:45 c0a800bd kernel: [321758.716607] FAT-fs (mmcblk0p1): FAT read failed (blocknr 802)
Aug 20 10:56:45 c0a800bd kernel: [321758.730084] FAT-fs (mmcblk0p1): unable to read boot sector to mark fs as dirty
Aug 20 10:56:45 c0a800bd systemd-udevd[20666]: Process '/bin/sh /home/domo/sdcard_removed.sh' failed with exit code 32.
Aug 20 10:56:45 c0a800bd systemd-udevd[20671]: Process '/bin/sh /home/domo/sdcard_removed.sh' failed with exit code 32.
Aug 20 10:56:45 c0a800bd systemd-udevd[20666]: Process '/bin/sh /home/domo/sdcard_removed.sh' failed with exit code 32.

But why 3-times?

Here the new scripts:
cat sdcard_insertion.sh
#! /bin/sh

/bin/mkdir /home/domo/sdcard_present
sleep 3s
fsck.vfat -v -a -w /dev/mmcblk0p1
/bin/mount /dev/mmcblk0p1 /media/card/


cat sdcard_removed.sh
#! /bin/sh

/bin/rm -R /home/domo/sdcard_present
/bin/umount /media/card/

business_kid 08-20-2017 10:17 AM

Code:

Aug 20 10:56:45 c0a800bd kernel: [321758.605831] mmc0: card 0007 removed
Aug 20 10:56:45 c0a800bd kernel: [321758.716607] FAT-fs (mmcblk0p1): FAT read failed (blocknr 802)

In the first line, why is it /dev/mmc0? Is your sdcard mounting sometimes as /dev/mmc0? It certainly would explain the FAT read fail.
Code:

grep mmc0 /var/log/messages
might clear this up.

Checking with 'grep -r', I have no mention of mmc0 in /lib/udev/ or /etc/udev, or of course in /dev, although I have /dev/mmcblk0, & /dev/mmcblk0p1.

fohnbit 08-20-2017 11:59 PM

Hello,

I dont know why there is mmc0.

Here are some informations:
cat /etc/udev/rules.d/69-sdcard.rules
Code:

KERNEL=="mmcblk0p1", SYMLINK+="sdcard", GOTO="sd_cards_auto_mount_end"

ACTION=="add", RUN+="/bin/sh /home/domo/sdcard_insertion.sh"
ACTION=="remove", RUN+="/bin/sh /home/domo/sdcard_removed.sh"

LABEL="sd_cards_auto_mount_end"

grep mmc0 /var/log/messages
Code:

Aug 21 06:48:48 foxc0a800bd kernel: [  332.849825] mmc0: host does not support reading read-only switch, assuming write-enable
Aug 21 06:48:48 foxc0a800bd kernel: [  332.855045] mmc0: new high speed SDHC card at address 0007
Aug 21 06:48:48 foxc0a800bd kernel: [  332.871791] mmcblk0: mmc0:0007 SD16G 14.4 GiB

dmesg
Code:

[  332.849825] mmc0: host does not support reading read-only switch, assuming write-enable
[  332.855045] mmc0: new high speed SDHC card at address 0007
[  332.871791] mmcblk0: mmc0:0007 SD16G 14.4 GiB
[  332.879312]  mmcblk0: p1

tail -f /var/log/syslog
Code:

Aug 21 06:48:48 c0a800bd kernel: [  332.849825] mmc0: host does not support reading read-only switch, assuming write-enable
Aug 21 06:48:48 c0a800bd rsyslogd-2007: action 'action 17' suspended, next retry is Mon Aug 21 06:49:18 2017 [try http://www.rsyslog.com/e/2007 ]
Aug 21 06:48:48 c0a800bd kernel: [  332.855045] mmc0: new high speed SDHC card at address 0007
Aug 21 06:48:48 c0a800bd kernel: [  332.871791] mmcblk0: mmc0:0007 SD16G 14.4 GiB
Aug 21 06:48:48 c0a800bd kernel: [  332.879312]  mmcblk0: p1
Aug 21 06:48:52 c0a800bd systemd-udevd[5459]: Process '/bin/sh /home/domo/sdcard_insertion.sh' failed with exit code 32.
Aug 21 06:48:55 c0a800bd systemd-udevd[5459]: Process '/bin/sh /home/domo/sdcard_insertion.sh' failed with exit code 32.

cat sdcard_insertion.sh
Code:

#! /bin/sh

/bin/mkdir /home/domo/sdcard_present
sleep 3s
fsck.vfat -v -a -w /dev/mmcblk0p1
/bin/mount /dev/mmcblk0p1 /media/card/


business_kid 08-21-2017 03:41 AM

Can we try a workaround? With the offending card out,
Code:

cd /dev
sudo ln - sf mmcblk0p1 mmc0

If that works, it's a gift, but I don't really expect it to. I did a basic grep in the kernel source, and mmc0 appears to be handed out by the kernel, and not udev, and seems to be part of various sdcard controllers and GPIO (General Purpose Input Output) stuff, as sdcards are not read by a bus. GPIO stuff is old, slow & clunky, implemented these days in a small corner of some asic. HP used to have a GPIO bus on their instruments to automate them (in the days before widespread automation), but nobody in their right mind outside of a factory environment ever used it :-/. I'm actually surprised to find it still alive.

fohnbit 08-21-2017 11:45 AM

Hello,

I get:
Code:

ln: target mmc0 is not a directory

business_kid 08-21-2017 11:54 AM

Right. I hoped rather than expected that to work. Can you post the output of
Code:

ls -l /dev/mmc*
both with the sdcard in and out.

fohnbit 08-21-2017 12:00 PM

with sdcard:
Code:

brw-rw---- 1 root disk 179, 24 Aug 21 18:58 /dev/mmcblk0
brw-rw---- 1 root disk 179, 25 Aug 21 18:58 /dev/mmcblk0p1
brw-rw---- 1 root disk 179,  0 Aug 21 13:36 /dev/mmcblk1
brw-rw---- 1 root disk 179,  8 Aug 21 13:36 /dev/mmcblk1boot0
brw-rw---- 1 root disk 179, 16 Aug 21 13:37 /dev/mmcblk1boot1
brw-rw---- 1 root disk 179,  1 Aug 21 13:37 /dev/mmcblk1p1

without sdcard:
Code:

brw-rw---- 1 root disk 179,  0 Aug 21 13:36 /dev/mmcblk1
brw-rw---- 1 root disk 179,  8 Aug 21 13:36 /dev/mmcblk1boot0
brw-rw---- 1 root disk 179, 16 Aug 21 13:37 /dev/mmcblk1boot1
brw-rw---- 1 root disk 179,  1 Aug 21 13:37 /dev/mmcblk1p1

Thanks for your help

business_kid 08-21-2017 01:45 PM

No mmc0? But it was there previously, and screwed things up.

I THINK what's going on is that mmc0 is some bit of the sdcard apparatus, which is mistaken by the kernel for the actual sdcard. Then the card is seen on mmc0, not mmcblk0. If that's right, you might solve it with one of the following bodges:

1. Put mmc0 in /etc/fstab with a 'noauto' option, (before mmcblk0p1) so it will not be mounted.

2. Copy the fstab line for mmcblk0p1, except make mmc0 the device node. This line should go after mmcblk0p1. Both should not exist at the same time. Scripts will have to be changed to use the mount directory instead of the device node.

For your script in post #1, you would have something like
Code:

if [ -b /dev/mmcblk0p1];
<syntax & stuff>
elif [ -b /dev/mmc0];
<syntax & stuff>

Otherwise, I'd look into whether you could cobble a kernel without mmc0. Distro kernels are inclined to have everything but the kitchen sink in them, but you mightn't need the offending stuff. What is /dev/mmcblk1 in your box?

rypikashu90 08-21-2017 03:09 PM

forum site's accounts .research involves looking at not just one source/website but lots of sources and websites/videos to gather as much information as you can then come to a conclusion ahhaha [racism+personal attack].come at me i dare you-[WWII era racism, also seen earlier]

rypikashu90 08-21-2017 03:09 PM

Note: Post very much edited by mod to remove harshest words and directed attacks against a member. It should seem clear that the OP here chose to go on the extreme offensive with the slightest provocation and this type of behavior is not acceptable within LQ ever.

fohnbit 08-22-2017 12:06 AM

Good morning,

ok, I can try ... but when I start the script at the commandline and not via udev, it works fine.

And this is my fstab:
Code:

cat /etc/fstab
# /etc/fstab: static file system information.
#
UUID=a26ac92c-c772-40fc-aa2e-0ccc87a4bb30  /  ext4  noatime,errors=remount-ro  0  1
debugfs  /sys/kernel/debug  debugfs  defaults  0  0

Quote:

Originally Posted by business_kid (Post 5750650)
No mmc0? But it was there previously, and screwed things up.

I THINK what's going on is that mmc0 is some bit of the sdcard apparatus, which is mistaken by the kernel for the actual sdcard. Then the card is seen on mmc0, not mmcblk0. If that's right, you might solve it with one of the following bodges:

1. Put mmc0 in /etc/fstab with a 'noauto' option, (before mmcblk0p1) so it will not be mounted.

2. Copy the fstab line for mmcblk0p1, except make mmc0 the device node. This line should go after mmcblk0p1. Both should not exist at the same time. Scripts will have to be changed to use the mount directory instead of the device node.

For your script in post #1, you would have something like
Code:

if [ -b /dev/mmcblk0p1];
<syntax & stuff>
elif [ -b /dev/mmc0];
<syntax & stuff>

Otherwise, I'd look into whether you could cobble a kernel without mmc0. Distro kernels are inclined to have everything but the kitchen sink in them, but you mightn't need the offending stuff. What is /dev/mmcblk1 in your box?


fohnbit 08-22-2017 01:58 AM

Hello,

crazy ... now it works without any script or udev rule.

I added this to the fstab:
Code:

/dev/mmcblk0p1  /media/card  auto auto,rw,async,user,nofail  0  0
Now anytime I put it the sd card in the beaglebone, it mount and unmount when I remove.

business_kid 08-22-2017 03:24 AM

If it ain't broke, don't fix it.

Mark this solved. It never occurred to me that you were running without an fstab line.


All times are GMT -5. The time now is 01:37 AM.