Skip to content
Draft
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions btrfs.sh
Original file line number Diff line number Diff line change
@@ -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."
38 changes: 38 additions & 0 deletions btrfs2.sh
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions btrfs3.sh
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions squashfs.sh
Original file line number Diff line number Diff line change
@@ -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*