summaryrefslogtreecommitdiffstats
path: root/initrd/initrd-stuff/init
blob: f3317a07d616f52c6225da065043e439eb78952b (plain) (blame)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# functions common for all distros
. /etc/functions || ( echo -e "The functions file contains a lot of script \
functionality. Without this\ninit script will not run." && exit 1 )
# configuration settings (several file and directory variables)
. /etc/sysconfig/config || error "  The distribution \
specific configuration file could not be found"

date
export PATH=/bin:/sbin:/usr/bin/:/usr/sbin
export date="07-01-2006"
exec < /dev/console > /dev/console 2>&1

export KERNEL="@@@KERNVER@@@"
export NWMODULES="@@@NWMODULES@@@"
export DISTRO="@@@DISTRO@@@"

DEVDIR="/dev"
mount -n -t tmpfs -o 'size=25%,mode=0755' initramfsdevs ${DEVDIR}
mkdir -p ${DEVDIR}/pts
mkdir -p ${DEVDIR}/shm
mkdir -p ${DEVDIR}/.udevdb

[ ! -f /proc/cpuinfo ] && mount -n -tproc proc /proc
echo 256 > /proc/sys/kernel/real-root-dev
[ ! -d /sys/class ] && mount -n -tsysfs sysfs /sys

# start device auto discovery (just the first case is tested yet)
if [ -x /bin/udev -a -x /bin/udevstart ] ; then
  echo "Starting udev"
  echo "/bin/udev" > /proc/sys/kernel/hotplug
  /bin/udevstart &
elif [ -x /bin/hotplug ] ; then
  echo "Enabling hotplug"
  echo "/bin/hotplug" > /proc/sys/kernel/hotplug
elif [ -s /dev.tgz ] ; then
  echo "Using traditional static /dev entries"
  tar -xpzf /dev.tgz -C / &
