#!/bin/bash # This script will install IPFS software and establish initial settings. # This must be executed with root privileges or with sudo. if [[ $(id -u) -ne 0 ]] ; then echo "Please run as root, or prefix with 'sudo'" ; exit 1 ; fi # Calculate disk space available for IPFS FREE=`df --output=avail -B 1 / | tail -n 1` # Free space in bytes on root partition MAX=`expr $FREE / 1320000000` # Approximately 75% of free space (in GB) printf -v STORAGE_MAX %dG $MAX # The default StorageMax parameter value AUTO=0 # Default autostart method 0 == systemd, 1 == cron @reboot WAIT=0 # Default value for single step / debug mode (no wait) DISTUP=0 # Default value for dist-upgrade (don't do it!) RPI_UFW=0 # Default value to install firewall on RPi (no) GOVER=NoGo # Default Go language version (Don't use go version) DEFGO=1.10.1 # Default version to use (tho now -g requires version) CONFIG=/home/ipfs/.ipfs/config # IPFS configuration file usage() { echo "$0 [-a] [-d] [-g ] [-m ] [-w] [-h | --help]" echo "-a == autostart method. Use cron @reboot instead of systemd unit" echo "-d == distribution upgrade. Specify -d to do a dist-upgrade" echo "-f == firewall on Raspberry Pi. Default is no. Use -f to enable on the RPi" echo "-g == go version. -g requires a version" echo "-m == max storage space. Default is 75% of disk. Option value integer in gigabytes" echo "-w == Wait after each step / debug mode. Default is no waiting" echo "-h == print this usage info and exit. Also for --help" } # Determine OpSys (Raspbian or Debian) OS=`lsb_release -is` if [[ $OS != "Raspbian" && $OS != "Debian" ]]; then echo "$OS Linux detected! This script is for Raspbian or Debian only for now." exit 1 fi if [ $OS == "Raspbian" ]; then ARCH=armv6l; else ARCH=amd64; # No support for 32 bit systems; should we? fi # Make sure we have enhanced getopt -- probably not required for most Linux distros getopt --test if [ $? -ne 4 ]; then echo "Ouch! getopt not available on this system - Bye!"; exit 1; fi # Process command line options # NOTE: double colons after g option (should be optional param for -g) swallows -m !!! # getopt from util-linux 2.29.2 on Raspbian Stretch Lite OS, 4/8/2019 release OPTS=`getopt -o adfg:m:wh --long help -n "$0" -- "$@"` eval set -- "$OPTS" # extract options and their arguments and set appropriate variables. while true; do case "$1" in -a) AUTO=1; shift ;; -d) DISTUP=1; shift ;; -f) RPI_UFW=1; shift ;; -g) if [ "$2" != "" ]; then GOVER=$2 echo "Will use golang version $GOVER" else GOVER=$DEFGO fi shift 2 ;; -m) if ! [ -z "${2//[0-9]}" ]; then # Positive number? echo "-m requires a positive integer (gigabytes)" exit 1 fi STORAGE_MAX="$2G"; shift 2 ;; -h|--help) usage; exit 0 ;; -w) WAIT=1; shift ;; --) shift; break ;; *) echo "No such option: $1" >&2; exit 1 ;; esac done # Show options parsed for this installation echo "OS=$OS, ARCH=$ARCH, AUTO=$AUTO, DISTUP=$DISTUP, RPI_UFW=$RPI_UFW, GOVER=$GOVER, STORAGE_MAX=$STORAGE_MAX, WAIT=$WAIT" echo "" if [ $WAIT == 1 ]; then read -n 1 -p "Press ^C to exit, any other key to proceed..." key; fi if [ ! -f ready4ipfs ]; then ping -q -w 1 -c 1 storage.googleapis.com > /dev/null 2>&1 if [ $? -ne 0 ]; then if [ $OS == "Raspbian" ]; then # Make sure the system is configured for network access echo "" echo "The system must be configured, at least to setup networking." echo "REBOOT if raspi-config asks, then run this again when rebooted." echo "You can just exit raspi-config the second time, don't reboot." read -n 1 -p "Press any key to launch the raspi-config utility..." key raspi-config else echo "" echo "It doesn't appear the system is online. Make sure networking" echo "is operational, then try again. Bye for now!" exit -1 fi fi echo "Updating the OS (Operating System)..." apt-get -y update apt-get -y upgrade touch ready4ipfs if [ $DISTUP == 1 ]; then echo "" echo "Performing a distribution upgrade, which may bump OS / kernel version..." apt-get -y dist-upgrade echo "" echo "Need to reboot. Afterwards, rerun this script to finish IPFS installation." read -n 1 -p "Press ^C to abort, or any other key to reboot..." key reboot fi fi if [ ! -d /home/ipfs ]; then echo "Preparing the ipfs user account..." if [ ! -d /home/ipfs ]; then useradd -m -s /bin/bash ipfs; fi if [ ! -d /home/ipfs/.ipfs ]; then mkdir /home/ipfs/.ipfs; fi if [ $GOVER != "NoGo" ]; then mkdir /home/ipfs/go echo "source /usr/local/bin/goInit" >> /home/ipfs/.profile # Sets PATH and GOPATH fi chown -R ipfs.ipfs /home/ipfs echo "" if [ $WAIT == 1 ]; then read -n 1 -p "Press ^C to exit, any other key to proceed..." key; fi fi if [ "$GOVER" == "NoGo" ]; then if [ ! -e /etc/apt/sources.list.d/siderus.list ]; then echo "Installing credentials for siderus software repository..." apt install -y apt-transport-https curl curl https://get.siderus.io/key.public.asc | apt-key add - echo "deb https://get.siderus.io/ apt/" | tee -a /etc/apt/sources.list.d/siderus.list IPFS=/usr/bin/ipfs apt update fi else # Install requested go language version if not already installed if [ ! -e "/usr/local/bin/go$GOVER.linux-$ARCH.tar.gz" ]; then pushd /usr/local/bin echo "Installing binary golang version $GOVER from googleapis. Please be patient..." wget https://storage.googleapis.com/golang/go$GOVER.linux-$ARCH.tar.gz tar -C /usr/local -xzf go$GOVER.linux-$ARCH.tar.gz popd fi # Create a bit of code to set env vars for all to source in their .profile echo 'export GOPATH=/home/$USER/go' > /usr/local/bin/goInit echo 'export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin' >> /usr/local/bin/goInit # Don't add to .profile if it's already there if ! grep -Fq goInit ~/.profile; then echo "source /usr/local/bin/goInit" >> ~/.profile fi source ~/.profile # We need those vars now to proceed VER=`go version` # Verify Installation if [[ ! "$VER" =~ $GOVER ]]; then echo "Bummer - failed to install go version $GOVER, Bye!" exit -1 else echo "$VER is installed" if [ ! -d /root/go ]; then mkdir /root/go; fi fi fi echo "" if [ $WAIT == 1 ]; then read -n 1 -p "Press ^C to exit, any other key to proceed..." key; fi echo "Installing IPFS and supporting packages. Please be patient..." apt-get -y install git tmux if [[ $OS != "Raspbian" || $RPI_UFW == 1 ]]; then apt-get -y install ufw; fi if [ $GOVER == "NoGo" ]; then apt-get -y install ipfs else IPFS=/home/ipfs/go/bin/ipfs # This is used for systemD unit if [ ! -e "/usr/local/bin/ipfs-update" ]; then echo "Installing ipfs-update..." # runuser -l ipfs -c 'go get -u github.com/ipfs/ipfs-update' # No worky anymore! curl https://dist.ipfs.io/ipfs-update/v1.5.2/ipfs-update_v1.5.2_linux-arm.tar.gz > ipfsUpdate.tgz tar -xzf ipfsUpdate.tgz mv ipfs-update/ipfs-update /usr/local/bin/. # Copy binary into place if [ "`which ipfs-update`" != "/usr/local/bin/ipfs-update" ]; then echo "Ouch - failed to install ipfs-update, Bye!" exit -1 else rm -rf ipfs-update # Remove the folder fi fi if [ "`which ipfs`" != "/usr/local/bin/ipfs" ]; then echo "Installing latest version of ipfs..." runuser -l ipfs -c 'ipfs-update install latest' # runuser -l ipfs -c 'ipfs-update --verbose install --no-check latest' if [ "`which ipfs`" != "/usr/local/bin/ipfs" ]; then echo "ipfs-update failed to install ipfs, Bye!" exit -1 fi fi fi # Initialize IPFS and log output runuser -l ipfs -c 'ipfs init --profile server > ~/.ipfs/init.log 2>&1' apt -y autoremove echo "" if [ $WAIT == 1 ]; then read -n 1 -p "Press ^C to exit, any other key to proceed..." key; fi if [[ $OS != "Raspbian" || $RPI_UFW == 1 ]]; then echo "Opening required firewall ports..." ufw allow 4001/tcp ufw allow 22/tcp echo "" if [ $WAIT == 1 ]; then read -n 1 -p "Press ^C to exit, any other key to proceed..." key; fi fi echo "Setting maximum IPFS storage to $STORAGE_MAX in config file..." sed -i "s/^\s*\"StorageMax.*$/ ~StorageMax~: ~$STORAGE_MAX~,/g" $CONFIG sed -i "s/~/\"/g" $CONFIG echo "" if [ $WAIT == 1 ]; then read -n 1 -p "Press ^C to exit, any other key to proceed..." key; fi if [ $AUTO == 0 ]; then # For some unknown reason this doesn't always work on debian minimal server echo "Creating SystemD unit to start IPFS on reboot..." ( cat < /etc/systemd/system/ipfs.service systemctl enable ipfs systemctl start ipfs else # Use this alternate way to start service on system start echo "#!/bin/bash" > /home/ipfs/autostart.bash echo ". /usr/local/bin/goInit" >> /home/ipfs/autostart.bash echo "export LimitNOFILE=65536" >> /home/ipfs/autostart.bash # Might not work as env echo "export IPFS_FD_MAX=4096" >> /home/ipfs/autostart.bash echo "export IPFS_PATH=/home/ipfs/.ipfs" >> /home/ipfs/autostart.bash echo "/home/ipfs/go/bin/ipfs daemon --enable-namesys-pubsub" >> /home/ipfs/autostart.bash echo "@reboot ipfs /bin/bash /home/ipfs/autostart.bash" > /etc/cron.d/autoStart chmod 755 /etc/cron.d/autoStart fi cat /home/ipfs/.ipfs/init.log echo "IPFS is now installed on this system." echo "" echo "Now set a password for the ipfs account. Press ^C and abort this now" echo "unless you are certain you have setup your system with appropriate" echo "locale, keyboard etc. Otherwise you may not be able to login." passwd ipfs