summaryrefslogtreecommitdiff
path: root/initramfs-tools/scripts/samizdat
blob: 4f93a7f666c4d77db86ce379c2be11ce15db485c (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
klogd -c1 # TODO: This should be even earlier.  Can it go on the kernel command line?

. common.sh
debug_log samizdat.init

my_configure_networking()
{
    configure_networking
    if [ -e /run/net-"$DEVICE".conf ]
    then
        . /run/net-"$DEVICE".conf
        printf 'Note: configure_networking: ROOTSERVER=%s\n', "$ROOTSERVER" >&2
    else
        false
    fi
}

mountroot()
{
  openvt -c 13 sh

  mkfifo "$MENUFIFO"
  samizdat_install_udev_rules

  if [ "$nbdroot" ]; then
      my_configure_networking

      (debug_log samizdat.nbd
       . /scripts/local-top/nbd
      bootdone nbd-script)

      wait_for_gnupghome_tar
  fi

  bootwait samizdat-gpg
  bootmenu
  bootwait root-mounted

  if [ "$IPV4DNS0" != '0.0.0.0' ]; then
    printf '%s\n' "$IPV4DNS0" "$IPV4DNS1" |
        sed -e '/^0.0.0.0$/d; s/^/nameserver /' > /root/etc/resolv.conf
  fi
  chvt 1
}

cdrom_has_keys()
{
    [ ! "$BOOTIF" ] || return
    bootwait samizdat-cdrom
    [ -d /cdrom/gnupghome ]
}

wait_for_gnupghome_tar()
{
  [ -e /gnupghome.tar ] && return
  echo -n Waiting to receive GPG keys through the network...
  my_configure_networking
  (while ! tftp -g -r gnupghome.tar -l /gnupghome.tar.$$ "$ROOTSERVER" 2>/run/initramfs/samizdat/log/tftp.$$.log; do
    sleep 1;
    echo -n .
  done
  mv /gnupghome.tar.$$ /gnupghome.tar)
  echo '  done.'
  (sleep 5; echo ) &
  (. common.sh && . btrfs-create.sh && init_gpg)
}

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

  # '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
}

samizdat_restart_udev()
{
  local LOG_DIR=/run/initramfs/samizdat/log
  mkdir -p "$LOG_DIR"
  killall systemd-udevd 2>/dev/null
  /lib/systemd/systemd-udevd --resolve-names=never --debug >$LOG_DIR/udevd-systemd.log 2>&1 &
  udevadm hwdb --update  # rule is not executed by 'udevadm trigger' otherwise.  not sure why
}


# TODO: do not duplicate these functions from common.sh

export MENUFIFO=/menu.fifo

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()
{
  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"
}

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 "$@"
}