-
Notifications
You must be signed in to change notification settings - Fork 1
/
make-iscsi-rootfs
executable file
·222 lines (170 loc) · 5.36 KB
/
make-iscsi-rootfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/sh
set -eu
PROGNAME=$(basename $0)
BASEDIR=$(cd $(dirname $0); pwd)
COPY_BOOT=n
ISCSI_PORTAL=
ISCSI_TARGET=
ISCSI_INITIATOR=
help() {
cat <<EOF >&2
Usage: $PROGNAME [options]
Help Options:
-h, --help
Required Options:
-b, --boot
Copy /boot into iSCSI storage
-p, --portal <PORTAL-IPADDR>
Target IP address
-t, --target <TARGET-IQN>
Target IQN
-i, --initiator <INITIATOR-IQN>
Initiator IQN
Description:
$PROGNAME creates rootfs in a specified iSCSI storage in accordance with steps
described in the following page:
Net boot (PXE + iSCSI) with a RaspberryPi 3 - darknao's stuff
https://stuff.drkn.ninja/post/2016/11/08/Net-boot-(PXE-iSCSI)-with-a-RaspberryPi-3
The open-iscsi package will be installed onto the local machine and the
open-iscsi systemd service is started and enabled. Before starting the
service, The value of InitiatorName in /etc/iscsi/initiatorname.iscsi is
replaced with the value of the --initiator option.
initrd.img-\$(uname -r) containing open-iscsi modules is created in /boot
in the local filesystem. It's necessary to mount the iSCSI storage in boot.
The iSCSI storage is formatted with \`mkfs.ext4 -F\`. And then files in the
local filesystem are copied into it.
Examples:
For MMC boot:
\$ $PROGNAME -p 10.1.2.3 -t iqn.2019-03.example.nas:iscsi:target \\
-i iqn.2019-03.example.rpi:iscsi:initiator
For PXE boot:
\$ $PROGNAME -b -p 10.1.2.3 -t iqn.2019-03.example.nas:iscsi:target \\
-i iqn.2019-03.example.rpi:iscsi:initiator
Bugs:
open-iscsi in Raspbian/Stretch has a bug at 2019-03-17, and it has not
fixed, yet:
https://bugs.launchpad.net/ubuntu/+source/open-iscsi/+bug/1566468/
Workaround:
\$ sudo sed -i s/^ib_iser/#ib_iser/ /lib/modules-load.d/open-iscsi.conf
EOF
exit 0
}
confirm() {
read -p "$1" ANSWER
if [ "$ANSWER" != y ]; then
exit 0
fi
}
error() {
echo "$1" >&2
exit 1
}
while [ $# -gt 0 ]
do
case "$1" in
'-h' | '--help')
help
;;
'-b' | '--boot')
COPY_BOOT=y
shift
;;
'-p' | '--portal')
ISCSI_PORTAL="$2"
shift 2
;;
'-t' | '--target')
ISCSI_TARGET="$2"
shift 2
;;
'-i' | '--initiator')
ISCSI_INITIATOR="$2"
shift 2
;;
esac
done
if [ -z "$ISCSI_PORTAL" ]; then
error "--portal is required"
fi
if [ -z "$ISCSI_TARGET" ]; then
error "--target is required"
fi
if [ -z "$ISCSI_INITIATOR" ]; then
error "--initiator is required"
fi
HOSTNAME=$(hostname)
KERNEL_VERION=$(uname -r)
cat <<EOF
HOSTNAME : $HOSTNAME
KERNEL_VERSION : $KERNEL_VERION
ISCSI_PORTAL : $ISCSI_PORTAL
ISCSI_TARGET : $ISCSI_TARGET
ISCSI_INITIATOR: $ISCSI_INITIATOR
EOF
echo "Installing open-iscsi..."
sudo mkdir -p /etc/iscsi
echo "InitiatorName=$ISCSI_INITIATOR" | sudo tee /etc/iscsi/initiatorname.iscsi
sudo apt-get update -qq
sudo apt-get install -y -qq --no-install-recommends open-iscsi initramfs-tools
# Disable to load ib_iser.
# https://bugs.launchpad.net/ubuntu/+source/open-iscsi/+bug/1566468/comments/14
sudo sed -i s/^ib_iser/#ib_iser/ /lib/modules-load.d/open-iscsi.conf
sudo systemctl restart open-iscsi || error "Failed to start open-iscsi service"
sleep 1
echo "Conneting to $ISCSI_TARGET..."
sudo iscsiadm -m discovery -p $ISCSI_PORTAL -t sendtargets
sudo iscsiadm -m node -p $ISCSI_PORTAL -T $ISCSI_TARGET -l || \
error "Failed to connect to $ISCSI_TARGET"
sleep 1
DEV=$(ls /dev/disk/by-path/ip-$ISCSI_PORTAL*$ISCSI_TARGET-lun-0 2>/dev/null)
test -n "$DEV" || error "Failed to attach iSCSI storage"
echo "Updating initramfs..."
sudo touch /etc/iscsi/iscsi.initramfs
sudo rm -f /boot/initrd.img-*
sudo update-initramfs -v -k $KERNEL_VERION -c
echo "Creating a GPT partition on $DEV..."
sudo parted -s $DEV mklabel gpt # will remove all partitions on the disk
sudo parted -s $DEV mkpart root 0% 100%
# Wait 1 second for the partition to be accessible
sleep 1
PART=$DEV-part1
echo "Formatting $PART with ext4..."
sudo mkfs.ext4 -F $PART
UUID=$(sudo blkid -o value -s UUID $PART)
echo "Copying files..."
sudo mount $PART /mnt
DIRS=$(/bin/ls -I dev -I lost+found -I media -I mnt -I proc -I run -I sys -I tmp /)
sudo cp -afv $DIRS /mnt/
sudo mkdir -p /mnt/dev /mnt/media /mnt/mnt /mnt/proc /mnt/run /mnt/sys /mnt/tmp
if [ "$COPY_BOOT" = y ]; then
sudo cp -afv /boot /mnt/
else
sudo mkdir -p /mnt/boot
fi
sudo umount $PART
sudo iscsiadm -m node -u
if which raspi-config >/dev/null; then
PXEBOOT_SHARED_FOLDER=$(cat /proc/cpuinfo | grep Serial | rev | cut -c 1-8 | rev)
else
PXEBOOT_SHARED_FOLDER="path/to/boot"
fi
cat <<EOF
Done.
You need to update /etc/fstab in the iSCSI LUN by hand:
sudo iscsiadm -m node -p $ISCSI_PORTAL -T $ISCSI_TARGET -l
sudo mount /dev/disk/by-uuid/$UUID /mnt
# Update the device field for the root partition with
# UUID=$UUID
sudo nano /mnt/etc/fstab
sudo umount /mnt
sudo iscsiadm -m node -u
You also need to add the following kernel parameters in order to mount the iSCSI
LUN at boot time:
ISCSI_TARGET_IP=$ISCSI_PORTAL
ISCSI_TARGET_NAME=$ISCSI_TARGET
ISCSI_INITIATOR=$ISCSI_INITIATOR
Copy files in /boot to an appropriate location if you like to use PXE boot:
sudo mount.cifs //nas/pxeboot/$PXEBOOT_SHARED_FOLDER /mnt
sudo cp -afv /boot/* /mnt/
sudo umount /mnt
EOF