142 lines
5.4 KiB
Bash
142 lines
5.4 KiB
Bash
#!/bin/bash
|
|
|
|
ISCSI_TARGET_IQN="iqn.2000-01.com.synology:SynologyNAS.Target-13.b63ef49539c"
|
|
ISCSI_TARGET_IP="192.168.1.5"
|
|
|
|
PI_SERIAL="$(vcgencmd otp_dump | grep 28: | sed s/.*://g)"
|
|
ISCSI_INITIATOR_IQN="$(tail -1 /etc/iscsi/initiatorname.iscsi | grep InitiatorName | cut -d '=' -f 2 | tr -d '"')"
|
|
ISCSI_DEVICE="/dev/sda"
|
|
ISCSI_ROOT_PARTITION="/dev/sda1"
|
|
TFTP_ROOT="/volume1/rpi/tftpboot/"
|
|
PI_HOSTNAME=$(hostname)
|
|
PI_IP=$(ifconfig eth0 | awk '/inet /{ print $2;}')
|
|
PI_MAC=$(ifconfig eth0 | awk '/ether/{ print $2;}' | tr ':' '-')
|
|
PI_NETMASK=$(ifconfig eth0 | awk '/netmask/{ print $4;}')
|
|
#ISCSI_INITIATOR_IQN=$(cat /etc/iscsi/iscsi.initramfs | grep 'ISCSI_INITIATOR=' | cut -d '=' -f 2)
|
|
#ISCSI_TARGET_IP=$(cat /etc/iscsi/iscsi.initramfs | grep 'ISCSI_TARGET_IP=' | cut -d '=' -f 2)
|
|
#ISCSI_TARGET_IQN=$(cat /etc/iscsi/iscsi.initramfs | grep 'ISCSI_TARGET=' | cut -d '=' -f 2)
|
|
NFS_DIR="/nfs/$(hostname -s)-boot"
|
|
NFS_IP=${ISCSI_TARGET_IP}
|
|
NFS_ROOT_PATH="${TFTP_ROOT}${PI_SERIAL}"
|
|
ISCSI_ROOT_DIR="/iscsi/$(hostname -s)-iscsi"
|
|
#ISCSI_ROOT_UUID=$(sudo blkid | grep ${ISCSI_ROOT_PARTITION} | tr ' ' '\n' | grep -w UUID | cut -d '=' -f 2 | tr -d '"')
|
|
#ISCSI_ROOT_PARTUUID=$(sudo blkid | grep ${ISCSI_ROOT_PARTITION} | tr ' ' '\n' | grep PARTUUID | cut -d '=' -f 2 | tr -d '"')
|
|
|
|
#Make sure ISCSI Initiator IQN is set
|
|
sudo iscsiadm \
|
|
--mode discovery \
|
|
--type sendtargets \
|
|
--portal ${ISCSI_TARGET_IP}
|
|
|
|
#Install Required Software
|
|
apt -y install net-tools nfs-common open-iscsi
|
|
|
|
#Install libraspberrypi-lib if not installed
|
|
LIBRASPBERRYPI_BIN_INSTALLED=$(apt-cache policy libraspberrypi-bin | grep 'Installed' | cut -d ':' -f 2)
|
|
LIBRASPBERRYPI_BIN_IS_INSTALLED=" (none)"
|
|
|
|
if [ "${LIBRASPBERRYPI_BIN_INSTALLED}" = "${LIBRASPBERRYPI_BIN_IS_INSTALLED}" ]
|
|
then
|
|
apt -y install libraspberrypi-bin
|
|
echo "ISCSI_INITIATOR_IQN=${ISCSI_INITIATOR_IQN}"
|
|
echo ""
|
|
echo ""
|
|
echo "Please re-run the script after rebooting"
|
|
echo ""
|
|
read -p "Press any key to reboot ..."
|
|
reboot now
|
|
else
|
|
echo ""
|
|
echo ""
|
|
echo ""
|
|
echo ""
|
|
echo "Make sure the ISCSI and NFS targets are set op on the server"
|
|
echo ""
|
|
echo "ISCSI_INITIATOR_IQN=${ISCSI_INITIATOR_IQN}"
|
|
read -p "PI_SERIAL=${PI_SERIAL}"
|
|
fi
|
|
|
|
# Copy over to the TFTP server, via the NFS mount.
|
|
mkdir -p /nfs/$(hostname -s)-boot
|
|
mount ${NFS_IP}:${NFS_ROOT_PATH} /nfs/$(hostname -s)-boot
|
|
rsync --info=progress2 -a --delete /boot/ /nfs/$(hostname -s)-boot
|
|
|
|
update-initramfs -v -k "$(uname -r)" -c -b /nfs/$(hostname -s)-boot
|
|
|
|
# Update the cmdline.txt file that the pi boots with
|
|
# https://www.raspberrypi.org/documentation/configuration/cmdline-txt.md
|
|
# ip= docs can be found at
|
|
# https://git.kernel.org/pub/scm/libs/klibc/klibc.git/tree/usr/kinit/ipconfig/README.ipconfig
|
|
# However, don't put anything after static in the line below, because it completely breaks
|
|
# everything in a very non-obvious way.
|
|
|
|
touch /nfs/$(hostname -s)-boot/cmdline.txt
|
|
cp config.txt /nfs/$(hostname -s)-boot/config.txt
|
|
|
|
sed -i -r -e \
|
|
"s/$/ net.ifnames=0 dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 rootfstype=btrfs elevator=deadline rootwait fixrtc ip=dhcp root=UUID=${ISCSI_ROOT_UUID}$ISCSI_INITIATOR=${ISCSI_INITIATOR_IQ>
|
|
/nfs/$(hostname -s)-boot/cmdline.txt
|
|
|
|
#Mount ISCSI
|
|
#sudo iscsiadm \
|
|
sudo iscsiadm \
|
|
--mode node \
|
|
--targetname ${ISCSI_TARGET_IQN} \
|
|
--portal ${ISCSI_TARGET_IP} \
|
|
--login
|
|
|
|
#read -p "Pausing after iscsiadm"
|
|
# Sleep otherwise it errors
|
|
sleep 5
|
|
|
|
# Partition the ISCSI Device
|
|
sudo parted ${ISCSI_DEVICE} mklabel gpt
|
|
sleep 5
|
|
sudo parted --align optimal ${ISCSI_DEVICE} mkpart primary btrfs 0% 100%
|
|
sleep 5
|
|
|
|
# Create the btrfs Filesystem
|
|
yes | sudo mkfs.btrfs -f /${ISCSI_ROOT_PARTITION}
|
|
sleep 5
|
|
sudo mkdir -p ${ISCSI_ROOT_DIR}
|
|
sudo mount "${ISCSI_ROOT_PARTITION}" ${ISCSI_ROOT_DIR}
|
|
|
|
# Copy the root filesystem
|
|
sudo rsync -xa --info=progress2 --exclude /nfs --exclude /iscsi / ${ISCSI_ROOT_DIR}
|
|
|
|
|
|
# Update /boot to point to the NFS mount of the TFTP folder for the Pi.
|
|
sudo sed -i -r -E \
|
|
"s@.*/boot +.*@${NFS_IP}:${NFS_ROOT_PATH} /boot nfs defaults,proto=tcp 0 0@" \
|
|
${ISCSI_ROOT_DIR}/etc/fstab
|
|
|
|
# Update / to point to the iSCSI drive.
|
|
sudo sed -i -r -E \
|
|
"s@.*/ +.*@PARTUUID=${ISCSI_ROOT_PARTUUID} / btrfs _netdev,noatime 0 1@" \
|
|
${ISCSI_ROOT_DIR}/etc/fstab
|
|
|
|
# Create links in /boot
|
|
ln /nfs/$(hostname -s)-boot/firmware/bcm2711-rpi-4-b.dtb /nfs/$(hostname -s)-boot/bcm2711-rpi-4-b.dtb
|
|
ln /nfs/$(hostname -s)-boot/firmware/start4.elf /nfs/$(hostname -s)-boot/start4.elf
|
|
ln /nfs/$(hostname -s)-boot/firmware/fixup4.dat /nfs/$(hostname -s)-boot/fixup4.dat
|
|
|
|
ISCSI_ROOT_UUID=$(sudo blkid | grep ${ISCSI_ROOT_PARTITION} | tr ' ' '\n' | grep -w UUID | cut -d '=' -f 2 | tr -d '"')
|
|
ISCSI_ROOT_PARTUUID=$(sudo blkid | grep ${ISCSI_ROOT_PARTITION} | tr ' ' '\n' | grep PARTUUID | cut -d '=' -f 2 | tr -d '"')
|
|
echo "ISCSI UUID=${ISCSI_ROOT_UUID}"
|
|
echo "ISCSI PARTUUID=${ISCSI_ROOT_PARTUUID}"
|
|
echo "ISCSI TARGET IQN=${ISCSI_TARGET_IQN}"
|
|
echo "ISCSI INITIATOR IQN=${ISCSI_INITIATOR_IQN}"
|
|
read -p "Installation Is Complete. Please remove the SD card and reboot."
|
|
|
|
# The connection to the iSCSI device is no longer needed, so clean it up.
|
|
sudo umount ${ISCSI_ROOT_DIR}
|
|
sudo iscsiadm --m node -T ${ISCSI_TARGET_IQN} --portal ${ISCSI_TARGET_IP}:3260 -u
|
|
sudo iscsiadm -m node -o delete -T ${ISCSI_TARGET_IQN} --portal ${ISCSI_TARGET_IP}:3260
|
|
sudo rm -rf ${ISCSI_ROOT_DIR}
|
|
|
|
# The connection to the NFS device is no longer needed, so clean it up
|
|
sudo umount ${NFS_DIR}
|
|
rm -rf ${NFS_DIR}
|
|
|
|
# Shutdown
|
|
sudo shutdown now |