Debian, linux, 树莓

Raspbian的通用软件安装程序

我写了这个小工具来方便我的软件安装 下一页CloudPi,但可用于任何Raspbian安装。

有了这个,我可以自动化

  • 通过SSH为正在运行的Raspberry Pi安装新服务
  • 向我的Raspbian映像添加新服务,并生成可以通过QEMU写入SD卡的映像,就像我在 这个帖子.

它具有简单的功能 诅咒 菜单以配置参数。

我的目标是使我轻松地更新系统。如果编写用于重现系统的脚本,则可以更轻松地对其进行升级,而无需花费很多时间进行配置并每次都记住所有步骤。

我什至可以保留默认值,因此可以完成安装和配置。

至于分享即用型图像 下一页CloudPi,我意识到’您可以在映像中打包很多东西而无需用户交互(例如进入其私有云的域),因此我想提供一种简单的方法来设置这些东西。

用法

使用QEMU安装到映像

./installer.sh <script.sh> <IP> <imgfile.img>

要通过SSH直接安装到运行中的Raspberry Pi,请省略image参数

./installer.sh <script.sh> <IP>

为了跳过交互式配置(您可以在脚本顶部编辑变量)

NO_CONFIG=1 ./installer.sh <script.sh> <IP> (<imgfile.img>)

为了使用默认的SSH用户和/或密码以外的其他密码,您可以使用相同的方式进行指定

PIUSER=nacho PIPASS=ownyourbits ./installer.sh <script.sh> <IP> (<imgfile.img>)

例如,为了安装动态DNS(请参阅post)

./installer.sh no-ip.sh 192.168.0.130 下一页CloudPi-lite_02-18-17_fail2ban.img

请注意,一个名为 config_no-ip.txt 将使用所使用的配置生成。

安装

吉特 clone //github.com/nachoparker/nextcloud-raspbian-generator.git

交互式配置功能需要 对话。例如,在Debian和Ubuntu中输入

sudo apt-get install 对话

细节

实际上,可以将安装脚本复制到Raspberry Pi中,通过编辑其变量进行配置并执行。

这只是有助于处理,并使其自动化以创建图像。

从这里它使用在QEMU上运行Raspbian的方法 以前的帖子,然后打包图像,并将脚本名称附加到输入图像。

#!/bin/bash

# Raspbian的通用软件安装程序. Online on a running RPi, or offline with QEMU.
# Tested with 2017-01-11-raspbian-jessie.img (and lite)
#
# Copyleft 2017 通过  Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# 用法:
#
#  使用QEMU安装到映像
#
#      ./installer.sh <script.sh> <IP> <imgfile.img> 
#
#  要通过SSH直接安装到运行中的Raspberry Pi,请省略image参数
#
#      ./installer.sh <script.sh> <IP> 
#
#  为了跳过交互式配置(您可以在脚本顶部编辑变量)
#
#      NO_CONFIG=1 ./installer.sh <script.sh> <IP> (<imgfile.img>)
#
#  为了使用默认的SSH用户和/或密码以外的其他密码,您可以使用相同的方式进行指定
#
#      PIUSER=nacho PIPASS=ownyourbits ./installer.sh <script.sh> <IP> (<imgfile.img>)
#
# Notes:
#  Use a Raspbian image to be run on QEMU
#  Use any script that would run locally on the image
#  Use the IP of your running QEMU Raspbian image (DHCP should assign always the same)
#

INSTALL_SCRIPT=$1       # First argument is the script to be run inside Raspbian
IP=$2                   # Second argument is the QEMU Raspbian IP address
IMGFILE=$3              # Third argument is the image file to start from ( empty for online installation )
 
source 库.sh       # initializes $IMGOUT

config $INSTALL_SCRIPT || exit 1

if [[ "$IMGFILE" != "" ]]; then
  launch_install_qemu "$IMGFILE" $IP || exit 1
  pack_image          "$IMGFILE" "$IMGOUT" 
else
  launch_installation $IP
fi


# License
#
# This script is free 软件; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published 通过 
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA
#!/bin/bash

# Library to install 软件 on Raspbian 臂 through QEMU
#
# Copyleft 2017 通过  Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!


IMGOUT=$( basename $IMGFILE .img )_$( basename $INSTALL_SCRIPT .sh ).img
CFGOUT=config_$( basename $INSTALL_SCRIPT .sh ).txt


function launch_install_qemu()
{
  local IMG=$1
  local IP=$2
  [[ "$IP"      == ""  ]] && { echo "usage: launch_install_qemu <script> <img> <IP>"; return 1; }
  test -f $IMG            || { echo "input file $IMG not found";                      return 1; }

  # take a copy of the input image for processing ( append "-stage1" )
  local BASE=$( sed 's=-stage[[:digit:]]=='         <<< $IMG )
  local NUM=$(  sed 's=.*-stage\([[:digit:]]\)=\1=' <<< $IMG )
  [[ "$BASE" == "$IMG" ]] && NUM=0
  local IMGFILE="$BASE-stage$(( NUM+1 ))"
  cp -v $IMG $IMGFILE || return 1

  local NUM_REBOOTS=$( grep -c reboot $INSTALL_SCRIPT )
  while [[ $NUM_REBOOTS != -1 ]]; do
    launch_qemu $IMGFILE &
    sleep 10
    wait_SSH $IP
    launch_installation $IP
    wait 
    NUM_REBOOTS=$(( NUM_REBOOTS-1 ))
  done
  echo "$IMGFILE generated successfully"
}

