[SCRIPT] create-autorun.sh

Introduce yourself to us and other members here, or share your own product reviews, suggestions, and tips and tricks of using QNAP products.
Post Reply
User avatar
OneCD
Ask me anything
Posts: 6021
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

[SCRIPT] create-autorun.sh

Post by OneCD » Fri Jan 06, 2017 6:32 pm

Description

This is a run-once BASH script to create an autorun environment on your NAS. This can be used to automatically execute your own scripts when the NAS boots-up.


What it does

This installer script writes an autorun.sh processor into your default volume, below the .system directory. It then symlinks this into the DOM so that it's triggered on NAS startup. It also creates a scripts directory for your own custom creations and NAS modifications. Everything in the scripts directory is run (in order) during NAS startup.


How to use it
  1. SSH into your NAS as the 'admin' user,
  2. Run the installer script:

    Code: Select all

    curl -skL https://git.io/create-autorun.sh | bash
  3. Place your executable scripts (or symlinks) into the 'scripts' directory.
Notes
  • For those with QTS 4.3.x, you'll also need to let QTS know that it should permit the autorun.sh script to execute. Navigate to Control Panel -> System -> Hardware, then enable the option "Run user defined processes during startup".
  • The location of the autorun system will depend on your default volume name. For example: my default volume is 'MD0_DATA', so the automatic processor is created at:
    /share/MD0_DATA/.system/autorun/autorun.sh

    ... and the scripts directory is created at:
    /share/MD0_DATA/.system/autorun/scripts/
  • autorun.sh is triggered at some point during NAS bootup, which then runs each executable file in the scripts directory in the default filename list order. If you need to run one script before the other, prefix them with a number such as:

    Code: Select all

    010-example.sh
    020-example.sh
    025-example.sh
    
  • A log file is created during autorun.sh execution. It is located at /var/log/autorun.log and contains the date-time and name of each of the scripts found in the scripts directory as they were run, as well as any captured stdout.
  • The source for this project can be found on GitHub
When is autorun.sh run?

As there has been no official word on this from QNAP, we're reduced to observations-only. Here are the results from my latest startup-sequence tests with QTS 4.2.6 #20171208 on my TS-559 Pro+.

I modified each installed QPKG to record the NAS uptime and the name of the package init script in the system log as each package was started. I also created a single autorun script called autotest.sh that logs the same information:
System started.
[232.60] /share/MD0_DATA/.qpkg/CloudDrive/CloudDrive.sh
[278.08] /share/MD0_DATA/.system/autorun/scripts/autotest.sh
[293.99] /share/MD0_DATA/.qpkg/Entware-3x/Entware-3x.sh
[296.08] /share/MD0_DATA/.qpkg/Par2cmdline-MT/Par2cmdline-MT.sh
[298.20] /share/MD0_DATA/.qpkg/QcloudSSLCertificate/QcloudSSLCertificate.sh
[300.30] /share/MD0_DATA/.qpkg/QDK/qdk
[302.39] /share/MD0_DATA/.qpkg/CloudDrive/CloudDrive.sh
[304.51] /share/MD0_DATA/.qpkg/SABnzbdplus/sabnzbd.sh
[317.06] /share/MD0_DATA/.qpkg/SortMyQPKGs/sort-my-qpkgs.sh
As you can see, QTS executed autorun.sh and then began launching the QPKGS as per /etc/config/qpkg.conf. I ran the same startup several times, and saw no more that a few seconds difference between each one.
  • The odd-man-out here is the first load of CloudDrive.sh. It seems that QTS preloads this package early-on, then loads it again later. I'm sure QNAP have a good reason for that. :S