fi
# read kernel commandline
TMPFSSIZE="50%"
export DEBUGLEVEL=0
echo "noldap" > /tmp/ldap-done
echo "nodhcp" > /tmp/dhcp-done
read KCMDLINE < /proc/cmdline
export KCMDLINE
for opts in ${KCMDLINE} ; do
  case ${opts} in
    # localization
    country=*)
      COUNTRY=${opts#country=}
      echo -e "\n# localization information gotten via kernel command line \
in $0\ncountry=\"${COUNTRY}\"" >> /etc/machine-setup
    ;;
    # single token for debugging
    debug)
      DEBUGLEVEL=1;;
    # debug level
    debug=*)
      DEBUGLEVEL=${opts#debug=};;
    # if configuration should be gathered by dhcp client
    dhcp)
      DHCP="yes"
      rm /tmp/dhcp-done;;
    # if ldap configuration should be triggered
    ldap)
      LDAP="yes"
      rm /tmp/ldap-done;;
    # if ld.so.cache should not be generated
    noldsc)
      NOLDSC=yes;;
    # if unionfs should be used
    unionfs*)
      UNIONFS=1;;
    # dnbd server:port 
    dnbdroot=*)
      DNBDOPT=${opts#dnbdroot=};;
    # nbd server:port,filesystem (filesystem is optional)
    nbdroot=*)
      NBDOPT=${opts#nbdroot=};;
    # ip configuration client-ip:server-ip:gateway:netmask
    ip=*)
      IPINFO=${opts#ip=};;
    # nfs server and path
    nfsroot=*)
      NFSROOT=${opts#nfsroot=}
      echo -e "\n# nfs root information gotten via kernel command line \ in $0\nnfsroot=\"${NFSROOT}\"" >> /etc/machine-setup
    ;;
    # size of tempfs if not max. 50% of RAM should be used
    tmpfssize=*)
      TMPFSSIZE=${opts#tmpfssize=};;
    # vendor code identifier for dhcp requests
    vci=*|VCI=*)
      VCI=${opts#VCI=};;
  esac
done
# check if at least one type of IP configuration is availabe
if [ -z "$DHCP" -a -z "$LDAP" -a -z "$IPINFO" ] ; then
  if strinfile "clientip" /etc/machine-setup ; then
    # IP configuration seems to be present in machine-setup file
    . /etc/machine-setup
    IPINFO="ip=$clientip:$serverip:$gateway:$subnet_mask"
  else
    error "  Unable to setup at \
least basic functionality, because no IP configuration\n  available. You \
might pass that information via kernel command line\n  through setting of \
'ipappend 1' in pxelinux.cfg/* or just enable dhcp\n  or ldap. They are \
enabled via tokens ('ldap' or 'dhcp') in kernel\n  command line."
  fi
fi
#[ -z "$DHCP" -a -n "$LDAP" -a -z "$IPINFO" ] && error "  Unable to setup at \
#might pass that information via kernel command line\n  through setting of \
#'ipappend 1' in pxelinux.cfg/* or just enable dhcp\n  or ldap. They are \
#enabled via tokens ('ldap' or 'dhcp') in kernel\n  command line."
#  fi
#fi
[ -z "$DHCP" -a -n "$LDAP" -a -z "$IPINFO" ] && error "  Unable to setup at \
least basic functionality, because no IP configuration\n  available. Please \
beware - ldap works only in combination of either\n  ipappend or similar or \
with dhcp!"
# set debug level and logfile
echo "Setting debuglevel to ${DEBUGLEVEL}"
export MODPRV=" "
export LOGFILE
if [ "${DEBUGLEVEL}" -gt 0 ] ; then
  # if LOGFILE should be used within initial ramdisk add '/mnt' in fron
  # of the variable
  LOGFILE="/var/log/dxs-boot.log"
  [ "${DEBUGLEVEL}" -gt 2 ] && { 
    set -x
    # modprobe should be verbose
    MODPRV="-v"; }
else
  # switch off most of kernel debug output
  echo "0 0 0 0" >/proc/sys/kernel/printk
  # modprobe should stay quiet
  MODPRV="-q"
  LOGFILE="/dev/null"
fi
# load network adaptor modules
modprobe ${MODPRV} -a ${NWMODULES} || error "  Failed to load the network \
adaptor modules defined via mkdxsinitrd\n  run. Please rerun and list the \
appropriate modules (without .ko)."
# set up loopback networking (ipsetup - function defined in /etc/functions)
ipsetup 127.0.0.1 255.0.0.0 0.0.0.0 127.255.255.255 lo
# analyze ip information from the kernel command line and put parts
# of it into several variables
if [ -n "$IPINFO" ] ; then
  IP=`echo $IPINFO|sed -e "s,:.*,,"`
  SERVER=`echo $IPINFO|sed -e "s;[0-9\.]\{3,\}:;;" -e "s,:.*,,"`
  GW=`echo $IPINFO|sed -e "s;[0-9\.]\{3,\}:[0-9\.]\{3,\}:;;" -e "s,:.*,,"`
  # remove last colon after netmask if it was left there ...
  IPINFO=`echo $IPINFO|sed -e "s,:$,,"`
  NM=`echo $IPINFO|sed -e "s,.*:,,"`
  echo -e "# ip configuration written by $0 script\nclientip=$IP\n\
sub_netmask=$NM\ngateway=$GW\nserverip=$SERVER" \
    >>/etc/machine-setup
  # set static ip address via function ipsetup
  ipsetup $IP $NM $GW 255.255.255.255 eth0
else
  noipyet="yes"
fi
# get configuration data via dhcp or ldap if available (in background)
[ -n "$DHCP" ] && rundhcp ${VCI} &
if [ -n "$noipyet" ] ; then
  waitfor /tmp/dhcp-done 20000
  . /etc/machine-setup
  ipsetup $clientip $subnet_mask $gateway $broadcast_address eth0
fi
[ -n "$LDAP" ] && ldapconf &

# if root filesystem should be imported via traditional network block device 
if [ -n "${NBDOPT}" ] ; then
  modprobe ${MODPRV} nbd || error "  Failed to load module nbd.ko. It is \
needed if you intend to use\n  network block device (NBD) for the client \
as root filesystem."
  nbdhost=${NBDOPT%:*}
  nbdopt=${NBDOPT#*:}
  nbdport=${NBDOPT%,*}
  nbdfs=${NBDOPT#*,}
  echo "Diskless client using nbd server $nbdhost:$nbdport,$nbdfs"
  nbd-client $nbdhost $nbdport /dev/nbd0
  if [ -z "$nbdfs" ]; then 
    RFST=ext2; 
  else 
    RFST=$nbdfs;
    modprobe ${MODPRV} ${RFST}
  fi
fi

# if root filesystem should be imported via udp based distributed network
# block device
if [ -n "${DNBDOPT}" ] ; then
  modprobe ${MODPRV} dnbd || echo "failed to load dnbd!"

fi

# if no type of root filesystem is passed via kernel command line try
# information gathered from dhcp process
if [ -z "${DNBDOPT}" -a -z "${NBDOPT}" -a -z "${NFSROOT}" ] ; then
  # information has to be read from dhcp configuration
  if [ -z "$noipyet" ] ; then
    waitfor /tmp/dhcp-done 20000
  fi
  waitfor /tmp/ldap-done 20000
  . /etc/machine-setup
  NFSROOT=${serverip}:${root_path}
fi

# call function for nfs mounts
if [ -z "${DNBDOPT}" -a -z "${NBDOPT}" ] ; then
  nfsmnt ${NFSROOT} ${MODPRV}
fi
# Bruchstücke?? Was hat das hier verloren?
#  You might want to check the list of mounted filesystems and if /mnt is\n\
#  not empty run 'ls /mnt/lib/modules' to check."
# start hardware configuration as background process
hwautocfg &

# try to use unionfs for rw access if available
RWDIR=/dev/shm
if [ -n "${UNIONFS}" ] ; then
  modprobe ${MODPRV} unionfs || { 
    error "  Loading of UnionFS failed - Either module is not present or \
module\n  does not match the running kernel. If you do not want to see this \
\n  message remove the token 'unionfs' from kernel command line." nonfatal 
    UNIONFS=""; }
fi

if [ -n "${UNIONFS}" ] ; then
  echo -n "Using UnionFS for rw access"
  mkdir -p ${RWDIR}/union ${RWDIR}/uniontmp
  mount -n -t tmpfs none ${RWDIR}/uniontmp
  mount -n --move /mnt /root
  mount -n -t unionfs -o dirs=${RWDIR}/uniontmp=rw:/root=ro none /mnt
  mkdir -p /mnt/uniontmp
  mount -n --move ${RWDIR}/uniontmp /mnt/uniontmp
  [ -z "${NOLDSC}" ] && ldconfig /etc/ld.so.cache &
else
  echo -n "Using bind mounts to ramdisk for rw access"
  mount -n -t tmpfs -o size=${TMPFSSIZE} ramfs ${RWDIR}
  for path in ${BINDMPTS} ; do
    mkdir -p ${RWDIR}/${path} >/dev/null 2>&1
    mount -n --bind ${RWDIR}/${path} /mnt/${path}
  done
  [ -z "${NOLDSC}" ] && ldconfig /tmp/ld.so.cache &
  for path in ${RODIRSINRW}; do
    if [ -d /mnt/${path} ] ; then
      LIST=${path}" "$LIST
      # exclude them from etc copy process too
      echo ${path}|sed -e "s,/root/,," >>/tmp/etc.exclude
      mkdir -p /root/${path} >/dev/null 2>&1
      #[ ${DEBUGLEVEL} -gt 0 ] && |      #echo " ${path} (without /root path component)" >>$LOGFILE
      mount -n --bind /mnt/${path} /root/${path} >/dev/null 2>&1
    fi
  done
  for path in /etc/${SYSCONFDIR} ${DIRINBINDMNT} ${LIST}; do
    mkdir -p ${RWDIR}/${path}
  done
  testmkd ${RWDIR}/var/tmp
  chmod a+rwxt ${RWDIR}/var/tmp >/dev/null 2>&1

  echo -e "${DISETCEXCL} @@@COMETCEXCL@@@" >>/tmp/etc.exclude
  # if ld.so.cache is to be generated then do not copy the file
  [ -z "${NOLDSC}" ] && echo -e "ld.so.cache*" >>/tmp/etc.exclude
  # for tar exclude lists might be used, more difficult for cp
  cd /mnt
  tar -X /tmp/etc.exclude -cp etc/* | \
    tar -xp -C ${RWDIR}>/dev/null ;
  cd /
  mount -n --bind ${RWDIR}/etc /mnt/etc
  mount -n --bind ${RWDIR}/var /mnt/var
  # get the "covered" mounts back into filesystem structure
  for i in ${LIST}; do
    #rm -rf /* >/dev/null 2>&1
    mount -n --move /root/$i /mnt/$i #>>$LOGFILE 2>&1
  done
  rm -rf /root/* >/dev/null 2>&1
  # run some specific stuff !?
fi
echo "fs complete" >/tmp/fscmpl

# write debug file information after filesystem setup completed
echo -e "# /etc${SYSCONFDIR}/logfile - file created by $0\n#\n# logfile for linux diskless client specific debugging output\nLOGFILE=\"$LOGFILE\"\n#\n# debug level\nDEBUGLEVEL=\"$DEBUGLEVEL\"" > /mnt/etc/${SYSCONFDIR}/logfile || \
  error "Unable to create the logfile configuration in /etc/${SYSCONFDIR}. \
That\n  might indicate some severe error."

# run distribution independent and dependent configuration of files and 
# services
servconfig &

for path in @@@COMDIRINDXS@@@ ${DISDIRINDXS} ${DIRINBINDMNT} ; do
  testmkd /mnt/${path}
done
for i in /var/run/utmp /var/log/wtmp /var/log/lastlog /etc/mtab ; do
  echo -n > /mnt$i
done
chmod a+rwxt /mnt/tmp /mnt/tmp/scratch 2>/dev/null &
chown 65534:0 /mnt/var/lib/nobody &
echo -e "# /etc/fstab - file generated by\n#\t$0:\n#\tDirk von \
Suchodoletz, dirk@goe.net\n\nrootfs\t\t/\t\trootfs\t\tro\t\t 0 0\n\
proc\t\t/proc\t\tproc\t\tdefaults\t 0 0\ninitramdevs\t/dev\t\ttmpfs\
\t\trw\t\t 0 0\ndevpts\t\t/dev/pts\tdevpts\t\tmode=0620,gid=5\t 0 0\n\
usbfs\t\t/proc/bus/usb\tusbfs\t\tnoauto\t\t 0 0">/mnt/etc/fstab || \
  error "Failed to create /etc/fstab in the clients root filesystem. The\n\
  noexistence of the file might produce some unexpected behaviour of\n\
  mount commands."
date
# copy library cache if generated
if [ -z "${NOLDSC}" ] ; then
  if waitfor /tmp/ldcfg 50000 ; then
    test -s /mnt/tmp/ld.so.cache && {
      cp /mnt/tmp/ld.so.cache /mnt/etc/ld.so.cache
      rm /mnt/tmp/ld.so.cache; }
  else 
    error "  For some reason the generation of \
ld.so.cache did not finish in time."
  fi
else
  [ "${DEBUGLEVEL}" -gt 0 ] && echo -e "You decided not to recreate \
/etc/ld.so.cache file. That might cause errors\nif libraries are installed \
after this file was created on server." >> /mnt/${LOGFILE}
fi
# save machine configuration
cp /etc/machine-setup /mnt/etc
waitfor /tmp/hwcfg 20000 || error "  For some reason the hardware autoconfig \
of this client did not finish in\n  time. You might check the process list \
and list the modules loaded until\n  now."

date
#ash
# IP configuration is made and should not be updated automatically
killall -9 dhcpcd dhclient pump 2>/dev/null
# unmount the bind mounted modules directory
umount -n /lib/modules/${KERNEL} || error "Unmount of the kernel \
modules directory failed for some reason."
# reset hotplug
echo "/sbin/hotplug" > /proc/sys/kernel/hotplug
echo ""
test -f /mnt/etc/inittab || error "  The file /etc/inittab does not exist or \
is no regular one. It is needed\n  for the bootup procedure to follow."
umount -n /sys || error "  Unmount of the kernel sys directory \
failed for some\nreason.  You will get some error messages that some files \
could not be\n  removed." nonfatal
mount -n --move /dev /mnt/dev
cd /mnt
umount -n /proc
[ -n "${UNIONFS}" ] && mount -n --move /root /mnt/mnt 
pivot_root . mnt || error "  Could not execute pivot_root due to missing \
command or wrong\n  parameters given."
exec `which chroot` . sh -c 'rm -rf mnt/*; exec /sbin/init' \
  <dev/console >dev/console 2>&1