#!/bin/sh #set -x # C. Ostheimer 05-2006, 10-2006, 12-2006; usb_install framework by dynamic # template cfg script addon=nbdswap addon_bin=nbd-client [ "$addon_bin" ] || addon_bin=$addon #put used defined post install commands into file cfg.customize #server=http://192.168.222.10 server=http://www.spblinux.de/fbox # cfg.common and cfg.customize are downloaded from $server and # firmware version specific files are downloaded from $server$svr24 (or $svr26) # (kernel 2.4/uClibc-0.9.26 or kernel 2.6/uClibc-0.9.28); # default sub directories are /24 and /26. # svr24=/24 # svr26=/26 # command line option parsing unset verbose quiet show_usage [ $# -eq 0 ] && show_usage=1 while [ x$1 != x ]; do case $1 in -v) verbose=1;; -q) quiet=1;; -h | -\? | --help) show_usage=1;; -*) echo "warning: unknown option $1";; *) break esac; shift; done # Source common functions for installation, (download if missing) f=cfg.common if [ -f ${0%cfg_*}$f ]; then . ${0%cfg_*}$f else wget $server/$f -O /var/$f || errorexit " Couldn't download $f from $server" . /var/$f fi modext=`_fct modext` svrsub=`_fct svrsub` if [ $modext = ko ]; then nbddev=/var/nbd0; else nbddev=/dev/nbd/0; fi export PATH=$PATH:/sbin:/usr/sbin:/var/$addon/bin usage() { echo -e "usage:\n" \ " [INST_DIR=installation_directory] ${0##*/} [-v] install\n" \ " (installs to ramdisk or to INST_DIR/addons if INST_DIR is given)\n" \ " ${0##*/} [-v] usb_install | remove\n" \ " (usb_install: installs to /addons of autodetected usb device or to INST_DIR)\n" \ " ${0##*/} [-v] start nbd_server_ip_address [portnumber] | [-v] stop\n" exit } usage_start() { errorexit "usage: ${0##*/} start nbd_server_ip_address [portnumber]\n" \ "(default port is 8844)" } is_addon_installed() { unset msg if [ -L /var/$addon/bin/$addon_bin -a -x /var/$addon/bin/$addon_bin ]; then msg="(usb_install: `ls -l /var/${addon}_usb |sed 's/^[^\/]*//'`)" elif [ -x "/var/$addon/bin/$addon_bin" ]; then msg="$addon is already installed in /var/$addon" fi if [ x$1 != x-q -a "$msg" ]; then echo "$addon is still installed. Installation aborted. Run ${0##*/} [remove|usb_remove] first" errorexit "$msg" fi #create return value [ "$msg" ] } #echo TEST #echo "modext $modext, svrsub $svrsub." #get_inst_realdir_ $INST_DIR #get_inst_realdir_ #get_missing_file_ chroot #_fct rdsk@new:ramdev && echo "--$ramdev--" #echo END_TEST [ "$show_usage" ] && usage cd /var case $1 in usb_install | install) is_addon_installed unset is_usb [ $1 = usb_install ] && is_usb=1 free if [ "$INST_DIR" ]; then if [ -d "$INST_DIR" ]; then [ $1 = install ] && echo -n "defaulting to " echo "usb_install on $INST_DIR(="`get_inst_realdir_ $INST_DIR`") (given by variable INST_DIR)" echo "(if this is not correct run: unset INST_DIR)" is_usb=1 else echo "warning: INST_DIR(=$INST_DIR) defined, but not a directory" echo " using default install to ramdisk (press ctrl+c to stop)" sleep 10 fi else echo "installing $addon ..." fi [ -d $addon ] || mkdir $addon if [ "$is_usb" ]; then ############ USB-INSTALL #################################### # check for user defined installation directory given in $INST_DIR if [ "$INST_DIR" ]; then _fct instdir@`get_inst_realdir_ $INST_DIR`:addons/$addon:inst_dir:inst_fs_type else _fct instdir@/var/media/ftp:addons/$addon:inst_dir:inst_fs_type fi case $inst_fs_type in msdos | vfat | ntfs) cp_opts="" ;; *) cp_opts="-pd" esac ############ USB-INSTALL, EXTRACT TO USB IF NOT EXTRACTED YET ####### if ! [ -f $inst_dir/addons/$addon/bin/$addon_bin ]; then echo "installing $addon on usb device (${inst_dir##*/})" # get common install scripts / tools / modules from $server$svrsub _fct wgetx@$server$svrsub:$inst_dir/addons/$addon:nbd.$modext _fct mkdr@$inst_dir/addons/$addon:bin _fct wgetx@$server$svrsub:$inst_dir/addons/$addon/bin:nbd-client:mkswap chmod +x $inst_dir/addons/$addon/bin/nbd-client $inst_dir/addons/$addon/bin/mkswap file_path=`_fct path_abs@$0` if [ $file_path != $inst_dir/addons ];then # not called from $inst_dir/addons: install on $inst_dir/addons cp $cp_opts $file_path/cfg.common $inst_dir/addons cp $cp_opts $file_path/${0##*/} $inst_dir/addons fi _fct wgetx@$server:$inst_dir/addons:cfg.customize chmod +x $inst_dir/addons/${0##*/} $inst_dir/addons/cfg.customize echo "$addon has been successfully installed to usb device ${inst_dir##*/}" else ############ USB-INSTALL, NO NEED TO EXTRACT FILES. ALREADY ON USB ##### echo "$addon already on USB-Device ... skipping extract phase." fi ########### CONTINUE USB-INSTALL AFTER REQUIRED FILES CONFIRMED ON USB-PATH ########### file_path=`_fct path_abs@$0` if [ $file_path = $inst_dir/addons ]; then # called from $inst_dir/addons: create copy in /var cp $cp_opts $inst_dir/addons/cfg.common /var cp $cp_opts $inst_dir/addons/${0##*/} /var fi [ -L ${addon}_usb ] || _fct lnsf@${addon}_usb:$inst_dir/addons/$addon # create links in /var/$addon pointing to usb device _fct lndfe@/var/$addon:/var/${addon}_usb:bin _fct mkdr@/var/$addon:bin _fct lndfe@/var/$addon/bin:/var/${addon}_usb/bin # additional symlinks inside /var/$addon d=/var/$addon/bin _fct lnsf2@$d:swapon:swapoff:mkswap cd /var else ########### RAM-INSTALL ############################################# _fct wgetx@$server$svrsub:/var/$addon:nbd.$modext _fct mkdr@/var/$addon:bin _fct wgetx@$server$svrsub:/var/$addon/bin:nbd-client:mkswap chmod +x /var/$addon/bin/nbd-client /var/$addon/bin/mkswap # additional symlinks inside /var/$addon d=/var/$addon/bin _fct lnsf2@$d:swapon:swapoff:mkswap _fct wgetx@$server:/var:cfg.customize chmod +x cfg.customize # Remove symlinks which might be left over from last usb_install for f in /var/${addon}_usb ; do [ -L $f ] && rm $f done fi ########### COMMON-INSTALL (RAM and USB) ############################# lsmod |grep -q "^nbd " || insmod /var/$addon/nbd.$modext lsmod |grep -q "^nbd " || errorexit "error: failed to load network block device module nbd.o" free echo "to use $addon type ./cfg_$addon start" if ! [ "$is_usb" ]; then [ "$0" != "./cfg_$addon" ] && echo "(or $0 start)" else echo "(or $inst_dir/addons/${0##*/} start)" echo echo "After a reboot you may reinstall $addon from usb with" echo "$inst_dir/addons/${0##*/} usb_install" fi # check for existence of "cfg.customize" to perform user specific cfg_xyz customizations for d in /var $inst_dir/addons;do if [ -f $d/cfg.customize -a -x $d/cfg.customize ];then [ "$verbose" ] && echo "Found addons customization script cfg.customize in $d. Will check & run customizations for $addon now." $d/cfg.customize allow ${0##*/} break fi done ;; usb_remove | remove) [ $1 = usb_remove ] && is_usb=1 inst_dir=`get_inst_realdir_` [ "$inst_dir" ] && is_usb=1 && echo -e "default set to usb_remove\n(if this is not correct run: rm /var/${addon}_usb )\n" if ! is_addon_installed -q; then [ "$quiet" ] || echo "addon $addon is not installed (has already been removed?)" else free echo "removing $addon ..." $0 -q stop ask || exit sleep 1 [ $modext = ko ] && rm $nbddev if lsmod |grep -q "^nbd "; then rmmod nbd || "information: failed to unload network block device module nbd.$modext" fi if [ "$is_usb" ]; then [ -L ${addon_usb} ] && rm ${addon}_usb fi [ -d $addon ] && rm -r $addon echo -n "done - (addon $addon has been removed" if [ "$is_usb" ]; then echo " from ram, not from usb)"; else echo ")"; fi free fi ;; start) if ! [ -x $addon/bin/$addon_bin ]; then errorexit "error: executable /var/$addon/bin/$addon_bin not found (try to run ${0##*/} install)" fi [ "$2" ] || usage_start ping -c 1 $2 || errorexit "error: no connection to nbd_server $2" portnr=8844 [ "$3" ] && portnr=$3 if ps |grep -v grep |grep -q "nbd-client"; then echo "information: nbd-client is already running (use ${0##*/} stop to terminate nbd-client)" else [ -b $nbddev ] || mknod $nbddev b 43 0 if ! $addon/bin/$addon_bin $2 $portnr $nbddev; then echo "error: failed to connect: $addon_bin $2 $3 $nbddev" echo echo "Did you start nbd-server on pc $2?" echo "(you may use www.spblinux.de/fbox/nbdserver_swap64mb.exe on windows" echo "and www.spblinux.de/fbox/nbdserver_swap64mb.tar.gz on linux)" fi fi if free |grep -q "Swap: * 0 "; then $addon/bin/mkswap $nbddev || errorexit "error: mkswap $nbddev failed" $addon/bin/swapon $nbddev || errorexit "error: swapon $nbddev failed" else echo "information: swapspace is already activated (use ${0##*/} stop to switch swap off)" fi free ;; stop) unset do_ask [ "x$2" = "xask" ] && do_ask=1 && shift f1=$addon_bin for x in stop TERM KILL result; do unset is_running for f in $f1; do if ps |grep -v grep |grep -q $f; then case $x in result) errorexit "error: cannot terminate process $f" ;; *) is_running=1 key=y if [ "$do_ask" -a $x = stop ]; then echo "$addon is running; do you want to terminate process $f (Y/n)" read key [ "n" = "$key" ] && errorexit "script terminated by user" echo "(to reset a terminal use command: stty sane)" fi if [ "$x" = "stop" ]; then if ! free |grep -q "Swap: *0 "; then ./$addon/bin/swapoff $nbddev free |grep -q "Swap: *0 " || errorexit "error: swap cannot be switched off now, try later" fi ./$addon/bin/$f -d $nbddev else killall -$x $f fi sleep 2 esac fi done [ "$is_running" ] || break done [ "$quiet" ] || echo "run ${0##*/} remove to free memory by removing addon $addon from the system" ;; *) usage esac