script to unmount and remount HD

Discussion in 'Asuswrt-Merlin' started by sabot105mm, Feb 27, 2013.

  1. sabot105mm

    sabot105mm Regular Contributor

    Joined:
    Feb 1, 2013
    Messages:
    52
    script to unmount and remount HD (Solved)

    could someone guide me on how to write a script to mount my HDD at 12pm and unmount it at 3am.
     
    Last edited: Feb 28, 2013
  2. joegreat

    joegreat Senior Member

    Joined:
    Jan 9, 2013
    Messages:
    481
    Location:
    Vienna, Austria
    Adding jobs to the crontab

    Hi,

    Here we go - hope it helps and there are not to many typos in the text... :rolleyes:

    With kind regards
    Joe :cool:

    Prerequesits:
    • Enable and format the Persistent JFFS2 partition on the router under Administration / System.
    • Make yourself familiar with Telnet (you can use PuTTY or the default Windows telnet command).
    • Make yourself comfortable with the vi editor (or install Entware and nano for a more comfortable editor).

    a) Find the USB attached HDD:
    Code:
    chief@RT-N66U:/tmp/home/root# fdisk -l
    
    Disk /dev/sda: 750.1 GB, 750156374016 bytes
    255 heads, 63 sectors/track, 91201 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
       Device Boot      Start         End      Blocks  Id System
    /dev/sda2              66          73       64260  82 Linux swap
    /dev/sda4             139       91201   731463547+ 83 Linux
    
    Disk /dev/sdb: 1018 MB, 1018691584 bytes
    30 heads, 29 sectors/track, 2286 cylinders
    Units = cylinders of 870 * 512 = 445440 bytes
    
       Device Boot      Start         End      Blocks  Id System
    /dev/sdb1               1        2287      994691+  6 FAT16
    Looking on the fdisk output my HDD is mapped to the device /dev/sda.
    The partion to be mounted and unmounted is mapped to the device name /dev/sda4
    To be able to remove the HDD in my case the Linux swap also need to be stopped.
    Of course you may have different device names!

    Now we need to know the standard mount points:
    Code:
    chief@RT-N66U:/tmp/home/root# mount
    rootfs on / type rootfs (rw)
    /dev/root on / type squashfs (ro)
    proc on /proc type proc (rw)
    tmpfs on /tmp type tmpfs (rw)
    devfs on /dev type tmpfs (rw,noatime)
    sysfs on /sys type sysfs (rw)
    devpts on /dev/pts type devpts (rw)
    /dev/mtdblock4 on /jffs type jffs2 (rw,noatime)
    usbfs on /proc/bus/usb type usbfs (rw)
    /dev/sda4 on /tmp/mnt/Data type ext3 (rw,nodev,noatime,data=ordered)
    /dev/sdb1 on /tmp/mnt/sdcard type ext3 (rw,nodev,noatime,data=ordered 
    The Data partition (/dev/sda4) is by default mounted to the mount point /tmp/mnt/Data.
    The file systems type is ext3 and the mount options are rw,nodev,noatime,data=ordered.

    b) Unmount the HDD:
    The test the unmount command:
    umount /tmp/mnt/Data

    Create a unmount script:
    nano (or vi) /jffs/scripts/unmount-hdd.sh
    Enter the text:
    #!/bin/sh
    /bin/umount /tmp/mnt/Data


    For nano type: "Ctrl-O" plus "Enter" to save and exit

    c) Mount the HDD:
    The test the mount command:
    /bin/mount -t ext3 -o rw,nodev,noatime,data=ordered /dev/sda4 /tmp/mnt/Data
    Remember to use your specific device path and mount point!

    Creat the mount script:
    nano (or vi) /jffs/scripts/mount-hdd.sh
    Enter the text:
    #!/bin/sh
    /bin/mount -t ext3 -o rw,nodev,noatime,data=ordered /dev/sda4 /tmp/mnt/Data


    For nano type: "Ctrl-O" plus "Enter" to save and exit

    d) Elaborate on cron jobs and cru:
    The full syntax of the cron jobs can be found in the cron wiki page

    What we need is the syntax for adding a cron job to mount the HDD at 12:00pm (and 3:00am), every day:
    Code:
    chief@RT-N66U:/jffs/scripts# cru a mount "0 12 * * * /jffs/scripts/mount-hdd.sh"
    chief@RT-N66U:/jffs/scripts# cru a unmount "0 3 * * * /jffs/scripts/unmount-hdd.sh" 
    Let's check the if the cru add was a success:
    Code:
    chief@RT-N66U:/jffs/scripts# cru l
    0 12 * * * /jffs/scripts/mount-hdd.sh #mount#
    0 3 * * * /jffs/scripts/unmount-hdd.sh #unmount#
    Let's delete the mount cru:
    Code:
    chief@RT-N66U:/jffs/scripts# cru d mount
    chief@RT-N66U:/jffs/scripts# cru d unmount
    chief@RT-N66U:/jffs/scripts# cru l
    e) Create a cron setup script:
    Add the script to jffs to execute the cron scheduling on each boot of the router. Details on the different user scripts can be found in Merlin's wiki page.

    nano (or vi) /jffs/scripts/services-start
    Enter the text:
    Code:
    [B]#!/bin/sh
    /usr/bin/logger -t START_$(basename $0) "started [$@]"  [/B]# logs the start of the script into the syslog.log
    [B]SCRLOG=/tmp/$(basename $0).log  [/B]# creates a log for this script (services-start.log)
    [B]touch $SCRLOG  [/B]# creates the log file
    [B]echo "START_$(basename $0) started [$@]" >> $SCRLOG  [/B]# writes the start into the log
    [B]/usr/sbin/cru a mount "0 12 * * 0 /jffs/scripts/mount-hdd.sh" >> $SCRLOG  [/B]# adds the mount cron job
    [B]/usr/sbin/cru a unmount "0 3 * * 0 /jffs/scripts/unmount-hdd.sh" >> $SCRLOG  [/B]# adds the unmount cron job
    [B]if [ "$?" -ne 0 ]  [/B]# checks for error
    [B]then
    echo "Error in services-start execution! Script: $0" >> $SCRLOG  [/B]# writes error message into the log file
    [B]exit $?  [/B]# exit with error code
    [B]else
    echo "Services-start execution OK. Script: $0" >> $SCRLOG[/B]  # writes OK (if no error) into the log file
    [B]exit 0  [/B]# exit with OK code
    [B]fi[/B]
    f) Make all scripts in JFFS executable:
    chmod a+rx /jffs/scripts/*

    g) Reboot the router to verify that everything is working
    Check the syslog.log for start of the user script:
    Code:
    chief@RT-N66U:/jffs/scripts# more /tmp/syslog.log | grep started
    Jan  1 01:00:13 START_services-start: started [] 
    Check the services-start.log for messages:
    Code:
    chief@RT-N66U:/jffs/scripts# more /tmp/services-start.log
    START_services-start started []
    Services-start execution OK. Script: /jffs/scripts/services-start
    Check cru for the cron events:
    cru l

    h) Have fun with your automated enabled / disabled HDD!
     
  3. sabot105mm

    sabot105mm Regular Contributor

    Joined:
    Feb 1, 2013
    Messages:
    52
    Thanks for this setup I like it but I'm having trouble remounting the hd.
    Code:
    mounting /dev/sda1 on /tmp/mnt/HD failed: No such device
    even though it says sda1 dosnt exist, that's what it used to mount it when I turned the router on.
    Also I have a question. I set the sda interface to sleep after 10 minutes will it still sleep if its unmounted?
     
    Last edited: Feb 28, 2013
  4. joegreat

    joegreat Senior Member

    Joined:
    Jan 9, 2013
    Messages:
    481
    Location:
    Vienna, Austria
    Hi,

    Can you also post the full mount command you used?

    With kind regards
    Joe :cool:
     
  5. Serpent

    Serpent Occasional Visitor

    Joined:
    Jul 2, 2012
    Messages:
    20
    Probably your HDD isn't anymore /dev/sda.
    Check this with:
    Code:
    # fdisk -l
    To avoid this kind of problems (device name change), you can use UUID to mount the partitions.
    UUIDs are always unique.

    To find a UUID of your partitions do:
    Code:
    # blkid
    Should look something like this:
    Code:
    # blkid
    /dev/sda2: LABEL="Seagate2TB" UUID="078f4b9d-9266-4188-97cd-54987910bf33"
    /dev/sda1: UUID="6be4cca1-622d-4753-afd3-35f4833ac4cd"
    Find your UUIDs and mount every partition with:
    Code:
    mount -t ext3 -o rw,nodev,noatime,data=ordered UUID="Your_UUID_Here" /tmp/mnt/Data
    Also for swap is UUID useful:
    Code:
    swapon UUID="Your_UUID_Here" 
     
  6. sabot105mm

    sabot105mm Regular Contributor

    Joined:
    Feb 1, 2013
    Messages:
    52
    Ok so I used the uuid to mount when I do I get this


    mount -t nfsd -o rw,nodev,noatime,nls=utf8,fmask=0,dmask=0,force
    UUID="AAEE24F8EE24BF07" tmp/mnt/HD


    Code:
    mounting /dev/sda1 on /tmp/mnt/HD failed: No such file or directory
     
    Last edited: Mar 1, 2013
  7. Serpent

    Serpent Occasional Visitor

    Joined:
    Jul 2, 2012
    Messages:
    20
    Hmmm, you miss a slash in front of mount point!!!
     
  8. sabot105mm

    sabot105mm Regular Contributor

    Joined:
    Feb 1, 2013
    Messages:
    52
    got it
    mount -t nfsd -o rw,nodev,noatime,nls=utf8,fmask=0,dmask=0,force UUID="AAEE24F8EE24BF07" /tmp/mnt/HD

    but when i go to the routers homepage it says the HD has 0 space left? and alos the smb shares arnt found for that HD
     

    Attached Files:

    Last edited: Mar 1, 2013
  9. Serpent

    Serpent Occasional Visitor

    Joined:
    Jul 2, 2012
    Messages:
    20
    I can't see the numbers from the pic.
    Post the output from df -kh and mount commands.
     
  10. sabot105mm

    sabot105mm Regular Contributor

    Joined:
    Feb 1, 2013
    Messages:
    52
    sorry it took so long. i went into the console and typed what you said after mounting the hd.
    Code:
    Filesystem                Size      Used Available Use% Mounted on
    /dev/root                22.5M     22.5M         0 100% /
    tmpfs                   117.1M      2.6M    114.6M   2% /tmp
    devfs                   117.1M         0    117.1M   0% /dev
    /dev/mtdblock4            7.9M    532.0K      7.4M   7% /jffs
    /dev/sdb1                 1.8G     15.0M      1.7G   1% /tmp/mnt/USB
    Code:
    [2013/03/03 11:29:27] minissdp.c:680: info: SSDP M-SEARCH from 10.9.10.5:49457 ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1, MX: 3, MAN: "ssdp:discover"
    [2013/03/03 11:30:00] minissdp.c:680: info: SSDP M-SEARCH from 10.9.10.5:49457 ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1, MX: 3, MAN: "ssdp:discover"
    [2013/03/03 11:30:33] minissdp.c:680: info: SSDP M-SEARCH from 10.9.10.5:49457 ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1, MX: 3, MAN: "ssdp:discover"
    rootfs on / type rootfs (rw)
    /dev/root on / type squashfs (ro)
    proc on /proc type proc (rw)
    tmpfs on /tmp type tmpfs (rw)
    devfs on /dev type tmpfs (rw,noatime)
    sysfs on /sys type sysfs (rw)
    devpts on /dev/pts type devpts (rw)
    /dev/mtdblock4 on /jffs type jffs2 (rw,noatime)
    usbfs on /proc/bus/usb type usbfs (rw)
    /dev/sdb1 on /tmp/mnt/USB type ext2 (rw,nodev,noatime)
    /dev/sda1 on /tmp/mnt/HD type nfsd (rw,nodev,noatime)
     
  11. Serpent

    Serpent Occasional Visitor

    Joined:
    Jul 2, 2012
    Messages:
    20
    So, your /dev/sda1 is mounted on /tmp/mnt/HD but isn't shown by df command.
    This mean that FileSystem is corrupted.
    Unmount it and run fsck:
    Code:
    # e2fsck -fv /dev/sda1
    Will take some time to finish.
    After that mount it again and check it.
     
  12. joegreat

    joegreat Senior Member

    Joined:
    Jan 9, 2013
    Messages:
    481
    Location:
    Vienna, Austria
    Hmm, do you really recommend to us e2fsck for checking a NTFS formatted drive??? :confused:

    I would suggest to connect the HDD to a Windows PC and to run the check there. :)

    With kind regards
    Joe :cool:
     
  13. Serpent

    Serpent Occasional Visitor

    Joined:
    Jul 2, 2012
    Messages:
    20
    I beg your pardon Sir, this will not happen again!!!
     

Share This Page