move disk-module to dump-dvb.nix

This commit is contained in:
oxapentane - 2022-09-10 15:50:09 +02:00
parent a7876810a9
commit 728dce2876
Signed by: oxapentane
GPG Key ID: 91FA5E5BF9AA901C
3 changed files with 35 additions and 528 deletions

View File

@ -12,11 +12,11 @@
]
},
"locked": {
"lastModified": 1658183882,
"narHash": "sha256-pGyYUJix8ofkc5uDo2EqTUSPKwDQn03jDq14LcXWZcI=",
"lastModified": 1662676776,
"narHash": "sha256-l3cokTnNg85CKD02bNsNlMUeOG7/+DtDZ2MEv1rIOUE=",
"owner": "dump-dvb",
"repo": "click",
"rev": "5c0a8e1f0bd773b7a1ba6682172198519098c3b6",
"rev": "98cb7330a35d5ee9879859f7f6240b01b6f444bf",
"type": "github"
},
"original": {
@ -35,21 +35,17 @@
"dump-dvb",
"nixpkgs"
],
"stops": [
"dump-dvb",
"stops-no-flake"
],
"utils": [
"dump-dvb",
"utils"
]
},
"locked": {
"lastModified": 1662237118,
"narHash": "sha256-57dRdzNa3oMm1iQieAFuV6ZgZo8pPe5/kaVFRb/CNiY=",
"lastModified": 1662652396,
"narHash": "sha256-7Q0jj0Nj30dUlNUfhk/4bYg+hUbU+LA3mDaqMd9lq9E=",
"owner": "dump-dvb",
"repo": "clicky-bunty-server",
"rev": "329964d3e4e410265cbfdcfca741de5a6ffb4480",
"rev": "8c102e73b04fda6f3bc94691399d8b1cfcd5530f",
"type": "github"
},
"original": {
@ -97,21 +93,17 @@
"dump-dvb",
"nixpkgs"
],
"stops": [
"dump-dvb",
"stops-no-flake"
],
"utils": [
"dump-dvb",
"utils"
]
},
"locked": {
"lastModified": 1662154682,
"narHash": "sha256-hdWJDAdwe+bjVVUPxWzoXxitK6et9RKz6A//VqoM/Pg=",
"lastModified": 1662762327,
"narHash": "sha256-Qqr1XFfDFRAF0Ujh+s5vQtswYJlRKSBo5OENkviGrhA=",
"owner": "dump-dvb",
"repo": "decode-server",
"rev": "69457bc9cdcdd2fbd27746d89a971c3740d394cf",
"rev": "99d774dc3390bc2822281e0d326e2e7efe40a8da",
"type": "github"
},
"original": {
@ -135,18 +127,17 @@
],
"radio-conf": "radio-conf",
"stops": "stops",
"stops-no-flake": "stops-no-flake",
"tracy": "tracy",
"utils": "utils_2",
"wartrammer": "wartrammer",
"windshield": "windshield"
},
"locked": {
"lastModified": 1662722718,
"narHash": "sha256-jA45yfoFKpEvhZ3MqV8Hv6wSOGlbGEGoUkn907LexLQ=",
"lastModified": 1662817637,
"narHash": "sha256-lrruA7Cc8e7q0amOQVmOOaXnsxamAtKyJVDx5g3I3O8=",
"owner": "dump-dvb",
"repo": "dump-dvb.nix",
"rev": "37f272bc35817b27e430e4a69908672966789492",
"rev": "330934995a6208b4981db60935304b95dd97fbd1",
"type": "github"
},
"original": {
@ -163,11 +154,11 @@
]
},
"locked": {
"lastModified": 1662579396,
"narHash": "sha256-cwGzNno58h0PedZ/ve7Ax0Zwa5ygNfXKEDQp3GHN4zs=",
"lastModified": 1662652805,
"narHash": "sha256-y1bDLNgodXYSoQPecmFa5okY1sjYjhshYCxmUK8oi3s=",
"owner": "dump-dvb",
"repo": "dump-dvb.rs",
"rev": "0c54becd842f165d54dd9fcfc14641c1bf69c84d",
"rev": "ce5c4a2b9f5e81fbc669e73e4923cf43aa3b8ae1",
"type": "github"
},
"original": {
@ -208,11 +199,11 @@
]
},
"locked": {
"lastModified": 1662584694,
"narHash": "sha256-TmEfq85aMkH9jQb1XRxI+jO33uFeiZKvfU6CnlMTfbA=",
"lastModified": 1662761957,
"narHash": "sha256-rg4pyErMizk7gr+TyrDPABgLiAadWMJgL/U2l7vhn6s=",
"owner": "dump-dvb",
"repo": "dvb-api",
"rev": "9e0ef48aa884f572d99ac79d0131c33625bd2cc1",
"rev": "99c52b03c1e195a24a5ced64e1ab6f6be6c4b4ef",
"type": "github"
},
"original": {
@ -272,11 +263,11 @@
]
},
"locked": {
"lastModified": 1661006543,
"narHash": "sha256-uRVmm3Mhl8oMcHrPd7pC3kMIyqm1T9hCRyp6b+iMUwI=",
"lastModified": 1662762215,
"narHash": "sha256-K8CpuwFoGifbR7jg9hplS7iyPbPlWf35skMCS/ltPQk=",
"owner": "dump-dvb",
"repo": "funnel",
"rev": "4d5cd1b488789633d80fb0342d253d09fa5f122c",
"rev": "5ec433229c7307a455970a3e2360bcf44d9007db",
"type": "github"
},
"original": {
@ -371,11 +362,11 @@
]
},
"locked": {
"lastModified": 1662550015,
"narHash": "sha256-Ffyv3nU+lWvU/QBPHvNXKIlsP432kmW7wf6L0Y19VoQ=",
"lastModified": 1662762828,
"narHash": "sha256-gOIGsnCHRx/64V1Lt3UO5JG1RjZwzDTIRSQFWRSBfnc=",
"owner": "dump-dvb",
"repo": "radio-conf",
"rev": "6b955fb966c8586e64d232ae75a507031cc941b9",
"rev": "a90150ac128bc1851025f7b4e6d825c1f9ede83f",
"type": "github"
},
"original": {
@ -447,27 +438,11 @@
]
},
"locked": {
"lastModified": 1662587207,
"narHash": "sha256-vUiP07kIEW63rLqBE4uqVHcsXULqr+Pt6zzDSNNi7HI=",
"lastModified": 1662587866,
"narHash": "sha256-RGmh+zMli1rSD6uKlB4pm1WVPUupJ9f7foibrO0k1jY=",
"owner": "dump-dvb",
"repo": "stop-names",
"rev": "be8460c53697edd84af93718b8cf9ebfda54ebe3",
"type": "github"
},
"original": {
"owner": "dump-dvb",
"repo": "stop-names",
"type": "github"
}
},
"stops-no-flake": {
"flake": false,
"locked": {
"lastModified": 1662587207,
"narHash": "sha256-vUiP07kIEW63rLqBE4uqVHcsXULqr+Pt6zzDSNNi7HI=",
"owner": "dump-dvb",
"repo": "stop-names",
"rev": "be8460c53697edd84af93718b8cf9ebfda54ebe3",
"rev": "ae192f2705008587fce8a910583d9bf0cf39cd32",
"type": "github"
},
"original": {
@ -573,11 +548,11 @@
]
},
"locked": {
"lastModified": 1662587029,
"narHash": "sha256-kNCAECTqjOMlsADZyCG8+K8ZsHfKT5h/vhmiQbNW2gk=",
"lastModified": 1662736161,
"narHash": "sha256-piQA7/tmx4Y6UuqpWgiOrreEvaRejdjgWSuqoeHOrBU=",
"owner": "dump-dvb",
"repo": "windshield",
"rev": "de0462e83b7e144f0ad28807bd5a2c7579b09d48",
"rev": "bba48f785ba92a383cb01fd7bd7ce9c0906aa8a7",
"type": "github"
},
"original": {

View File

@ -48,21 +48,6 @@
}
];
diskModule = { config, lib, pkgs, ... }: {
boot.growPartition = true;
system.build.diskImage = import ./modules/make-disk-image.nix {
name = "${config.networking.hostName}-disk";
partitionTableType = "efi";
additionalSpace = "0G";
copyChannel = false;
inherit config lib pkgs;
postVM = ''
mkdir -p $out/nix-support
echo file binary-dist $diskImage >> $out/nix-support/hydra-build-products
'';
};
};
# function that generates a system with the given number
generate_system = (id: arch: extraModules:
{
@ -93,7 +78,7 @@
arch = "x86_64-linux";
extraModules = [
./hardware/dell-wyse-3040.nix
diskModule
dump-dvb.nixosModules.disk-module
];
}
{
@ -102,7 +87,7 @@
arch = "x86_64-linux";
extraModules = [
./hardware/dell-wyse-3040.nix
diskModule
dump-dvb.nixosModules.disk-module
];
}
{
@ -111,7 +96,7 @@
arch = "x86_64-linux";
extraModules = [
./hardware/dell-wyse-3040.nix
diskModule
dump-dvb.nixosModules.disk-module
];
}
{
@ -129,7 +114,7 @@
arch = "x86_64-linux";
extraModules = [
./hardware/dell-wyse-3040.nix
diskModule
dump-dvb.nixosModules.disk-module
];
}
];
@ -161,7 +146,7 @@
system = "x86_64-linux";
specialArgs = inputs;
modules = [
diskModule
dump-dvb.nixosModules.disk-module
dump-dvb.nixosModules.default
./hosts/mobile-box/configuration.nix
./hosts/mobile-box/hardware-configuration.nix

View File

@ -1,453 +0,0 @@
{ pkgs
, lib
, # The NixOS configuration to be installed onto the disk image.
config
, # The size of the disk, in megabytes.
# if "auto" size is calculated based on the contents copied to it and
# additionalSpace is taken into account.
diskSize ? "auto"
, # additional disk space to be added to the image if diskSize "auto"
# is used
additionalSpace ? "512M"
, # size of the boot partition, is only used if partitionTableType is
# either "efi" or "hybrid"
# This will be undersized slightly, as this is actually the offset of
# the end of the partition. Generally it will be 1MiB smaller.
bootSize ? "256M"
, # The files and directories to be placed in the target file system.
# This is a list of attribute sets {source, target, mode, user, group} where
# `source' is the file system object (regular file or directory) to be
# grafted in the file system at path `target', `mode' is a string containing
# the permissions that will be set (ex. "755"), `user' and `group' are the
# user and group name that will be set as owner of the files.
# `mode', `user', and `group' are optional.
# When setting one of `user' or `group', the other needs to be set too.
contents ? [ ]
, # Type of partition table to use; either "legacy", "efi", or "none".
# For "efi" images, the GPT partition table is used and a mandatory ESP
# partition of reasonable size is created in addition to the root partition.
# For "legacy", the msdos partition table is used and a single large root
# partition is created.
# For "legacy+gpt", the GPT partition table is used, a 1MiB no-fs partition for
# use by the bootloader is created, and a single large root partition is
# created.
# For "hybrid", the GPT partition table is used and a mandatory ESP
# partition of reasonable size is created in addition to the root partition.
# Also a legacy MBR will be present.
# For "none", no partition table is created. Enabling `installBootLoader`
# most likely fails as GRUB will probably refuse to install.
partitionTableType ? "legacy"
, # Whether to invoke `switch-to-configuration boot` during image creation
installBootLoader ? true
, # The root file system type.
fsType ? "ext4"
, # Filesystem label
label ? if onlyNixStore then "nix-store" else "nixos"
, # The initial NixOS configuration file to be copied to
# /etc/nixos/configuration.nix.
configFile ? null
, # Shell code executed after the VM has finished.
postVM ? ""
, # Copy the contents of the Nix store to the root of the image and
# skip further setup. Incompatible with `contents`,
# `installBootLoader` and `configFile`.
onlyNixStore ? false
, name ? "nixos-disk-image"
, # Disk image format, one of qcow2, qcow2-compressed, vdi, vpc, raw.
format ? "raw"
, # Whether a nix channel based on the current source tree should be
# made available inside the image. Useful for interactive use of nix
# utils, but changes the hash of the image when the sources are
# updated.
copyChannel ? true
, # Additional store paths to copy to the image's store.
additionalPaths ? [ ]
}:
assert partitionTableType == "legacy" || partitionTableType == "legacy+gpt" || partitionTableType == "efi" || partitionTableType == "hybrid" || partitionTableType == "none";
# We use -E offset=X below, which is only supported by e2fsprogs
assert partitionTableType != "none" -> fsType == "ext4";
# Either both or none of {user,group} need to be set
assert lib.all
(attrs: ((attrs.user or null) == null)
== ((attrs.group or null) == null))
contents;
assert onlyNixStore -> contents == [ ] && configFile == null && !installBootLoader;
with lib;
let format' = format; in let
format = if format' == "qcow2-compressed" then "qcow2" else format';
compress = optionalString (format' == "qcow2-compressed") "-c";
filename = "nixos." + {
qcow2 = "qcow2";
vdi = "vdi";
vpc = "vhd";
raw = "img";
}.${format} or format;
rootPartition = {
# switch-case
legacy = "1";
"legacy+gpt" = "2";
efi = "2";
hybrid = "3";
}.${partitionTableType};
partitionDiskScript = {
# switch-case
legacy = ''
parted --script $diskImage -- \
mklabel msdos \
mkpart primary ext4 1MiB -1
'';
"legacy+gpt" = ''
parted --script $diskImage -- \
mklabel gpt \
mkpart no-fs 1MB 2MB \
set 1 bios_grub on \
align-check optimal 1 \
mkpart primary ext4 2MB -1 \
align-check optimal 2 \
print
'';
efi = ''
parted --script $diskImage -- \
mklabel gpt \
mkpart ESP fat32 8MiB ${bootSize} \
set 1 boot on \
mkpart primary ext4 ${bootSize} -1
'';
hybrid = ''
parted --script $diskImage -- \
mklabel gpt \
mkpart ESP fat32 8MiB ${bootSize} \
set 1 boot on \
mkpart no-fs 0 1024KiB \
set 2 bios_grub on \
mkpart primary ext4 ${bootSize} -1
'';
none = "";
}.${partitionTableType};
nixpkgs = cleanSource pkgs.path;
# FIXME: merge with channel.nix / make-channel.nix.
channelSources = pkgs.runCommand "nixos-${config.system.nixos.version}" { } ''
mkdir -p $out
cp -prd ${nixpkgs.outPath} $out/nixos
chmod -R u+w $out/nixos
if [ ! -e $out/nixos/nixpkgs ]; then
ln -s . $out/nixos/nixpkgs
fi
rm -rf $out/nixos/.git
echo -n ${config.system.nixos.versionSuffix} > $out/nixos/.version-suffix
'';
binPath = with pkgs; makeBinPath (
[
rsync
util-linux
parted
e2fsprogs
lkl
config.system.build.nixos-install
config.system.build.nixos-enter
nix
systemdMinimal
] ++ stdenv.initialPath
);
# I'm preserving the line below because I'm going to search for it across nixpkgs to consolidate
# image building logic. The comment right below this now appears in 4 different places in nixpkgs :)
# !!! should use XML.
sources = map (x: x.source) contents;
targets = map (x: x.target) contents;
modes = map (x: x.mode or "''") contents;
users = map (x: x.user or "''") contents;
groups = map (x: x.group or "''") contents;
basePaths = [ config.system.build.toplevel ]
++ lib.optional copyChannel channelSources;
additionalPaths' = subtractLists basePaths additionalPaths;
closureInfo = pkgs.closureInfo {
rootPaths = basePaths ++ additionalPaths';
};
blockSize = toString (4 * 1024); # ext4fs block size (not block device sector size)
prepareImage = ''
export PATH=${binPath}
# Yes, mkfs.ext4 takes different units in different contexts. Fun.
sectorsToKilobytes() {
echo $(( ( "$1" * 512 ) / 1024 ))
}
sectorsToBytes() {
echo $(( "$1" * 512 ))
}
# Given lines of numbers, adds them together
sum_lines() {
local acc=0
while read -r number; do
acc=$((acc+number))
done
echo "$acc"
}
mebibyte=$(( 1024 * 1024 ))
# Approximative percentage of reserved space in an ext4 fs over 512MiB.
# 0.05208587646484375
# × 1000, integer part: 52
compute_fudge() {
echo $(( $1 * 52 / 1000 ))
}
mkdir $out
root="$PWD/root"
mkdir -p $root
# Copy arbitrary other files into the image
# Semi-shamelessly copied from make-etc.sh. I (@copumpkin) shall factor this stuff out as part of
# https://github.com/NixOS/nixpkgs/issues/23052.
set -f
sources_=(${concatStringsSep " " sources})
targets_=(${concatStringsSep " " targets})
modes_=(${concatStringsSep " " modes})
set +f
for ((i = 0; i < ''${#targets_[@]}; i++)); do
source="''${sources_[$i]}"
target="''${targets_[$i]}"
mode="''${modes_[$i]}"
if [ -n "$mode" ]; then
rsync_chmod_flags="--chmod=$mode"
else
rsync_chmod_flags=""
fi
# Unfortunately cptofs only supports modes, not ownership, so we can't use
# rsync's --chown option. Instead, we change the ownerships in the
# VM script with chown.
rsync_flags="-a --no-o --no-g $rsync_chmod_flags"
if [[ "$source" =~ '*' ]]; then
# If the source name contains '*', perform globbing.
mkdir -p $root/$target
for fn in $source; do
rsync $rsync_flags "$fn" $root/$target/
done
else
mkdir -p $root/$(dirname $target)
if ! [ -e $root/$target ]; then
rsync $rsync_flags $source $root/$target
else
echo "duplicate entry $target -> $source"
exit 1
fi
fi
done
export HOME=$TMPDIR
# Provide a Nix database so that nixos-install can copy closures.
export NIX_STATE_DIR=$TMPDIR/state
nix-store --load-db < ${closureInfo}/registration
chmod 755 "$TMPDIR"
echo "running nixos-install..."
nixos-install --root $root --no-bootloader --no-root-passwd \
--system ${config.system.build.toplevel} \
${if copyChannel then "--channel ${channelSources}" else "--no-channel-copy"} \
--substituters ""
${optionalString (additionalPaths' != []) ''
nix --extra-experimental-features nix-command copy --to $root --no-check-sigs ${concatStringsSep " " additionalPaths'}
''}
# delete /nix/store/.links (hydra build produces it, my local build does not produce it...)
echo "Deleting root/nix/store/.links"
rm -rf $root/nix/store/.links
diskImage=nixos.raw
${if diskSize == "auto" then ''
${if partitionTableType == "efi" || partitionTableType == "hybrid" then ''
# Add the GPT at the end
gptSpace=$(( 512 * 34 * 1 ))
# Normally we'd need to account for alignment and things, if bootSize
# represented the actual size of the boot partition. But it instead
# represents the offset at which it ends.
# So we know bootSize is the reserved space in front of the partition.
reservedSpace=$(( gptSpace + $(numfmt --from=iec '${bootSize}') ))
'' else if partitionTableType == "legacy+gpt" then ''
# Add the GPT at the end
gptSpace=$(( 512 * 34 * 1 ))
# And include the bios_grub partition; the ext4 partition starts at 2MB exactly.
reservedSpace=$(( gptSpace + 2 * mebibyte ))
'' else if partitionTableType == "legacy" then ''
# Add the 1MiB aligned reserved space (includes MBR)
reservedSpace=$(( mebibyte ))
'' else ''
reservedSpace=0
''}
additionalSpace=$(( $(numfmt --from=iec '${additionalSpace}') + reservedSpace ))
# Compute required space in filesystem blocks
diskUsage=$(find . ! -type d -print0 | du --files0-from=- --apparent-size --block-size "${blockSize}" | cut -f1 | sum_lines)
# Each inode takes space!
numInodes=$(find . | wc -l)
# Convert to bytes, inodes take two blocks each!
diskUsage=$(( (diskUsage + 2 * numInodes) * ${blockSize} ))
# Then increase the required space to account for the reserved blocks.
fudge=$(compute_fudge $diskUsage)
requiredFilesystemSpace=$(( diskUsage + fudge ))
diskSize=$(( requiredFilesystemSpace + additionalSpace ))
# Round up to the nearest mebibyte.
# This ensures whole 512 bytes sector sizes in the disk image
# and helps towards aligning partitions optimally.
if (( diskSize % mebibyte )); then
diskSize=$(( ( diskSize / mebibyte + 1) * mebibyte ))
fi
truncate -s "$diskSize" $diskImage
printf "Automatic disk size...\n"
printf " Closure space use: %d bytes\n" $diskUsage
printf " fudge: %d bytes\n" $fudge
printf " Filesystem size needed: %d bytes\n" $requiredFilesystemSpace
printf " Additional space: %d bytes\n" $additionalSpace
printf " Disk image size: %d bytes\n" $diskSize
'' else ''
truncate -s ${toString diskSize}M $diskImage
''}
${partitionDiskScript}
${if partitionTableType != "none" then ''
# Get start & length of the root partition in sectors to $START and $SECTORS.
eval $(partx $diskImage -o START,SECTORS --nr ${rootPartition} --pairs)
mkfs.${fsType} -b ${blockSize} -F -L ${label} $diskImage -E offset=$(sectorsToBytes $START) $(sectorsToKilobytes $SECTORS)K
'' else ''
mkfs.${fsType} -b ${blockSize} -F -L ${label} $diskImage
''}
echo "copying staging root to image..."
cptofs -p ${optionalString (partitionTableType != "none") "-P ${rootPartition}"} \
-t ${fsType} \
-i $diskImage \
$root${optionalString onlyNixStore builtins.storeDir}/* / ||
(echo >&2 "ERROR: cptofs failed. diskSize might be too small for closure."; exit 1)
'';
moveOrConvertImage = ''
${if format == "raw" then ''
mv $diskImage $out/${filename}
'' else ''
${pkgs.qemu}/bin/qemu-img convert -f raw -O ${format} ${compress} $diskImage $out/${filename}
''}
diskImage=$out/${filename}
'';
buildImage = pkgs.vmTools.runInLinuxVM (
pkgs.runCommand name
{
preVM = prepareImage;
buildInputs = with pkgs; [ util-linux e2fsprogs dosfstools ];
postVM = moveOrConvertImage + postVM;
memSize = 1024;
} ''
export PATH=${binPath}:$PATH
rootDisk=${if partitionTableType != "none" then "/dev/vda${rootPartition}" else "/dev/vda"}
# Some tools assume these exist
ln -s vda /dev/xvda
ln -s vda /dev/sda
# make systemd-boot find ESP without udev
mkdir /dev/block
ln -s /dev/vda1 /dev/block/254:1
mountPoint=/mnt
mkdir $mountPoint
mount $rootDisk $mountPoint
# Create the ESP and mount it. Unlike e2fsprogs, mkfs.vfat doesn't support an
# '-E offset=X' option, so we can't do this outside the VM.
${optionalString (partitionTableType == "efi" || partitionTableType == "hybrid") ''
mkdir -p /mnt/boot
mkfs.vfat -n ESP /dev/vda1
mount /dev/vda1 /mnt/boot
''}
# Install a configuration.nix
mkdir -p /mnt/etc/nixos
${optionalString (configFile != null) ''
cp ${configFile} /mnt/etc/nixos/configuration.nix
''}
${lib.optionalString installBootLoader ''
# Set up core system link, GRUB, etc.
NIXOS_INSTALL_BOOTLOADER=1 nixos-enter --root $mountPoint -- /nix/var/nix/profiles/system/bin/switch-to-configuration boot
# The above scripts will generate a random machine-id and we don't want to bake a single ID into all our images
rm -f $mountPoint/etc/machine-id
''}
# Set the ownerships of the contents. The modes are set in preVM.
# No globbing on targets, so no need to set -f
targets_=(${concatStringsSep " " targets})
users_=(${concatStringsSep " " users})
groups_=(${concatStringsSep " " groups})
for ((i = 0; i < ''${#targets_[@]}; i++)); do
target="''${targets_[$i]}"
user="''${users_[$i]}"
group="''${groups_[$i]}"
if [ -n "$user$group" ]; then
# We have to nixos-enter since we need to use the user and group of the VM
nixos-enter --root $mountPoint -- chown -R "$user:$group" "$target"
fi
done
umount -R /mnt
# Make sure resize2fs works. Note that resize2fs has stricter criteria for resizing than a normal
# mount, so the `-c 0` and `-i 0` don't affect it. Setting it to `now` doesn't produce deterministic
# output, of course, but we can fix that when/if we start making images deterministic.
${optionalString (fsType == "ext4") ''
tune2fs -T now -c 0 -i 0 $rootDisk
''}
''
);
in
if onlyNixStore then
pkgs.runCommand name { }
(prepareImage + moveOrConvertImage + postVM)
else buildImage