This forum has been archived. All content is frozen. Please use KDE Discuss instead.

post-mount script for linking to music drive

Tags: None
(comma "," separated)
Damian Eads
Registered Member
Posts
2
Karma
0
Hi all,

I started using Amarok about two years ago and I really love it. I think it kicks butt in terms of usability, performance, and features.

I recently switched to Fedora from Ubuntu and I noticed that when Fedora mounts hotplugged drives, it uses a pool of names /media/disk-i where i corresponds to the i'th unused mounting point. Since I plug in my four external hard drives in many different orders, it is unlikely a disk has the same path name each time it is mounted. This poses a problem because Amarok stores the absolute path (e.g. /media/blah/path-to-music-file) for each filename in the Amarok collection database. This means that when the drive where I have my Amarok music is mounted with a different mounting point than the mounting point used during the collection scan, Amarok can't find any of the files in its database.

My solution is the following. I put a file called amarok-disk in /media/amarok-drive. The following bash script is called whenever the drive containing my music is mounted.

Code: Select all
#!/bin/sh

JUST_MOUNTED=$1

# If the disk just mounted was the disk with our Amarok music, link it
# appropriately.
if [ -f ${JUST_MOUNTED}/amarok-disk ]; then

    # If there was a previous link, remove it.
    if [ -f /media/amarok ]; then
        rm /media/amarok
    fi
    ln -s ${JUST_MOUNTED} /media/amarok
fi


Then I call the script above as follows

Code: Select all
amarok-work /media/disk-3


which performs the soft-link appropriately, which links /media/amarok to the drive I passed in. I also make sure that I use the static pathname /media/amarok as the base path for all the music files in my Amarok collection.

Someone most definitely has a better solution than what I came up with. However, I'd like to know how to call the script above whenever a drive is hotplugged. I skimmed the lengthy automounter, fstab, and hotplug HOW-TO documents but I wasn't able to quickly find any leads. Also, how do I populate the JUST_MOUNTED variable so that it points to the mounting point of the device just mounted?

I tried to see if crontab had a feature that would look for conditions (like a drive just got mounted) instead of matching times but it does not.

Also, the script could be generalized to handle multiple music drives. The user puts a unique, static pathname (e.g. /media/amarok-2) in the /media/x/amarok-disk file for every music disk they have. When building their collection database, only these pathnames are used. Here is the modified version of the script.

Code: Select all
#!/bin/sh

JUST_MOUNTED=$1

# If the disk just mounted was the disk with our Amarok music, link it
# appropriately.
if [ -f ${JUST_MOUNTED}/amarok-disk ]; then
    # Get the disk path name from the amarok-disk file
    AMAROK_DISK_PATHNAME=`cat ${JUST_MOUNTED}/amarok-disk`
    if [ -z "${AMAROK_DISK_PATHNAME}" ]; then
        AMAROK_DISK_PATHNAME=/media/amarok
    fi

    # If there was a previous link, remove it.
    if [ -f ${AMAROK_DISK_PATHNAME} ]; then
        rm ${AMAROK_DISK_PATHNAME}
    fi

    ln -s ${JUST_MOUNTED} $AMAROK_DISK_PATHNAME
fi


Can someone tell me if they're using a better approach? If not, can someone help me find out how to call script automatically after a mount and populate the JUST_MOUNTED variable?

Thanks a lot!

Regards,

Damian Eads

Last edited by Damian Eads on Sat Mar 08, 2008 6:43 am, edited 1 time in total.
User avatar
dangle_wtf
Moderator
Posts
1252
Karma
0
Excellent first post here! A step up from the usual ;)  Thanks for shedding a bit more light on something quite a few people have complained about. Might not be elegant, but it's a great start.

Good luck with your search for more info.


"There are two theories to arguing with women. Neither one works."
.
If men could get pregnant, we'd learn the true meaning of "screaming nancyboy wuss"
Damian Eads
Registered Member
Posts
2
Karma
0
Thanks.

After searching quite a bit, I finally came to a suitable solution, and it's derived from http://www.nabble.com/-F8-i386--Drive-m ... 39964.html.

As it turns out, if the disk partition is labeled with a volume label X, /media/X is used; otherwise /media/disk-i is used. Since all of my disks didn't have volume labels, they were mounting with  /media/disk-i as the mounting point, not a consistent, static, mounting point. The e2label and df commands verify this.

Code: Select all
bash$ df -h
Filesystem            Size  Used Avail Use% Mounted on

      [...]

/dev/sdb1             230G   31G  187G  15% /media/disk-3
bash$ e2label /dev/sdb1
bash$


Now let's unmount the drive so that after we label the drive, we can test that it mounts correctly. Next, use the e2label command to specify a new volume label. I chose 'mymusic' as the volume label for my music drive.
Code: Select all
bash$ umount /dev/sdb1
bash$ e2label /dev/sdb1 mymusic


Now, hot plug the drive to test that it mounts correctly. Once the drive is mounted, list the currently mounted partitions.

Code: Select all
bash$ df -h
Filesystem            Size  Used Avail Use% Mounted on

      [...]

/dev/sdb1             230G   31G  187G  15% /media/mymusic


It mounts as /media/mymusic! It does so regardless of the order its mounted.

So, in short, you can forget about my previous solution as it is obviously overly complicated, and this solution is much simpler, easier to setup, and more portable.

Note that the e2label solution only works for ext3 drives. The posting at nabble says mlabel changes the volume label for a FAT drive, and this command installs with the mtools package. As the author indicates in his post, he is uncertain if mlabel also works for NTFS drives. Unfortunately, I don't have a FAT or NTFS drive to test his solution.

I hope this helps.

Best Regards,

Damian


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]