View Single Post
Posts: 21 | Thanked: 7 times | Joined on Jul 2010 @ Ipswich, England
#6
OK, here's a way to achieve this. Admittedly a bit of a cludge, but it has the advantage of failing non-fatally.

One limitation is that it won't work if any processes open any files on either the SD card or MyDocs before it's run, as that will prevent the filesystem from being unmounted. As an upstart script it's probably not great, but it's working for me...

If it fails, it tries to restore the system to as close as possible to its original state (eg, re-mounting the original MyDocs partition).

If you've got processes that need to wait for the new MyDocs partition to be ready (for example my MySQL server, whose data files are on MyDocs) this script emits MYDOCS_REPLACED when it's successfully done its job, so you can change the upstart file's "start on" stanza to:
Code:
start on MYDOCS_REPLACED
Upstart script replaceMyDocs
Code:
description "unmounts normal MyDocs filesystem and replaces it with the SD card"

# Runs afer rcS-late has mounted everything
start on MOUNTS_OK

console none

script

        # Alter the next six lines if necessary for your system
        MYDOCS_DEVICE=/dev/mmcblk0p1
        SD_DEVICE=/dev/mmcblk1p1
        MYDOCS_MOUNTPOINT=/home/user/MyDocs
        MYDOCS_FILESYSTEM=vfat
        SD_MOUNTPOINT=/media/mmc1
        SD_FILESYSTEM=vfat

        # Check that MyDocs hasn't already been replaced
        if [ ! -z "`mount | grep $MYDOCS_MOUNTPOINT | grep $SD_DEVICE`" ]
        then
                # Already mounted.
                normal exit 0
        fi
        # It can take a few seconds for MyDocs to actually mount after rsS-late
        # has finished, so we'll watch for it
        i=0
        while [ -z "`mount | grep $MYDOCS_MOUNTPOINT`" -o -z "`mount | grep $SD_MOUNTPOINT`" ]
        do
                i=$(($i+1))
                if [ $i -eq 120 ]
                then
                        # If it hasn't mounted within two minutes, something's gone
                        # horribly wrong, so we may as well give up
                        normal exit 0
                fi
                sleep 1
        done
        # Get mount options from the fstab file
        OPTS=`grep $MYDOCS_MOUNTPOINT /etc/fstab | awk ' { print $4 } '`

        # Unmount the SD card
        umount $SD_MOUNTPOINT
        # Check it unmouted OK
        if [ $? -eq 0 ]
        then
                # Unmount the MyDocs filesystem
                umount $MYDOCS_MOUNTPOINT
                # Check it unmounted OK
                if [ $? -eq 0 ]
                then
                        # Mount the SD on MyDocs
                        mount $SD_DEVICE -t $SD_FILESYSTEM -o "$OPTS" $MYDOCS_MOUNTPOINT
                        if [ $? -ne 0 ]
                        then
                                # Failed to mount the SD so fall back to original
                                mount $MYDOCS_DEVICE -t $MYDOCS_FILESYSTEM -o "$OPTS" $MYDOCS_MOUNTPOINT
                        else
                                initctl emit MYDOCS_REPLACED
                        fi
                fi
        fi

end script
normal exit 0