* Poor man's cloud stuff.
svn path=/configurations/trunk/tud/; revision=26508
Eelco Dolstra committed
Mar 25, 2011 at 11:40 UTC
5a920439e88578392efb59ef06c2f4c0912290e2
8 files changed
+192
cloud/cloud-vm.nix
new
+22
@@ -0,0 +1,22 @@
1
+{ config, pkgs, ... }:
2
+
3
+{
4
+ boot.loader.grub.version = 2;
5
+ boot.loader.grub.device = "/dev/vda";
6
+ boot.initrd.kernelModules = [ "virtio_blk" "virtio_pci" ];
7
+
8
+ fileSystems =
9
+ [ { mountPoint = "/";
10
+ label = "nixos";
11
+ }
12
+ ];
13
+
14
+ swapDevices = [ { label = "swap"; } ];
15
+
16
+ networking.hostName = "";
17
+ networking.firewall.enable = true;
18
+
19
+ services.openssh.enable = true;
20
+
21
+ services.mingetty.ttys = [ "hvc0" "tty1" "tty2" ];
22
+}
cloud/scripts/connect-to-console
new
+11
@@ -0,0 +1,11 @@
1
+#! /bin/sh
2
+
3
+socket="$1"
4
+if [ -z "$socket" ]; then
5
+ echo "syntax: $0 CONSOLE-SOCKET"
6
+ exit 1
7
+fi
8
+
9
+echo "Note: press Ctrl-] to disconnect." >&2
10
+
11
+socat unix:"$1" stdio,raw,echo=0,escape=0x1d
cloud/scripts/create-vm
new
+77
@@ -0,0 +1,77 @@
1
+#! /bin/sh
2
+
3
+set -e
4
+
5
+id="$1"
6
+if [ -z "$id" ]; then
7
+ echo "Syntax: $0 ID"
8
+ exit 1
9
+fi
10
+
11
+xml=/vmdisks/adhoc-cloud/"$id".xml
12
+image=/vmdisks/adhoc-cloud/"$id".qcow2
13
+info=/vmdisks/adhoc-cloud/"$id".info
14
+
15
+if [ -e "$info" -o -e "$xml" -o -e "$image" ]; then
16
+ echo "Virtual machine $id exists!"
17
+ exit 1
18
+fi
19
+
20
+# Assign a number to the VM. From this we derive the MAC and IPv6 addresses.
21
+lastnr=$(flock -x /vmdisks/adhoc-cloud/lock -c 'lastnr=$(cat /vmdisks/adhoc-cloud/last-nr || echo 65536); lastnr=$(($lastnr + 1)); echo $lastnr; echo $lastnr > /vmdisks/adhoc-cloud/last-nr')
22
+
23
+# Compute the MAC.
24
+hex=$(printf "%06x" $lastnr)
25
+mac="00:16:3e:${hex:0:2}:${hex:2:2}:${hex:4:2}"
26
+ipv6="2001:610:685:1:216:3eff:fe${hex:0:2}:${hex:2:2}${hex:4:2}"
27
+
28
+echo "MAC address is $mac"
29
+echo "IPv6 address is $ipv6"
30
+
31
+echo "$ipv6" > "$info"
32
+
33
+cat > "$xml" <<EOF
34
+<domain type="kvm">
35
+ <name>adhoc-cloud-$id</name>
36
+ <memory>524288</memory>
37
+ <os>
38
+ <type arch="x86_64">hvm</type>
39
+ </os>
40
+ <features>
41
+ <acpi/>
42
+ </features>
43
+ <devices>
44
+ <graphics type='vnc' socket="/root/vnc-sockets/adhoc-cloud-$id"/>
45
+ <disk type='file'>
46
+ <driver name='qemu' type='qcow2'/>
47
+ <source file='$image'/>
48
+ <target dev='hda' bus='virtio'/>
49
+ </disk>
50
+ <interface type='bridge'>
51
+ <source bridge='veth0'/>
52
+ <model type='virtio'/>
53
+ <mac address="$mac"/>
54
+ </interface>
55
+ <console type='unix'>
56
+ <source mode="bind" path="/root/console-sockets/adhoc-cloud-$id"/>
57
+ <target type='virtio' port='0'/>
58
+ </console>
59
+ </devices>
60
+</domain>
61
+EOF
62
+
63
+qemu-img create -f qcow2 "$image" -b /vmdisks/nixos-0.1pre26294-x86_64-base.qcow2
64
+
65
+virsh define "$xml"
66
+
67
+virsh start "adhoc-cloud-$id"
68
+
69
+# Wait until SSH is up.
70
+if [ "$ASYNC" != 1 ]; then
71
+ echo -n "Waiting for machine to finish booting..." >&2
72
+ for ((i = 0; i < 60; i++)); do
73
+ if ping6 -c1 "$ipv6" > /dev/null; then break; fi
74
+ echo -n "."
75
+ done
76
+ echo
77
+fi
cloud/scripts/destroy-vm
new
+25
@@ -0,0 +1,25 @@
1
+#! /bin/sh
2
+
3
+set -e
4
+
5
+id="$1"
6
+if [ -z "$id" ]; then
7
+ echo "Syntax: $0 ID"
8
+ exit 1
9
+fi
10
+
11
+xml=/vmdisks/adhoc-cloud/"$id".xml
12
+
13
+if [ ! -e "$xml" ]; then
14
+ echo "Virtual machine $id doesn't exists!"
15
+ exit 1
16
+fi
17
+
18
+virsh destroy "adhoc-cloud-$id" || true
19
+virsh undefine "adhoc-cloud-$id"
20
+
21
+trash=/vmdisks/adhoc-cloud/old/$(date +%s)
22
+mkdir -p "$trash"
23
+mv -n /vmdisks/adhoc-cloud/"$id".* "$trash"/
24
+
25
+rm -f "/root/vnc-sockets/adhoc-cloud-$id" "/root/console-sockets/adhoc-cloud-$id"
cloud/scripts/query-vm
new
+19
@@ -0,0 +1,19 @@
1
+#! /bin/sh
2
+
3
+set -e
4
+
5
+id="$1"
6
+if [ -z "$id" ]; then
7
+ echo "Syntax: $0 ID" >&2
8
+ exit 1
9
+fi
10
+
11
+info=/vmdisks/adhoc-cloud/"$id".info
12
+
13
+if [ ! -e "$info" ]; then
14
+ echo "Virtual machine $id doesn't exists!" >&2
15
+ exit 1
16
+fi
17
+
18
+ipv6=$(cat "$info")
19
+echo "$ipv6"
cloud/scripts/run-vnc
new
+12
@@ -0,0 +1,12 @@
1
+#! /bin/sh
2
+
3
+socket="$1"
4
+if [ -z "$socket" ]; then
5
+ echo "syntax: $0 VNC-SOCKET"
6
+ exit 1
7
+fi
8
+
9
+port=$((1024 + $$))
10
+socat unix:"$socket" tcp-listen:$port &
11
+sleep 0.3
12
+vncviewer :$port
common.nix
+2
@@ -3,6 +3,8 @@
3
{
4
boot.kernelModules = [ "coretemp" ];
5
6
+ boot.initrd.kernelModules = [ "ext4" ];
7
+
8
environment.systemPackages =
9
[ pkgs.emacs pkgs.subversion pkgs.sysstat pkgs.hdparm pkgs.sdparm pkgs.lsiutil
10
pkgs.htop pkgs.sqlite
stan.nix
new
+24
@@ -0,0 +1,24 @@
1
+{ config, pkgs, ... }:
2
+
3
+{
4
+ require = [ ./build-machines-common.nix ];
5
+
6
+ nixpkgs.system = "x86_64-linux";
7
+
8
+ boot.initrd.kernelModules = [ "mptsas" ];
9
+ boot.kernelModules = [ "acpi-cpufreq" "kvm-intel" ];
10
+
11
+ nix.maxJobs = 8;
12
+
13
+ services.httpd.enable = true;
14
+ services.httpd.adminAddr = "foo@example.org";
15
+
16
+ networking.firewall.enable = false;
17
+ networking.firewall.allowedTCPPorts = [ 80 10050 ];
18
+ networking.firewall.rejectPackets = true;
19
+ networking.firewall.allowPing = true;
20
+
21
+ networking.bridges.veth0.interfaces = [ "eth0" ];
22
+
23
+ virtualisation.libvirtd.enable = true;
24
+}