[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
Guru
Posts: 12010
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

[SCRIPT] create-autorun.sh

Post by OneCD »

Image


Description

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

The aim of this project is to support all QNAP NAS models and all QTS & QuTS hero versions. Please advise if you encounter any errors when running it on your NAS.


What it does

This installer script writes an autorun.sh processor into your default volume, below the .system directory. It then symlinks this from the DOM back to your default data volume so that it is run on NAS startup. This means you don't need to load the DOM partition every time you want to change the contents of autorun.sh

The autorun device and partition are automatically determined by this script.


How to create your autorun.sh

Code: Select all

curl -skL https://git.io/create-autorun | sudo bash

Notes
  • If you didn't have an autorun.sh file before, then the autorun.sh file created by this utility will contain a script directory processor, and will make a scripts directory available for your own shell-scripts. Everything in this scripts directory is run (in-order) during NAS startup by the default autorun.sh file created only. The notes below are only applicable to the autorun.sh written by this utility. If you already had another autorun.sh file, then it will remain and be used instead, and the following notes won't apply.
  • The location of the autorun system will depend on your default volume name. For example: if your default volume is CACHEDEV1_DATA, then the automatic script processor will be created at:

    Code: Select all

    /share/CACHEDEV1_DATA/.system/autorun/autorun.sh
    ... and the scripts directory will be created at:

    Code: Select all

    /share/CACHEDEV1_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

    10-example.sh
    20-example.sh
    25-example.sh
    30-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 executable scripts found in the scripts directory as they were run, along with any stdout and stderr generated by these scripts.
  • The source for this project can be found on GitHub
Last edited by OneCD on Mon May 06, 2019 9:05 am, edited 29 times in total.

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

Re: [SCRIPT] autorun.sh installer

Post by ugduibill »

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
Guru
Posts: 12010
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD »

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)"

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

Re: [SCRIPT] autorun.sh installer

Post by ugduibill »

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 »

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
Guru
Posts: 12010
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD »

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 $?

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

Re: [SCRIPT] autorun.sh installer

Post by ugduibill »

here you go

[~] # getcfg -f /etc/config/uLinux.conf "System" "System Device"; echo $?
/dev/mtdblock
User avatar
OneCD
Guru
Posts: 12010
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD »

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

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

Re: [SCRIPT] autorun.sh installer

Post by ugduibill »

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&h ... sh#p523342
User avatar
OneCD
Guru
Posts: 12010
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD »

ugduibill wrote:sorry, yes
No problem. :)
ugduibill wrote:but.. take a look at this

viewtopic.php?f=160&t=117665&p=523342&h ... sh#p523342
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

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

Re: [SCRIPT] autorun.sh installer

Post by ugduibill »

[/] # parted -s /dev/mtdblock print
Error: Could not stat device /dev/mtdblock - No such file or directory.
User avatar
OneCD
Guru
Posts: 12010
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD »

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:

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

Re: [SCRIPT] autorun.sh installer

Post by ugduibill »

thank you! :)
User avatar
OneCD
Guru
Posts: 12010
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [SCRIPT] autorun.sh installer

Post by OneCD »

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

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

Re: [SCRIPT] autorun.sh installer

Post by ugduibill »

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”