Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix: Add smallDiskImage derivation for HTTP server demo #42

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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