Tool to check that cloning was successful, new versions
diff-image-drive version 1.2 and 1.3.
Here are new versions of diff-image-drive. When the general check with sha256sum fails, it will check the content except the partition table (and backup table in case of img).
Version 1.2:
Code:
#!/bin/bash
LANG=C
inversvid="\0033[7m"
resetvid="\0033[0m"
#redback="\0033[1;37;41m"
redback="\0033[97;41m"
#greenback="\0033[1;37;42m"
greenback="\0033[97;42m"
#faintvid="\0033[2m"
faintvid="\0033[90m"
size=
skip=
chksum=
skpsum=
########################################################################
function usage {
echo "usage: sudo ${0##*/} <image-file> <device>
examples: sudo ${0##*/} ubuntu.iso /dev/sdc
sudo ${0##*/} myfile.img /dev/sdc
version: 1.2"
exit 1
}
########################################################################
function checksum {
chksum=$(pv -Ss $size "$1" | sha256sum)
}
########################################################################
function skiphead {
skpsum=$(pv -Ss $size "$1" | \
dd bs=512 skip="$skip" 2>/dev/null | sha256sum)
}
########################################################################
# main
########################################################################
if [ "$EUID" != "0" ] || [ $# -ne 2 ] || [ "$1" == "-h" ] || ! test -s "$1" || ! test -b "$2"
then
usage
fi
echo -e "$inversvid Comparing content of '$1' and '$2' $resetvid"
echo -en "$faintvid"
echo -e "partprobe: "
partprobe
sleep 1
echo -e "umount ${2}*: "
if umount ${2}* 2>&1 | grep 'busy'
then
echo -e "$resetvid$redback umount: could not unmount all partitions on $2 $resetvid"
exit 1
else
echo -e "done - no partition mounted on $2 $resetvid"
fi
echo -e "$inversvid calculating sha256sums ...$resetvid"
size=$(stat --format %s "$1")
checksum "$1"
chk1=$chksum
echo "file: $chk1"
checksum "$2"
chk2=$chksum
echo "device: $chk2"
if [ "$chk1" == "$chk2" ]
then
echo -e "$greenback content of iso/img file and device match $resetvid"
else
echo -e "$redback content of iso/img file and device differ $resetvid"
echo "systems cloned from Ubuntu iso files are modified when used and
mkusb-dus fixes the partition table and its backup of img files with gpt
so trying again ..."
echo -e "$inversvid skipping head/tail where the partition table resides $resetvid"
if [ "${1##*.}" == "iso" ] || [ "${1##*.}" == "ISO" ]
then
skip=4
else
skip=33
size=$((size-33*512))
fi
skiphead "$1"
chk1=$skpsum
echo "file except head/tail: $chk1"
skiphead "$2"
chk2=$skpsum
echo "device except head/tail: $chk2"
if [ "$chk1" == "$chk2" ]
then
echo -e "$greenback except head/tail iso/img file and device match $resetvid"
else
echo -e "$redback except head/tail iso/img file and device differ $resetvid"
fi
fi
Version 1.3:
Code:
#!/bin/bash
LANG=C
inversvid="\0033[7m"
resetvid="\0033[0m"
redback="\0033[97;41m"
greenback="\0033[97;42m"
faintvid="\0033[90m"
size=
skip=
chksum=
skpsum=
str=
########################################################################
function usage {
echo "usage: sudo ${0##*/} <image-file> <device>
examples: sudo ${0##*/} ubuntu.iso /dev/sdc
sudo ${0##*/} myfile.img /dev/sdc
version: 1.3"
exit 1
}
########################################################################
function checksum {
chksum=$(pv -Ss $size "$1" | sha256sum)
}
########################################################################
function skiphead {
skpsum=$(pv -Ss $size "$1" | \
dd bs=512 skip="$skip" 2>/dev/null | sha256sum)
}
########################################################################
function tablecmp {
tmp1=$(mktemp)
tmp2=$(mktemp)
echo -e "${faintvid}fdisk:"
fdisk "$1" -lu -o start,end,sectors,size,type | sed -e '1,/^$/'d | tr -s ' ' ' ' > "$tmp1"
fdisk "$2" -lu -o start,end,sectors,size,type | sed -e '1,/^$/'d | tr -s ' ' ' ' > "$tmp2"
echo -en "$resetvid"
str0=$(head -n1 "$tmp1")
str=$(diff "$tmp1" "$tmp2")
ret1=$?
str=$(echo "$str" |grep -v 'not in disk order' |sed -ne '/^[<>] /p' \
|cut -d ' ' -f 1,5- |grep -v '^>$' |sed -e 's/^</file:/' -e 's/^>/device:/'| sort -k1 )
rm "$tmp1" "$tmp2"
return $ret1
}
########################################################################
# main
########################################################################
if [ "$EUID" != "0" ] || [ $# -ne 2 ] || [ "$1" == "-h" ] || ! test -s "$1" || ! test -b "$2"
then
usage
fi
echo -e "$inversvid Comparing content of '$1' and '$2' $resetvid"
echo -en "$faintvid"
echo -e "partprobe: "
partprobe
sleep 1
echo -e "umount ${2}*: "
if umount ${2}* 2>&1 | grep 'busy'
then
echo -e "$resetvid$redback umount: could not unmount all partitions on $2 $resetvid"
exit 1
else
echo -e "done - no partition mounted on $2 $resetvid"
fi
echo -n "target device: "
lsblk -ndo model "$2"
lsblk -o name,size,fstype,label,uuid "$2" | sed 's/writable.*/writable/'
echo -e "$inversvid calculating sha256sums ...$resetvid"
size=$(stat --format %s "$1")
if [ "${1##*.}" == "iso" ] && ! tablecmp "$1" "$2"
then
echo "unmatched partition(s) :
$str"
chk1=1
chk2=2
else
checksum "$1"
chk1=$chksum
echo "file: $chk1"
checksum "$2"
chk2=$chksum
echo "device: $chk2"
fi
if [ "$chk1" == "$chk2" ]
then
echo -e "$greenback content of iso/img file and device match $resetvid"
else
echo -e "$redback content of iso/img file and device differ $resetvid"
echo "systems cloned from Ubuntu iso files are modified when used and
mkusb-dus fixes the partition table and its backup of img files with gpt
so trying ..."
echo -e "$inversvid skipping head/tail where the partition table resides $resetvid"
if [ "${1##*.}" == "iso" ] || [ "${1##*.}" == "ISO" ]
then
skip=4
else
skip=33
size=$((size-33*512))
fi
skiphead "$1"
chk1=$skpsum
echo "file except head/tail: $chk1"
skiphead "$2"
chk2=$skpsum
echo "device except head/tail: $chk2"
if [ "$chk1" == "$chk2" ]
then
echo -e "$greenback except head/tail iso/img file and device match $resetvid"
else
echo -e "$redback except head/tail iso/img file and device differ $resetvid"
fi
fi
Demo with diff-image-drive version 1.2.
Testing with a [maybe old now] Ubuntu Server Jammy compressed image daily iso file and USB SSD drive where it is cloned. The check fails because the partition table (and backup partition table) is fixed bus mkusb-dus and the modified partition table affects the checksum. Then a second check is performed where the partition table is skipped, and this check succeeds.
Code:
$ sudo diff-image-drive jammy-preinstalled-server-amd64.img /dev/sdc
[sudo] password for sudodus:
Comparing content of 'jammy-preinstalled-server-amd64.img' and '/dev/sdc'
partprobe:
umount /dev/sdc*:
done - no partition mounted on /dev/sdc
calculating sha256sums ...
3,50GiB 0:00:17 [ 207MiB/s] [=================================>] 100%
file: cf51e240b865c94d72ad3b7b8c2c79c7f814092c775231a47648306e7f7df269 -
3,50GiB 0:00:17 [ 205MiB/s] [=================================>] 100%
device: 654d25bf029f8c7e83e340a0709c7640342c91a1ef6ba0b07597e8b7e2d43246 -
content of iso/img file and device differ
systems cloned from Ubuntu iso files are modified when used and
mkusb-dus fixes the partition table and its backup of img files with gpt
so trying again ...
skipping head/tail where the partition table resides
3,50GiB 0:00:17 [ 200MiB/s] [=================================>] 100%
file except head/tail: 2eda9fb2dd8564a7d810d7fba08eade3ee8927d0aaba51456bef1ed53db017ff -
3,50GiB 0:00:17 [ 199MiB/s] [=================================>] 100%
device except head/tail: 2eda9fb2dd8564a7d810d7fba08eade3ee8927d0aaba51456bef1ed53db017ff -
except head/tail iso/img file and device match
Testing a [maybe old now] Lubuntu Mantic daily iso file and USB SSD drive where it is cloned. The check fails because the drive is used (booted from) and a partition labeled 'writable' was created at boot, and this modified the partition table. Then a second check is performed where the partition table is skipped, and this check succeeds.
See also the first screenshot matching this demo example, where the high-lighting colours are shown. (You may notice that the speed and times differ a little, and this is because I repeated the command to get the screenshot.)
Code:
$ sudo diff-image-drive mantic-desktop-amd64.iso /dev/sdc
Comparing content of 'mantic-desktop-amd64.iso' and '/dev/sdc'
partprobe:
umount /dev/sdc*:
done - no partition mounted on /dev/sdc
calculating sha256sums ...
2,92GiB 0:00:14 [ 207MiB/s] [=================================>] 100%
file: a0ee778df45fed50bfd6d1bafe008b716ec211dffe2249a00efaaf88899e4c92 -
2,92GiB 0:00:24 [ 123MiB/s] [=================================>] 100%
device: 724e192f0c65e39fd9fdc233d5d2966421f4196102b4424485030db8b1666273 -
content of iso/img file and device differ
systems cloned from Ubuntu iso files are modified when used and
mkusb-dus fixes the partition table and its backup of img files with gpt
so trying again ...
skipping head/tail where the partition table resides
2,92GiB 0:00:15 [ 197MiB/s] [=================================>] 100%
file except head/tail: 14ba2da53e11c2619cef7656c8b7b5011027ef444e423251f070b3db0739d7db -
2,92GiB 0:00:24 [ 122MiB/s] [=================================>] 100%
device except head/tail: 14ba2da53e11c2619cef7656c8b7b5011027ef444e423251f070b3db0739d7db -
except head/tail iso/img file and device match
Next I tested with 'the wrong USB drive', so also checking 'except head/tail' (alias except the partition table) fails.
Code:
$ sudo diff-image-drive mantic-desktop-amd64.iso /dev/sdc
Comparing content of 'mantic-desktop-amd64.iso' and '/dev/sdc'
partprobe:
umount /dev/sdc*:
done - no partition mounted on /dev/sdc
calculating sha256sums ...
2,92GiB 0:00:14 [ 209MiB/s] [=================================>] 100%
file: a0ee778df45fed50bfd6d1bafe008b716ec211dffe2249a00efaaf88899e4c92 -
2,92GiB 0:00:14 [ 204MiB/s] [=================================>] 100%
device: e7629228960239dbef5f76fb7893a7c0ed691efa8a75b6a38a6ded24c75c0323 -
content of iso/img file and device differ
systems cloned from Ubuntu iso files are modified when used and
mkusb-dus fixes the partition table and its backup of img files with gpt
so trying again ...
skipping head/tail where the partition table resides
2,92GiB 0:00:14 [ 199MiB/s] [=================================>] 100%
file except head/tail: 14ba2da53e11c2619cef7656c8b7b5011027ef444e423251f070b3db0739d7db -
2,92GiB 0:00:14 [ 202MiB/s] [=================================>] 100%
device except head/tail: 336f1555ffd8846788737bc106e6b0680c4e1fc548ada39e3428666ad1b2d16f -
except head/tail iso/img file and device differ
Demo with diff-image-drive version 1.3.
See the second and third screenshots, where the high-lighting colours are shown. Please notice that the calculation of sha256sums is only made once in the file and once in the device when checking iso files. This will save time when iso-testing, particularly when using slow USB pendrives.
1. Directly after cloning, diff-image-drive_demo-1.3-1.png
2. After the drive was booted, so that a partition was created, diff-image-drive_demo-1.3-2.png
Bookmarks