From 4ee60671d768e2f695cc1269d07643d996710732 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Tue, 31 Oct 2023 09:36:36 +0000 Subject: [PATCH] nix: Add smallDiskImage drv for HTTP server demo Signed-off-by: Nick Spinale --- .../microkit/http-server/default.nix | 111 ++++++++++-------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/hacking/nix/scope/world/instances/microkit/http-server/default.nix b/hacking/nix/scope/world/instances/microkit/http-server/default.nix index f22740efa..25ed80e9a 100644 --- a/hacking/nix/scope/world/instances/microkit/http-server/default.nix +++ b/hacking/nix/scope/world/instances/microkit/http-server/default.nix @@ -36,67 +36,82 @@ let rev = "0a579415c4837c96c4d4629e4b4d4691aaff07ca"; }; - diskImage = buildPackages.vmTools.runInLinuxVM (runCommand "disk-image" { - nativeBuildInputs = [ python3 kmod parted fatresize dosfstools ]; - preVM = '' - mkdir scratch - scratch=$(realpath scratch) - QEMU_OPTS+=" -virtfs local,path=$scratch,security_model=none,mount_tag=scratch" - ''; - } '' - mkdir /tmp/scratch - mount -t 9p scratch /tmp/scratch -o trans=virtio,version=9p2000.L,msize=131072 - cd scratch + diskImage = mkDiskImage {}; + smallDiskImage = mkDiskImage { excludePatterns = [ "*.mp4" "*.pdf" ]; }; + + mkDiskImage = + { maxIndividualFileSize ? null + , excludePatterns ? null + }: + buildPackages.vmTools.runInLinuxVM (runCommand "disk-image" { + nativeBuildInputs = [ python3 kmod parted fatresize dosfstools ]; + preVM = '' + mkdir scratch + scratch=$(realpath scratch) + QEMU_OPTS+=" -virtfs local,path=$scratch,security_model=none,mount_tag=scratch" + ''; + } '' + mkdir /tmp/scratch + mount -t 9p scratch /tmp/scratch -o trans=virtio,version=9p2000.L,msize=131072 + cd scratch + + modprobe loop + + mkdir /mnt - modprobe loop + # HACK: fatresize segfaults when run on a filesystem that's not on a partition (?) - mkdir /mnt + img_size=500M + img=disk.img + touch $img + truncate -s $img_size $img - # HACK: fatresize segfaults when run on a filesystem that's not on a partition (?) + dev=$(losetup --find --show $img) - img_size=500M - img=disk.img - touch $img - truncate -s $img_size $img + parted -s $dev mklabel msdos + parted -s $dev mkpart primary fat16 512B 100% - dev=$(losetup --find --show $img) + partprobe $dev + partition=''${dev}p1 - parted -s $dev mklabel msdos - parted -s $dev mkpart primary fat16 512B 100% - - partprobe $dev - partition=''${dev}p1 + mkfs.vfat -F 16 $partition - mkfs.vfat -F 16 $partition - - mount $partition /mnt + mount $partition /mnt - # HACK: - # - some filesystem layer doesn't seem to like '?' in filename - # - rsync doesn't play nicely with some filesystem layer - cp -r --no-preserve=owner,mode ${content}/localhost x/ - find x/ -name '*\?' -delete - cp -r x/* /mnt/ + # HACK: + # - some filesystem layer doesn't seem to like '?' in filename + # - rsync doesn't play nicely with some filesystem layer + cp -r --no-preserve=owner,mode ${content}/localhost x/ + find x/ -name '*\?' -delete + ${lib.optionalString (excludePatterns != null) '' + find x/ -type f \( ${ + lib.concatMapStringsSep " -o " (pat: "-name '${pat}'") excludePatterns + } \) -delete + ''} + ${lib.optionalString (maxIndividualFileSize != null) '' + find x/ -size +${maxIndividualFileSize} -delete + ''} + cp -r x/* /mnt/ - umount /mnt + umount /mnt - min=$(fatresize --info $partition | sed -rn 's/Min size: (.*)$/\1/p') - min_rounded_up=$(python3 -c "print(512 * ($min // 512 + 1))") - total_disk_size=$(expr $min_rounded_up + 512) + min=$(fatresize --info $partition | sed -rn 's/Min size: (.*)$/\1/p') + min_rounded_up=$(python3 -c "print(max(64 << 20, 512 * ($min // 512 + 1)))") + total_disk_size=$(expr $min_rounded_up + 512) - fatresize -v -s $min_rounded_up $partition + fatresize -v -s $min_rounded_up $partition - real_img=real-disk.img - touch $real_img - truncate -s $total_disk_size $real_img - parted -s $real_img mklabel msdos - parted -s $real_img mkpart primary fat16 512B 100% - dd if=$partition of=$real_img oflag=seek_bytes seek=512 count=$min_rounded_up + real_img=real-disk.img + touch $real_img + truncate -s $total_disk_size $real_img + parted -s $real_img mklabel msdos + parted -s $real_img mkpart primary fat16 512B 100% + dd if=$partition of=$real_img oflag=seek_bytes seek=512 count=$min_rounded_up - losetup -d $dev + losetup -d $dev - mv $real_img $out/disk.img - ''); + mv $real_img $out/disk.img + ''); libcDir = "${stdenv.cc.libc}/${hostPlatform.config}"; @@ -166,7 +181,7 @@ lib.fix (self: mkMicrokitInstance { }; } // { inherit pds; - inherit diskImage; + inherit diskImage smallDiskImage; } // lib.optionalAttrs canSimulate rec { automate = let