LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2017, 04:25 AM   #1
fohnbit
LQ Newbie
 
Registered: Aug 2017
Posts: 15

Rep: Reputation: Disabled
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!
 
Old 08-13-2017, 04:50 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 10,893

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
/dev/mmcblk0p1/ is actually a node, a special type of thing.

Try
Code:
if [ -b "/dev/mmcblk0p1/" ]
 
Old 08-13-2017, 05:38 AM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,511

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006
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.

Last edited by AwesomeMachine; 08-13-2017 at 11:32 AM.
 
Old 08-13-2017, 11:16 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,223
Blog Entries: 9

Rep: Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617
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
 
Old 08-13-2017, 02:00 PM   #5
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 10,893

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
Old 08-13-2017, 05:05 PM   #6
RockDoctor
Senior Member
 
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Ubuntu, Linux Mint
Posts: 1,714

Rep: Reputation: 400Reputation: 400Reputation: 400Reputation: 400Reputation: 400
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
 
Old 08-14-2017, 01:52 AM   #7
fohnbit
LQ Newbie
 
Registered: Aug 2017
Posts: 15

Original Poster
Rep: Reputation: Disabled
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!
 
Old 08-14-2017, 02:28 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,223
Blog Entries: 9

Rep: Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617Reputation: 3617
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?
 
Old 08-14-2017, 03:15 AM   #9
fohnbit
LQ Newbie
 
Registered: Aug 2017
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
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
 
Old 08-14-2017, 03:25 AM   #10
fohnbit
LQ Newbie
 
Registered: Aug 2017
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
Attached Files
File Type: pdf Sd-Card Logic.pdf (159.7 KB, 43 views)
 
Old 08-14-2017, 04:45 AM   #11
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 10,893

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
Old 08-14-2017, 07:23 AM   #12
fohnbit
LQ Newbie
 
Registered: Aug 2017
Posts: 15

Original Poster
Rep: Reputation: Disabled
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?
 
Old 08-14-2017, 08:16 AM   #13
fohnbit
LQ Newbie
 
Registered: Aug 2017
Posts: 15

Original Poster
Rep: Reputation: Disabled
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!
 
Old 08-14-2017, 10:26 AM   #14
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 10,893

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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?
 
Old 08-15-2017, 02:42 AM   #15
fohnbit
LQ Newbie
 
Registered: Aug 2017
Posts: 15

Original Poster
Rep: Reputation: Disabled
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 :-(
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 02:36 PM
MLED installation script as a proof-of-concept gegechris99 Microlinux / MLED 139 01-11-2017 09:03 AM
LXer: 4 host-proof or PRISM-proof Cloud storage services LXer Syndicated Linux News 0 08-19-2013 03:30 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration