Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (116 sloc) 4.458 kb
7a4bc6a @brain0 Add a new file init_functions that contains common functions used in …
brain0 authored
1 # This file contains common functions used in init and in hooks
2
3 msg () {
4 [ "${quiet}" != "y" ] && echo $@
5 }
6 err () {
7 echo "ERROR: $@"
8 }
1382531 @brain0 Add common poll_device function
brain0 authored
9
10 poll_device() {
09bbcaf @falconindy init_functions: refactor poll_device
falconindy authored
11 local device=$1 seconds=${2//[!0-9]}
12
13 [ -z "$seconds" ] && seconds=5
14
15 [ -b "$device" ] && return 0
16
17 if [ "$udevd_running" -eq 1 ]; then
18 msg "Waiting $seconds seconds for device $device ..."
19 while [ ! -b "$device" -a "$seconds" -gt 0 ]; do
ae65c7c @brain0 poll_device: do not wait for a device to appear if udevd is not running
brain0 authored
20 sleep 1
09bbcaf @falconindy init_functions: refactor poll_device
falconindy authored
21 seconds=$(( $seconds - 1 ))
ae65c7c @brain0 poll_device: do not wait for a device to appear if udevd is not running
brain0 authored
22 done
23 fi
09bbcaf @falconindy init_functions: refactor poll_device
falconindy authored
24
25 [ -b "$device" ]
1382531 @brain0 Add common poll_device function
brain0 authored
26 }
7504fd6 @brain0 Nicer shell prompt in the break/emergency shell
brain0 authored
27
28 launch_interactive_shell() {
3460ba7 @brain0 launch_interactive_shell: add "export" for PS1
brain0 authored
29 export PS1='[ramfs \W]\$ '
81e7742 @falconindy init: declare PATH, remove absolute paths
falconindy authored
30 [ "$1" = "--exec" ] && exec sh -i
31 sh -i
7504fd6 @brain0 Nicer shell prompt in the break/emergency shell
brain0 authored
32 }
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
33
d1264eb @brain0 Fix problems in parsing the kernel command line
brain0 authored
34 parse_cmdline() {
01b2f55 @brain0 Rewrite parse_cmdline (again)
brain0 authored
35 local w in_quotes lhs rhs
36 in_quotes=0
37 for w in $(cat /proc/cmdline); do
38 if [ ${in_quotes} -eq 0 ]; then
39 case "${w}" in
40 \#*) break ;; # ignore everything after a # in the commandline
41 # The kernel passes those to init on its own
42 [0123456Ss]) ;;
43 single) ;;
b655460 @falconindy init_functions: tighten up mount option gathering
falconindy authored
44 rw|ro) rwopt="$w" ;;
01b2f55 @brain0 Rewrite parse_cmdline (again)
brain0 authored
45 # only export stuff that does work with ash :)
6ed825e @falconindy init_functions: avoid setting var without LHS
falconindy authored
46 =*) ;;
e1538a0 @falconindy init_functions: replace grep/sed with PEs
falconindy authored
47 *=*) rhs=${w#*=}
48 lhs=${w%%=*}
49 lhs=${lhs//[-.]/_}
50 if [ '"' = "${rhs:0:1}" ]; then
51 if [ "${rhs:$((${#rhs}-1))}" = '"' ]; then
01b2f55 @brain0 Rewrite parse_cmdline (again)
brain0 authored
52 rhs="${rhs:1:$((${#rhs}-2))}"
53 else
54 rhs="${rhs:1}"
55 in_quotes=1
56 continue
57 fi
58 fi
e1538a0 @falconindy init_functions: replace grep/sed with PEs
falconindy authored
59 [ "$lhs" = "${lhs//[^0-9a-zA-Z]}" ] && [ "$lhs" = "${lhs#[0-9]}" ] && eval ${lhs}=\${rhs}
01b2f55 @brain0 Rewrite parse_cmdline (again)
brain0 authored
60 ;;
e1538a0 @falconindy init_functions: replace grep/sed with PEs
falconindy authored
61 *) lhs=${w//[-.]/_}
62 [ "$lhs" = "${lhs//[^0-9a-zA-Z]}" ] && [ "$lhs" = "${lhs#[0-9]}" ] && eval ${lhs}=y
01b2f55 @brain0 Rewrite parse_cmdline (again)
brain0 authored
63 ;;
64 esac
65 else
e1538a0 @falconindy init_functions: replace grep/sed with PEs
falconindy authored
66 if [ '"' = "${w:$((${#w}-1))}" ]; then
67 rhs="${rhs} ${w%\"}"
01b2f55 @brain0 Rewrite parse_cmdline (again)
brain0 authored
68 in_quotes=0
e1538a0 @falconindy init_functions: replace grep/sed with PEs
falconindy authored
69 [ "$lhs" = "${lhs//[^0-9a-zA-Z]}" ] && [ "$lhs" = "${lhs#[0-9]}" ] && eval ${lhs}=\${rhs}
01b2f55 @brain0 Rewrite parse_cmdline (again)
brain0 authored
70 else
71 rhs="${rhs} ${w}"
72 fi
73 fi
d1264eb @brain0 Fix problems in parsing the kernel command line
brain0 authored
74 done
75 }
76
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
77 default_mount_handler() {
acf51fb @falconindy init_functions: support {LABEL,UUID}= syntax
falconindy authored
78 # resolve tag name to block device
79 if [ "${root:0:5}" = 'UUID=' ] || [ "${root:0:6}" = 'LABEL=' ]; then
726ff1f @brain0 init_functions/default_mount_handler: use -l option for blkid to avoi…
brain0 authored
80 device=$(blkid -l -t "$root" -o device)
acf51fb @falconindy init_functions: support {LABEL,UUID}= syntax
falconindy authored
81 if [ -n "$device" ]; then
82 root=$device
83 fi
84 unset device
85 fi
86
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
87 if [ ${root:0:5} != "/dev/" ] || ! poll_device "${root}" ${rootdelay}; then
e7ad4fe @brain0 Cosmetic change: 'msg' function does not support '\n'
brain0 authored
88 msg "Root device '${root}' doesn't exist. Attempting to create it."
568e4aa @brain0 default_mount_hook: Use major and minor variables instead of a single…
brain0 authored
89 major=""
90 minor=""
92a9fa8 @brain0 default_mount_handler: Only try to manually create the root device if…
brain0 authored
91 if [ ${devtmpfs_mounted} -eq 0 -a ${root:0:5} = "/dev/" ]; then
5aa2f58 @falconindy init_functions: simplify mount case of root=/dev/*
falconindy authored
92 # It might be a block device (/dev/sda) -> /sys/class/block/sda/dev
93 if [ -e /sys/class/block/${root:5}/dev ]; then
94 IFS=':' read major minor < "/sys/class/block/${root:5}/dev"
95 break
96 fi
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
97 # It might be a major/minor pair (8:1)
e1538a0 @falconindy init_functions: replace grep/sed with PEs
falconindy authored
98 elif [ "$root" != "${root/:}" ]; then
568e4aa @brain0 default_mount_hook: Use major and minor variables instead of a single…
brain0 authored
99 major="$(echo ${root} | cut -d: -f1)"
100 minor="$(echo ${root} | cut -d: -f2)"
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
101 root="/dev/root"
102 # It might be major/minor encoded as a single hex-number (lilo-style) (801)
e1538a0 @falconindy init_functions: replace grep/sed with PEs
falconindy authored
103 elif [ ${#root} -le 4 -a ${#root} -gt 2 -a -z "${root//[0-9a-fA-F]}" ]; then
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
104 str_offset=$((${#root}-2))
5dc7684 Use $(( )) instead of printf "%d"
Benjamin Richter authored
105 major=$((0x${root:0:${str_offset}}))
106 minor=$((0x${root:${str_offset}}))
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
107 root="/dev/root"
108 fi
568e4aa @brain0 default_mount_hook: Use major and minor variables instead of a single…
brain0 authored
109 if [ -n "${major}" -a -n "${minor}" ]; then
110 msg "Creating root device ${root} with major ${major} and minor ${minor}."
111 mknod ${root} b ${major} ${minor}
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
112 else
113 err "Unable to determine major/minor number of root device '${root}'."
114 echo "You are being dropped to a recovery shell"
115 echo " Type 'exit' to try and continue booting"
116 launch_interactive_shell
117 msg "Trying to continue (this will most likely fail) ..."
118 fi
119 fi
c06a144 @falconindy init: use util-linux's /bin/mount
falconindy authored
120
121 if ! mount ${fstype:+-t $fstype} -o ${rwopt:-ro}${rootflags:+,$rootflags} "$root" "$1"; then
b655460 @falconindy init_functions: tighten up mount option gathering
falconindy authored
122 echo "You are now being dropped into an emergency shell."
123 launch_interactive_shell
124 msg "Trying to continue (this will most likely fail) ..."
2348d65 @brain0 Remove the special handling for the default mount procedure, move it …
brain0 authored
125 fi
126 }
8ac91b1 @falconindy use consistent vim modelines
falconindy authored
127
128 # vim: set ft=sh ts=4 sw=4 et:
Something went wrong with that request. Please try again.