"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/align-test2" (13 Jan 2022, 10921 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "align-test2": 2.3.6_vs_2.4.0.

    1 #!/bin/bash
    2 
    3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
    4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
    5 DEV=""
    6 DEV_STACKED="luks0xbabe"
    7 DEV_NAME="dummyalign"
    8 MNT_DIR="./mnt_luks"
    9 PWD1="93R4P4pIqAH8"
   10 PWD2="mymJeD8ivEhE"
   11 FAST_PBKDF="--pbkdf pbkdf2 --pbkdf-force-iterations 1000"
   12 
   13 cleanup() {
   14     udevadm settle >/dev/null 2>&1
   15     if [ -d "$MNT_DIR" ] ; then
   16         umount -f $MNT_DIR 2>/dev/null
   17         rmdir $MNT_DIR 2>/dev/null
   18     fi
   19     [ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove --retry $DEV_STACKED >/dev/null 2>&1
   20     [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove --retry $DEV_NAME >/dev/null 2>&1
   21     # FIXME scsi_debug sometimes in-use here
   22     sleep 1
   23     rmmod scsi_debug >/dev/null 2>&1
   24     sleep 1
   25 }
   26 
   27 fail()
   28 {
   29     if [ -n "$1" ] ; then echo "FAIL $1" ; fi
   30     echo "FAILED backtrace:"
   31     while caller $frame; do ((frame++)); done
   32     cleanup
   33     exit 100
   34 }
   35 
   36 skip()
   37 {
   38     echo "TEST SKIPPED: $1"
   39     cleanup
   40     exit 77
   41 }
   42 
   43 function dm_crypt_features()
   44 {
   45     VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
   46     [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
   47 
   48     VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
   49     VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
   50     VER_PTC=$(echo $VER_STR | cut -f 3 -d.)
   51 
   52     [ $VER_MAJ -lt 1 ] && return
   53     [ $VER_MAJ -gt 1 ] && {
   54         DM_PERF_CPU=1
   55         DM_SECTOR_SIZE=1
   56         return
   57     }
   58 
   59     [ $VER_MIN -lt 14 ] && return
   60     DM_PERF_CPU=1
   61     if [ $VER_MIN -ge 17 -o \( $VER_MIN -eq 14 -a $VER_PTC -ge 5 \) ]; then
   62         DM_SECTOR_SIZE=1
   63     fi
   64 }
   65 
   66 add_device() {
   67     modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
   68     if [ $? -ne 0 ] ; then
   69         echo "This kernel seems to not support proper scsi_debug module, test skipped."
   70         exit 77
   71     fi
   72 
   73     sleep 2
   74     DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
   75 
   76     if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
   77         echo "This kernel seems to not support topology info, test skipped."
   78         cleanup
   79         exit 77
   80     fi
   81 
   82     DEV="/dev/$DEV"
   83     [ -b $DEV ] || fail "Cannot find $DEV."
   84 }
   85 
   86 format() # expected [forced] [encryption_sector_size]
   87 {
   88     local _sec_size=512
   89 
   90     local _exp=$1
   91 
   92     if [ "${2:0:1}" = "s" ]; then
   93         _sec_size=${2:1}
   94         shift
   95     fi
   96 
   97     test "${3:0:1}" = "s" && _sec_size=${3:1}
   98 
   99     test $_sec_size -eq 512 || local _smsg=" (encryption sector size $_sec_size)"
  100 
  101     if [ -z "$2" ] ; then
  102         echo -n "Formatting using topology info$_smsg..."
  103         echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null 2>&1 || fail
  104     else
  105         echo -n "Formatting using forced sector alignment $2$_smsg..."
  106         echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --align-payload=$2 --sector-size $_sec_size >/dev/null || fail
  107     fi
  108 
  109     # check the device can be activated
  110     if [ -n "$DM_SECTOR_SIZE" ] ; then
  111         echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME || fail
  112         $CRYPTSETUP close $DEV_NAME || fail
  113     fi
  114 
  115     ALIGN=$($CRYPTSETUP luksDump $DEV | grep -A1 "0: crypt" | grep "offset:" | cut -d ' ' -f2)
  116     # echo "ALIGN = $ALIGN"
  117 
  118     [ -z "$ALIGN" ] && fail
  119     ALIGN=$((ALIGN/512))
  120     [ $ALIGN -ne $_exp ] && fail "Expected alignment differs: expected $_exp != detected $ALIGN"
  121 
  122     # test some operation, just in case
  123     echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV $FAST_PBKDF --key-slot 1
  124     [ $? -ne 0 ] && fail "Keyslot add failed."
  125 
  126     $CRYPTSETUP -q luksKillSlot $DEV 1
  127     [ $? -ne 0 ] && fail "Keyslot removal failed."
  128 
  129     echo "PASSED"
  130 }
  131 
  132 format_fail() # expected [forced] [encryption_sector_size]
  133 {
  134     local _sec_size=512
  135 
  136     local _exp=$1
  137 
  138     if [ "${2:0:1}" = "s" ]; then
  139         _sec_size=${2:1}
  140         shift
  141     fi
  142 
  143     test "${3:0:1}" = "s" && _sec_size=${3:1}
  144 
  145     test $_sec_size -eq 512 || local _smsg=" (encryption sector size $_sec_size)"
  146 
  147     if [ -z "$2" ] ; then
  148         echo -n "Formatting using topology info$_smsg (must fail)..."
  149         echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null 2>&1 && fail
  150     else
  151         echo -n "Formatting using forced sector alignment $2$_smsg (must fail)..."
  152         echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --align-payload=$2 --sector-size $_sec_size >/dev/null 2>&1 && fail
  153     fi
  154 
  155     echo "PASSED"
  156 }
  157 
  158 if [ $(id -u) != 0 ]; then
  159     echo "WARNING: You must be root to run this test, test skipped."
  160     exit 77
  161 fi
  162 
  163 dm_crypt_features
  164 modprobe --dry-run scsi_debug >/dev/null 2>&1 || skip "This kernel seems to not support proper scsi_debug module, test skipped."
  165 cleanup
  166 if [ -d /sys/module/scsi_debug ] ; then
  167     echo "Cannot use scsi_debug module (in use or compiled-in), test skipped."
  168     exit 77
  169 fi
  170 
  171 add_device dev_size_mb=32
  172 echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q  >/dev/null || fail
  173 EXPCT=$($CRYPTSETUP luksDump $DEV | grep "offset: " | cut -f 2 -d ' ')
  174 test "$EXPCT" -gt 512 || fail
  175 EXPCT=$((EXPCT/512))
  176 echo "Default alignment detected: $EXPCT sectors"
  177 cleanup
  178 
  179 echo "# Create desktop-class 4K drive"
  180 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
  181 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 num_tgts=1
  182 format $EXPCT
  183 format $EXPCT s1024
  184 format $EXPCT s2048
  185 format $EXPCT s4096
  186 format $EXPCT 1
  187 format $EXPCT 1 s1024
  188 format $EXPCT 1 s2048
  189 format $EXPCT 1 s4096
  190 format $EXPCT 8
  191 format $EXPCT 8 s1024
  192 format $EXPCT 8 s2048
  193 format $EXPCT 8 s4096
  194 format $((EXPCT+1)) $((EXPCT+1))
  195 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
  196 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
  197 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
  198 format $EXPCT $EXPCT
  199 format $EXPCT $EXPCT s1024
  200 format $EXPCT $EXPCT s2048
  201 format $EXPCT $EXPCT s4096
  202 cleanup
  203 
  204 echo "# Create desktop-class 4K drive with misaligned opt-io (some bad USB enclosures)"
  205 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0, opt-io=1025)"
  206 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 num_tgts=1 opt_blks=1025
  207 format $EXPCT
  208 format $EXPCT s1024
  209 format $EXPCT s2048
  210 format $EXPCT s4096
  211 format $EXPCT 1
  212 format $EXPCT 1 s1024
  213 format $EXPCT 1 s2048
  214 format $EXPCT 1 s4096
  215 format $EXPCT 8
  216 format $EXPCT 8 s1024
  217 format $EXPCT 8 s2048
  218 format $EXPCT 8 s4096
  219 format $((EXPCT+1)) $((EXPCT+1))
  220 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
  221 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
  222 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
  223 format $EXPCT $EXPCT
  224 format $EXPCT $EXPCT s1024
  225 format $EXPCT $EXPCT s2048
  226 format $EXPCT $EXPCT s4096
  227 cleanup
  228 
  229 echo "# Create drive with misaligned opt-io to page-size (some bad USB enclosures)"
  230 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0, opt-io=33553920)"
  231 add_device dev_size_mb=32 sector_size=512 num_tgts=1 opt_blks=65535
  232 format $EXPCT
  233 format $EXPCT s1024
  234 format $EXPCT s2048
  235 format $EXPCT s4096
  236 format $EXPCT 1
  237 format $EXPCT 1 s1024
  238 format $EXPCT 1 s2048
  239 format $EXPCT 1 s4096
  240 format $EXPCT 8
  241 format $EXPCT 8 s1024
  242 format $EXPCT 8 s2048
  243 format $EXPCT 8 s4096
  244 format $((EXPCT+1)) $((EXPCT+1))
  245 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
  246 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
  247 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
  248 format $EXPCT $EXPCT
  249 format $EXPCT $EXPCT s1024
  250 format $EXPCT $EXPCT s2048
  251 format $EXPCT $EXPCT s4096
  252 cleanup
  253 
  254 echo "# Create desktop-class 4K drive w/ 1-sector shift (original bug report)"
  255 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=512)"
  256 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 lowest_aligned=1 num_tgts=1
  257 format $((EXPCT+1))
  258 format_fail $((EXPCT+1)) s1024
  259 format_fail $((EXPCT+1)) s2048
  260 format_fail $((EXPCT+1)) s4096
  261 format $EXPCT 1
  262 format $EXPCT 1 s1024
  263 format $EXPCT 1 s2048
  264 format $EXPCT 1 s4096
  265 format $EXPCT 8
  266 format $EXPCT 8 s1024
  267 format $EXPCT 8 s2048
  268 format $EXPCT 8 s4096
  269 format $((EXPCT+1)) $((EXPCT+1))
  270 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
  271 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
  272 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
  273 format $EXPCT $EXPCT
  274 format $EXPCT $EXPCT s1024
  275 format $EXPCT $EXPCT s2048
  276 format $EXPCT $EXPCT s4096
  277 cleanup
  278 
  279 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
  280 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
  281 add_device dev_size_mb=32 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
  282 format $((EXPCT+7))
  283 format_fail $((EXPCT+7)) s1024
  284 format_fail $((EXPCT+7)) s2048
  285 format_fail $((EXPCT+7)) s4096
  286 format $EXPCT 1
  287 format $EXPCT 1 s1024
  288 format $EXPCT 1 s2048
  289 format $EXPCT 1 s4096
  290 format $EXPCT 8
  291 format $EXPCT 8 s1024
  292 format $EXPCT 8 s2048
  293 format $EXPCT 8 s4096
  294 format $((EXPCT+1)) $((EXPCT+1))
  295 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
  296 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
  297 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
  298 format $EXPCT $EXPCT
  299 format $EXPCT $EXPCT s1024
  300 format $EXPCT $EXPCT s2048
  301 format $EXPCT $EXPCT s4096
  302 cleanup
  303 
  304 echo "# Create enterprise-class 4K drive"
  305 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
  306 add_device dev_size_mb=32 sector_size=4096 num_tgts=1 opt_blks=64
  307 format $EXPCT
  308 format $EXPCT s1024
  309 format $EXPCT s2048
  310 format $EXPCT s4096
  311 format $EXPCT 1
  312 format $EXPCT 1 s1024
  313 format $EXPCT 1 s2048
  314 format $EXPCT 1 s4096
  315 format $EXPCT 8
  316 format $EXPCT 8 s1024
  317 format $EXPCT 8 s2048
  318 format $EXPCT 8 s4096
  319 #FIXME: kernel limits issue?
  320 ##format $((EXPCT+1)) $((EXPCT+1))
  321 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
  322 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
  323 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
  324 format $EXPCT $EXPCT
  325 format $EXPCT $EXPCT s1024
  326 format $EXPCT $EXPCT s2048
  327 format $EXPCT $EXPCT s4096
  328 cleanup
  329 
  330 echo "# Create classic 512B drive and stack dm-linear"
  331 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
  332 add_device dev_size_mb=32 sector_size=512 num_tgts=1
  333 DEV2=$DEV
  334 DEV=/dev/mapper/$DEV_STACKED
  335 dmsetup create $DEV_STACKED --table "0 65536 linear $DEV2 0"
  336 format $EXPCT
  337 format $EXPCT s1024
  338 format $EXPCT s2048
  339 format $EXPCT s4096
  340 format $EXPCT 1
  341 format $EXPCT 1 s1024
  342 format $EXPCT 1 s2048
  343 format $EXPCT 1 s4096
  344 format $EXPCT 8
  345 format $EXPCT 8 s1024
  346 format $EXPCT 8 s2048
  347 format $EXPCT 8 s4096
  348 format $((EXPCT+1)) $((EXPCT+1))
  349 format_fail $((EXPCT+1)) $((EXPCT+1)) s1024
  350 format_fail $((EXPCT+1)) $((EXPCT+1)) s2048
  351 format_fail $((EXPCT+1)) $((EXPCT+1)) s4096
  352 format $EXPCT $EXPCT
  353 format $EXPCT $EXPCT s1024
  354 format $EXPCT $EXPCT s2048
  355 format $EXPCT $EXPCT s4096
  356 cleanup
  357 
  358 echo "# Create enterprise-class 4K drive with fs and LUKS images."
  359 # loop device here presents 512 block but images have 4k block
  360 # cryptsetup should properly use 4k block on direct-io
  361 add_device dev_size_mb=32 sector_size=4096 physblk_exp=0 num_tgts=1 opt_blks=64
  362 for file in $(ls img_fs_*.img.xz) ; do
  363     echo "Format using fs image $file."
  364     xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
  365     [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
  366     mount $DEV $MNT_DIR || skip "Mounting image is not available."
  367     echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $MNT_DIR/luks.img --offset 8192 || fail
  368     echo $PWD2 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $MNT_DIR/luks.img --header $MNT_DIR/luks_header.img || fail
  369     umount $MNT_DIR
  370 done
  371 cleanup