Netboot ISCSI

Here we are, with our netbooted bunch of Raspberry Pis.
Our excitement will last, up until we find out, we wont be able to run containerized
applications, due to storage driver incompatibility.

We never give up, if there is a way, we will find it.
The solution is to provide block level storage to our devices,
effectively equating it to a physically connected media,
but also speeding up read/writes up to ethernet speed : 1Gbit/sec.

Yet another shell script we used to automate the ISCSI provisioning,
detailed explaination of it's working, is found in the comments.

#!/bin/bash

echo "Insert NODEX number"

read NUM

echo "Insert ISCSI Host"

read HOST

sudo apt-get install open-iscsi -y   # Install open-iscsi

sudo systemctl restart iscsi.service   # Start iscsi service

TARGET=$(iscsiadm -m discovery -t sendtargets -p $HOST | grep Target-$NUM | awk -F ' ' '{print $2;exit;}')  # Retrieve iscsi target

sudo iscsiadm --portal $HOST -T $TARGET --mode node --login  # Login to iscsi target

sudo touch /etc/iscsi/iscsi.initramfs  # Creates initramfs to include iscsi at boot time

sudo mkfs.ext4 -m0 /dev/sda  # Make filesystem ext4 on /dev/sda

UUID=$(sudo blkid | awk -F ' ' '{print $2}')  # Retrieve UUID 

sudo mkdir /mnt/iscsi  #  Create mount directory

sudo mount /dev/sda /mnt/iscsi  #  Mounts the LUN to /dev/sda

sudo rm -rf /mnt/iscsi/*  #  Ensures the mounted LUN is empty

sudo rsync -avhP --exclude /boot --exclude /proc --exclude /run --exclude /sys --exclude /mnt --exclude /media / /mnt/iscsi/  # Copy the whole system onto the new scsi mount

sudo mkdir /mnt/iscsi/{proc,run,sys,boot,mnt,media}   

sudo cp /boot/cmdline.txt /boot/cmdline.txt.org
sudo cp /boot/cmdline.txt /boot/cmdline.txt.lun
sudo cp /boot/config.txt /boot/config.txt.org
sudo cp /boot/config.txt /boot/config.txt.lun

cat << "EOF" >  /boot/switch_boot_setup.sh
#!/bin/bash

if [[ ! $(whoami) =~ "root" ]]; then
echo "**********************************"
echo "*** This should be run as root ***"
echo "**********************************"
exit 1
fi

function ask_yes_or_no() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}

if [ "$1" = "lun" ]; then
echo "Switch to 'lun' boot setup."
sudo cp /boot/cmdline.txt.lun /boot/cmdline.txt
sudo cp /boot/config.txt.lun /boot/config.txt

if [[ "yes" == $(ask_yes_or_no "Would you like to sync files first?") ]]; then
echo "Wait...."
sudo /root/scripts/sync_sd-card_2_lun.sh
else
echo "File Synchronisation Skipped."
fi
if [[ "yes" == $(ask_yes_or_no "REBOOT now?") ]]; then
if [[ "yes" == $(ask_yes_or_no "Are you *really* sure?") ]]; then
sudo reboot
exit 0
else
echo "Reboot Skipped."
fi
else
echo "Reboot Skipped."
fi

elif [ "$1" = "org" ]; then
echo "Switch to 'org' boot setup."
sudo cp /boot/cmdline.txt.org /boot/cmdline.txt
sudo cp /boot/config.txt.org /boot/config.txt

if [[ "yes" == $(ask_yes_or_no "Would you like to sync files first?") ]]; then
echo "Wait...."
sudo /root/scripts/sync_lun_2_sd-card.sh
else
echo "File Synchronisation Skipped."
fi
if [[ "yes" == $(ask_yes_or_no "REBOOT now?") ]]; then
if [[ "yes" == $(ask_yes_or_no "Are you *really* sure?") ]]; then
sudo reboot
exit 0
else
echo "Reboot Skipped."
fi
else
echo "Reboot Skipped."
fi

else
echo "Parameter should be 'lun' or 'org'."
echo "e.g. /boot/switch_boot_setup.sh lun"
fi
EOF

sudo chmod +x /boot/switch_boot_setup.sh

sudo apt-get install initramfs-tools -y

cd /boot

sudo update-initramfs -v -k `uname -r` -c > /home/pi/initramfs.txt

KERN=$(uname -r)

echo "initramfs initrd.img-$KERN followkernel" >> /boot/config.txt.lun

INITIATOR=$(sudo tail -n 1 /etc/iscsi/initiatorname.iscsi | awk -F '=' '{print $2}')

IP=$(hostname -I | sed 's/ *$//')

cat << EOF > cmdline.txt.lun
dwc_otg.lpm_enable=0 ip=$IP:$HOST:$DEFAULT_GATEWAY:255.255.255.0:node$NUM:eth0:off ISCSI_INITIATOR=$INITIATOR ISCSI_TARGET_NAME=$TARGET ISCSI_TARGET_IP=$HOST ISCSI_TARGET_PORT=3260 ISCSI_TARGET_GROUP=1 rw rootfs=ext4 root=$UUID fsck.repair=yes elevator=deadline rootwait panic=15 cgroup_memory=1 cgroup_enable=memory
EOF

echo "$UUID       /       ext4    defaults        1       1" >> /mnt/iscsi/etc/fstab

cd /boot

./switch_boot_setup.sh lun

Comments

Subscribe to our Newsletter