tangled
alpha
login
or
join now
pyrox.dev
/
nixpkgs
0
fork
atom
lol
0
fork
atom
overview
issues
pulls
pipelines
nova-image: use make-disk-image.nix
Domen Kožar
10 years ago
f1508b3a
1703d4dc
+25
-79
3 changed files
expand all
collapse all
unified
split
nixos
lib
make-disk-image.nix
modules
virtualisation
nova-config.nix
nova-image.nix
+1
-1
nixos/lib/make-disk-image.nix
···
110
110
umount /mnt/proc /mnt/dev /mnt/sys
111
111
umount /mnt
112
112
113
113
-
# Do an fsck to make sure resize2fs works.
113
113
+
# Do a fsck to make sure resize2fs works.
114
114
fsck.${fsType} -f -y $rootDisk
115
115
''
116
116
)
-5
nixos/modules/virtualisation/nova-config.nix
···
1
1
-
{ config, pkgs, modulesPath, ... }:
2
2
-
3
3
-
{
4
4
-
imports = [ "${modulesPath}/virtualisation/nova-image.nix" ];
5
5
-
}
+24
-73
nixos/modules/virtualisation/nova-image.nix
···
1
1
+
# Usage:
2
2
+
# $ NIXOS_CONFIG=`pwd`/nixos/modules/virtualisation/nova-image.nix nix-build '<nixpkgs/nixos>' -A config.system.build.novaImage
3
3
+
1
4
{ config, lib, pkgs, ... }:
2
5
3
6
with lib;
4
7
5
8
{
6
6
-
imports = [ ../profiles/qemu-guest.nix ../profiles/headless.nix ./ec2-data.nix ];
7
7
-
8
8
-
system.build.novaImage =
9
9
-
pkgs.vmTools.runInLinuxVM (
10
10
-
pkgs.runCommand "nova-image"
11
11
-
{ preVM =
12
12
-
''
13
13
-
mkdir $out
14
14
-
diskImage=$out/image
15
15
-
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "4G"
16
16
-
mv closure xchg/
17
17
-
'';
18
18
-
buildInputs = [ pkgs.utillinux pkgs.perl ];
19
19
-
exportReferencesGraph =
20
20
-
[ "closure" config.system.build.toplevel ];
9
9
+
system.build.novaImage = import ../../lib/make-disk-image.nix {
10
10
+
inherit pkgs lib config;
11
11
+
partitioned = true;
12
12
+
diskSize = 1 * 1024;
13
13
+
configFile = pkgs.writeText "configuration.nix"
14
14
+
''
15
15
+
{
16
16
+
imports = [ <nixpkgs/nixos/modules/virtualisation/nova-image.nix> ];
21
17
}
22
22
-
''
23
23
-
# Create a single / partition.
24
24
-
${pkgs.parted}/sbin/parted /dev/vda mklabel msdos
25
25
-
${pkgs.parted}/sbin/parted /dev/vda -- mkpart primary ext2 1M -1s
26
26
-
. /sys/class/block/vda1/uevent
27
27
-
mknod /dev/vda1 b $MAJOR $MINOR
18
18
+
'';
19
19
+
};
28
20
29
29
-
# Create an empty filesystem and mount it.
30
30
-
${pkgs.e2fsprogs}/sbin/mkfs.ext3 -L nixos /dev/vda1
31
31
-
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1
32
32
-
mkdir /mnt
33
33
-
mount /dev/vda1 /mnt
34
34
-
35
35
-
# The initrd expects these directories to exist.
36
36
-
mkdir /mnt/dev /mnt/proc /mnt/sys
37
37
-
mount --bind /proc /mnt/proc
38
38
-
mount --bind /dev /mnt/dev
39
39
-
mount --bind /sys /mnt/sys
40
40
-
41
41
-
# Copy all paths in the closure to the filesystem.
42
42
-
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
43
43
-
44
44
-
mkdir -p /mnt/nix/store
45
45
-
${pkgs.rsync}/bin/rsync -av $storePaths /mnt/nix/store/
46
46
-
47
47
-
# Register the paths in the Nix database.
48
48
-
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
49
49
-
chroot /mnt ${config.nix.package}/bin/nix-store --load-db --option build-users-group ""
50
50
-
51
51
-
# Create the system profile to allow nixos-rebuild to work.
52
52
-
chroot /mnt ${config.nix.package}/bin/nix-env --option build-users-group "" \
53
53
-
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
54
54
-
55
55
-
# `nixos-rebuild' requires an /etc/NIXOS.
56
56
-
mkdir -p /mnt/etc
57
57
-
touch /mnt/etc/NIXOS
58
58
-
59
59
-
# `switch-to-configuration' requires a /bin/sh
60
60
-
mkdir -p /mnt/bin
61
61
-
ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
62
62
-
63
63
-
# Install a configuration.nix.
64
64
-
mkdir -p /mnt/etc/nixos
65
65
-
cp ${./nova-config.nix} /mnt/etc/nixos/configuration.nix
66
66
-
67
67
-
# Generate the GRUB menu.
68
68
-
chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
69
69
-
70
70
-
umount /mnt/proc /mnt/dev /mnt/sys
71
71
-
umount /mnt
72
72
-
''
73
73
-
);
21
21
+
imports = [
22
22
+
../profiles/qemu-guest.nix
23
23
+
../profiles/headless.nix
24
24
+
./ec2-data.nix
25
25
+
];
74
26
75
27
fileSystems."/".device = "/dev/disk/by-label/nixos";
76
28
77
29
boot.kernelParams = [ "console=ttyS0" ];
78
78
-
79
79
-
boot.loader.grub.version = 2;
80
30
boot.loader.grub.device = "/dev/vda";
81
31
boot.loader.grub.timeout = 0;
82
32
33
33
+
# Allow root logins
34
34
+
services.openssh.enable = true;
35
35
+
services.openssh.permitRootLogin = "without-password";
36
36
+
83
37
# Put /tmp and /var on /ephemeral0, which has a lot more space.
84
38
# Unfortunately we can't do this with the `fileSystems' option
85
39
# because it has no support for creating the source of a bind
86
40
# mount. Also, "move" /nix to /ephemeral0 by layering a unionfs-fuse
87
41
# mount on top of it so we have a lot more space for Nix operations.
42
42
+
88
43
/*
89
44
boot.initrd.postMountCommands =
90
45
''
···
106
61
'';
107
62
108
63
boot.initrd.supportedFilesystems = [ "unionfs-fuse" ];
109
109
-
*/
64
64
+
*/
110
65
111
111
-
# Allow root logins only using the SSH key that the user specified
112
112
-
# at instance creation time.
113
113
-
services.openssh.enable = true;
114
114
-
services.openssh.permitRootLogin = "without-password";
115
66
}