#!/usr/bin/env zsh #export LINUX_DISK_IMG=/home/artur/winux/VM/linux/linux.img export LINUX_DISK_IMG=/home/artur/winux/VM/deb-orig/debian.img export WINUX_DISK_IMG=./winux.img # Mount the image of oryginal # # Mounting the image function wxumount { echo "unmounting :$1" while lsof +D $1 >/dev/null 2>&1 || fuser $1 >/dev/null 2>&1 do fuser -a $1 echo "Waiting for $1 to be free..." sleep 1 done mkdir -p $1 if mountpoint -q $1; then echo "Unmounting $1..." umount $1 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 tune2fs ${WINUX_LOOP_DEV}p1 -U 1be261e2-a12d-4468-aa45-cbd7e68636eb 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 SAVED_PWD=`pwd` 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 cd "$SAVED_PWD" #### TESTTING CODE # losetup -j $WINUX_DISK_IMG # Clean up after building sync sleep 1 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 echo "making the archive" # Best compressing rato, but slow and memory hungry (I LIKE IT) #tar --sparse -I 'zstd -T0 --ultra -22 --long=31' \ # -cvf winux.img.tar.zst winux.img ## SO I PICKED NOW FOR SPEED tar --sparse -I 'zstd -T0 -9' -cvf winux.img.tar.zst winux.img # extract # brew install gnu-tar # tar -I zstd -xvf winux.img.tar.zst # on macOS gtar or /opt/homebrew/opt/gnu-tar/libexec/gnubin/tar -I 'zstd --long=31' -xvf winux.img.tar.zst winux.img # FINISHED #======================================================= # compressing with XZ. Supprisely it seems to have worst compresion rate than zstd # tar --sparse -I 'xz -T0 -9e' -cvf winux.img.tar.xz winux.img ## Extracting # tar -I xz -xvf winux.img.tar.xz # # 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