Skip to content

Commit

Permalink
nix: Add smallDiskImage drv for HTTP server demo
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Oct 31, 2023
1 parent f3bdcdf commit afcf30b
Showing 1 changed file with 68 additions and 48 deletions.
116 changes: 68 additions & 48 deletions hacking/nix/scope/world/instances/microkit/http-server/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,67 +36,87 @@ 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
# HACK: fatresize segfaults when run on a filesystem that's not on a partition (?)
modprobe loop
img_size=500M
img=disk.img
touch $img
truncate -s $img_size $img
mkdir /mnt
dev=$(losetup --find --show $img)
# HACK: fatresize segfaults when run on a filesystem that's not on a partition (?)
parted -s $dev mklabel msdos
parted -s $dev mkpart primary fat16 512B 100%
img_size=500M
img=disk.img
touch $img
truncate -s $img_size $img
partprobe $dev
partition=''${dev}p1
dev=$(losetup --find --show $img)
mkfs.vfat -F 16 $partition
parted -s $dev mklabel msdos
parted -s $dev mkpart primary fat16 512B 100%
partprobe $dev
partition=''${dev}p1
mount $partition /mnt
mkfs.vfat -F 16 $partition
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
${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/
# 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/
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))")
partition_size=$(python3 -c "print(max(64 << 20, $min_rounded_up))")
total_disk_size=$(expr $partition_size + 512)
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)
# NOTE
# Somehow $partition sometimes ends up smaller than $partition_size.
# conv=notrunc works around this possibility.
fatresize -v -s $partition_size $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%
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
dd if=$partition of=$real_img iflag=count_bytes oflag=seek_bytes seek=512 count=$partition_size conv=notrunc
losetup -d $dev
losetup -d $dev
mv $real_img $out/disk.img
'');
mv $real_img $out/disk.img
'');

libcDir = "${stdenv.cc.libc}/${hostPlatform.config}";

Expand Down Expand Up @@ -166,7 +186,7 @@ lib.fix (self: mkMicrokitInstance {
};
} // {
inherit pds;
inherit diskImage;
inherit diskImage smallDiskImage;
} // lib.optionalAttrs canSimulate rec {
automate =
let
Expand Down

0 comments on commit afcf30b

Please sign in to comment.