-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathqemu.sh
executable file
·155 lines (133 loc) · 4.43 KB
/
qemu.sh
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
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p nix jq gnused qemu
set -eu -o pipefail
NIXIOSK="$PWD"
if [ "$#" -gt 0 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ] ; }; then
echo Usage: "$0" nixiosk.json.sample
exit 1
fi
vnc=
if [ "$#" -gt 0 ] && [ "$1" = --vnc ]; then
vnc=1
shift
fi
flake=
if [ "$#" -gt 0 ] && [ "$1" = --flake ]; then
shift
flake="${1-.#nixosConfiguration}"
if [ "$#" -gt 0 ]; then
shift
fi
fi
tmpdir="$(mktemp -d)"
hardware=
hostName=
qemuArch=
NIX_DISK_IMAGE=
cleanup() {
rm -rf "$tmpdir"
if [ "$hardware" = qemu-no-virtfs ] && [ -n "$NIX_DISK_IMAGE" ]; then
rm -f "$NIX_DISK_IMAGE"
fi
}
trap cleanup EXIT
custom=./nixiosk.json
if [ -n "$flake" ]; then
hardware="$(nix eval --raw "$flake.config.nixiosk.hardware")"
hostName="$(nix eval --raw "$flake.config.nixiosk.hostName")"
qemuArch="$(nix eval --raw "$flake._module.args.pkgs.hostPlatform.qemuArch")"
else
if [ "$#" -gt 0 ]; then
if [ "${1:0:1}" != "-" ]; then
custom="$1"
shift
fi
fi
if ! [ -f "$custom" ]; then
echo "No custom file provided, $custom does not exist."
echo "Consult README.org for a template to use."
exit 1
fi
hardware="$(jq -r .hardware $custom)"
hostName="$(jq -r .hostName "$custom")"
fi
if ! [[ "$hardware" = qemu* ]]; then
echo "Config $custom must set hardware to qemu"
exit 1
fi
if [ "$hardware" = qemu ] && ! [ "$(uname)" = Linux ]; then
echo "Set hardware to qemu-no-virtfs on non-Linux systems"
exit 1
fi
system=
if [ -n "$flake" ]; then
nix --experimental-features 'nix-command flakes' build "$flake.config.system.build.toplevel" --out-link "$tmpdir/system" ${NIX_OPTIONS:-}
system=$(readlink -f $tmpdir/system)
else
qemuArch=$(nix-instantiate --no-gc-warning \
--arg custom "builtins.fromJSON (builtins.readFile $(realpath "$custom"))" \
"$NIXIOSK/boot" --eval -A _module.args.pkgs.hostPlatform.qemuArch ${NIX_OPTIONS:-} | sed 's,^",,; s,"$,,')
system=$(nix-build --no-gc-warning --no-out-link \
--arg custom "builtins.fromJSON (builtins.readFile $(realpath "$custom"))" \
"$NIXIOSK/boot" -A config.system.build.toplevel ${NIX_OPTIONS:-})
fi
if [ "$hardware" = qemu-no-virtfs ]; then
NIX_DISK_IMAGE=${NIX_DISK_IMAGE:-$tmpdir/nixos.qcow2}
qcow2=
if [ -n "$flake" ]; then
nix --experimental-features 'nix-command flakes' build "$flake.config.system.build.qcow2" --out-link "$tmpdir/qcow2" ${NIX_OPTIONS:-}
qcow2=$(readlink -f $tmpdir/qcow2)/nixos.qcow2
else
qcow2=$(nix-build --no-gc-warning --no-out-link \
--arg custom "builtins.fromJSON (builtins.readFile $(realpath "$custom"))" \
"$NIXIOSK/boot" -A config.system.build.qcow2 ${NIX_OPTIONS:-})/nixos.qcow2
fi
cp -f $qcow2 $NIX_DISK_IMAGE
chmod +w $NIX_DISK_IMAGE
else
NIX_DISK_IMAGE=${NIX_DISK_IMAGE:-./$hostName.qcow2}
if ! test -e "$NIX_DISK_IMAGE"; then
qemu-img create -f qcow2 "$NIX_DISK_IMAGE" 512M
fi
fi
qemuFlags=
if [ "$hardware" = qemu-no-virtfs ]; then
qemuFlags+=" -drive if=virtio,file=$NIX_DISK_IMAGE,werror=report"
else
qemuFlags+=" -virtfs local,path=/nix/store,security_model=none,mount_tag=store"
qemuFlags+=" -drive id=drive0,if=none,file=$NIX_DISK_IMAGE"
qemuFlags+=" -device virtio-blk-pci,werror=report,drive=drive0"
fi
if [ "$(uname -s)" = Darwin ] && { [ "$(uname -s)" = arm64 ] && [ "$qemuArch" = aarch64 ] || [ "$(uname -s)" = "$qemuArch" ] ; }; then
qemuFlags+=" -accel hvf"
else
qemuFlags+=" -cpu max"
fi
if [ -n "$vnc" ]; then
qemuFlags+=" -vnc :0,password"
qemuFlags+=" -monitor stdio"
fi
if [ "$(uname -s)" = Linux ]; then
if [ -e /dev/kvm ]; then
qemuFlags+=" -enable-kvm"
else
echo "Warning: qemu will be very slow without Linux KVM support"
fi
fi
if [ "$qemuArch" = aarch64 ]; then
qemuFlags+=" -machine virt"
qemuFlags+=" -device virtio-gpu-pci"
else
qemuFlags+=" -vga virtio"
fi
"qemu-system-$qemuArch" -name "$hostName" -m 1024 \
-nic user \
-device virtio-rng-pci \
-device virtio-tablet-pci \
-device virtio-keyboard-pci \
-device virtio-balloon \
-soundhw all \
-display default,show-cursor=on \
-kernel $system/kernel -initrd $system/initrd \
-append "$(cat $system/kernel-params) init=$system/init" \
$qemuFlags "$@"