Merge pull request #25165 from copumpkin/google-image-common

google-compute-image: switch to use the common make-disk-image.nix

authored by

Daniel Peebles and committed by
GitHub
f2d1aa05 331efb3d

+16 -84
+16 -84
nixos/modules/virtualisation/google-compute-image.nix
··· 2 2 3 3 with lib; 4 4 let 5 - diskSize = "1G"; 5 + diskSize = 1024; # MB 6 6 in 7 7 { 8 8 imports = [ ../profiles/headless.nix ../profiles/qemu-guest.nix ./grow-partition.nix ]; ··· 10 10 # https://cloud.google.com/compute/docs/tutorials/building-images 11 11 networking.firewall.enable = mkDefault false; 12 12 13 - system.build.googleComputeImage = 14 - pkgs.vmTools.runInLinuxVM ( 15 - pkgs.runCommand "google-compute-image" 16 - { preVM = 17 - '' 18 - mkdir $out 19 - diskImage=$out/$diskImageBase 20 - truncate $diskImage --size ${diskSize} 21 - mv closure xchg/ 22 - ''; 23 - 24 - postVM = 25 - '' 26 - PATH=$PATH:${pkgs.stdenv.lib.makeBinPath [ pkgs.gnutar pkgs.gzip ]} 27 - pushd $out 28 - mv $diskImageBase disk.raw 29 - tar -Szcf $diskImageBase.tar.gz disk.raw 30 - rm $out/disk.raw 31 - popd 32 - ''; 33 - diskImageBase = "nixos-image-${config.system.nixosLabel}-${pkgs.stdenv.system}.raw"; 34 - buildInputs = [ pkgs.utillinux pkgs.perl ]; 35 - exportReferencesGraph = 36 - [ "closure" config.system.build.toplevel ]; 37 - } 38 - '' 39 - # Create partition table 40 - ${pkgs.parted}/sbin/parted /dev/vda mklabel msdos 41 - ${pkgs.parted}/sbin/parted /dev/vda mkpart primary ext4 1 ${diskSize} 42 - ${pkgs.parted}/sbin/parted /dev/vda print 43 - . /sys/class/block/vda1/uevent 44 - mknod /dev/vda1 b $MAJOR $MINOR 45 - 46 - # Create an empty filesystem and mount it. 47 - ${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda1 48 - ${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1 49 - 50 - mkdir /mnt 51 - mount /dev/vda1 /mnt 52 - 53 - # The initrd expects these directories to exist. 54 - mkdir /mnt/dev /mnt/proc /mnt/sys 55 - 56 - mount --bind /proc /mnt/proc 57 - mount --bind /dev /mnt/dev 58 - mount --bind /sys /mnt/sys 59 - 60 - # Copy all paths in the closure to the filesystem. 61 - storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure) 62 - 63 - mkdir -p /mnt/nix/store 64 - echo "copying everything (will take a while)..." 65 - ${pkgs.rsync}/bin/rsync -a $storePaths /mnt/nix/store/ 66 - 67 - # Register the paths in the Nix database. 68 - printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \ 69 - chroot /mnt ${config.nix.package.out}/bin/nix-store --load-db --option build-users-group "" 70 - 71 - # Create the system profile to allow nixos-rebuild to work. 72 - chroot /mnt ${config.nix.package.out}/bin/nix-env \ 73 - -p /nix/var/nix/profiles/system --set ${config.system.build.toplevel} \ 74 - --option build-users-group "" 75 - 76 - # `nixos-rebuild' requires an /etc/NIXOS. 77 - mkdir -p /mnt/etc 78 - touch /mnt/etc/NIXOS 79 - 80 - # `switch-to-configuration' requires a /bin/sh 81 - mkdir -p /mnt/bin 82 - ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh 83 - 84 - # Install a configuration.nix. 85 - mkdir -p /mnt/etc/nixos /mnt/boot/grub 86 - cp ${./google-compute-config.nix} /mnt/etc/nixos/configuration.nix 87 - 88 - # Generate the GRUB menu. 89 - ln -s vda /dev/sda 90 - chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot 91 - 92 - umount /mnt/proc /mnt/dev /mnt/sys 93 - umount /mnt 94 - '' 95 - ); 13 + system.build.googleComputeImage = import ../../lib/make-disk-image.nix { 14 + name = "google-compute-image"; 15 + postVM = '' 16 + PATH=$PATH:${pkgs.stdenv.lib.makeBinPath [ pkgs.gnutar pkgs.gzip ]} 17 + pushd $out 18 + mv $diskImage disk.raw 19 + tar -Szcf nixos-image-${config.system.nixosLabel}-${pkgs.stdenv.system}.tar.gz disk.raw 20 + rm $out/disk.raw 21 + popd 22 + ''; 23 + configFile = ./google-compute-config.nix; 24 + format = "raw"; 25 + inherit diskSize; 26 + inherit config lib pkgs; 27 + }; 96 28 97 29 fileSystems."/" = { 98 30 device = "/dev/disk/by-label/nixos";