113 lines
4.4 KiB
Bash
113 lines
4.4 KiB
Bash
#!/bin/bash
|
|
TFTP_ROOT="/volume1/rpi/tftpboot/"
|
|
ISCSI_TARGET_IP="192.168.1.5"
|
|
ISCSI_TARGET_IQN="iqn.2000-01.com.synology:SynologyNAS.Target-11.b63ef49539c"
|
|
ISCSI_INITIATOR_IQN="iqn.1993-08.org.debian:01:9174e3877a3"
|
|
ISCSI_DEVICE="/dev/sda"
|
|
ISCSI_ROOT_PARTITION="/dev/sda1"
|
|
PI_SERIAL="6218cd17"
|
|
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"
|
|
|
|
# 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 -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.
|
|
cp cmdline.txt /nfs/$(hostname -s)-boot/cmdline.txt
|
|
cp config.txt /nfs/$(hostname -s)-boot/config.txt
|
|
#sed -i -r -e \
|
|
# "s/$/ ip=${PI_IP}:::${PI_NETMASK}:${PI_HOSTNAME}:eth0:static ISCSI_INITIATOR=${ISCSI_INITIATOR_IQN} ISCSI_TARGET_NAME=${ISCSI_TARGET_IQN} ISCSI_TARGET_IP=${ISCSI_TARGET_IP} rw/g" \
|
|
# /nfs/$(hostname -s)-boot/cmdline.txt
|
|
|
|
# Update /boot/config.txt to use our new initramfs.
|
|
#sed -i -r -e \
|
|
# "s@\[pi4\]@[pi4]\ninitramfs initrd.img-$(uname -r) followkernel@" \
|
|
# /nfs/$(hostname -s)-boot/config.txt
|
|
|
|
#umount /nfs/$(hostname -s)-boot
|
|
|
|
|
|
#Mount iSCSI
|
|
sudo iscsiadm \
|
|
--mode discovery \
|
|
--type sendtargets \
|
|
--portal ${ISCSI_TARGET_IP}
|
|
sudo iscsiadm \
|
|
--mode node \
|
|
--targetname ${ISCSI_TARGET_IQN} \
|
|
--portal ${ISCSI_TARGET_IP} \
|
|
--login
|
|
|
|
|
|
sudo parted ${ISCSI_DEVICE} mklabel gpt
|
|
sudo parted --align optimal ${ISCSI_DEVICE} mkpart primary btrfs 0% 100%
|
|
|
|
# Create the btrfs Filesystem
|
|
sudo mkfs.btrfs -f /${ISCSI_ROOT_PARTITION}
|
|
sudo mkdir -p ${ISCSI_ROOT_DIR}
|
|
sudo mount "${ISCSI_ROOT_PARTITION}" ${ISCSI_ROOT_DIR}
|
|
|
|
# Mount the Root Partion in the Generated Image
|
|
#CREATION_OUTPUT=$(sudo partx --add -v output-arm-image/image)
|
|
#LOOP_DEVICE=$(echo ${CREATION_OUTPUT} | sed -E 's@.*(/dev/loop[0-9]+).*@\1@')
|
|
#IMAGE_ROOT_DIR="$(mktemp -d)"
|
|
#sudo mount "${LOOP_DEVICE}p2" ${IMAGE_ROOT_DIR}
|
|
|
|
# Begin the Copy
|
|
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.
|
|
ISCSI_ROOT_PARTUUID=$(sudo blkid | grep ${ISCSI_ROOT_PARTITION} | tr ' ' '\n' | grep PARTUUID | cut -d '=' -f 2 | tr -d '"')
|
|
#sudo sed -i -r -E \
|
|
# "s@.*/ +.*@PARTUUID=${ISCSI_ROOT_PARTUUID} / btrfs _netdev,noatime 0 1@" \
|
|
# ${ISCSI_ROOT_DIR}/etc/fstab
|
|
|
|
# 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
|
|
|
|
# Mount the NFS Folder
|
|
#NFS_DIR="$(mktemp -d)"
|
|
#sudo mount ${NFS_IP}:${NFS_ROOT_PATH} ${NFS_DIR}
|
|
|
|
# Update the PARTUUID
|
|
#sudo sed -i -r -E \
|
|
# "s@(.*root=PARTUUID=)[A-Za-z0-9-]+(.*)@\1${ISCSI_ROOT_PARTUUID}\2@" \
|
|
# ${NFS_DIR}/cmdline.txt
|
|
|
|
# Unmount the NFS Folder
|
|
#sudo umount ${NFS_DIR}
|
|
|
|
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
|
|
|
|
printf ISCSI UUID=
|
|
printf ISCSI PARTUUID=${ISCSI_ROOT_PARTUUID}
|
|
printf ISCSI TARGET IQN=${ISCSI_TARGET_IQN}
|
|
printf ISCSI INITIATOR IQN=${ISCSI_INITIATOR_IQN} |