• Home
  • Help
  • Search
  • Login
  • Register
Pages: [1] 2 3
Author Topic: Managing a USB hard drive on the Plug  (Read 29361 times)
restamp
Global Moderator
Sr. Member
*****

Karma: 4
Posts: 273


View Profile
« on: July 08, 2009, 09:02:24 PM »

This is admittedly a minor success, but I thought some folks might find the solution useful, or at least interesting...

I don't boot from, nor do I have a root fs on, a USB hard drive, but I would like to be able to have one attached to my Plug for some significant data storage needs.  As such, I have not tried to pre-start the USB subsystem from the Uboot, nor do I specify any rootdelay on boot.  Linux is capable of starting up the USB subsystem just fine.  However, it turns out the Plug boots faster than the USB subsystem initializes and therein lies the problem:  If I code a line to mount the hard drive in /etc/fstab, specifying (as I should) that it be fscked, if necessary, the fsck command fails because the devices are not available in /dev yet.  This causes the /etc/init.d/checkfs.sh to stop the boot sequence and require manual intervention.

My solution:  Add an init.d script to check /etc/fstab to see if any /dev/sda* device is slated to be mounted, and if so, wait for the device to appear in /dev.  Here is the script I added, which I call wait4usbdisks:
Code:
#!/bin/sh
#
# If /etc/fstab has been configured to mount a USB drive, pause to give
# the USB drive devices time to show up in /dev.  If this is not done,
# checkfs.sh will fail, requiring manual intervention...
#
case "$1" in
start)
        grep -q ^/dev/sda /etc/fstab &&
        for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
        do
          [ -b /dev/sda ] && exit 0
          sleep 1
        done
        exit 1
        ;;
stop)
        ;;
esac
After creating this file, I then linked it into /etc/rcS.d, to be invoked before checkfs.sh during the initialization sequence:
Code:
# ln -s ../init.d/wait4usbdisks /etc/rcS.d/S25wait4usbdisks
Now when I boot, it hits wait4usbdisks and delays things about 4 seconds until the USB subsystem identifies the hard drive and creates its associated /dev entries.  The boot sequence then goes on to quick-fsck the drive and continue the boot sequence.  If I remove the drive and comment its entry out in /etc/fstab, wait4usbdisks essentially becomes a quick NOP.  I've tried booting several times, so far with 100% success, buy YMMV.

BTW, for those interested, I'm using the hard drive (among other things) as a storage device for recording OTA digital TV using the HDHomeRun digital tuner.  The HDHomeRun device decodes the digital stream and sends it via Ethernet to the Plug for storage.  For the time being, I am NFS-exporting the directory where the programs are saved and using VLC to watch them remotely. The HDHomeRun unit actually has two built-in tuners.  With both tuners simultaneously streaming programs into the plug, I was able to watch one of the streams with only an occasional dropped packet -- and apparently the glitches were only on what was being served to vlc (via NFS); the data streams being stored showed no losses whatsoever.  FWIW, the top program shows the plug utilizing between 50%-60% of its CPU during this test.
Logged

jdonth
Jr. Member
**

Karma: 0
Posts: 75

Azle, Texas


View Profile
« Reply #1 on: July 09, 2009, 07:52:35 AM »

I would be very interested in the code that you use to record from the hdhomerun.

I currently am using an XP machine to record 4 channels (2 HDHR boxes) of OTA and would love to switch to a Plug.

Thanks,
~Joe
Logged

...I've always depended on the kindness of strangers

restamp
Global Moderator
Sr. Member
*****

Karma: 4
Posts: 273


View Profile
« Reply #2 on: July 09, 2009, 08:35:46 AM »

Joe, the software is actually provided by the HDHomeRun folks.  It can be downloaded from:

http://www.silicondust.com/downloads/linux

Of course the GUI is not going to be of value unless you either have a head on your Plug, or export the display to a machine that does (which is doable with "ssh -X"), but if you compile the "libhdhomerun" code, you'll get an executable, hdhomerun_config, which is a command-line program that allows you to control the tuner, and even has a built-in command for saving the stream to disk.  This is what I use, encapsulated in a script to make everything easier.  With a bit of help from cron, I can even record programs when I'm not around.  For information on using hdhomerun_config from the command line, see

http://www.silicondust.com/hdhomerun/instructions

(especially the Development Guide)

Basically, to start streaming to disk, you'll need to do something like:

Code:
hdhomerun_config FFFFFFFF set /tuner0/channel 8vsb:38
hdhomerun_config FFFFFFFF set /tuner0/program 1
hdhomerun_config FFFFFFFF save /tuner0 /hd/programs/PBS-Nova


Bear in mind that what is saved is the raw stream, which is quite a lot of data, especially in 1080i mode (or 1080p if you're streaming off cable).  It can be compressed down to something more reasonable, but probably not in real time on the Plug.  MythTV it ain't -- of course, MythTV does all this seamlessly -- but for now, it's good enough for my needs.

BTW, I would think 4 simultaneous HDHR streams would pretty much consume a 100Base-T Ethernet.  Even though the Plug has 1000Base-T, I think that would be a challenge for it.

Good luck!
« Last Edit: July 09, 2009, 08:40:37 AM by restamp » Logged

jdonth
Jr. Member
**

Karma: 0
Posts: 75

Azle, Texas


View Profile
« Reply #3 on: July 09, 2009, 09:27:31 AM »

Thanks!

I'm going to give it a try with one HDHR and see how it holds up.
Logged

...I've always depended on the kindness of strangers

jaycatt
Newbie
*

Karma: 0
Posts: 4


View Profile
« Reply #4 on: July 29, 2009, 11:20:06 AM »

Restamp,

Thanks for the script!  Booting takes a little longer, but at least now it's possible.  After adding my USB HD to fstab set to fsck, I had to use the USB-serial console to get things moving again, and I'd hoped to survive by SSH alone!  Disabling the fsck let it boot OK, but without automatically mounting.  Now I'm back in business.  But I never imagined it would be so hard to get a USB drive running.

One thing, thought, that I had to do to to get the script running was make it executable. 

From the directory where you saved the script:
Code:
sudo chmod +x wait4usbdisks

Assuming that's what actually did the trick for me after creating a text file with nano from your provided code, I figure you folks had enough experience to know to do that, but as someone who never really stumbled past the occasional ssh or scp on a home network, it drove me nuts.  I've been wondering if the sheevaplug thing is over my head!

Anway, I hope that helps somebody else trying this as their first brush with init scripts.  But if I'm mistaken, please point it out before I lead someone astray....

Again, thanks for the script.

John
Logged

CqCn
Full Member
***

Karma: 0
Posts: 169



View Profile
« Reply #5 on: August 11, 2009, 10:04:20 AM »

restamp, Clever wait script!

I am also interested in trying your application; but first I need to read up and then perhaps with your help if I am stuck, I would like to try this!

BTW, there may be a minor problem with the wait script.  If I read it correctly, the check is only for the existence of fstab line.  But if it is specified no auto mount, then this script will unncessarily delay 20 secs.  Right?
Logged

Cordially, CqCn

restamp
Global Moderator
Sr. Member
*****

Karma: 4
Posts: 273


View Profile
« Reply #6 on: August 11, 2009, 01:00:29 PM »

You are right, CqCn, provided the drive was not plugged in at boot time, but I figured fixing that could be left as an exercise for the user.*  There is also the possibility that someone may have two or more USB drives attached and, as a result, would also have to alter the script accordingly.

* (Not tested, but I think "grep -v noauto /etc/fstab | grep -q ^/dev/sda" would resolve your concern.)
Logged

hypn0toad
Newbie
*

Karma: 0
Posts: 29


View Profile
« Reply #7 on: August 11, 2009, 09:02:44 PM »

works great! my hd now mounts at startup!
Logged

joosty
Newbie
*

Karma: 0
Posts: 26


View Profile
« Reply #8 on: August 12, 2009, 11:17:13 AM »

Wow, a very elegant solution, restamp! I'm now using the following script to wait for my multiple disks, which I am mounting by UUID:

Code:
#!/bin/bash

FS=`grep "^UUID" /etc/fstab | sed 's/^UUID=\([a-z0-9-]*\).*$/\/dev\/disk\/by-uuid\/\1/i'`
for f in $FS ;
do
    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    do
        [ -h $f ] && continue 2
        sleep 1
    done
done

exit 0
« Last Edit: August 13, 2009, 12:23:57 AM by joosty » Logged

mail4asim
Newbie
*

Karma: 0
Posts: 39



View Profile
« Reply #9 on: October 19, 2009, 10:20:52 PM »

It would be nice to write this script up as a How-to on the WIKI..

-Asim
Logged

birdman
Sr. Member
****

Karma: 4
Posts: 440


View Profile WWW
« Reply #10 on: October 20, 2009, 04:00:08 PM »

I think it should be possible to create a udev rule that will mount this at any time it appears on the bus (so boot time or later).
The only problem would be that you'd still have to umount it by-hand if you ever needed to.  As root.
Something like (totally untested):
Code:
ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="xxxx",  ATTR{idProduct} RUN=mount /where/I/want/it
with a suitable noauto entry in fstab for /where/I/want/it using a LABEL and putting a label on the file ssytem(s).
Logged

mgillespie
Full Member
***

Karma: 7
Posts: 239



View Profile
« Reply #11 on: October 28, 2009, 01:39:22 AM »

Done some small tweaks (added a header with info) to the script so that it can be managed with rcconf on debain, as it refused to work on Debain without it.

Code:
#!/bin/sh
### BEGIN INIT INFO
# Provides:          wait4usbdisks
# Required-Start:    checkfs
# Required-Stop:
# Should-Start:
# Should-stop:
# Default-Start:     S
# Default-Stop:
# X-Interactive:     true
# Short-Description: Delays boot to ensure USB disks are connected.
### END INIT INFO

#
# If /etc/fstab has been configured to mount a USB drive, pause to give
# the USB drive devices time to show up in /dev.  If this is not done,
# checkfs.sh will fail, requiring manual intervention...
#

. /lib/lsb/init-functions

case "$1" in
start)
        grep -q ^/dev/sda /etc/fstab &&
        for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
        do
          [ -b /dev/sda ] && exit 0
          sleep 1
        done
        exit 1
        ;;
stop)
        ;;
esac

Logged

truxntrax
Newbie
*

Karma: 0
Posts: 21


View Profile
« Reply #12 on: November 03, 2009, 03:25:24 PM »

Doesn't work for me...

FSTAB file:

Code:
# UNCONFIGURED FSTAB FOR BASE SYSTEM
//192.168.1.155/Volume_1/ /mnt/nas/ cifs username=*****,password=*****,auto,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
/dev/sda2 /mnt/usb auto users,uid=1000,gid=100,utf8,dmask=027,fmask=137 0 0

As instructed copied the script into a file called wait4usbdiscs chmod +x then ln -s ../init.d/wait4usbdisks /etc/rcS.d/S25wait4usbdisks.

My usb drive mounts fine when I run mount -a...  Any ideas?

Many thanks

Logged

torpesco
Newbie
*

Karma: 0
Posts: 5


View Profile
« Reply #13 on: November 16, 2009, 02:09:29 AM »

Just used the udev method (on Debian) to auto-mount an LVM volume group on two external drives.  Note: I have modified this post to correct an error I made when I only had one external drive connected.

I placed the udev rules in /etc/udev/99-local.rules.  By running them last, usb_id and blkid have already run in other default udev rules, allowing me to make use of the environment variables populated by them.
Code:
SUBSYSTEMS=="usb", ENV{ID_VENDOR}=="ST325082", ENV{ID_MODEL}=="4AS", SYMLINK+="uda%n"

SUBSYSTEMS=="usb", ENV{ID_VENDOR}=="WDC_WD15", ENV{ID_MODEL}=="EADS-00P8B0", SYMLINK+="udb%n"

SUBSYSTEMS=="usb", ENV{ID_FS_TYPE}=="LVM2_member", RUN+="/usr/local/bin/lvm_update"

I could have maybe used ID_SERIAL_SHORT for the USB drives, but I'm using a 4-disk enclosure (no RAID, etc -- just shows up to four HDD devices to the OS) and for some reason, while VENDOR/MODEL are correct, the ID_SERIAL_SHORT shows the second disk's serial number for both.  If you're not using LVM, you could always create specific rules to match ID_FS_UUID for each partition to mount the file system on it: RUN+="mount /mnt/whatever".

The first two rules ensure the drives and partitions always have device names that stay the same.

The third rule runs lvm_update in an attempt to activate and mount the logical volumes:
Code:
#!/bin/bash

/sbin/vgchange -a y
/bin/mount /data/misc
/bin/mount /data/backup

I'm sure that could be a little more elegant... ensuring all the physical volumes are present before trying to run vgchange... but it works for now, so good enough for me.

Be sure to test your rules by running udevadm:
Code:
# udevadm test /block/sda
# udevadm test /block/sda/sda1
# udevadm test /block/sdb
# udevadm test /block/sda/sdb1

Just before all of the device attributes are listed, you should see a note like "node_symlink" that indicates, for instance, that /dev/uda1 is a symlink to /dev/sda1.  At the end of the device attributes, you should see a line indicating the script specified in the third rule (if you've created one like it) will be run -- "udevadm_test: run: '/usr/local/bin/lvm_update'"


« Last Edit: November 18, 2009, 01:35:24 AM by torpesco » Logged

hungarianhc
Jr. Member
**

Karma: 2
Posts: 85


View Profile
« Reply #14 on: December 16, 2009, 12:15:32 AM »

I can't seem to get this to work... I've tried a couple of the scripts above, and while I'm not great at shell scripting, I even tried modifying some.

First off, I know my /etc/fstab file is correct as mount -a mounts everything properly.

Then I even tried making my own custom script that just waits for 12 seconds  Grin. I figured that would be enough time, and the script does execute as I had it output that it was going for a 12 second nap.... so the script IS getting excuted, but no mounted USB disks. Help!
Logged

Pages: [1] 2 3
Print
Jump to: