This repository has no description
2.4 kB
107 lines
1{ pkgs, lib, vgname ? "vgname", luksmap ? "luksmap", ... }:
2
3with lib;
4
5pkgs.writeScriptBin "init" ''
6 #!/usr/bin/env nix-shell
7 #! nix-shell -i bash -p cryptsetup gptfdisk jq libxfs
8 set -xefuo pipefail
9
10 disk=$1
11
12 if mount | grep -q "$disk"; then
13 echo "target device is already mounted, bailout"
14 exit 2
15 fi
16
17 bootdev="$disk"2
18 luksdev="$disk"3
19 luksmap=/dev/mapper/${luksmap}
20
21 vgname=${vgname}
22
23
24 rootdev=/dev/mapper/${vgname}-root
25 homedev=/dev/mapper/${vgname}-home
26
27 read -p "LUKS Password: " lukspw
28
29 #
30 # partitioning
31 #
32
33 # http://en.wikipedia.org/wiki/GUID_Partition_Table
34 # undo:
35 # dd if=/dev/zero bs=512 count=34 of=/dev/sda
36 # TODO zero last 34 blocks (lsblk -bno SIZE /dev/sda)
37 if ! test "$(blkid -o value -s PTTYPE "$disk")" = gpt; then
38 sgdisk -og "$disk"
39 sgdisk -n 1:2048:4095 -c 1:"BIOS Boot Partition" -t 1:ef02 "$disk"
40 sgdisk -n 2:4096:+1G -c 2:"EFI System Partition" -t 2:ef00 "$disk"
41 sgdisk -n 3:0:0 -c 3:"LUKS container" -t 3:8300 "$disk"
42 fi
43
44 if ! test "$(blkid -o value -s PARTLABEL "$luksdev")" = "LUKS container"; then
45 echo zonk2
46 exit 23
47 fi
48
49 if ! cryptsetup isLuks "$luksdev"; then
50 # aes xts-plain64
51 echo -n "$lukspw" | cryptsetup luksFormat "$luksdev" - \
52 -h sha512 \
53 --iter-time 5000
54 fi
55
56 if ! test -e "$luksmap"; then
57 echo "$lukspw" | cryptsetup luksOpen "$luksdev" "$(basename "$luksmap")" -
58 fi
59
60 if ! test "$(blkid -o value -s TYPE "$luksmap")" = LVM2_member; then
61 pvcreate "$luksmap"
62 fi
63
64 if ! vgdisplay -s "$vgname"; then vgcreate "$vgname" "$luksmap"; fi
65
66 lvchange -a y /dev/mapper/"$vgname"
67
68 if ! test -e "$rootdev"; then lvcreate -L 3G -n root "$vgname"; fi
69
70 #
71 # formatting
72 #
73
74 if ! test "$(blkid -o value -s TYPE "$bootdev")" = vfat; then
75 mkfs.vfat "$bootdev"
76 fi
77
78 if ! test "$(blkid -o value -s TYPE "$rootdev")" = xfs; then
79 mkfs.xfs "$rootdev"
80 fi
81
82 if ! test "$(lsblk -n -o MOUNTPOINT "$rootdev")" = /mnt; then
83 mkdir -p /mnt
84 mount "$rootdev" /mnt
85 fi
86 if ! test "$(lsblk -n -o MOUNTPOINT "$bootdev")" = /mnt/boot; then
87 mkdir -m 0000 -p /mnt/boot
88 mount "$bootdev" /mnt/boot
89 fi
90
91 #
92 # dependencies for stockholm
93 #
94
95 # TODO: get sentinal file from target_path
96 mkdir -p /mnt/var/src
97 touch /mnt/var/src/.populate
98
99 #
100 # print all the infos
101 #
102
103 gdisk -l "$disk"
104 lsblk "$disk"
105
106 echo READY.
107''