#!/usr/bin/env zsh export LINUX_DISK_IMG=/home/artur/winux/VM/linux/linux.img export WINUX_DISK_IMG=./winux.img # Mount the image of oryginal # # Mounting the image function wxumount { echo "unmounting :$1" mkdir -p $1 if mountpoint -q $1; then echo "Unmounting $1..." umount /mnt/winux if [ $? -eq 0 ]; then echo "Successfully unmounted $1." else echo "Failed to unmount $1." fi else echo "$1 is not mounted." fi } wxumount /mnt/winux wxumount /mnt/linux # (re)create an empty sparsed file rm -rf $WINUX_DISK_IMG dd if=/dev/zero of=$WINUX_DISK_IMG bs=1 count=0 seek=20G # Check size # real size: du -h winux.img # appeared as: ls -lh winux.img # Copy boot IMG="/home/artur/winux/VM/linux/linux.img" SECTOR_SIZE=$(LC_ALL=C sfdisk -d "$LINUX_DISK_IMG" | awk 'match($0,/sector-size:\s*([0-9]+)/,m){print m[1]; exit}') START=$(LC_ALL=C sfdisk -d "$LINUX_DISK_IMG" | awk 'match($0,/start=\s*([0-9]+)/,m){print m[1]; exit}') echo "Sector size: $SECTOR_SIZE" echo "Start sector: $START" dd if="$LINUX_DISK_IMG" of="$WINUX_DISK_IMG" bs="$SECTOR_SIZE" count="$START" conv=notrunc # Formatting the disk # No altering the first 1MB echo '2048,,83,*' | sudo sfdisk --no-reread $WINUX_DISK_IMG # List all loops: sudo losetup -a WINUX_LOOP_DEV=$(sudo losetup --find --show $WINUX_DISK_IMG) # Scan also for partitions: sudo losetup --find --show --partscan winux.img echo "Loop device: $WINUX_LOOP_DEV" export WINUX_LOOP_DEV="$(losetup --find --show -P $WINUX_LOOP_DEV)" mkfs.ext2 ${WINUX_LOOP_DEV}p1 mount ${WINUX_LOOP_DEV}p1 /mnt/winux ## The image is formatted and ready to copy files over export LINUX_LOOP_DEV="$(losetup --find --show --partscan $LINUX_DISK_IMG)" mount ${LINUX_LOOP_DEV}p1 /mnt/linux # copying files cd /mnt/linux tar --numeric-owner --xattrs --acls -cpf - \ --exclude='lost+found' \ --exclude='var/log/*' \ --exclude='var/tmp/*' \ --exclude='tmp/*' \ --exclude='var/cache/*' \ --exclude='swapfile' \ --exclude='dev/*' \ --exclude='proc/*' \ --exclude='sys/*' \ --exclude='run/*' \ . | tar --numeric-owner --xattrs --acls -xpf - -C /mnt/winux #### TESTTING CODE losetup -j $WINUX_DISK_IMG # Clean up after building wxumount /mnt/winux wxumount /mnt/linux for dev in $(losetup -j $WINUX_DISK_IMG | cut -d: -f1); do losetup -d "$dev" done for dev in $(losetup -j $LINUX_DISK_IMG | cut -d: -f1); do losetup -d "$dev" done # Create an archive # echo "making the archive" zstd -T0 --ultra -22 winux.img -o winux.img.zst # decompression with # unzstd --sparse winux.img.zst # or # zstd -d --sparse winux.img.zst -o winux.img