From 02496d7afcbac169721eff34f4e9fa70020be7cb Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Mon, 3 Feb 2025 19:18:56 -0800 Subject: [PATCH] experiments with alternate root fs --- btrfs.sh | 34 ++++++++++++++++++++++++++++++++++ btrfs2.sh | 38 ++++++++++++++++++++++++++++++++++++++ btrfs3.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ squashfs.sh | 9 +++++++++ 4 files changed, 128 insertions(+) create mode 100755 btrfs.sh create mode 100755 btrfs2.sh create mode 100755 btrfs3.sh create mode 100755 squashfs.sh diff --git a/btrfs.sh b/btrfs.sh new file mode 100755 index 0000000..92c5edb --- /dev/null +++ b/btrfs.sh @@ -0,0 +1,34 @@ +#!/bin/bash +set -e + +# Variables +ROOTFS_DIR="build/agnos-rootfs" +IMAGE_NAME="agnos-btrfs-test.img" +IMAGE_SIZE="4G" # Adjust this according to your needs + +# Check if the rootfs directory exists +if [ ! -d "$ROOTFS_DIR" ]; then + echo "Error: $ROOTFS_DIR does not exist." + exit 1 +fi + +# Create an empty image file +dd if=/dev/zero of=$IMAGE_NAME bs=1 count=0 seek=$IMAGE_SIZE + +# Format the image with BTRFS +mkfs.btrfs -f -d compress -m compress $IMAGE_NAME + +# Mount the image to a temporary directory +MOUNT_POINT=$(mktemp -d) +sudo mount -o loop $IMAGE_NAME $MOUNT_POINT + +# Copy the rootfs to the mounted image +sudo cp -a $ROOTFS_DIR/* $MOUNT_POINT/ + +# Unmount the image +sudo umount $MOUNT_POINT + +# Clean up the temporary mount point +rmdir $MOUNT_POINT + +echo "BTRFS image $IMAGE_NAME created successfully." diff --git a/btrfs2.sh b/btrfs2.sh new file mode 100755 index 0000000..c3e1e16 --- /dev/null +++ b/btrfs2.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# Variables +ROOTFS_DIR="build/agnos-rootfs" +IMAGE_NAME="agnos-btrfs-test.img" +IMAGE_SIZE="4G" # Adjust this according to your needs + +# Create an empty image file +dd if=/dev/zero of=$IMAGE_NAME bs=1 count=0 seek=$IMAGE_SIZE + +# Format the image with BTRFS, enabling compression for both data and metadata +mkfs.btrfs -f $IMAGE_NAME + +# Create a loop device and mount it +LOOP_DEVICE=$(sudo losetup -f --show $IMAGE_NAME) +MOUNT_POINT=$(mktemp -d) + +# Enable compression for the filesystem +sudo mount -o loop,compress-force=zlib:15 $LOOP_DEVICE $MOUNT_POINT +#sudo mount -o loop,compress=zlib:15,compress-force=zlib:15 $LOOP_DEVICE $MOUNT_POINT +#sudo mount -o loop $LOOP_DEVICE $MOUNT_POINT + +# Copy the rootfs to the mounted image +sudo cp -a $ROOTFS_DIR/* $MOUNT_POINT/ + +#sudo btrfs filesystem defragment -r -v -clzo $MOUNT_POINT + +# Unmount the image +sudo umount $MOUNT_POINT + +# Detach the loop device +sudo losetup -d $LOOP_DEVICE + +# Clean up the temporary mount point +rmdir $MOUNT_POINT + +echo "BTRFS image $IMAGE_NAME with compression created successfully." +du -hs $IMAGE_NAME diff --git a/btrfs3.sh b/btrfs3.sh new file mode 100755 index 0000000..d159694 --- /dev/null +++ b/btrfs3.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Directory containing the root filesystem +ROOTFS_DIR="build/agnos-rootfs" + +# Output file name for the BTRFS image +IMAGE_FILE="agnos-rootfs.img" + +# Temporary directory for the BTRFS filesystem +TEMP_DIR=$(mktemp -d) + +# Size for the initial BTRFS image (adjust as necessary) +INITIAL_SIZE="10G" + +# Compression to use for BTRFS (LZO is commonly supported, but ZSTD might be better if available) +COMPRESSION="lzo" # or "zstd" if your kernel supports it + +# Create a BTRFS filesystem in a loopback file +truncate -s "$INITIAL_SIZE" "$IMAGE_FILE" +mkfs.btrfs --compress=$COMPRESSION "$IMAGE_FILE" + +# Mount the BTRFS filesystem +sudo mount -o compress=$COMPRESSION "$IMAGE_FILE" "$TEMP_DIR" + +# Copy the rootfs into the mounted BTRFS filesystem +sudo rsync -aHAXx --info=progress2 "$ROOTFS_DIR"/ "$TEMP_DIR"/ + +# Unmount the filesystem +sudo umount "$TEMP_DIR" + +# Attempt to shrink the filesystem +sudo losetup -fP "$IMAGE_FILE" +LOOPDEV=$(losetup -j "$IMAGE_FILE" | awk '{print $1}' | sed 's/://') +sudo btrfs filesystem resize min /dev/$LOOPDEV +sudo btrfs check --repair /dev/$LOOPDEV +sudo losetup -d /dev/$LOOPDEV + +# Resize the image file to the actual size of the filesystem +# Note: This step requires knowing or estimating the exact size needed +# Here, we'll use the size of the last block used by BTRFS +SIZE=$(sudo btrfs inspect-internal dump-super "$IMAGE_FILE" | grep "total bytes" | awk '{print $3}') +truncate -s "$SIZE" "$IMAGE_FILE" + +# Clean up +rmdir "$TEMP_DIR" + +echo "BTRFS image created and shrunk: $IMAGE_FILE" diff --git a/squashfs.sh b/squashfs.sh new file mode 100755 index 0000000..3336f8f --- /dev/null +++ b/squashfs.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +rm -rf output/system.squash* + +#mksquashfs build/agnos-rootfs output/system.squashfs.lzo -comp lzo -b 1M -no-xattrs +#sudo mksquashfs build/agnos-rootfs output/system.squashfs.gzip -comp gzip -Xcompression-level 1 -b 1M -no-xattrs +sudo mksquashfs build/agnos-rootfs output/system.squashfs.lz4 -comp lz4 -b 4k -no-fragments -no-xattrs # -no-acls + +du -hs output/sys*