I then introduced a 60 second delay into autotest.sh that forced it to remain active for that time. This had the effect that every QPKG after autorun.sh was loaded approximately 60 seconds later than usual:
System started.
[231.55] /share/MD0_DATA/.qpkg/CloudDrive/CloudDrive.sh
[283.42] /share/MD0_DATA/.system/autorun/scripts/autotest.sh
[283.61] /share/MD0_DATA/.system/autorun/scripts/autotest.sh - begin 60 second sleep
[343.73] /share/MD0_DATA/.system/autorun/scripts/autotest.sh - end 60 second sleep
[358.10] /share/MD0_DATA/.qpkg/Entware-3x/Entware-3x.sh
[360.20] /share/MD0_DATA/.qpkg/Par2cmdline-MT/Par2cmdline-MT.sh
[362.30] /share/MD0_DATA/.qpkg/QcloudSSLCertificate/QcloudSSLCertificate.sh
[364.41] /share/MD0_DATA/.qpkg/QDK/qdk
[366.50] /share/MD0_DATA/.qpkg/CloudDrive/CloudDrive.sh
[368.61] /share/MD0_DATA/.qpkg/SABnzbdplus/sabnzbd.sh
[378.95] /share/MD0_DATA/.qpkg/SortMyQPKGs/sort-my-qpkgs.sh
So, it seems autorun.sh can affect the loading of QPKGs.

edit log milestones
20180916: moved to GitHub repo.
20171228: included startup-sequence test results.
20171217: major rewrite and support for exceptions (ubifs->ext4) - thank you @z3d0_ and @schumaku
20171117: included note to enable autorun during startup in Control Panel - thank you @schumaku
20170212: added support for system device [sdx] - thank you @HB and @schumaku
20170206: mount point is now under [/tmp] - thank you @mundeli and @schumaku
20170204: added support for [UBIFS] - thank you @ugduibill and @schumaku
Last edited by OneCD on Mon May 06, 2019 9:05 am, edited 29 times in total.

production NAS: TS-569 Pro with Debian 9.9 'Stretch' (power on/off times are < 1 minute)
backup NAS: TS-559 Pro+ with QTS 4.2.6 #20190322

one.cd.only@gmail.com

Image Image Image Image

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 2:30 am

hello, on my brand new TS-431P with latest firmware, I get this error

---| Details |----------------------------------------------
* NAS model : TS-X41 (TS-431P)
* QTS version : 4.2.3 #20170121
* default volume : /share/CACHEDEV1_DATA

---| Log |--------------------------------------------------
X Unable to confirm that DOM device is present!

---| Result |-----------------------------------------------
X Autorun installation failed!

I tried for the last two hours to configure autorun.sh manually with no luck, so I tried your script with no luck either :(
any ideas?

thanks!

User avatar
OneCD
Ask me anything
Posts: 6021
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD » Wed Feb 01, 2017 2:43 am

Hi and welcome to the forum. :)

Thanks for posting the script output.

Can you please run this command and post the result back here? This will help track down why the script couldn't find your system device (DOM).

Code: Select all

echo -e "$(head -n40 < /etc/config/uLinux.conf)\n\n$(lsblk)"

production NAS: TS-569 Pro with Debian 9.9 'Stretch' (power on/off times are < 1 minute)
backup NAS: TS-559 Pro+ with QTS 4.2.6 #20190322

one.cd.only@gmail.com

Image Image Image Image

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 3:07 am

thanke for the quick reply!
here you go

-sh: lsblk: command not found
[System]
Model = TS-X41
Internal Model = TS-X41
Server Comment = NAS Server
Version = 4.2.3
Build Number = 20170121
Number = 0386
Time Zone = Europe/Amsterdam
Enable Daylight Saving Time = TRUE
Workgroup=NAS
Code Page = 437
System Device = /dev/mtdblock
Test Mode = FALSE
Auto Create Raid = raid0
Wan Access = TRUE
Lan Access = TRUE
Web Access Port = 8080
E2KEY Support = FALSE
Rsync Support = TRUE
Rsync Model = QNAP
FS Type = ext3
Auto PowerOn = FALSE
Quota Type = vfsv0
Write Connection Log = 0
Enable Live Update = TRUE
Qsirch = 0
Build Date = 2017-01-21
LoginTheme410 = 10
LoginBgNum = 1
Server Name = MEDIASTORAGE
UPNP_UUID = d6529fcb-4e5f-4bf9-ae1c-3fe5073f3525
Date Format Index = 1
Update Remind Time = 0
Password constraints = 0
Latest Check Live Update = 2017/01/31 19:22:22
Booting = 0
psType = 0
show_link = 0
showVersion = 1
[Test]

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 3:14 am

