summaryrefslogtreecommitdiff
path: root/old-school/menu-select
blob: 55360b681220e4f7b677d55e4a46d25b0921d628 (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
#!/bin/sh
# usage:
#  $0 boot-ram                                          - use memory-only overlay
#  $0 boot-new       [dev name] [loop file] [megabytes] - create new luks-encrypted overlay
#  $0 boot-overwrite [dev name] [loop file] [megabytes] - overwrite with new luks overlay
#  $0 boot-luks      [dev name] [loop file]             - boot existing luks-encrypted overlay
#  $0 boot-gpg       [key id] [gnupg homedir] [???]     - boot any device signed with the key

. lvm-create.sh
. common.sh
exec 4>&1
debug_log

error()
{
  local sleep=3

  clear >&4
  echo "error -- ${*:-:(}" >&4

  if [ $sleep -gt 0 ]; then
    echo "will try again in $sleep seconds..." >&4
    sleep $sleep
  fi
  bootmenu do_trigger no_panic
  exit
}

badopts=
fs=
while [ $# -ge 1 ]; do
  case $1 in
    --fs=*) fs="${1#--fs=}"; shift; continue ;;
    --*) echo "error: unknown option $1"; badopts=true; shift; continue ;;
  esac
  break
done
[ -z "$badopts" ] || error 'usage error'

[ $# -ge 2 -o "$1" = 'boot-ram' ] || error 'usage error'

remountrw()
{
  local fs="$1" dev="$2" loopfile="$3"
  if [ "$fs" = hfsplus ]; then
    mountpoint="/mnt/${dev##*/}"
    umount "$dev" || error
    fsck.hfsplus -q "$dev" || error
    mount -o force "$dev" "$mountpoint" || error
  else
    mount -o remount,rw "$dev" || error
  fi
}

hwclock_to_system()
{
  local fs="$1" UTC=UTC
  case "$fs" in ntfs|vfat) UTC=LOCAL ;; esac
  printf '0.0 0 0.0\n0\n%s' $UTC > /etc/adjtime
  hwclock --hctosys
}

case "$1" in
  boot-ram)
    mkdir /cdrom /btrfs
    bootwait samizdat-cdrom
    mount -t btrfs -o loop,subvol=ROOT /cdrom/live/filesystem.btrfs /btrfs
    modprobe brd rd_size=$((256 * 1024 * 2)) # TODO: choose intelligently
    btrfs device add /dev/ram0 /btrfs
    mount -o rw,remount /btrfs
    mount -o move /btrfs /root
    mkdir /root/cdrom
    mount -o move /cdrom /root/cdrom

    bootdone root-mounted
  ;;
  boot-overwrite|boot-new|boot-luks)
    dev="$2"
    loopfile="$3"
    megs="$4"

    [ "$1" != 'boot-new' -o ! -e "$loopfile" ] || error

    remountrw "$fs" "$dev" "$loopfile" || error

    hwclock_to_system "$fs"

    if [ "$1" = 'boot-overwrite' ]; then
      rm "$loopfile" "$loopfile"k
    fi

    init_gpg || error

    if [ "$1" = 'boot-luks' ]; then
      open_samizdat "$loopfile" "$loopfile"k || error
      exit
    fi

    start_meter "Allocating ${megs}MB in '$loopfile' on $dev..."

    if init_samizdat "$loopfile" "$megs" "$loopfile"k; then
      stop_meter done.
    else
      stop_meter error!
      rm "$loopfile" "$loopfile"k
      error
    fi
  ;;
  *)
    error "Unimplemented boot command: $*"
  ;;
esac

# vim:ts=2 sw=2 et