Blame tests/Squashfs-compression-test.sh

Bruno Wolff III d81a1f
#!/bin/bash
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# We need rpmdev-vercmp
Bruno Wolff III d81a1f
rpm -q rpmdevtools > /dev/null 2>&1 || (echo 'rpmdev-vercmp from rpmdevtools is needed to run this script, aborting (failed).'; exit 1)
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Define block sizes
Bruno Wolff III d81a1f
blocks=(4K 1M)
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Define fill files
Bruno Wolff III d81a1f
fill=(/dev/zero /dev/urandom)
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Define number of iterations
Bruno Wolff III d81a1f
iter=5
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Define fragment sizes
Bruno Wolff III d81a1f
frags=(0 1 2047 4095)
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Define test directory
Bruno Wolff III d81a1f
testdir=/tmp/test-squashfs
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Define mount point
Bruno Wolff III d81a1f
mp=${testdir}/mnt
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Define data directory
Bruno Wolff III d81a1f
datadir=${testdir}/data
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Check for squashfs-tools version and set compression types to test
Bruno Wolff III d81a1f
sqfsver=`rpm -q --qf '%{EVR}' squashfs-tools`
Bruno Wolff III d81a1f
if rpmdev-vercmp 4.1 ${sqfsver} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  ucomp=('gzip')
Bruno Wolff III d81a1f
elif rpmdev-vercmp 4.2 ${sqfsver} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  ucomp=(gzip lzo lzma)
Bruno Wolff III d81a1f
elif rpmdev-vercmp 4.3-1 ${sqfsver} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  ucomp=(gzip lzo lzma xz)
Bruno Wolff III d81a1f
elif rpmdev-vercmp 4.3-21 ${sqfsver} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  ucomp=(gzip lzo lzma xz lz4)
Bruno Wolff III d81a1f
else
Bruno Wolff III d81a1f
  ucomp=(gzip lzo lzma xz lz4 zstd)
Bruno Wolff III d81a1f
fi
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Check for kernel verion and set mount test compression types
Bruno Wolff III d81a1f
kernel=`uname -r`
Bruno Wolff III d81a1f
if rpmdev-vercmp 2.6.36 ${kernel} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  mcomp=('gzip')
Bruno Wolff III d81a1f
elif rpmdev-vercmp 2.6.38 ${kernel} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  mcomp=(gzip lzo)
Bruno Wolff III d81a1f
elif rpmdev-vercmp 3.19 ${kernel} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  mcomp=(gzip lzo xz)
Bruno Wolff III d81a1f
elif rpmdev-vercmp 4.14 ${kernel} > /dev/null; [ $? == '11' ]; then
Bruno Wolff III d81a1f
  mcomp=(gzip lzo xz lz4)
Bruno Wolff III d81a1f
else
Bruno Wolff III d81a1f
  mcomp=(gzip lzo xz lz4 zstd)
Bruno Wolff III d81a1f
fi
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Check for uid 0 and print a warning if not
Bruno Wolff III d81a1f
[ ${UID} -ne 0 ] && echo 'Mount tests will not be performed when not running as root'
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Check if test directory exists and make if not
Bruno Wolff III d81a1f
[ -d ${testdir} ] || mkdir ${testdir}
Bruno Wolff III d81a1f
[ -d ${testdir} ] || (echo "Unable to make '${testdir}', aborting (failed)."; exit 1)
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Check if mount point directory exists and make if not
Bruno Wolff III d81a1f
[ -d ${mp} ] || mkdir ${mp}
Bruno Wolff III d81a1f
[ -d ${mp} ] || (echo "Unable to make '${mp}', aborting (failed)."; exit 1)
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Check if data directory exists and make if not
Bruno Wolff III d81a1f
if [ -d ${datadir} ]; then
Bruno Wolff III d81a1f
  echo "Using existing data directory."
Bruno Wolff III d81a1f
else
Bruno Wolff III d81a1f
  echo "Building data directory."
Bruno Wolff III d81a1f
  mkdir ${datadir}
Bruno Wolff III d81a1f
  [ -d ${datadir} ] || (echo "Unable to make '${datadir}', aborting (failed)."; exit 1)
Bruno Wolff III d81a1f
  for size in ${frags[*]}; do
