How-To Autostart własnych aplikacji wraz z uruchomieniem systemu

Dyskusja w 'Oh'Linux? Software hacking' rozpoczęta przez użytkownika Silas Mariusz, 27 Sierpień 2013.

Ładowanie...
  1. Silas Mariusz
    Offline

    Silas Mariusz SysOp Administrator

    Dołączył:
    5 Kwiecień 2008
    Wiadomości:
    6 032
    Miejscowość:
    Nowy Sącz
    Local Time:
    22:27
    Oceny:
    +1 339 / 30 / -5
    Followers:
    22
    QNAP:
    TVS-x71
    Ethernet:
    1 GbE
    TVS-x71 1 GbE
    Some advanced uses of your NAS may require you to run custom applications at boot-time. Even though QNAP NAS are Linux-based, you cannot use the usual Linux methods for launching an application at startup: default config files are reset on every startup.

    Before firmware 3.8.2, QNAP OS automatically launched a special script (if present) but since this update this trick doesn't work anymore.
    Here, you can find the old and new method.

    Wymagania
    • must be able to remote login via SSH Login or Telnet Login (e.g. use SSH PuTTY)
    • must know how to edit files using nano, vi, or edit via SFTP (e.g. use WinSCP)
    Metoda bazująca na QPKG
    This method consists of declaring a dummy QPKG which launches your script at startup.
    • Log into your QNAP device using SSH or Telnet, for instance by using PuTTY
    • Edit QPKG config file:
      Kod (Bash):
      1. vi /etc/config/qpkg.conf
    • Declare a new dummy package by adding something like that in this file:
      Kod (Text):
      1. [autorun]
      2. Name = autorun
      3. Version = 0.1
      4. Author = neomilium
      5. Date = 2013-05-06
      6. Shell = /share/MD0_DATA/.qpkg/autorun/autorun.sh
      7. Install_Path = /share/MD0_DATA/.qpkg/autorun
      8. Enable = TRUE
      As you can see, shell is the interesting variable: at boot-time, QNAP OS will launch each QPKG's shell variable content.Note: if your NAS doesn't have /share/MD0_DATA (i.e. is a one-drive NAS), put the right directory into the shell and Install_Path variables and adapt the following commands to your needs.
    • Create the dummy package directory:
      Kod (Bash):
      1. mkdir /share/MD0_DATA/.qpkg/autorun
    • Create the autorun script with the contents of your choice:
      Kod (Bash):
      1. vi /share/MD0_DATA/.qpkg/autorun/autorun.sh
      Note: don't forget "#!/bin/sh" at the beginning of script.
    • Set the execute bit:
    • Kod (Bash):
      1. chmod +x /share/MD0_DATA/.qpkg/autorun/autorun.sh
    • Reboot and enjoy!
    (stara) Metoda przez utworzenie pliki autorun.sh w pamięci flash (MTD)
    WARNING: this method is DEPRECATED for two reasons:
    • This method implies editing a file on MTD (a flash device) which has a low write cycle limit;
    • Since firmware version 3.8.2, this method does not work anymore.
    autorun.sh is a script which will be executed on every startup of the TS-x09, TS-x19 and TS-x39. Editing this file allows you to start your own programs or overwrite config files with your own copies.

    Ręczna edycja pliku autorun.sh
    1. Log into your QNAP device using SSH or Telnet, for instance by using PuTTY
    2. Optional: install nano; use ipkg install nano & edit with nano instead of vi
      • TS-201: Mount the config ramblock /dev/mtdblock4:
        Kod (Bash):
        1. mount -t ext2 /dev/mtdblock4 /tmp/config
      • TS-109, TS-109P, TS-110, TS-119, TS-209, TS-209P, TS-219, TS-409 (Marvell ARM), TS-412, TS-419P: Mount the config ramblock /dev/mtdblock5:
        Kod (Bash):
        1. mount -t ext2 /dev/mtdblock5 /tmp/config
      • (TS-219P II: since the new firmware update you maybe have to use ext4 instead of ext2):
        Kod (Bash):
        1. mount -t ext2 /dev/mtdblock5 /tmp/config
      • TS-439, TS-509, TS-639, TS-809, TS-809U (x86): Mount the config ramblock /dev/sdx6:
        Kod (Bash):
        1. mount -t ext2 /dev/sdx6 /tmp/config
    3. Create/Edit /tmp/config/autorun.sh either using vi:
      Kod (Bash):
      1. vi /tmp/config/autorun.sh
      • Get vi editor into edit mode: press a
      • Edit whatever you need to
      • Exit edit mode: press ESC
      • Save and exit: press ZZ
    4. ..or editing it using a desktop PC and e.g. SFTP
    Ensure that /tmp/config/autorun.sh is executable:
    Kod (Bash):
    1. chmod +x /tmp/config/autorun.sh
    IMPORTANT: Unmount the mounted flash partition:
    Kod (Bash):
    1. umount /tmp/config
    editautorun.sh - skrypt otwierający edycję autorun.sh
    If you edit this file regularly you can save some time by creating a shell script (e.g. editautorun.sh) to automate the process. You can call the script by either putting it in the environment path, or add its folder to the path or call it by an alias.

    The script contents are:
    For TS-201 use ...
    Kod (Bash):
    1. mount -t ext2 /dev/mtdblock4 /tmp/configvi /tmp/config/autorun.sh
    2. chmod +x /tmp/config/autorun.sh
    3. echo .
    4. echo "unmounting /tmp/config..."
    5. umount /tmp/config
    For TS-109, TS-109P, TS-119, TS-209, TS-209P, TS-219, TS-412, TS-409 (Marvell ARM) use ...
    Kod (Bash):
    1. mount -t ext2 /dev/mtdblock5 /tmp/config
    2. vi /tmp/config/autorun.sh
    3. chmod +x /tmp/config/autorun.sh
    4. echo .
    5. echo "unmounting /tmp/config..."
    6. umount /tmp/config
    TS-439, TS-509, TS-639, TS-809, TS-809U (x86) use ...
    Kod (Bash):
    1. mount -t ext2 /dev/sdx6 /tmp/config
    2. vi /tmp/config/autorun.sh
    3. chmod +x /tmp/config/autorun.sh
    4. echo .
    5. echo "unmounting /tmp/config..."
    6. umount /tmp/config
    Wydelegowanie pliku autorun.sh do zewnętrznego skryptu
    Frequently mounting and editing autorun.sh on the flash could be an annoying task. More important, it may reduce the lifetime of some flash blocks. Flash blocks have limited write/erase cycles, and the mtdblock device driver does little to prevent their wear. Read more on this on the Linux mtd website.

    To avoid this, you could configure autorun.sh to launch another script located in the inner drive: in this way there no need to always mount and modify the file inside the flash. but only edit the script file located on your drive.

    Create the directory /share/HDA_DATA/.qpkg/autorun and file autorun.sh with:
    Kod (Bash):
    1. mkdir /share/HDA_DATA/.qpkg/autorun
    2. cd /share/HDA_DATA/.qpkg/autorun/
    3. touch autorun.sh
    4. chmod +x autorun.sh
    The autorun.sh located on the flash could be something like this (just two lines that won't need many changes!):

    Kod (Bash):
    1. #!/bin/sh
    2. /share/HDA_DATA/.qpkg/autorun/autorun.sh &
    and then edit the file /share/HDA_DATA/.qpkg/autorun/autorun.sh to be used to launch all your startup scripts.


    UWAGI!
    1. Never put any larger files on your flashboot devices and ramdisk; instead, create symbolic links to whatever you want to put there, e.g.:
      ''Create a link from /usr (which is in ramdisk) to /share/MD0_DATA/jre1.6.0_10 (which is on a hard disk) at the mountpoint /usr/java''
      Kod (Bash):
      1. ln -sf /share/MD0_DATA/jre1.6.0_10 /usr/java
    2. Always use the full system path because locations like /opt/bin or /opt/sbin might not have been exported yet when Autorun.sh is executed, e.g:No good.
      Kod (Bash):
      1. svnserve -d --listen-port=4000 -r /share/subversion
      This is better.
      Kod (Bash):
      1. /opt/bin/svnserve -d --listen-port=4000 -r /share/subversion
      If it still fails to start svnserve, you might try adding this line to your autoexec script:
      Kod (Bash):
      1. /bin/ln -sf /opt/bin/ /share/HDA_DATA/opt/bin/
    3. Many startup scripts in /etc/init.d overwrite their corresponding configuration files in /etc. In this case overwriting the config file using autorun.sh is not enough; we also have to overwrite the startup script itself. Moreover, many startup scripts get excecuted before autorun, such that we also have to restart the service. In this case an autorun.sh may look like this:
      Kod (Bash):
      1. #!/bin/sh
      2. cp /share/MD0_DATA/admin/nfs /etc/init.d/nfscp /share/MD0_DATA/admin/exports /etc
      3.  
      4. /etc/init.d/nfs restart
      Very ugly, indeed! However, it seems this is the only way to make it work (unless you want to throw out the QNAP OS and install a 'better' OS on your NAS).
    4. On our QNAP TS-879 Pro we were not able to run
      Kod (Bash):
      1. /opt/bin/rsyncd-acl.sh start
      ...from the autorun.sh as /opt is not the one from Optware but a directory containing one file, i.e. nasconfig_fs.img.tgz.Thus we modified /tmp/config/autorun.sh to
      Kod (Bash):
      1. #!/bin/shlog=/share/MD0_DATA/.qpkg/Optware/var/log/autorundate > $log# removing bogus /opt/bin/rm /opt/nasconfig_fs.img.tgz /opt 2>> $log >> $log/bin/rmdir /opt 2>> $log >> $log# link correct /opt/bin/ln -s /share/MD0_DATA/.qpkg/Optware /opt 2>> $log >> $log# run autorun.sh/opt/etc/autorun.sh 2>> $log >> $log
      created a log file directory
      Kod (Bash):
      1. mkdir -p /opt/var/log
      and created /opt/etc/autorun.sh on the disk
      Kod (Bash):
      1. #!/bin/sh
      2. /opt/bin/rsyncd-acl.sh start
      thus no mounting of the flash partition is necessary anymore.
    Trick & tips
    Oczekiwanie na zaszyfrowane partycje
    If your data partition is encrypted, you might have some script that has to wait until the encrypted partition is available. I added a script called waitforenc.sh in my autorun-directory:
    Kod (Bash):
    1. #! /bin/sh
    2.  
    3. # This script ends after the encrypted filesystem has been mounted.
    4.  
    5. # The following exits successfully (0) if MD0 is mounted
    6. cat /etc/mtab | grep -q MD0
    7. while [[ $? -ne 0 ]] ; do
    8.         sleep 5
    9.         cat /etc/mtab | grep -q MD0
    10. done
    And now I'm able to call scripts *after* the encrypted partition is available, without blocking other scripts:
    Kod (Bash):
    1. (./waitforenc.sh; /etc/init.d/ldap_server.sh restart ) &
    Uruchamianie wszystkich skryptów znajdujących się w określonym katalogu
    Place a file called listoffiles.sh in a directory, create a subdirectory called scripts, add listoffiles.sh to your autorun:
    Kod (Bash):
    1. #! /bin/sh
    2. # listoffiles.sh
    3.  
    4. BASEDIR=$(dirname $0)
    5.  
    6. echo "" > log/userfiles.log
    7.  
    8. for i in scripts/*.sh ; do
    9.         if [[ -x $i ]] ; then
    10.                 echo -n "$i " >> log/userfiles.log
    11.                 echo `date` >> log/userfiles.log
    12.                 $i 2>&1 >> log/userfiles.log
    13.                 cd $BASEDIR
    14.         fi
    15. done
    Optymalizacja interfejsu sieciowego/sieci
    *You can also try different values from SpeedGuide.net

    Kod (Bash):
    1. ifconfig eth0 txqueuelen 50000
    2. ifconfig eth1 txqueuelen 50000
    3. echo 1 > /proc/sys/net/ipv4/tcp_rfc1337
    4. echo 2 > /proc/sys/net/ipv4/tcp_frto
    5. echo 2 > /proc/sys/net/ipv4/tcp_frto_response
    6. echo 1 > /proc/sys/net/ipv4/tcp_mtu_probing
    7. echo 1 > /proc/sys/net/ipv4/tcp_window_scaling
    8. echo 1 > /proc/sys/net/ipv4/tcp_workaround_signed_windows
    9. echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
    10. echo 0 > /proc/sys/net/ipv4/tcp_tw_recycle
    11. echo 1 > /proc/sys/net/ipv4/tcp_low_latency
    12. echo 1 > /proc/sys/net/ipv4/tcp_ecn
     
    Last edited: 7 Listopad 2013
    • Lubię to! Lubię to! x 1
    • Informacyjne Informacyjne x 1
    • Przyjaciel Przyjaciel x 1
    • Piwo Piwo x 1

Poleć tę stronę