this also can be useful I think....

[~] # mount -t ext2 /dev/mtdblock5 /tmp/config
mount: wrong fs type, bad option, bad superblock on /dev/mtdblock5,
missing codepage or other error
In some cases useful info is found in syslog - try
dmesg | tail or so

same error for mtdblock6 (and 1,2,3,4 :( )

User avatar
OneCD
Ask me anything
Posts: 6021
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD » Wed Feb 01, 2017 3:43 am

ugduibill wrote:-sh: lsblk: command not found

Hmmm... something wrong there.... :S

ugduibill wrote:System Device = /dev/mtdblock

OK, that's good. The system device is listed.

To check that this can be read, please run the following

Code: Select all

getcfg -f /etc/config/uLinux.conf "System" "System Device"; echo $?

production NAS: TS-569 Pro with Debian 9.9 'Stretch' (power on/off times are < 1 minute)
backup NAS: TS-559 Pro+ with QTS 4.2.6 #20190322

one.cd.only@gmail.com

Image Image Image Image

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 3:45 am

here you go

[~] # getcfg -f /etc/config/uLinux.conf "System" "System Device"; echo $?
/dev/mtdblock

User avatar
OneCD
Ask me anything
Posts: 6021
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD » Wed Feb 01, 2017 3:47 am

Was there a number shown after that output?
e.g.

Code: Select all

# getcfg -f /etc/config/uLinux.conf "System" "System Device"; echo $?
/dev/sdf
0

production NAS: TS-569 Pro with Debian 9.9 'Stretch' (power on/off times are < 1 minute)
backup NAS: TS-559 Pro+ with QTS 4.2.6 #20190322

one.cd.only@gmail.com

Image Image Image Image

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 3:50 am

sorry, yes

[/] # getcfg -f /etc/config/uLinux.conf "System" "System Device"; echo $?
/dev/mtdblock
0

but.. take a look at this

viewtopic.php?f=160&t=117665&p=523342&hilit=431+autorun.sh#p523342

User avatar
OneCD
Ask me anything
Posts: 6021
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD » Wed Feb 01, 2017 3:53 am

ugduibill wrote:sorry, yes

No problem. :)


Good find. I hadn't seen that post. Thank you. :geek:

Can you please run this one too?

Code: Select all

parted -s /dev/mtdblock print

production NAS: TS-569 Pro with Debian 9.9 'Stretch' (power on/off times are < 1 minute)
backup NAS: TS-559 Pro+ with QTS 4.2.6 #20190322

one.cd.only@gmail.com

Image Image Image Image

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 3:57 am

[/] # parted -s /dev/mtdblock print
Error: Could not stat device /dev/mtdblock - No such file or directory.

User avatar
OneCD
Ask me anything
Posts: 6021
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD » Wed Feb 01, 2017 4:02 am

ugduibill wrote:[/] # parted -s /dev/mtdblock print
Error: Could not stat device /dev/mtdblock - No such file or directory.

That's quite interesting... :DD :S

I'll be unavailable to troubleshoot this issue for the next 12 hours or so, as I won't be near my NAS. I'll be back later though, and will probably have more questions. Thanks for working with me to get this fixed. :geek:

production NAS: TS-569 Pro with Debian 9.9 'Stretch' (power on/off times are < 1 minute)
backup NAS: TS-559 Pro+ with QTS 4.2.6 #20190322

one.cd.only@gmail.com

Image Image Image Image

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 4:04 am

thank you! :)

User avatar
OneCD
Ask me anything
Posts: 6021
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD » Wed Feb 01, 2017 2:45 pm

