Skip to content

Commit 9b78d3f

Browse files
committed
Handling NBD boot (Fixes #6)
1 parent ec14183 commit 9b78d3f

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ vmlinuz:
5454

5555
uInitrd: initrd.gz
5656
$(MAKE) uInitrd-local || $(MAKE) uInitrd-docker
57+
touch $@
5758

5859
uInitrd-local: initrd.gz
5960
mkimage $(MKIMAGE_OPTS) -d initrd.gz uInitrd
@@ -73,18 +74,24 @@ uInitrd-docker: initrd.gz
7374

7475

7576
tree/usr/bin/oc-metadata:
77+
mkdir -p $(shell dirname $@)
7678
wget https://raw.githubusercontent.com/online-labs/ocs-scripts/master/skeleton/usr/local/bin/oc-metadata -O $@
7779
chmod +x $@
7880

7981

82+
tree/usr/sbin/@xnbd-client.link: tree/usr/sbin/xnbd-client
83+
ln -sf $(<:tree%=%) $(@:%.link=%)
84+
touch $@
85+
86+
8087
tree/bin/sh: tree/bin/busybox
8188
cd tree && mkdir -p bin sbin etc proc sys newroot usr/bin usr/sbin
8289
ln -s busybox $@
8390

8491

85-
initrd.gz: $(addprefix tree/, $(DEPENDENCIES)) $(wildcard tree/*) tree/bin/sh tree/usr/bin/oc-metadata Makefile
92+
initrd.gz: $(addprefix tree/, $(DEPENDENCIES)) $(wildcard tree/*) tree/bin/sh tree/usr/bin/oc-metadata tree/usr/sbin/@xnbd-client.link Makefile
8693
find tree \( -name "*~" -or -name ".??*~" -or -name "#*#" -or -name ".#*" \) -delete
87-
cd tree && find . -print0 | cpio --null -ov --format=newc | gzip -9 > $(PWD)/$@
94+
cd tree && find . -print0 | cpio --null -o --format=newc | gzip -9 > $(PWD)/$@
8895

8996

9097
$(addprefix tree/, $(DEPENDENCIES)): dependencies/Dockerfile

tree/boot-nbd

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,60 @@
11
# -*- shell-script -*-
22

3+
4+
NBD_CLIENT=/usr/sbin/@xnbd-client
5+
6+
7+
setup_nbd_device() {
8+
local device="$1" # ie: 0
9+
local retries=900
10+
11+
12+
# Fetching metadatas
13+
local export_uri=$(oc-metadata VOLUMES_${device}_EXPORT_URI)
14+
[ "$export_uri" = "" ] && log_fatal_msg "Unable to load metadatas..."
15+
16+
local nbd_host=$(echo $export_uri | sed -n 's#nbd://\(.*\):.*$#\1#p')
17+
local nbd_port=$(echo $export_uri | sed -n 's#nbd://.*:\(.*\)$#\1#p')
18+
[ "$nbd_host" = "" -o "$nbd_port" = "" ] && log_fatal_msg "Parse error of $export_uri"
19+
20+
21+
# Checking if device is already attached
22+
$NBD_CLIENT -c /dev/nbd${device} >/dev/null 2>/dev/null
23+
if [ $? -eq 0 ]; then
24+
return
25+
fi
26+
27+
# Connecting the device
28+
$NBD_CLIENT --blocksize 4096 --retry=$retries $nbd_host $nbd_port /dev/nbd${device} 2>/dev/null
29+
30+
# Checking if device is already attached
31+
$NBD_CLIENT -c /dev/nbd${device} >/dev/null 2>/dev/null || \
32+
log_fatal_msg "Device connection failed."
33+
34+
# Fixing IO scheduler
35+
if grep -q '\[cfq\]' /sys/block/nbd${device}/queue/scheduler
36+
then
37+
echo deadline > /sys/block/nbd${device}/queue/scheduler
38+
fi
39+
}
40+
41+
342
mountroot() {
4-
echo "boot-nbd.mountroot(): TODO"
5-
# /scripts/local-top/ocs-nbd
6-
# mkdir -p /mnt/root
7-
# mount /dev/mapper/nbd0
8-
# echo 0x0100 > /proc/sys/kernel/real-root-dev
9-
# mount -o move /dev /mnt/dev
10-
# mount -o move /dev/pts /mnt/dev/pts
11-
# mount -o move /sys /mnt/sys
12-
# mount -o move /proc /mnt/proc
13-
# mount -o move /run /mnt/run
14-
# sync
15-
# echo "Let's play"
16-
# export PS1="# "
17-
# /bin/sh
18-
# exec switch_root /mnt/root /init
43+
log_begin_msg "Mounting NBD root"
44+
45+
# Attaching NBD
46+
setup_nbd_device 0 # nbd0
47+
48+
# Prepare switch_root
49+
mount /dev/nbd0 ${rootmnt}
50+
echo 0x0100 > /proc/sys/kernel/real-root-dev
51+
mount -o move /dev ${rootmnt}/dev
52+
mount -o move /dev/pts ${rootmnt}/dev/pts
53+
mount -o move /sys ${rootmnt}/sys
54+
mount -o move /proc ${rootmnt}/proc
55+
mount -o move /run ${rootmnt}/run
56+
sync
57+
#exec switch_root ${rootmnt}/root /init
58+
59+
log_end_msg
1960
}

0 commit comments

Comments
 (0)