function launch_qemu()
{
  local IMG=$1
  test -f $1 || { echo "Image $IMG not found"; return 1; }
  test -d e木-raspbian-network || 吉特 clone //github.com/nachoparker/qemu-raspbian-network.git
  sed -i '30s/NO_NETWORK=1/NO_NETWORK=0/' e木-raspbian-network/qemu-pi.sh
  echo "Starting QEMU image $IMG"
  ( cd e木-raspbian-network && sudo ./qemu-pi.sh ../$IMG 2>/dev/null )
}

function ssh_pi()
{
  local IP=$1
  local ARGS=${@:2}
  local PIUSER=${PIUSER:-pi}
  local PIPASS=${PIPASS:-raspberry}
  local SSH=( ssh -q  -o UserKnownHostsFile=/dev/null\
                      -o StrictHostKeyChecking=no\
                      -o ServerAliveInterval=20\
                      -o ConnectTimeout=20\
                      -o LogLevel=quiet                  )
  type sshpass &>/dev/null && local SSHPASS=( sshpass -p$PIPASS )
  ${SSHPASS[@]} ${SSH[@]} ${PIUSER}@$IP $ARGS
}

function wait_SSH()
{
  local IP=$1
  echo "Waiting for SSH to be up on $IP..."
  while true; do
    ssh_pi $IP : && break
    sleep 1
  done
  echo "SSH is up"
}

function launch_installation()
{
  local IP=$1
  [[ "$INSTALLATION_CODE" == "" ]] && { echo "Need to run config first"; return 1; }
  echo "Launching installation"
  echo -e "$INSTALLATION_CODE" | ssh_pi $IP || echo "SSH to $IP failed"
  echo "configuration saved to $CFGOUT"
}

# Initializes $INSTALLATION_CODE
function config()
{
  local INSTALL_SCRIPT="$1"
  local BACKTITLE="下一页CloudPi installer configuration"

  test -f "$INSTALL_SCRIPT" || { echo "file "$INSTALL_SCRIPT" not found"; return 1; }
  local VARS=( $( grep "^[[:alpha:]]\+_=" "$INSTALL_SCRIPT" | cut -d= -f1 | sed 's|_$||' ) )
  local VALS=( $( grep "^[[:alpha:]]\+_=" "$INSTALL_SCRIPT" | cut -d= -f2 ) )

  [[ "$NO_CONFIG" == "1" ]] || test ${#VARS[@]} -eq 0 && { INSTALLATION_CODE="$( cat "$INSTALL_SCRIPT" )"; return; }

  for i in `seq 1 1 ${#VARS[@]} `; do
    local PARAM+="${VARS[$((i-1))]} $i 1 ${VALS[$((i-1))]} $i 15 30 0 "
  done

  local DIALOG_OK=0
  local DIALOG_CANCEL=1
  local DIALOG_HELP=2
  local DIALOG_EXTRA=3
  local DIALOG_ITEM_HELP=4
  local DIALOG_ERROR=254
  local DIALOG_ESC=255
  local RET=0

  while test $RET != 1 && test $RET != 250; do
    exec 3>&1
    local value
    value=$( 对话 --ok-label "Start" \
                    --backtitle "$BACKTITLE" \
                    --form "Enter the desired configuration" \
                    20 50 0 $PARAM \
            2>&1 1>&3 )
    RET=$?
    exec 3>&-

    case $RET in
      $DIALOG_CANCEL)
        对话 \
          --clear \
          --backtitle "$BACKTITLE" \
          --yesno "Really quit?" 10 30
        case $? in
          $DIALOG_OK)
            echo "Aborted"
            return 1
            ;;
          $DIALOG_CANCEL)
            RET=99
            ;;
        esac
        ;;
      $DIALOG_OK)
        对话 \
          --clear \
          --backtitle "$BACKTITLE" --no-collapse --cr-wrap \
          --yesno "The following configuration will be used\n\n$value" 10 60
        case $? in
          $DIALOG_OK)
            local RET=( $value )
            for i in `seq 0 1 $(( ${#RET[@]} - 1 )) `; do
              local SEDRULE+="s|^${VARS[$i]}_=.*|${VARS[$i]}_=${RET[$i]}|;"
              local CONFIG+="${VARS[$i]}=${RET[$i]}\n"
            done
            break
            ;;
          $DIALOG_CANCEL)
            RET=99
            ;;
        esac
        ;;
      $DIALOG_HELP)
        echo "Button 2 (Help) pressed."
        return 1
        ;;
      $DIALOG_EXTRA)
        echo "Button 3 (Extra) pressed."
        return 1
        ;;
      $DIALOG_ERROR)
        echo "ERROR!$value"
        return 1
        ;;
      $DIALOG_ESC)
        echo "ESC pressed."
        return 1
        ;;
      *)
        echo "Return code was $RET"
        return 1
        ;;
    esac
  done

  INSTALLATION_CODE="$( sed $SEDRULE "$INSTALL_SCRIPT" )"
  echo -e "$CONFIG" > $CFGOUT
  clear
}

function pack_image()
{
  local IMGFILE="$1"
  local IMGOUT="$2"
  local TARNAME=$( basename $IMGOUT .img ).tar.bz2
  cp -v $( ls -1t $IMGFILE-stage* | head -1 ) $IMGOUT
  tar -I pbzip2 -cvf $TARNAME $IMGOUT &>/dev/null && \
    echo -e "$TARNAME packed successfully"
}

# License
#
# This script is free 软件; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published 通过 
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

作者: 纳乔帕克

谦虚地分享我认为有用的东西 [ 的github 码头工人hub ]

1 评论

发表评论

您的电子邮件地址不会被公开。 必需的地方已做标记 *