USB Syncing: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 96: Line 96:
</source>
</source>


sudo nano /etc/usbmount/usbmount.conf
<source lang="bash">
<source lang="bash">
usbmount options...
...
MOUNTOPTIONS="noexec,nodev,noatime,nodiratime"
FS_MOUNTOPTIONS="-fstype=vfat,uid=pi,gid=pi,umask=0"
...
</source>
</source>
NB: it was necessary to put -fstype=vfat. Despite the expectation that if you remove -fstype it would apply to ALL file types, it seems not to actually work!
[[Category:Cookbook]]
[[Category:Cookbook]]

Revision as of 18:36, 13 January 2014

import os, time

initial = set(os.listdir("/media"))

while True:
    media = set(os.listdir("/media"))
    new = media.difference(initial)
    if (len(new) == 2):
        sticks = list(new)
        print "TIME TO SYNC"
        os.system("mplayer -loop 0 sticksync/sync.ogg &")

        # rsync 0 => 1
        cmd = "rsync -rv /media/{0}/ /media/{1}/ ".format(sticks[0], sticks[1])
        print cmd
        os.system(cmd)

        # rsync 1 => 0
        cmd = "rsync -rv /media/{1}/ /media/{0}/ ".format(sticks[0], sticks[1])
        print cmd
        os.system(cmd)
        os.system("killall mplayer")

        while True:
            print "waiting for less than two sticks"
            media = set(os.listdir("/media"))
            new = media.difference(initial)
            if (len(new) < 2):
                break
            time.sleep(1)

        # os.system(cmd)
    else:
        print "nothing to do"

    time.sleep(1)

https://archive.org/details/EqueTheartoflisteningSync

Auto login on pi: http://stackoverflow.com/questions/17830333/start-raspberry-pi-without-login

Getting it to work on a PI

Getting it to work on a pi required some altering. First we needed to install the usbmount package.

  sudo apt-get install usbmount

This however creates a set of /media/usb0, /media/usb1 mount points that are always there. So we can't simply check to see if new file names exist. The fix is to alter the usbmount options to make sure that the auto-mounted files are owned by the "pi" user, and then to filter the list to only check those owned by pi (aka uid 1000).

import os, time

initial = set(os.listdir("/media"))

def checknew ():
    files = os.listdir("/media")
    files = [x for x in files if os.stat("/media/"+x).st_uid == 1000]
    return set(files).difference(initial)

def do(cmd):
    print cmd
    os.system(cmd)

while True:
    new = checknew()
    if (len(new) == 2):
        sticks = list(new)
        print "TIME TO SYNC"
        do("mplayer -loop 0 sticksync/sync.ogg &")

        # rsync 0 => 1
        cmd = "rsync -rv /media/{0}/ /media/{1}/ ".format(sticks[0], sticks[1])
        do(cmd)
        # rsync 1 => 0
        cmd = "rsync -rv /media/{1}/ /media/{0}/ ".format(sticks[0], sticks[1])
        do(cmd)

        do("pumount /media/{0}".format(sticks[0]))
        do("pumount /media/{0}".format(sticks[1]))
        do("killall mplayer")

        while True:
            print "waiting for less than two sticks"
            new = checknew()
            if (len(new) < 2):
                break
            time.sleep(1)

        # os.system(cmd)
    else:
        print "nothing to do"

    time.sleep(1)

sudo nano /etc/usbmount/usbmount.conf

...
MOUNTOPTIONS="noexec,nodev,noatime,nodiratime"
FS_MOUNTOPTIONS="-fstype=vfat,uid=pi,gid=pi,umask=0"
...

NB: it was necessary to put -fstype=vfat. Despite the expectation that if you remove -fstype it would apply to ALL file types, it seems not to actually work!