1# Builds an ext4 image containing a populated /nix/store with the closure 2# of store paths passed in the storePaths parameter. The generated image 3# is sized to only fit its contents, with the expectation that a script 4# resizes the filesystem at boot time. 5{ pkgs 6, storePaths 7, volumeLabel 8}: 9 10pkgs.stdenv.mkDerivation { 11 name = "ext4-fs.img"; 12 13 buildInputs = with pkgs; [e2fsprogs libfaketime perl]; 14 15 # For obtaining the closure of `storePaths'. 16 exportReferencesGraph = 17 map (x: [("closure-" + baseNameOf x) x]) storePaths; 18 19 buildCommand = 20 '' 21 # Add the closures of the top-level store objects. 22 storePaths=$(perl ${pkgs.pathsFromGraph} closure-*) 23 24 # Also include a manifest of the closures in a format suitable 25 # for nix-store --load-db. 26 printRegistration=1 perl ${pkgs.pathsFromGraph} closure-* > nix-path-registration 27 28 # Make a crude approximation of the size of the target image. 29 # If the script starts failing, increase the fudge factors here. 30 numInodes=$(find $storePaths | wc -l) 31 numDataBlocks=$(du -c -B 4096 --apparent-size $storePaths | awk '$2 == "total" { print int($1 * 1.03) }') 32 bytes=$((2 * 4096 * $numInodes + 4096 * $numDataBlocks)) 33 echo "Creating an EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks)" 34 35 truncate -s $bytes $out 36 faketime "1970-01-01 00:00:00" mkfs.ext4 -L ${volumeLabel} -U 44444444-4444-4444-8888-888888888888 $out 37 38 # Populate the image contents by piping a bunch of commands to the `debugfs` tool from e2fsprogs. 39 # For example, to copy /nix/store/abcd...efg-coreutils-8.23/bin/sleep: 40 # cd /nix/store/abcd...efg-coreutils-8.23/bin 41 # write /nix/store/abcd...efg-coreutils-8.23/bin/sleep sleep 42 # sif sleep mode 040555 43 # sif sleep gid 30000 44 # In particular, debugfs doesn't handle absolute target paths; you have to 'cd' in the virtual 45 # filesystem first. Likewise the intermediate directories must already exist (using `find` 46 # handles that for us). And when setting the file's permissions, the inode type flags (__S_IFDIR, 47 # __S_IFREG) need to be set as well. 48 ( 49 echo write nix-path-registration nix-path-registration 50 echo mkdir nix 51 echo cd /nix 52 echo mkdir store 53 54 # XXX: This explodes in exciting ways if anything in /nix/store has a space in it. 55 find $storePaths -printf '%y %f %h %m\n'| while read -r type file dir perms; do 56 # echo "TYPE=$type DIR=$dir FILE=$file PERMS=$perms" >&2 57 58 echo "cd $dir" 59 case $type in 60 d) 61 echo "mkdir $file" 62 echo sif $file mode $((040000 | 0$perms)) # magic constant is __S_IFDIR 63 ;; 64 f) 65 echo "write $dir/$file $file" 66 echo sif $file mode $((0100000 | 0$perms)) # magic constant is __S_IFREG 67 ;; 68 l) 69 echo "symlink $file $(readlink "$dir/$file")" 70 ;; 71 *) 72 echo "Unknown entry: $type $dir $file $perms" >&2 73 exit 1 74 ;; 75 esac 76 77 echo sif $file gid 30000 # chgrp to nixbld 78 done 79 ) | faketime "1970-01-01 00:00:00" debugfs -w $out -f /dev/stdin > errorlog 2>&1 80 81 # The debugfs tool doesn't terminate on error nor exit with a non-zero status. Check manually. 82 if egrep -q 'Could not allocate|File not found' errorlog; then 83 cat errorlog 84 echo "--- Failed to create EXT4 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks) ---" 85 return 1 86 fi 87 ''; 88}