summaryrefslogtreecommitdiff
path: root/old-school/lvm-create.sh
blob: 7f5bea450d2f4c01f6df743da73057b71c1f86ea (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/sh

losetup() { /sbin/losetup "$@"; }

luks_secret()
{
  local parms=$-; # this junk keeps set -x from being too annoying
  set +x
  [ -n "$luks_secret" ] || luks_secret="$(head -c256 /dev/urandom)"
  printf %s "$luks_secret"
  case $parms in *x*) set -x; set -x ;; esac
}

floor4()
{
  # Negatives round up, but aren't used.
  echo $(($1 / 4 * 4))
}

ceil4()
{
  local x="$1"
  [ $((x % 4)) -eq 0 ] || x=$((x + 4 - x % 4))
  printf '%d\n' "$x"
}

. loop-layer.sh

losetup_layers()
{
    for fs in /cdrom/rootfs/*.btrfs; do
        fs_rw=/"${fs##*/}".rw
        dd if=/dev/zero of="$fs_rw" bs=1M count=10
        losetup_snapshot "$fs" "$fs_rw" || return
    done
}

init_samizdat()
{
  local imgfile="$1" megs="$2" keyfile="$3" dev fs fs_rw

  init_samizdat_blockdev "$imgfile" "$megs" "$keyfile" || return
  local blockdev=/dev/mapper/samizdatcrypt uuid

  losetup_layers || return
  modprobe btrfs || return
  btrfs device scan || return

  uuid=$(choose_uuid) || return
  [ "$uuid" ]

  mount -t btrfs -o subvol=ROOT UUID="$uuid" /root || return

  btrfs device add "$blockdev" /root || return
  mount -o rw,remount /root || return

  initialize_root_filesystem || return

  bootdone root-mounted
}

initialize_root_filesystem()
{
    btrfs subvolume create /root/gpg                    || return
    mv /gpg/gnupghome /root/gpg/                        || return

    rm -r /root/var/cache/apt/archives
    btrfs subvolume create /root/var/cache/apt/archives || return

    rmdir /root/home
    btrfs subvolume create /root/home                   || return

    [ -x /root/sbin/mdadm ] || cp /sbin/mdadm /root/sbin/
    [ -e /root/sbin/mdadm-dup.sh ] || cp /bin/mdadm-dup.sh /root/sbin/

    sed -i -e 's/^root:x:/root::/' /root/etc/passwd
    cp /patchroot/* /root/root/

    true
}

# Get the uuid of the filesystem with the most devices,
# excluding filesystems that don't incorporate loop devices.
# This is used to choose the latest seed -- which should have
# the most layers.
choose_uuid()
{
    local seen_loop= seen_uuid= seen_devs=
    btrfs filesystem show |
        while read line; do
            case "$line" in
                Label*)
                    seen_uuid=${line##*uuid: }
                    seen_devs=
                    seen_loop=
                    ;;
                *Total\ devices*)
                    seen_devs=${line#*Total devices }
                    seen_devs=${seen_devs%% *}
                    ;;
                *path\ /dev/mapper/*)
                    seen_loop=t;;
            esac
            [ "$seen_loop" ] && echo "$seen_devs $seen_uuid"
        done |
        uniq | sort -nr | head -n1 | (read _ x; echo $x)
}

filesystem_incomplete()
{
    local n
    n=$(btrfs filesystem show "$1" | sed -ne 's/.*Total devices \([^ ]*\) .*/\1/p')
    [ "$n" != 1 ]
}

open_samizdat()
{
  open_samizdat_blockdev "$@" || return
  local blockdev=/dev/mapper/samizdatcrypt fs

  # For this part, we don't necessarily need the cdrom.
  # Unfortunately the init_gpg code is still getting the GPG key there.
  if filesystem_incomplete "$blockdev"; then
     losetup_layers
  fi
  modprobe btrfs || return
  btrfs device scan || return
  mount -t btrfs -o subvol=ROOT "$blockdev" /root || return
  LoSetup -D
  bootdone root-mounted
}

init_samizdat_lodev()
{
  local imgfile="$1" megs=$(ceil4 "$2")
  truncate -s ${megs}M "$imgfile" || return
  dev=$(losetup -f) && losetup "$dev" "$imgfile" || return
  echo "$dev"
}

open_samizdat_blockdev()
{
  local imgfile="$1" keyfile="$2" dev
  local cryptname=samizdatcrypt
  dev=$(losetup -f) && losetup "$dev" "$imgfile" || return

  gpg2 --verify "$keyfile" || return
  # The first --decrypt merely strips the signature.  The option is
  # poorly named for that case.
  gpg2 --decrypt "$keyfile" | gpg2 --decrypt | cryptsetup --key-file - luksOpen "$dev" "$cryptname" || return

  [ -b /dev/mapper/"$cryptname" ] || return

}

init_samizdat_blockdev()
{
  local imgfile="$1" megs="$2" keyfile="$3" dev
  local cryptname=samizdatcrypt

  dev=$(init_samizdat_lodev "$imgfile" "$megs") || return

  [ ! -b /dev/mapper/"$cryptname" ] || return

  luks_secret >/dev/null
  luks_secret | gpg2 --default-recipient-self --encrypt --armor | gpg2 --clearsign --output "$keyfile" || return

  luks_secret | cryptsetup luksFormat "$dev" - || return
  cryptsetup luksDump "$dev" >&2
  luks_secret | cryptsetup --key-file - luksOpen "$dev" "$cryptname" || return

  [ -b /dev/mapper/"$cryptname" ] || return
}

majmin()
{
  local dev="$1" major minor
  eval $(stat -c 'major=%t minor=%T' "$dev") || return
  [ "$major" -a "$minor" ] || return
  printf '%d:%d\n' 0x$major 0x$minor
}

cryptdev_to_dev()
{
  local dev="$1" majmin
  majmin=$(majmin "$dev") || return
  set -- /sys/dev/block/$majmin/slaves/*
  [ $# = 1 ] || return

  cryptsetup status "$dev" |while read k v; do if [ "$k" = device: ]; then echo $v; break; fi; done
}

cryptdev_to_backing_file()
{
  local dev="$1" majmin result
  majmin="$(majmin "$dev")" || return
  set -- /sys/dev/block/$majmin/slaves/*
  [ $# = 1 ] || return
  read result < "$1"/loop/backing_file || return
  printf '%s\n' "$result"
}

lodev_to_file()
{
  local result majmin dev="$1"
  majmin="$(majmin "$dev")" || return
  read result < /sys/dev/block/$majmin/loop/backing_file || return
  printf '%s' "$result"
}

mountpoint_to_dev()
{
  local wantmp="$1" dev mp rest
  mountpoint -q "$wantmp" || return
  while read dev mp rest; do if [ "$mp" = "$wantmp" ]; then echo "$dev"; return; fi; done < /proc/mounts
  return 1
}

get_cdrom_sizelimit()
{
  # returns bytes
  local dev="$1" sectors
  sectors=$(blockdev --getsz "$dev") || return
  if dd count=2 if="$dev" bs=2048 skip=$((sectors/4 - 2)) of=/dev/null 2>/dev/null; then
    return
  else
    echo $(((sectors-8)*512))
  fi
}

init_gpg()
{
  bootwait samizdat-cdrom
  export GNUPGHOME=/gpg/gnupghome
  mkdir -p "$GNUPGHOME"
  (umask 077; rsync --exclude '/luks-key*' --ignore-existing -rpP /cdrom/gnupghome/ "$GNUPGHOME")

  if samizdat-password-agent >/var/log/samizdat-password-agent.log 2>&1; then
    clear
    true
  else
    false
  fi
}

start_meter()
{
  local startmsg="$*"
  (exec >&4
  clear
  echo -n $startmsg
  set +x
  while sleep 2; do
    echo -n .
  done) &
  meterpid=$!
}

stop_meter()
{
  local endmsg="$*"
  kill $meterpid
  echo "  $endmsg" >&4
}