Found this script here as part of a discussion on the differences of your model and another QNAP NAS (at least, I think that's what it's about - it's in German). For your model, it seems this is how to mount & unmount the DOM partition:

Code: Select all

#!/bin/sh
MOUNTPOINT="/tmp/config"
BOOT_CONF=`/bin/cat /etc/default_config/BOOT.conf 2>/dev/null`
CONFIG_DEV_NODE=`/sbin/getcfg "CONFIG STORAGE" DEVICE_NODE -f /etc/platform.conf`
CONFIG_DEV_PART=`/sbin/getcfg "CONFIG STORAGE" FS_ACTIVE_PARTITION -f /etc/platform.conf`
CONFIG_DEV_FS=`/sbin/getcfg "CONFIG STORAGE" FS_TYPE -f /etc/platform.conf`

# check if the HAL subsystem exist
if [ -x /sbin/hal_app ]; then
    BOOT_DEV=$(/sbin/hal_app --get_boot_pd port_id=0)
elif [ "x${BOOT_CONF}" = "xTS-NASARM" ]; then
   BOOT_DEV="/dev/mtdblock"
else
    BOOT_DEV="/dev/sdx"
fi

if [ "x$CONFIG_DEV_NODE" != "x" ]; then
    DEV_NAS_CONFIG=${CONFIG_DEV_NODE}${CONFIG_DEV_PART}
elif [ "x${BOOT_CONF}" = "xTS-NASARM" ]; then
    DEV_NAS_CONFIG=${BOOT_DEV}5
else
    DEV_NAS_CONFIG=${BOOT_DEV}6
fi
EXPR="/usr/bin/expr"

mount_config_dev() {
[ -d $MOUNTPOINT ] || /bin/mkdir $MOUNTPOINT
if [ "x$CONFIG_DEV_NODE" != "x" ]; then
   if [ "x$CONFIG_DEV_FS" = "xubifs" ]; then
      /sbin/ubiattach -m $CONFIG_DEV_PART -d 2
      /bin/mount -t ubifs ubi2:config $MOUNTPOINT > /dev/null 2>&1
   else
      return 0
   fi
else
   /bin/mount $DEV_NAS_CONFIG -t ext2 $MOUNTPOINT > /dev/null 2>&1
fi
[ $? = 0 ] || /bin/echo "$0: mount $DEV_NAS_CONFIG failed."
}

umount_config_dev() {
CNT=0
while [ $CNT -lt 5 ]; do
   if [ "x$CONFIG_DEV_NODE" != "x" ]; then
      if [ "x$CONFIG_DEV_FS" = "xubifs" ]; then
         /bin/umount /tmp/config
         /sbin/ubidetach -m $CONFIG_DEV_PART
      else
         break;
      fi
   else
      /bin/umount $DEV_NAS_CONFIG 2>/dev/null
   fi
   if [ $? = 0 ]; then
      break;
   fi
   sleep 1
   CNT=`expr $CNT + 1`
done
[ $? = 0 ] || /bin/echo "$0: umount $DEV_NAS_CONFIG failed."
}

case "$1" in
  mount_cfg)
      mount_config_dev
      exit 0
   ;;
  umount_cfg)
      umount_config_dev
      exit 0
   ;;
  *)
  echo Usage: "$0 <mount_cfg|umount_cfg>"
esac

If you'd be willing to test it, you'd need to save the whole code-block into a file. Maybe call it test-mount.sh

Then mark it executable:

Code: Select all

chmod +x test-mount.sh

... and run it:

Code: Select all

./test-mount.sh mount_cfg

Your DOM partition should then be available at /tmp/config

If you've made it this far, can you run this to obtain a file list and a partition size?

Code: Select all

echo -e "$(cat /proc/partitions)\n\n$(ll /tmp/config)"

Then, don't forget to unmount afterwards:

Code: Select all

./test-mount.sh umount_cfg

production NAS: TS-569 Pro with Debian 9.9 'Stretch' (power on/off times are < 1 minute)
backup NAS: TS-559 Pro+ with QTS 4.2.6 #20190322

one.cd.only@gmail.com

Image Image Image Image

ugduibill
Starting out
Posts: 10
Joined: Wed Feb 01, 2017 1:28 am

Re: [SCRIPT] autorun.sh installer

Post by ugduibill » Wed Feb 01, 2017 5:43 pm

thanks a lot for the help, I managed to run che command but as you can see I have no autorun.sh

[/tmp/config] # ls
smb.conf smb.conf.cksum system.map.key uLinux.conf

Post Reply

Return to “Users' Corner”