Bruno Wolff III d81a1f
    for file in ${fill[*]}; do
Bruno Wolff III d81a1f
      dd if=${file} of=${datadir}/frag-`basename ${file}`-${size} bs=1 count=${size} > /dev/null 2>&1
Bruno Wolff III d81a1f
    done
Bruno Wolff III d81a1f
  done
Bruno Wolff III d81a1f
  for size in ${blocks[*]}; do
Bruno Wolff III d81a1f
    for ((count=1;${count}<=${iter};count++)); do
Bruno Wolff III d81a1f
      for file in ${fill[*]}; do
Bruno Wolff III d81a1f
        dd if=${file} of=${datadir}/file-`basename ${file}`-${size}-${count} bs=${size} count=${count} > /dev/null 2>&1
Bruno Wolff III d81a1f
      done
Bruno Wolff III d81a1f
    done
Bruno Wolff III d81a1f
  done
Bruno Wolff III d81a1f
  for size1 in ${frags[*]}; do
Bruno Wolff III d81a1f
    for file1 in ${fill[*]}; do
Bruno Wolff III d81a1f
      for size2 in ${blocks[*]}; do
Bruno Wolff III d81a1f
        for ((count=1;${count}<=${iter};count++)); do
Bruno Wolff III d81a1f
          for file2 in ${fill[*]}; do
Bruno Wolff III d81a1f
            cat ${datadir}/file-`basename ${file2}`-${size2}-${count} ${datadir}/frag-`basename ${file1}`-${size1} > ${datadir}/combined-`basename ${file2}`-${size2}-${count}-`basename ${file1}`-${size1}
Bruno Wolff III d81a1f
          done
Bruno Wolff III d81a1f
        done
Bruno Wolff III d81a1f
      done
Bruno Wolff III d81a1f
    done
Bruno Wolff III d81a1f
  done
Bruno Wolff III d81a1f
fi
Bruno Wolff III d81a1f
Bruno Wolff III d81a1f
# Run unmounted tests
Bruno Wolff III d81a1f
for comp in ${ucomp[*]}; do
Bruno Wolff III d81a1f
  echo "Building squashfs image using ${comp} compression."
Bruno Wolff III d81a1f
  if [ "${comp}" == gzip ]; then
Bruno Wolff III d81a1f
    mksquashfs ${datadir} ${testdir}/sq.img || (echo "mksquashfs failed for ${comp} compression."; continue)
Bruno Wolff III d81a1f
  else
Bruno Wolff III d81a1f
    mksquashfs ${datadir} ${testdir}/sq.img -comp ${comp} || (echo "mksquashfs failed for ${comp} compression."; continue)
Bruno Wolff III d81a1f
  fi
Bruno Wolff III d81a1f
  echo "Testing unmounted extract using ${comp} compression."
Bruno Wolff III d81a1f
  unsquashfs -d ${testdir}/sq ${testdir}/sq.img  || echo "unsquashfs failed for ${comp} compression."
Bruno Wolff III d81a1f
  diff -r -q ${testdir}/sq ${datadir} || (echo "Extract test failed for ${comp} compression."; exit)
Bruno Wolff III d81a1f
  rm -rf ${testdir}/sq
Bruno Wolff III d81a1f
  if [ ${UID} == 0 ]; then
Bruno Wolff III d81a1f
    for kern in ${mcomp[*]}; do
Bruno Wolff III d81a1f
      if [ ${kern} == ${comp} ]; then
Bruno Wolff III d81a1f
        echo "Testing mounted image using ${comp} compression."
Bruno Wolff III 71002d
        mount -r -o loop -t squashfs ${testdir}/sq.img ${mp} || echo "Mount failed.";
Bruno Wolff III d81a1f
        diff -r -q ${mp} ${datadir} || echo "Mounted test failed for ${comp} compression."
Bruno Wolff III d81a1f
        umount ${mp}
Bruno Wolff III d81a1f
        break
Bruno Wolff III d81a1f
      fi
Bruno Wolff III d81a1f
    done
Bruno Wolff III d81a1f
  fi
Bruno Wolff III d81a1f
  rm -f ${testdir}/sq ${testdir}/sq.img
Bruno Wolff III d81a1f
done