Skip to content

Commit eb1613f

Browse files
committed
Parsing cmdline and importing boot-type files (#6, #7)
1 parent 394a24f commit eb1613f

File tree

5 files changed

+92
-32
lines changed

5 files changed

+92
-32
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
S3_TARGET ?= s3://$(shell whoami)/
22
BUSYBOX_URL ?= http://launchpadlibrarian.net/181784411/busybox-static_1.22.0-8ubuntu1_armhf.deb
33
KERNEL_URL ?= http://ports.ubuntu.com/ubuntu-ports/dists/lucid/main/installer-armel/current/images/versatile/netboot/vmlinuz
4-
CMDLINE ?= 'root=/dev/ram'
4+
CMDLINE ?= ip=dhcp root=/dev/nbd0 nbd.max_parts=8 boot=local nometadata
55

66

77
.PHONY: publish_on_s3 qemu dist dist_do dist_teardown all
@@ -16,7 +16,7 @@ qemu: vmlinuz initrd.gz
1616
-kernel ./vmlinuz \
1717
-initrd ./initrd.gz \
1818
-m 256 \
19-
-append $(CMDLINE)
19+
-append "$(CMDLINE)"
2020

2121
publish_on_s3: uInitrd initrd.gz
2222
for file in $<; do \

tree/boot-nbd

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- shell-script -*-
2+
3+
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
19+
}

tree/boot-rescue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- shell-script -*-
2+
3+
mountroot() {
4+
echo "boot-rescue.mountroot(): TODO"
5+
}

tree/functions

+19-9
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@ signal_state() {
3535

3636
retries=0
3737
while [ "${retries}" -lt "${RETRIES}" ]; do
38-
log_begin_msg "Signaling the server is ${state} to the control plane"
39-
http_patch "${METADATA_IP}" "${METADATA_PORT}" "{\"state_detail\": \"${state}\"}"
40-
if [ ${?} -eq 0 ]; then
38+
log_begin_msg "Signaling the server is ${state} to the control plane"
39+
has_opt nometadata || \
40+
http_patch "${METADATA_IP}" "${METADATA_PORT}" "{\"state_detail\": \"${state}\"}"
41+
if [ ${?} -eq 0 ]; then
4142
log_success_msg "control plane is aware that server is ${state}"
4243
log_end_msg
4344
return
44-
else
45+
else
4546
log_failure_msg "unable to signal server is ${state}"
4647
log_end_msg
47-
fi
48-
48+
fi
49+
4950
retries=$((${retries}+1))
50-
test ${retries} -eq ${RETRIES} || sleep ${SLEEP_BETWEEN_RETRIES}
51+
test ${retries} -eq ${RETRIES} || sleep ${SLEEP_BETWEEN_RETRIES}
5152
done
5253

5354
log_failure_msg "Unsable to signal state=$state after ${RETRIES} attempts, continuing boot..."
@@ -70,7 +71,16 @@ content-type: application/json
7071
Content-Length: ${content_length}
7172
7273
${content}"
73-
74+
7475
( echo -n "${patch_body}" && sleep 3 ) \
75-
| nc "${metadata_ip}" "${metadata_port}" | grep '200 OK'
76+
| nc "${metadata_ip}" "${metadata_port}" | grep '200 OK'
77+
}
78+
79+
# Function for parsing command line options with "=" in them
80+
get_opt() {
81+
cat /proc/cmdline | tr " " "\n" | cut -d "=" -f 2
82+
}
83+
84+
has_opt() {
85+
cat /proc/cmdline | tr " " "\n" | grep "^$1\$" >/dev/null
7686
}

tree/init

+47-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#!/bin/sh
22

3-
set -x
4-
5-
63
# Create all the symlinks to /bin/busybox
74
/bin/busybox --install -s
85

9-
106
# Mount things needed by this script
117
[ -d /dev ] || mkdir -m 0755 /dev
128
[ -d /sys ] || mkdir /sys
@@ -26,27 +22,57 @@ mount -t devpts -o noexec,nosuid,gid=5,mode=0620 none /dev/pts
2622
signal_state kernel-started
2723

2824

29-
# NBD boot
30-
# . /scripts/functions
31-
# /scripts/local-top/ocs-nbd
32-
# mkdir -p /mnt/root
33-
# mount /dev/mapper/nbd0
34-
# echo 0x0100 > /proc/sys/kernel/real-root-dev
35-
# mount -o move /dev /mnt/dev
36-
# mount -o move /dev/pts /mnt/dev/pts
37-
# mount -o move /sys /mnt/sys
38-
# mount -o move /proc /mnt/proc
39-
# mount -o move /run /mnt/run
40-
# sync
41-
# echo "Let's play"
42-
# export PS1="# "
43-
# /bin/sh
44-
# exec switch_root /mnt/root /init
25+
# Defaults
26+
init="/sbin/init"
27+
root="/dev/nbd0"
28+
boot="local"
29+
30+
# Process command line options
31+
for i in $(cat /proc/cmdline); do
32+
case "${i}" in
33+
root\=*)
34+
root=$(get_opt "${i}")
35+
;;
36+
init\=*)
37+
init=$(get_opt "${i}")
38+
;;
39+
boot\=*)
40+
init=$(get_opt "${i}")
41+
;;
42+
esac
43+
done
44+
45+
46+
# Import boot-type functions
47+
case "${boot}" in
48+
local)
49+
. ./boot-nbd
50+
;;
51+
rescue)
52+
. ./boot-rescue
53+
;;
54+
esac
55+
56+
57+
# Mountroot
58+
mountroot
4559

4660

4761
# Signal to the console that the server is booted
4862
signal_state booted
4963

5064

51-
# Debug fallback
65+
# Check if $init exists and is executable
66+
if [[ -x "/newroot/${init}" ]] ; then
67+
# Unmount all other mounts so that the ram used by
68+
# the initramfs can be cleared after switch_root
69+
umount /sys /proc
70+
71+
# Switch to the new root and execute init
72+
exec switch_root /newroot "${init}"
73+
fi
74+
75+
76+
# This will only be run if the exec above failed
77+
echo "Failed to switch_root, dropping to a shell"
5278
exec /bin/sh

0 commit comments

Comments
 (0)