summaryrefslogtreecommitdiff
path: root/src/initrd/common.sh
blob: 070ecba5e3b9ca3929d1eeaf5bae069767575cc7 (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
#!/bin/sh
REQUIRED_MB=250 # minimum megabytes available to offer install
MENUFIFO=/menu.fifo
DEBUG=y
SAMIZDAT_LOG_DIR=/run/initramfs/samizdat/log

debug_log()
{
  if [ -n "$DEBUG" ]; then
    if [ -n "$1" ]; then
      DEBUG_LOG=$SAMIZDAT_LOG_DIR/"$1".$$.log
    else
      DEBUG_LOG=$SAMIZDAT_LOG_DIR/$(basename $0).$$.log
    fi
    mkdir -p $SAMIZDAT_LOG_DIR
    exec >>$DEBUG_LOG 2>&1
    set -x
  fi
}

addmenu()
{
  cat <<END >>$MENUFIFO # mind the tabs
setItem "$1" "dummy" "$2" "$3"
END
}

menutitle()
{
  printf 'setTitle "%s"\n' "$1" >>$MENUFIFO
  printf 'setWelcomeText "%s"\n' "$2" >>$MENUFIFO
}

force_grok_block()
{
  udevadm trigger --subsystem-match=block --action=add
}

samizdat_install_udev_rules()
{
    local target=/etc/udev/rules.d/z00_blockdev_mountroot.rules
    [ -e "$target" ] && return
    mkdir -p /etc/udev/rules.d
    echo 'ACTION=="add", SUBSYSTEM=="block", RUN+="/bin/grok-block $env{DEVNAME}"' \
         > "$target"

    # 'udevadm trigger --action=add' does not work here; need to restard udevd
    # first. not sure why
    samizdat_restart_udev

    udevadm trigger -s block --action add
}

bootmenu()
{
  local do_trigger="$1" no_panic="$2"
  OpenVT -f -c 7 -- dynmenu "$MENUFIFO" &&
  chvt 7 &&
  menutitle 'Samizdat\n\nAs the Internet develops there are\ntransitions in the management arrangements.\nThe time has come to take\na small step in one of those transitions.' 'Choose an installation target.'
# menutitle 'Samizdat\nfreedom from surveillance\nno trusted authorities' 'Choose an installation target.'
  addmenu "ramdisk" "[ Boot to RAM without installing anything ]" "menu-select boot-ram"
  if [ $? != 0 -a ! "$no_panic" ]; then
    panic "error loading boot menu!  the system won't be usable :("
  fi

  if [ "$do_trigger" ]; then
    force_grok_block
  fi
}

netbooting()
{
    [ "$nbdroot" ]
}

xtrace()
{
  case "$-" in
  *x*) "$@" ;;
  *) set -x; "$@"; set +x ;;
  esac
}

sleepcmd() {
  local t=$1
  shift
  echo "about to run '$*' (in $t)"
  sleep $t
  "$@"
}

sleep_forever_verbose() {
  sleep 4294967295 &
  local sleep=$!
  warn "sleeping until you kill $sleep..."
  wait $sleep
}

warn() { [ -z "$warnings" ] || echo "$@" >&2; }

panic()
{
  set +x
  exec </dev/tty1 >/dev/tty1 2>&1
  reset
  echo "[p$$] initramfs /init: fatal error: $@"
  echo "[p$$] will now exec emergency shell"
  export PS1="[p$$ \\w]# "
  chvt 1
  exec /bin/sh -i
}

bootwait()
{
  mkdir -p /bootwait
  local i=$#; while [ $i -gt 0 ]; do
    i=$((i-1))
    local f="$1"; shift; set -- "$@" "/bootwait/$f" 
  done
  wait_for_files "$@"
}

bootdone()
{
  mkdir -p /bootwait
  local i=$#; while [ $i -gt 0 ]; do
    i=$((i-1))
    local f="$1"; shift; set -- "$@" "/bootwait/$f" 
  done
  touch "$@"
}

my_openvt()
{
  /bin/openvt -c "$@"
}

# This runs before way before NTP and on a LiveCD we have no
# reason to trust the system clock.
gpg2_nobatch() { GPG_TTY=$(tty) command gpg2 --ignore-time-conflict --ignore-valid-from "$@"; }
gpg2() { gpg2_nobatch --batch "$@"; }

xcp() { if [ -f "$1" -a ! -f "$2" ]; then cp "$1" "$2"; fi; }