aboutsummaryrefslogtreecommitdiff
path: root/init
blob: b733fde53c0833186bc6f5938ff43e9b1a7f642d (plain)
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
#!/bin/bash

# Init script for build2 buildos.
#
# Loosely based on the one that comes in Debian initrd.img (since we are
# using its kernel image as is).
#
trap "exit 1" ERR
set -o errtrace # Trap in functions.

# Note: diagnostics goes to stdout.
#
function info () { echo "$*"; }
function error () { info "$*"; exit 1; }

export PATH=/sbin:/usr/sbin:/bin:/usr/bin

# One would expect rootflags=size=1g to work but it doesn't (perhaps init
# is expected to interpret it)?
#
mount -o remount,size=1G /

mkdir -p /sys /proc
mount -t sysfs -o nodev,noexec,nosuid sysfs /sys
mount -t proc -o nodev,noexec,nosuid proc /proc

info "init starting up..."

mount -t devtmpfs -o nosuid,mode=0755 udev /dev
mkdir -p /dev/pts
mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts || true

mkdir -p /run
mount -t tmpfs -o "noexec,nosuid,size=10%,mode=0755" tmpfs /run

mkdir -p /tmp
mount -t tmpfs -o "nodev,nosuid,size=10%,mode=1777" tmpfs /tmp

# Start udev.
#
# Based on Debian initrd's init-top/udev. Note that we don't stop it at
# at the end of init.
#
info "starting udev..."

if [ -w /sys/kernel/uevent_helper ]; then
  echo >/sys/kernel/uevent_helper
fi

SYSTEMD_LOG_LEVEL=info /lib/systemd/systemd-udevd --daemon --resolve-names=never

udevadm trigger --action=add
udevadm settle || true

# Parse command line.
#
cmdline="$(cat /proc/cmdline)"
info "boot cmdline: $cmdline"

# Figure out network configuration and generate the corresponding
# /etc/network/interfaces.
#
info "starting network..."

# We are using udev's predictable interface names. The two character prefixes
# based on the type of interface:
#
#  en -- ethernet
#  sl -- serial line IP (slip)
#  wl -- wlan
#  ww -- wwan
#
eth_all="$(cd /sys/class/net && ls -d en?*)"

if [ -z "$eth_all" ]; then
  info "no ethernet interfaces found among:"
  ip link show
fi

eth=
eth_up=
for s in 1 2 4 8; do

  # Try to bring them all up and find the one that has carrier.
  #
  for i in $eth_all; do
    ip link set "$i" up || true
  done

  sleep "$s"

  for i in $eth_all; do
    if [ "$(cat "/sys/class/net/$i/carrier")" -eq "1" ]; then
      info "detected carrier on $i"
      eth_up+=" $i"
    fi
  done

  # Bring them all down.
  #
  for i in $eth_all; do
    ip link set "$i" down || true
  done

  # If we didn't find anything, try to wait for carrier longer.
  #
  if [ -z "$eth_up" ]; then
    continue
  fi

  # If we end up with several interfaces we simply unleash dhcp on all of
  # them and use the first that gets configured.
  #
  # Note also that it's possible the interface that we want is not yet ready
  # in which case we will try to wait for carrier a bit longer.
  #
  for i in $eth_up; do
    if dhclient -v "$i"; then
      eth="$i"
      break
    fi
  done

  if [ -n "$eth" ]; then
    break
  fi
done

if [ -z "$eth_up" ]; then
  error "no ethernet interfaces with carrier among:"
  ip link show
fi

if [ -z "$eth" ]; then
  error "no ethernet interfaces with DHCP among:"
  ip link show
fi

mac="$(cat "/sys/class/net/$eth/address")"

info "configured $eth ($mac)"

# Set the hostname.
#
hname="$(hostname)"

if [ "$hname" = "(none)" ]; then
  hname="build-$(sed -e 's/://g' <<<"$mac")"
  hostname "$hname"
fi

echo "$hname" >/etc/hostname

info "hostname $hname"

# --machine-id
#
#exec /lib/systemd/systemd #</dev/console >/dev/console 2>&1

exec /bin/bash