Last active
July 21, 2020 16:59
-
-
Save 5p0ng3b0b/3f0025e731d6e7f45ce04bb34ca6d244 to your computer and use it in GitHub Desktop.
ix4-300d boot script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# IX4-300D startup and optware-ng init script | |
# Set path for additional files use USB or HDD | |
# If USB then specify UUID of USB drive | |
files=HDD | |
UUID="1827a6c8-c9a7-442c-adda-34806977c107" | |
# Wait until storage pool is mounted | |
while [ ! "$(ls -a /mnt/pools/A/A0/ 2>/dev/null)" ]; do sleep 2; done | |
echo "$files" >/mnt/pools/A/A0/.optsrc | |
# Wait for external usb if selected as data drive | |
[ "$files" = "USB" ] && while [ ! "$(blkid | grep "$UUID")" ]; do sleep 2; done | |
# Assign variables and define functions | |
[ "$files" = "USB" ] && OPT=$(mount | grep "$(blkid | grep "$UUID" | awk -F ':' '{print $1}') on /mnt" | awk '{print $3}' | grep -v '/opt') | |
[ "$files" = "HDD" ] && { OPT="/mnt/pools/A/A0/opt"; mkdir -p "$OPT"; } | |
#HOME="$OPT/home/$USER" | |
HOME="/opt/home/$USER" | |
PROFILE="/opt/etc/profile" | |
v_gt() { [ "$(echo -e "$1\n$2" | sort -V | head -n 1)" != "$1" ] && echo "1"; } | |
log() { echo "$(date) : $1" >>"/mnt/pools/A/A0/init-opt.log"; } | |
log "Starting init-opt.sh with $files as optware source at $OPT." | |
# Setup folders and mounts | |
for DIR in "apps" "bin" "sbin" "etc" "var" "lib" "usr" "var" "include" "tmp" "home" "share" "opt" "ipkg" \ | |
"arm-buildroot-linux-uclibcgnueabi" "libexec" "local" "info" "doc" "docs" "man"; do | |
[ ! -d "$OPT/$DIR" ] && { mkdir -p "$OPT/$DIR"; log "Created $OPT/$DIR."; } | |
[ -L "/opt/$DIR" ] && rm "/opt/$DIR" | |
[ ! -L "/opt/$DIR" ] && { ln -s "$OPT/$DIR" "/opt/$DIR"; log "Linking $OPT/$DIR to /opt/$DIR."; } | |
# Dunno why, but bind mounted folders gave problems when installing some ipkg modules so | |
# symlinked all folders instead. 'ipkg install git' is an example. | |
#[ ! -d "/opt/$DIR" ] && mkdir -p "/opt/$DIR" | |
#[ ! "$(mount | grep " on /opt/$DIR ")" ] && mount --bind "$OPT/$DIR" "/opt/$DIR" | |
if [ ! -d "$OPT/$DIR" ] && [ ! -L "/opt/$DIR" ]; then echo "Something went wrong with $DIR."; fi | |
done | |
for DIR in "usr/bin" "usr/sbin" "var/log" "var/tmp" "var/lock" "share/terminfo"; do | |
mkdir -p "$OPT/$DIR" | |
done | |
# Use storage pool for /var/tmp to prevent running out of space | |
[ ! -d "/mnt/pools/A/A0/tmp" ] && mkdir -p "/mnt/pools/A/A0/tmp" | |
mount --bind "/mnt/pools/A/A0/tmp" "/var/tmp" | |
log "Mountpoints complete." | |
# Use storage pool for /opt/apps to prevent running out of space | |
[ ! -d "$OPT/apps" ] && mkdir -p "$OPT/apps" | |
#mount --bind "$OPT/apps" "/opt/apps" | |
rm -Rf /opt/apps | |
ln -s "$OPT/apps" "/opt/apps" | |
# Setup home folder | |
CURR_HOME="$(cat /etc/passwd | grep $USER | awk -F ':' '{print $6}')" | |
if [ ! "$CURR_HOME" = "$HOME" ]; then | |
awk -v u="$USER" -v h="$HOME" 'BEGIN{FS=OFS=":"}$1==u{$6=h}1' "/etc/passwd" > "/etc/passwd.tmp" | |
cat "/etc/passwd.tmp" > "/etc/passwd"; rm "/etc/passwd.tmp" | |
fi | |
mkdir -p "$HOME" | |
[ ! -f "$HOME/.profile" ] && echo -e '#!/bin/sh\n. /opt/etc/profile' >"$HOME/.profile" | |
[ ! "$(cat $HOME/.profile | grep '. /opt/etc/profile')" ] && echo '. /opt/etc/profile' >>"$HOME/.profile" | |
awk '!a[$0]++' "$HOME/.profile" >"$HOME/.profile.tmp"; mv -f "$HOME/.profile.tmp" "$HOME/.profile" | |
chmod +x "$HOME/.profile" | |
log "Home folder complete." | |
# Install optware-ng if missing | |
if [ ! -f "/opt/bin/ipkg" ]; then | |
# rm -r arm-buildroot-linux-uclibcgnueabi bin etc doc docs include info ipkg lib libexec local man opt sbin share ssl tmp usr var | |
log "Installing optware-ng." | |
feed="http://ipkg.nslu2-linux.org/optware-ng/buildroot-armeabi-ng" | |
ipk_name="$(wget -qO- $feed/Packages | awk '/^Filename: ipkg-static/ {print $2}')" | |
wget -qO "/tmp/$ipk_name" "$feed/$ipk_name" | |
tar -C /tmp -xzf "/tmp/$ipk_name" "./data.tar.gz"; tar -C /tmp -xzf "/tmp/data.tar.gz"; cp -r /tmp/opt/* $OPT/ | |
rm -f "/tmp/$ipk_name" "/tmp/data.tar.gz"; rm -rf "/tmp/opt" | |
echo "src/gz optware-ng $feed" > "/opt/etc/ipkg.conf"; echo "dest /opt/ /" >> "/opt/etc/ipkg.conf" | |
mv "/opt/bin/ipkg" "/opt/bin/ipkgbin" | |
echo -e '#!/bin/sh\n/opt/bin/ipkgbin -t /opt/tmp -force-space $@' >"/opt/bin/ipkg" | |
chmod +x "/opt/bin/ipkg" | |
/opt/bin/ipkg update >/dev/null | |
log "Optware-ng is installed." | |
fi | |
# The default /opt/etc/profile script is base64 encoded below. | |
# If you mess up /opt/etc/profile just delete it and reboot to restore it. | |
# Ideally all your modifications to paths etc should go in ~/.profile. | |
# Some packages install the optware version of bash which overwrites | |
# /opt/etc/profile. | |
#begin_profile | |
profile=\ | |
IyEvYmluL3NoCiMgb3B0d2FyZS1uZyBsb2dpbiBzY3JpcHQKCiMgU2V0dXAgZW52aXJvbWVudCB2\ | |
YXJpYWJsZXMKVVVJRD0iMTgyN2E2YzgtYzlhNy00NDJjLWFkZGEtMzQ4MDY5NzdjMTA3IgojIGV4\ | |
cG9ydCBQQVRIPSIvb3B0L2Jpbjovb3B0L3NiaW46L29wdC9sb2NhbC9iaW46JChlY2hvICRQQVRI\ | |
IHwgc2VkIC1lICdzIy9vcHQvYmluIyNnJyAtZSAncyMvb3B0L3NiaW4jI2cnI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50bXAiCiAgICAgICAgICAgICAgICBhd2sgLXYgaT0iJChj\ | |
YXQgJHRwKSIgJyQxPT0iI2JlZ2luX3Byb2ZpbGUie3A9MX0gcCAmJiAkMT09IiNlbmRfcHJvZmls\ | |
ZSJ7cHJpbnQgaX0gMScgIiRpZmlsZS50bXAiID4iJGlmaWxlIgogICAgICAgICAgICAgICAgcm0g\ | |
IiRpZmlsZS50bXAiOyBybSAiJHRwIjsgY2QgIiRvcHdkIjsgfQpiYW5uZXIxKCkgICAgICAgeyAj\ | |
IFByaW50IG9wdHdhcmUtbmcgYmFubmVyLgogICAgICAgICAgICAgICAgbG9jYWwgbWRsPSQocHJp\ | |
bnRmICclLTEycycgIiQoY2F0IC9tbnQvYXBwcy91c3IvbG9jYWwvY2ZnL0Zpcm13YXJlLnhtbCB8\ | |
IGdyZXAgJ01vZGVsPScgfCBhd2sgLUYgJyInICd7cHJpbnQgJDR9JykiKQogICAgICAgICAgICAg\ | |
ICAgbG9jYWwgdz0nXGVbMzdtJzsgbG9jYWwgYj0nXGVbMzRtJwogICAgICAgICAgICAgICAgbG9j\ | |
YWwgdmVyPXYkKHByaW50ZiAnJS0xM3MnICIkKGNhdCAvZXRjL3NvaG9GbGFzaC54bWwgfCBncmVw\ | |
ICdGaXJtd2FyZVJldicgfCBhd2sgLUYgJyInICd7cHJpbnQgJDZ9JykiKQogICAgICAgICAgICAg\ | |
ICAgZWNobyAtZSAiIF9fX19fICAgICAgIF9fIgogICAgICAgICAgICAgICAgZWNobyAtZSAifCAg\ | |
ICAgfC0tLS0tfCAgfF8uLS0uLS0uLS0uLS0tLS0uLS0tLS4tLS0tLS5fXy4tLS0tLS4tLS0tLS4i\ | |
CiAgICAgICAgICAgICAgICBlY2hvIC1lICJ8ICAtICB8ICBfICB8ICAgX3wgIHwgIHwgIHwtLV8g\ | |
IHwgICBffCAgLV9ffF9ffCAgICAgfCAgXyAgfCIKICAgICAgICAgICAgICAgIGVjaG8gLWUgInxf\ | |
X19fX3wgICBfX3xfX19ffF9fX19fX19ffF9fX19ffF9ffCB8X19fX198ICB8X198X198X18gICB8\ | |
IgogICAgICAgICAgICAgICAgZWNobyAtZSAiICAgICAgfF9ffCAgICAgICAgJGIkbWRsICAgICAg\ | |
ICAkdmVyJHd8X19fX198IjsgfQoKIyBSZW1vdmUgZHVwbGljYXRlIGNhbGxzIHRvIHRoaXMgZmls\ | |
ZSBmcm9tIH4vLnByb2ZpbGUKYXdrICchYVskMF0rKycgfi8ucHJvZmlsZSA+fi8ucHJvZmlsZS50\ | |
bXA7IG12IC1mIH4vLnByb2ZpbGUudG1wIH4vLnByb2ZpbGUKY2htb2QgK3ggfi8ucHJvZmlsZQoK\ | |
IyBTZXR1cCBhbGFpc2VzCmFsaWFzIG9jb25maWd1cmU9Ii4vY29uZmlndXJlIC0tcHJlZml4PS9v\ | |
cHQgLS1zeXNjb25mZGlyPS9vcHQvZXRjIgphbGlhcyBvbWFrZT0ibWFrZSAtZSIKCiMgUHJpbnQg\ | |
bG9naW4gYmFubmVyCmNsZWFyCmJhbm5lcjEKCiMgTG9nIHNlc3Npb24KbG9nICJSZW1vdGUgbG9n\ | |
aW4gZnJvbSAkU1NIX0NMSUVOVCIKCiMgU2V0dXAgcGF0aHMKYWRkcGF0aCAiL29wdC9iaW4iCmFk\ | |
ZHBhdGggIi9vcHQvc2JpbiIKYWRkcGF0aCAiL29wdC9sb2NhbC9iaW4iCmFkZHBhdGggIi9vcHQv\ | |
dXNyL2JpbiIKYWRkcGF0aCAiL29wdC91c3IvZ28vYmluIgoKIyBGaW5pc2gKY2QgIiRIT01FIgo=\ | |
#end_profile | |
[ ! -f "$PROFILE" ] && { echo "$profile" | base64 -d >"$PROFILE"; chmod +x "$PROFILE"; } | |
# Now we source ~/.profile to setup paths | |
. "$HOME/.profile" | |
# Configure nano settings | |
if [ ! -f "$HOME/.nanorc" ]; then | |
nanov="$(nano -V | head -1 | awk '{print $4}')" # Get nano version number | |
if [ "$(v_gt $nanov 2.7.0)" = "1" ]; then echo 'set linenumbers' >>"$HOME/.nanorc"; fi # Add line numbers | |
if [ "$(v_gt $nanov 2.2.6)" = "1" ]; then echo 'set constantshow' >>"$HOME/.nanorc"; else echo 'set const' >>"$HOME/.nanorc"; fi | |
awk '!a[$0]++' "$HOME/.nanorc" >"$HOME/.nanorc.tmp"; mv -f "$HOME/.nanorc.tmp" "$HOME/.nanorc" | |
fi | |
# Set endless loop. If this script ends or exits without a reboot | |
# and it has been started from /usr/local/cfg/sohoProcs.xml, you | |
# could trash your system and lose all data. Nice one Lenovo! | |
# Visit https://mega.nz/#F!b9N2XQzZ!hdhSgCK0VQlPbYpyOXTmYA to | |
# download a usb flashable firmware should you bork your NAS. You | |
# cannot get one or any support from the Lenovo dinlos. | |
echo "Type ctrl-c to continue." | |
log "init-opt.sh completed, script sleeping" | |
while [ "1" ]; do sleep 360d; done | |
reboot | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment