Skip to content

Instantly share code, notes, and snippets.

@ryansturmer
Last active July 18, 2019 14:31
Show Gist options
  • Save ryansturmer/e72fcdc35c83f61f0e462e116d9de5a6 to your computer and use it in GitHub Desktop.
Save ryansturmer/e72fcdc35c83f61f0e462e116d9de5a6 to your computer and use it in GitHub Desktop.
Script for Building a Beaglebone Black Archlinux ARM system with JupyterLab.
#!/bin/bash
TMPDIR=.tmp
DEVICE=/dev/mmcblk1
CURDIR=`pwd`
PARTITION=$DEVICE"p1"
PACKAGES="\
base-devel git vim \
nodejs npm \
jupyter jupyter-notebook jupyterlab python2-ipykernel python-numpy python-scipy python-pandas python-requests \
avahi"
function init {
pacman -S arch-install-scripts wget
rm -rf $TMPDIR
mkdir -p $TMPDIR
cd $TMPDIR
mkdir mnt
}
function create_partition {
dd if=/dev/zero of=$DEVICE bs=1M count=8
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk $DEVICE
o # clear the in memory partition table
n # new partition
p # primary partition
1 # partition number 1
2048 # starting at 2048
# default last sector
w # write and exit
EOF
mkfs.ext4 $PARTITION
}
function fetch_image {
wget http://os.archlinuxarm.org/os/ArchLinuxARM-am33x-latest.tar.gz
}
function install_image {
bsdtar -xpf ArchLinuxARM-am33x-latest.tar.gz -C mnt
sync
}
function install_bootloader {
dd if=mnt/boot/MLO of=$DEVICE count=1 seek=1 conv=notrunc bs=128k
dd if=mnt/boot/u-boot.img of=$DEVICE count=2 seek=1 conv=notrunc bs=384k
}
function setup_system {
cat > mnt/opt/bootstrap.sh << EOL
#!/bin/bash
# Initialize package manager
pacman-key --init
pacman-key --populate archlinuxarm
pacman --noconfirm -Syu
pacman --noconfirm -S $PACKAGES
# Create unpriveleged user for Jupyter
useradd jupyter
mkdir -p /home/jupyter
groupadd jupyter
usermod -a -G jupyter jupyter
chown jupyter /home/jupyter
# Setup sudo priveleges for jupyter user
groupadd sudo
usermod -a -G sudo jupyter
# Map port 80 (which can't be hosted by the jupyter user) to port 8888 (which can)
echo ""
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8888
iptables-save > /etc/iptables/iptables.rules
systemctl enable iptables
# Create and enable the jupyter service
cat <<EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter Notebook
[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/usr/bin/jupyter lab --config=/home/jupyter/.jupyter/jupyter_notebook_config.py
User=jupyter
Group=jupyter
WorkingDirectory=/home/jupyter/
Restart=always
RestartSec=10
#KillMode=mixed
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable jupyter
# Configure jupyterlab
sudo -i -u jupyter jupyter notebook --generate-config
cat <<EOF >> /home/jupyter/.jupyter/jupyter_notebook_config.py
c.NotebookApp.password = u''
c.NotebookApp.ip = u'0.0.0.0'
c.NotebookApp.token = u''
EOF
# Install, configure and enable avahi (bonjour) service discovery
systemctl disable systemd-resolved
systemctl enable avahi-daemon
cat <<EOF > /etc/avahi/services/jupyter.service
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h JupyterLab</name>
<service>
<type>_http._tcp</type>
<port>80</port>
</service>
</service-group>
EOF
jupyter labextension install @jupyter-widgets/jupyterlab-manager
EOL
# Install the bootstrap script (above, sets up target system) and execute it in the target chroot
chmod a+x mnt/opt/bootstrap.sh
arch-chroot mnt /opt/bootstrap.sh
}
function cleanup {
umount mnt
sync
}
init
fetch_image
create_partition
mount $PARTITION mnt
{
install_image
install_bootloader
setup_system
cleanup
} || {
echo "FAILED"
umount mnt
cd $CURDIR
rm -rf $TMPDIR
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment