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