summaryrefslogtreecommitdiff
path: root/old-school/common.sh
blob: 4aa8528a661c9b8fa85bcdbd735cc3c1e1af6be0 (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
LOGBASE=/var/log

debug_log()
{
  if [ -n "$DEBUG" ]; then
    if [ -n "$1" ]; then
      DEBUG_LOG=$LOGBASE/"$1".$$.log
    else
      DEBUG_LOG=$LOGBASE/$(basename $0).$$.log
    fi
    mkdir -p $LOGBASE
    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
}
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
    udevadm trigger --subsystem-match=block --action=add
  fi
}
find_squashfs_root()
{
  # TODO: "make" puts the correct location in $iso_squashfs_dir.  Get
  # information into this function!

  bootwait samizdat-cdrom
  for dir in /cdrom/live /cdrom/liveos /cdrom/aptosid /cdrom/*
  do
    [ -d "$dir" ] || continue;
    if [ -f "$dir"/filesystem.module ]; then
      while read fs; do
	[ -f "$dir"/"$fs" ] && echo "$dir" "$fs"
      done < "$dir"/filesystem.module
      return
    fi
  done
  for fs in /cdrom/live/filesystem.squashfs /cdrom/live/grml-small.squashfs /cdrom/liveos/squashfs.img /cdrom/aptosid/aptosid.* /cdrom/*/*.squashfs
  do
    if [ -f "$fs" ]; then
      echo "${fs%/*}" "${fs##*/}"
      break
    fi
  done
}
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; }

mountsquashes()
{
  local name dirname basename
  while read dirname basename && [ -d "$dirname" -a -f "$dirname/$basename" ]; do
    name=${basename%.squashfs}
    mkdir -p "/squashes/$name" || return 1
    xcp "$dirname"/filesystem.module /squashes/filesystem.module || return 1
    mountpoint -q "/squashes/$name" ||
    mount -o ro,loop "$dirname/$basename" "/squashes/$name" || return 1
  done
}