"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/device-test" (13 Jan 2022, 13778 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 "device-test": 2.3.6_vs_2.4.0.

    1 #!/bin/bash
    2 
    3 [ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
    4 CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
    5 MNT_DIR="./mnt_luks"
    6 DEV_NAME="dummy"
    7 DEV_NAME2="ymmud"
    8 PWD1="93R4P4pIqAH8"
    9 PWD2="mymJeD8ivEhE"
   10 FAST_PBKDF_OPT="--pbkdf pbkdf2 --pbkdf-force-iterations 1000"
   11 SKIP_COUNT=0
   12 
   13 cleanup() {
   14     [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove --retry $DEV_NAME
   15     udevadm settle >/dev/null 2>&1
   16     if [ -d "$MNT_DIR" ] ; then
   17         umount -f $MNT_DIR 2>/dev/null
   18         rmdir $MNT_DIR 2>/dev/null
   19     fi
   20     rmmod scsi_debug >/dev/null 2>&1
   21 }
   22 
   23 fail()
   24 {
   25     [ -n "$1" ] && echo "FAIL $1"
   26     echo "FAILED backtrace:"
   27     while caller $frame; do ((frame++)); done
   28     cleanup
   29     exit 100
   30 }
   31 
   32 skip()
   33 {
   34     echo "TEST SKIPPED: $1"
   35     cleanup
   36     exit 77
   37 }
   38 
   39 add_device() {
   40     rmmod scsi_debug >/dev/null 2>&1
   41     [ -d /sys/module/scsi_debug ] && skip "Cannot use scsi_debug module (in use or compiled-in)."
   42 
   43     modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
   44     [ $? -ne 0 ] && skip "This kernel seems to not support proper scsi_debug module."
   45 
   46     sleep 1
   47     SCSI_DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
   48 
   49     [ -b "/dev/$SCSI_DEV" ] || fail "Cannot find $SCSI_DEV."
   50 }
   51 
   52 function dm_crypt_features()
   53 {
   54     modprobe dm-crypt >/dev/null 2>&1 || fail "dm-crypt failed to load"
   55     VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
   56     [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
   57 
   58     VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
   59     VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
   60     VER_PTC=$(echo $VER_STR | cut -f 3 -d.)
   61 
   62     [ $VER_MAJ -lt 1 ] && return
   63     [ $VER_MAJ -gt 1 ] && {
   64         DM_PERF_CPU=1
   65         DM_SECTOR_SIZE=1
   66         test -d /proc/sys/kernel/keys && DM_KEYRING=1
   67         return
   68     }
   69 
   70     [ $VER_MIN -lt 14 ] && return
   71     DM_PERF_CPU=1
   72     if [ $VER_MIN -ge 17 -o \( $VER_MIN -eq 14 -a $VER_PTC -ge 5 \) ]; then
   73         DM_SECTOR_SIZE=1
   74     fi
   75     if [ $VER_MIN -gt 18 -o \( $VER_MIN -eq 18 -a $VER_PTC -ge 1 \) ]; then
   76          test -d /proc/sys/kernel/keys && DM_KEYRING=1
   77     fi
   78 
   79     [ $VER_MIN -lt 22 ] && return
   80     DM_PERF_NO_WORKQUEUE=1
   81 }
   82 
   83 function dm_crypt_keyring_support()
   84 {
   85     VER_STR=$(dmsetup targets | grep crypt | cut -f2 -dv)
   86     [ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."
   87 
   88     VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
   89     VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
   90 
   91     # run the test with dm-crypt v1.15.0+ on purpose
   92     # the fix is in dm-crypt v1.18.1+
   93     [ $VER_MAJ -gt 1 ] && return 0
   94     [ $VER_MAJ -lt 1 ] && return 1
   95     [ $VER_MIN -ge 15 ]
   96 }
   97 
   98 format() # format
   99 {
  100     dd if=/dev/zero of=$DEV bs=1M count=32 >/dev/null 2>&1
  101 
  102     echo $PWD1 | $CRYPTSETUP luksFormat --type $1 $DEV -q  $FAST_PBKDF_OPT -c aes-cbc-essiv:sha256
  103     [ $? -ne 0 ] && fail "Format failed."
  104 
  105     # test some operation, just in case
  106     echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV -i1 --key-slot 1
  107     [ $? -ne 0 ] && fail "Keyslot add failed."
  108 
  109     $CRYPTSETUP -q luksKillSlot $DEV 1
  110     [ $? -ne 0 ] && fail "Keyslot removal failed."
  111 }
  112 
  113 check_sector_size() # $1 expected sector size
  114 {
  115     $CRYPTSETUP status $DEV_NAME | grep "sector size" | grep -q $1 || fail
  116     if [ $S -gt 512 ]; then
  117         dmsetup table $DEV_NAME | grep -q "sector_size:$1" || fail
  118     fi
  119 }
  120 
  121 if [ $(id -u) != 0 ]; then
  122     skip "You must be root to run this test, test skipped."
  123 fi
  124 
  125 dm_crypt_features
  126 
  127 [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
  128 
  129 echo "[1] Using tmpfs for image"
  130 DEV="$MNT_DIR/test.img"
  131 mount -t tmpfs none $MNT_DIR || skip "Mounting tmpfs not available."
  132 format luks1
  133 
  134 echo "[2] Kernel dmcrypt performance options"
  135 if [ -z "$DM_PERF_CPU" ]; then
  136     echo "TEST SKIPPED: dmcrypt options not available"
  137     SKIP_COUNT=$((SKIP_COUNT+1))
  138 else
  139     echo -n "PLAIN: same_cpu_crypt submit_from_cpus "
  140     echo -e "$PWD1" | $CRYPTSETUP open -q --type plain --hash sha256 $DEV $DEV_NAME --perf-same_cpu_crypt --perf-submit_from_crypt_cpus || fail
  141     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  142     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
  143     $CRYPTSETUP close $DEV_NAME || fail
  144     echo -n "allow_discards "
  145     echo -e "$PWD1" | $CRYPTSETUP open -q --type plain --hash sha256 $DEV $DEV_NAME --perf-same_cpu_crypt --allow-discards || fail
  146     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  147     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  148     $CRYPTSETUP close $DEV_NAME || fail
  149     echo -e "$PWD1" | $CRYPTSETUP open -q --type plain --hash sha256 $DEV $DEV_NAME || fail
  150     echo -e "$PWD1" | $CRYPTSETUP refresh --hash sha256 -q $DEV_NAME --perf-same_cpu_crypt --allow-discards || fail
  151     # Hash affects volume key for plain device. Check we can detect it
  152     echo -e "$PWD1" | $CRYPTSETUP refresh -q $DEV_NAME --hash sha512 --perf-same_cpu_crypt --allow-discards 2>/dev/null && fail
  153     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  154     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  155     echo -e "$PWD1" | $CRYPTSETUP refresh --hash sha256 -q $DEV_NAME --allow-discards || fail
  156     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  157     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt && fail
  158     echo -e "$PWD1" | $CRYPTSETUP refresh --hash sha256 -q $DEV_NAME || fail
  159     $CRYPTSETUP status $DEV_NAME | grep -q discards && fail
  160     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt && fail
  161     echo -e "$PWD1" | $CRYPTSETUP refresh --hash sha256 $DEV $DEV_NAME2 2>/dev/null && fail
  162     if [ -n "$DM_PERF_NO_WORKQUEUE" ]; then
  163         echo -n "no_read_workqueue no_write_workqueue"
  164         echo -e "$PWD1" | $CRYPTSETUP refresh --hash sha256 -q $DEV_NAME --perf-no_read_workqueue --perf-no_write_workqueue || fail
  165         $CRYPTSETUP status $DEV_NAME | grep -q no_read_workqueue || fail
  166         $CRYPTSETUP status $DEV_NAME | grep -q no_write_workqueue || fail
  167     fi
  168     $CRYPTSETUP close $DEV_NAME || fail
  169     echo
  170 
  171     echo -n "LUKS: same_cpu_crypt submit_from_cpus "
  172     echo -e "$PWD1" | $CRYPTSETUP open --type luks1 $DEV $DEV_NAME --perf-same_cpu_crypt --perf-submit_from_crypt_cpus || fail
  173     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  174     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
  175     $CRYPTSETUP close $DEV_NAME || fail
  176     echo -n "allow_discards "
  177     echo -e "$PWD1" | $CRYPTSETUP open --type luks1 $DEV $DEV_NAME --perf-same_cpu_crypt --allow-discards || fail
  178     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  179     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  180     $CRYPTSETUP close $DEV_NAME || fail
  181     echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME || fail
  182     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV_NAME --allow-discards || fail
  183     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  184     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt && fail
  185     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV_NAME --allow-discards --perf-same_cpu_crypt || fail
  186     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  187     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  188     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV_NAME || fail
  189     $CRYPTSETUP status $DEV_NAME | grep -q discards && fail
  190     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt && fail
  191     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME2 2>/dev/null && fail
  192     if [ -n "$DM_PERF_NO_WORKQUEUE" ]; then
  193         echo -n "no_read_workqueue no_write_workqueue"
  194         echo -e "$PWD1" | $CRYPTSETUP refresh $DEV_NAME  --perf-no_read_workqueue --perf-no_write_workqueue || fail
  195         $CRYPTSETUP status $DEV_NAME | grep -q no_read_workqueue || fail
  196         $CRYPTSETUP status $DEV_NAME | grep -q no_write_workqueue || fail
  197     fi
  198     $CRYPTSETUP close $DEV_NAME || fail
  199     echo
  200 
  201     format luks2
  202     echo -n "LUKS2: same_cpu_crypt submit_from_cpus "
  203     echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME --perf-same_cpu_crypt --perf-submit_from_crypt_cpus --persistent || fail
  204     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  205     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
  206     $CRYPTSETUP close $DEV_NAME || fail
  207     # Stored in metadata
  208     echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME || fail
  209     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  210     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
  211     $CRYPTSETUP close $DEV_NAME || fail
  212     echo -n "allow_discards [persistent flags] "
  213     echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME --perf-same_cpu_crypt --allow-discards --persistent || fail
  214     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  215     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  216     $CRYPTSETUP close $DEV_NAME || fail
  217     echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME || fail
  218     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  219     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  220     $CRYPTSETUP close $DEV_NAME || fail
  221 
  222     echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME --persistent || fail
  223     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt && fail
  224     $CRYPTSETUP status $DEV_NAME | grep -q discards && fail
  225     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME --perf-same_cpu_crypt --perf-submit_from_crypt_cpus --persistent || fail
  226     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  227     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
  228     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV_NAME || fail
  229     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  230     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
  231     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME --perf-same_cpu_crypt --allow-discards --persistent || fail
  232     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  233     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  234     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV_NAME || fail
  235     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  236     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  237     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME --perf-submit_from_crypt_cpus || fail
  238     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt || fail
  239     $CRYPTSETUP status $DEV_NAME | grep -q discards || fail
  240     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus || fail
  241     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME || fail
  242     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus && fail
  243     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME --persistent || fail
  244     $CRYPTSETUP status $DEV_NAME | grep -q same_cpu_crypt && fail
  245     $CRYPTSETUP status $DEV_NAME | grep -q discards && fail
  246     $CRYPTSETUP status $DEV_NAME | grep -q submit_from_crypt_cpus && fail
  247     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME --disable-keyring || fail
  248     $CRYPTSETUP status $DEV_NAME | grep -q keyring && fail
  249     if [ -n "$DM_KEYRING" ]; then
  250         echo -n "keyring "
  251         echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME || fail
  252         $CRYPTSETUP status $DEV_NAME | grep -q keyring || fail
  253     fi
  254     if [ -n "$DM_PERF_NO_WORKQUEUE" ]; then
  255         echo -n "no_read_workqueue no_write_workqueue"
  256         echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME --perf-no_read_workqueue --perf-no_write_workqueue --persistent || fail
  257         $CRYPTSETUP status $DEV_NAME | grep -q no_read_workqueue || fail
  258         $CRYPTSETUP status $DEV_NAME | grep -q no_write_workqueue || fail
  259         $CRYPTSETUP close $DEV_NAME || fail
  260         echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME || fail
  261         $CRYPTSETUP status $DEV_NAME | grep -q no_read_workqueue || fail
  262         $CRYPTSETUP status $DEV_NAME | grep -q no_write_workqueue || fail
  263     fi
  264     echo -e "$PWD1" | $CRYPTSETUP refresh $DEV $DEV_NAME2 2>/dev/null && fail
  265     $CRYPTSETUP close $DEV_NAME || fail
  266     echo
  267 fi
  268 
  269 echo "[3] Kernel dmcrypt sector size options"
  270 echo -e "$PWD1" | $CRYPTSETUP open --type plain --hash sha256 $DEV $DEV_NAME --sector-size 4096 >/dev/null 2>&1
  271 ret=$?
  272 [ -z "$DM_SECTOR_SIZE" -a $ret -eq 0 ] && fail "cryptsetup activated device with --sector-size option on incompatible kernel!"
  273 if [ $ret -ne 0 ] ; then
  274     SKIP_COUNT=$((SKIP_COUNT+1))
  275     if [ $SKIP_COUNT -ge 2 ]; then
  276         skip "dmcrypt sector-size option not available"
  277     fi
  278     echo "TEST SKIPPED: dmcrypt sector-size option not available"
  279 else
  280     $CRYPTSETUP close $DEV_NAME || fail
  281 
  282     echo -n "PLAIN sector size:"
  283     echo -e "$PWD1" | $CRYPTSETUP open --type plain --hash sha256 $DEV $DEV_NAME --sector-size 1234 >/dev/null 2>&1 && fail
  284     for S in 512 1024 2048 4096; do
  285         echo -n "[$S]"
  286         echo -e "$PWD1" | $CRYPTSETUP open -q --type plain --hash sha256 $DEV $DEV_NAME --sector-size $S || fail
  287         check_sector_size $S
  288         $CRYPTSETUP close $DEV_NAME || fail
  289     done
  290 
  291     echo -e "$PWD1" | $CRYPTSETUP open --type plain --hash sha256 $DEV $DEV_NAME --iv-large-sectors >/dev/null 2>&1 && fail
  292     for S in 1024 2048 4096; do
  293         echo -n "[$S/IV]"
  294         echo -e "$PWD1" | $CRYPTSETUP open -q --type plain --hash sha256 $DEV $DEV_NAME --sector-size $S --iv-large-sectors || fail
  295         check_sector_size $S
  296         dmsetup table $DEV_NAME | grep -q "iv_large_sectors" || fail
  297         $CRYPTSETUP close $DEV_NAME || fail
  298     done
  299     echo
  300 
  301     echo -n "LUKS2 sector size:"
  302     echo -e "$PWD1" | $CRYPTSETUP luksFormat --type luks2 -$DEV --sector-size 1234 >/dev/null 2>&1 && fail
  303     for S in 512 1024 2048 4096; do
  304         echo -n "[$S]"
  305         echo -e "$PWD1" | $CRYPTSETUP -q luksFormat --type luks2 --pbkdf pbkdf2 --pbkdf-force-iterations 1000 $DEV --sector-size $S || fail
  306         echo -e "$PWD1" | $CRYPTSETUP open $DEV $DEV_NAME || fail
  307         check_sector_size $S
  308         $CRYPTSETUP close $DEV_NAME || fail
  309     done
  310     echo
  311 fi
  312 
  313 echo "[4] Disappeared device test:"
  314 KEY="00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
  315 for F in LUKS1 LUKS2 BITLK TCRYPT; do
  316     add_device dev_size_mb=1 sector_size=512 num_tgts=1 lbpu=1
  317     echo -n "$F"
  318     # Fake CRYPT UUID to force code to parse type-specific path
  319     dmsetup create $DEV_NAME --uuid CRYPT-$F-$DEV_NAME --table "0 1024 crypt aes-xts-plain64 $KEY 16 /dev/$SCSI_DEV 16"
  320     $CRYPTSETUP status $DEV_NAME >/dev/null 2>&1 || fail
  321     echo 1 > /sys/block/$SCSI_DEV/device/delete
  322     udevadm settle >/dev/null 2>&1
  323     $CRYPTSETUP status $DEV_NAME >/dev/null 2>&1 || fail
  324     dmsetup remove $DEV_NAME --retry || fail
  325     rmmod scsi_debug >/dev/null 2>&1
  326     echo -n "[OK] "
  327 done
  328 echo
  329 
  330 cleanup
  331 exit 0