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,

I´m 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,

I´m 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 I´m not able to realize the automount. Tried different ways :-(


All times are GMT -5. The time now is 12:11 AM.