Skip to content

Instantly share code, notes, and snippets.

@jsign
Created November 24, 2020 16:41
Show Gist options
  • Save jsign/d195d881d9e48a6594da43eace367ee1 to your computer and use it in GitHub Desktop.
Save jsign/d195d881d9e48a6594da43eace367ee1 to your computer and use it in GitHub Desktop.
#!/bin/bash
### Configs
LOTUS_TAG=v1.2.1
EXTERNAL_DISK_SIZE=1T
GOLANG_TAR=go1.15.5.linux-amd64.tar.gz
IPFS_DOWNLOAD=https://dist.ipfs.io/go-ipfs/v0.7.0/go-ipfs_v0.7.0_linux-amd64.tar.gz
SWAP_SIZE=100G
####
### If /data exists, then most prob this VM is already setup,
### fail fast to avoid accidentally data loss.
if [ -d "/data" ]; then
printf 'Data directory exists!\nInfo:\n'
ifconfig | grep -A 1 eth0 | tail -n 1 | awk '{print "IP",$2}'
LOTUS_PATH=/data/lotus lotus wallet list | tail -n 1 | awk '{print "Wallet", $1}'
echo "Lotus token $(cat /data/lotus/token)"
exit 1
fi
set -xeuo pipefail
# Increase pid_max at kernel level.
sysctl -w kernel.pid_max=4194303
echo "kernel.pid_max=4194303" >> /etc/sysctl.conf
# Format and mount external disk in /data
DISKNAME=$(lsblk -o NAME,SIZE | grep -i $EXTERNAL_DISK_SIZE | awk '{ print $1 }')
parted /dev/$DISKNAME --script mklabel gpt mkpart xfspart xfs 0% 100%
mkfs.xfs /dev/$DISKNAME -f
partprobe /dev/$DISKNAME
mkdir /data
mount /dev/$DISKNAME /data
DISKUUID=$(blkid -s UUID -o value /dev/$DISKNAME)
echo "UUID=$DISKUUID /data xfs defaults,nofail 1 2" >> /etc/fstab
# Add swap
fallocate -l $SWAP_SIZE /data/swapfile
chmod 600 /data/swapfile
mkswap /data/swapfile
swapon /data/swapfile
echo "/data/swapfile swap swap defaults 0 0" >> /etc/fstab
# Install Go
wget https://golang.org/dl/$GOLANG_TAR
tar -C /usr/local -xzf $GOLANG_TAR
# Lotus deps
apt update
apt install mesa-opencl-icd ocl-icd-opencl-dev pkg-config build-essential libclang-dev gcc git bzr jq curl clang hwloc libhwloc-dev -y
apt upgrade -y
# Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
export PATH=$PATH:/usr/local/go/bin:/home/azureuser/.cargo/bin
echo "export PATH=$PATH:/usr/local/go/bin:/home/azureuser/.cargo/bin" >> ~/.bashrc
echo "export LOTUS_PATH=/data/lotus" >> ~/.bashrc
echo "export IPFS_PATH=/data/ipfs" >> ~/.bashrc
# Lotus
mkdir -p /data/lotus
git clone https://github.com/filecoin-project/lotus.git
cd lotus
git checkout $LOTUS_TAG
FFI_BUILD_FROM_SOURCE=1 RUSTFLAGS="-C target-cpu=native -g" CGO_CFLAGS="-D__BLST_PORTABLE__" make build install
cd ..
# IPFS
mkdir -p /data/ipfs
wget https://dist.ipfs.io/go-ipfs/v0.7.0/go-ipfs_v0.7.0_linux-amd64.tar.gz
tar -xvzf go-ipfs_v0.7.0_linux-amd64.tar.gz
./go-ipfs/install.sh
rm *.tar.gz
rm -rf go-ipfs
IPFS_PATH=/data/ipfs ipfs init
sed -i.bak -e 's/\/ip4\/127.0.0.1\/tcp\/5001/\/ip4\/0.0.0.0\/tcp\/5001/g' /data/ipfs/config
# Lotus snapshot
LOTUS_PATH=/data/lotus lotus daemon --import-snapshot=https://fil-chain-snapshots-fallback.s3.amazonaws.com/mainnet/minimal_finality_stateroots_latest.car --halt-after-import
chown -R azureuser ~/.cargo ~/go ~/lotus /data
echo '
[API]
ListenAddress = "/ip4/0.0.0.0/tcp/1234/http"
[Client]
UseIpfs = true
IpfsMAddr = "/ip4/127.0.0.1/tcp/5001"
IpfsUseForRetrieval = true
' > /data/lotus/config.toml
# systemd config for go-ipfs
echo '[Unit]
Description=Ipfs Pow
After=network-online.target
Requires=network-online.target
[Service]
Environment=IPFS_PATH=/data/ipfs
ExecStart=/usr/local/bin/ipfs daemon --migrate=true
Restart=always
RestartSec=10
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
' > ipfs-pow-daemon.service
install -C -m 0644 ipfs-pow-daemon.service /etc/systemd/system/ipfs-pow-daemon.service
systemctl daemon-reload
systemctl enable ipfs-pow-daemon
systemctl start ipfs-pow-daemon
rm ipfs-pow-daemon.service
# systemd config for Lotus
mkdir /data/lotustmp
echo '[Unit]
Description=Lotus Daemon
After=network-online.target
Requires=network-online.target
[Service]
Environment=LOTUS_PATH=/data/lotus
Environment=LOTUS_BACKUP_BASE_PATH=/home/azureuser
Environment=TMPDIR=/data/lotustmp
ExecStart=/usr/local/bin/lotus daemon
Restart=always
RestartSec=10
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
' > lotus-daemon.service
install -C -m 0644 lotus-daemon.service /etc/systemd/system/lotus-daemon.service
systemctl daemon-reload
systemctl enable lotus-daemon
systemctl start lotus-daemon
rm lotus-daemon.service
# Automatic backup of wallets and metadata of Lotus node
echo '#!/bin/bash
set -xeuo pipefail
BASE_PATH=/home/azureuser
BACKUP_FOLDER=$BASE_PATH/backups/$(date +%Y%m%d%H%M)
mkdir -p $BACKUP_FOLDER
lotus backup $BACKUP_FOLDER/metadata.bak
sudo cp -R /data/lotus/keystore $BACKUP_FOLDER/
ls -A1td $BASE_PATH/backups/* | tail -n +96 | sudo xargs rm -rf
' > backup.sh
chown -R azureuser backup.sh
chmod 755 backup.sh
echo 'PATH=$PATH:/usr/local/bin:/bin:/usr/bin
LOTUS_PATH=/data/lotus
*/15 * * * * /home/azureuser/backup.sh > /home/azureuser/cron.log 2>&1
' > /var/spool/cron/crontabs/azureuser
chown -R azureuser /var/spool/cron/crontabs/azureuser
chmod 600 /var/spool/cron/crontabs/azureuser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment