-
Notifications
You must be signed in to change notification settings - Fork 57
/
stage1
executable file
·63 lines (51 loc) · 1.75 KB
/
stage1
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
#!/bin/bash
set -eu
here=$1
root_mount=$2
root_type=$3
grub_device=$4
digitalocean=$5
extra_config=$6
## Bring in some helper functions
. "$here/util"
## Enable networking
log "Setting up chroot networking"
cd host
mkdir -p etc dev proc sys
cp /etc/resolv.conf etc/external-resolv.conf
for fn in dev dev/shm dev/pts proc sys; do mount --bind "/$fn" "$fn"; done
## Patch the ISO for local chroot
log_start "Looking for NixOS init... "
INIT=$(find . -type f -path '*nixos*/init')
log_end "$INIT"
log_start "Looking for NixOS bash... "
BASH=$(find . -type f -path '*/bin/bash' | tail -n 1)
log_end "$BASH"
## Don't run systemd on chroot start, run our stage2 script
log "Patching init"
sed -i "s,exec systemd,exec /$BASH -i /nixos-in-place/stage2 $root_mount $root_type $grub_device $digitalocean," "$INIT"
sed -i "s,starting systemd,starting /nixos-in-place/stage2," "$INIT"
## Don't try to remount / since we didn't do a proper phase 1
sed -i "s|mount -n -o remount,rw none /|echo 'not remounting /'|" "$INIT"
## The chroot will install outside of itself, into the main system
log "Binding remaining environment"
mkdir -p /nixos nixos
mount --bind /nixos nixos
## Allow stage2 to access these scripts
mkdir -p nixos-in-place
mount --bind "$here" nixos-in-place
## Copy extra config, if it exists
if [ ! "$extra_config"x = "x" ];
then
cp -f "$extra_config" /nixos/extra-config
fi
## Imbue the modified live CD environment so we can install
log "Embarking stage2!"
chroot . "/$INIT"
## Various platforms use different flags; just try what we can to clean up
log "Cleaning up mounts"
umount -fR nixos >/dev/null 2>&1 || true
umount -f nixos >/dev/null 2>&1 || true
umount -f nixos-in-place >/dev/null 2>&1 || true
umount -f nix/store >/dev/null 2>&1 || true
rmdir nixos-in-place