From nobody Thu Feb 12 22:04:32 2026 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFF58140389 for ; Fri, 7 Jun 2024 04:29:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734577; cv=none; b=UCCoR6mIh9uX+O71qgLh/RVGhp5UIm8VGb1fosHwGwNEICtODHZye2Hu6K8skEY/nVnM7ciyp2zRm4SS9gyc07hdjgE5M8HaQkS4ygLYjlytyXCASAoja54V0NG1bDOcuv2uHyyFVsv1f+jg5+5S4Iy5FgvcdHY4l2zNC+8SrhA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734577; c=relaxed/simple; bh=3gNVX/tR3WL/3TAimSewXAuyfUleqp1YDP5iJLC54Hw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=grdKZGyWtcnkBZUZDEosc8kkMu5oaX0+RIw6IgOhCEEABvkl7+Eyrmce5J609ovbyxHmbaz5q11QkaK6qYg0OmhlZBBfKFrv4R2rx/mS3LDB48ZW2H4Z2aZQbZUNHW4oMf1qM4/xSwLNfrDqvd2diq9608fE5Wp662eTWR9WgBk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VwSmb2nLdzwPFT; Fri, 7 Jun 2024 12:25:35 +0800 (CST) Received: from kwepemm600013.china.huawei.com (unknown [7.193.23.68]) by mail.maildlp.com (Postfix) with ESMTPS id 4FA79140203; Fri, 7 Jun 2024 12:29:19 +0800 (CST) Received: from huawei.com (10.175.104.67) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 7 Jun 2024 12:27:39 +0800 From: Zhihao Cheng To: , , , , , CC: , Subject: [RFC PATCH mtd-utils 099/110] tests: Add common libs for testing fsck.ubifs/mkfs.ubifs Date: Fri, 7 Jun 2024 12:26:04 +0800 Message-ID: <20240607042615.2069840-100-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240607042615.2069840-1-chengzhihao1@huawei.com> References: <20240607042615.2069840-1-chengzhihao1@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600013.china.huawei.com (7.193.23.68) Content-Type: text/plain; charset="utf-8" This is a preparation for adding testcases for fsck.ubifs and mkfs.ubifs. Add some common functions, for example: powercut, load_mtdram, mount_ubifs, encryption operations, etc. Signed-off-by: Zhihao Cheng --- .gitignore | 1 + Makefile.am | 1 + configure.ac | 3 +- tests/ubifs_tools-tests/Makemodule.am | 2 + tests/ubifs_tools-tests/lib/common.sh.in | 350 +++++++++++++++++++++++++++= ++++ 5 files changed, 356 insertions(+), 1 deletion(-) create mode 100644 tests/ubifs_tools-tests/Makemodule.am create mode 100755 tests/ubifs_tools-tests/lib/common.sh.in diff --git a/.gitignore b/.gitignore index 3eb66c14..de0fce1f 100644 --- a/.gitignore +++ b/.gitignore @@ -113,6 +113,7 @@ tests/fs-tests/stress/fs_stress00.sh tests/fs-tests/stress/fs_stress01.sh tests/ubi-tests/runubitests.sh tests/ubi-tests/ubi-stress-test.sh +tests/ubifs_tools-tests/lib/common.sh =20 # # Files generated by autotools diff --git a/Makefile.am b/Makefile.am index 887ce938..0ebd45bb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -63,6 +63,7 @@ include tests/jittertest/Makemodule.am include tests/checkfs/Makemodule.am include tests/fs-tests/Makemodule.am include tests/mtd-tests/Makemodule.am +include tests/ubifs_tools-tests/Makemodule.am endif =20 if UNIT_TESTS diff --git a/configure.ac b/configure.ac index cf3a959e..d32541a1 100644 --- a/configure.ac +++ b/configure.ac @@ -289,7 +289,8 @@ AC_CONFIG_FILES([tests/fs-tests/fs_help_all.sh tests/fs-tests/stress/fs_stress00.sh tests/fs-tests/stress/fs_stress01.sh tests/ubi-tests/runubitests.sh - tests/ubi-tests/ubi-stress-test.sh]) + tests/ubi-tests/ubi-stress-test.sh + tests/ubifs_tools-tests/lib/common.sh]) =20 AC_OUTPUT([Makefile]) =20 diff --git a/tests/ubifs_tools-tests/Makemodule.am b/tests/ubifs_tools-test= s/Makemodule.am new file mode 100644 index 00000000..265c9cc7 --- /dev/null +++ b/tests/ubifs_tools-tests/Makemodule.am @@ -0,0 +1,2 @@ +test_SCRIPTS +=3D \ + tests/ubifs_tools-tests/lib/common.sh diff --git a/tests/ubifs_tools-tests/lib/common.sh.in b/tests/ubifs_tools-t= ests/lib/common.sh.in new file mode 100755 index 00000000..5a07ebcd --- /dev/null +++ b/tests/ubifs_tools-tests/lib/common.sh.in @@ -0,0 +1,350 @@ +#!/bin/sh +# Copyright (c), 2024, Huawei Technologies Co, Ltd. +# Author: Zhihao Cheng +# +# Provide basic functions. + +UBI_NUM=3D0 +DEV=3D/dev/ubi0_0 +MNT=3D/mnt/test_file_system +TMP_FILE=3D/tmp/ubifs_test_file +LOG_FILE=3D/tmp/ubifs_log +KEY_FILE=3D/tmp/key +nandsim_patt=3D"NAND simulator" +mtdram_patt=3D"mtdram test device" + +# fsck returning code +FSCK_OK=3D0 # No errors +FSCK_NONDESTRUCT=3D1 # File system errors corrected +FSCK_REBOOT=3D2 # System should be rebooted +FSCK_UNCORRECTED=3D4 # File system errors left uncorrected +FSCK_ERROR=3D8 # Operational error +FSCK_USAGE=3D16 # Usage or syntax error +FSCK_CANCELED=3D32 # Aborted with a signal or ^ +FSCK_LIBRARY=3D128 # Shared library error + +function fatal() +{ + echo "Error: $1" 1>&2 + exit 1 +} + +# All loaded modules(mtdram/nandsim/ubi/ubifs) won't be removed if errors +# happen, it is useful to debug based on the UBIFS image. +function cleanup_handler() +{ + local ret=3D"$1" + + if [ "$ret" =3D=3D "0" ]; then + umount $MNT >/dev/null 2>&1 ||: + modprobe -r ubifs >/dev/null 2>&1 ||: + modprobe -r ubi >/dev/null 2>&1 ||: + modprobe -r nandsim >/dev/null 2>&1 ||: + modprobe -r mtdram >/dev/null 2>&1 ||: + rm -rf $MNT >/dev/null 2>&1 ||: + rm -f $TMP_FILE >/dev/null 2>&1 ||: + rm -f $KEY_FILE >/dev/null 2>&1 ||: + rm -f $LOG_FILE >/dev/null 2>&1 ||: + exit 0 + else + exit 1 + fi +} +trap 'cleanup_handler $?' EXIT +trap 'cleanup_handler 1' HUP PIPE INT QUIT TERM + +function find_mtd_device() +{ + printf "%s" "$(grep "$1" /proc/mtd | sed -e "s/^mtd\([0-9]\+\):.*$/\1/")" +} + +function powercut() +{ + dmesg -c > /dev/null + echo 1 > /sys/kernel/debug/ubifs/tst_recovery; + while true; + do + msg=3D`dmesg -c | grep "Power cut emulated"`; + if [[ "$msg" !=3D "" ]]; + then + break; + fi + ro_error=3D`cat /sys/kernel/debug/ubifs/ubi${UBI_NUM}_0/ro_error` + if [[ $ro_error !=3D 0 ]]; then + break; + fi + done + echo 0 > /sys/kernel/debug/ubifs/tst_recovery +} + +# Load mtdram with specified size and PEB size +# Usage: load_mtdram +# 1. Flash size is specified in MiB +# 2. PEB size is specified in KiB +function load_mtdram() +{ + local size=3D"$1"; shift + local peb_size=3D"$1"; shift + + size=3D"$(($size * 1024))" + modprobe mtdram total_size=3D"$size" erase_size=3D"$peb_size" +} + +function check_fsstress() +{ + cmd=3D`fsstress | grep "op_name"` + if ! [[ "$cmd" =3D~ "op_name" ]]; then + fatal "fsstress is not found" + fi +} + +# Check error messages +function check_err_msg() +{ + msg=3D`dmesg | grep -E "dump_stack|UBIFS error|switched to read-only mode= "`; + if [[ "$msg" !=3D "" ]] + then + dmesg + fatal "error message detected!" + fi + dmesg -c > /dev/null +} + +# Iterate all files under certain dir +# $1: dir +# $2: "md5sum" means that need record md5 for regular file, otherwise don'= t record md5 for regular file +function read_dir() { + for file in `ls -a $1` + do + cur_f=3D$1"/"$file + if [ -b $cur_f ] + then + major=3D`stat -c %t $cur_f` + minor=3D`stat -c %T $cur_f` + echo "block $cur_f $major $minor" >> $TMP_FILE + elif [ -c $cur_f ] + then + major=3D`stat -c %t $cur_f` + minor=3D`stat -c %T $cur_f` + echo "char $cur_f $major $minor" >> $TMP_FILE + elif [ -L $cur_f ] + then + link=3D`stat -c %N $cur_f` + echo "symlink $cur_f $link" >> $TMP_FILE + elif [ -S $cur_f ] + then + echo "sock $cur_f" >> $TMP_FILE + elif [ -p $cur_f ] + then + echo "fifo $cur_f" >> $TMP_FILE + elif [ -f $cur_f ] + then + sz=3D`stat -c %s $cur_f` + if [[ "$2" !=3D "md5sum" ]]; then + echo "reg $cur_f $sz" >> $TMP_FILE + else + md5=3D`md5sum $cur_f | awk '{print $1}'` + echo "reg $cur_f $md5 $sz" >> $TMP_FILE + fi + elif [ -d $cur_f ] + then + if [[ $file !=3D '.' && $file !=3D '..' ]] + then + echo "dir $cur_f" >> $TMP_FILE + read_dir $1"/"$file $2 + fi + else + fatal "record unknown file type $cur_f" + fi + done +} + +# Check whether there are files lost after fsck/mkfs +# $1: "md5sum" means need record md5 for regular file, otherwise don't che= ck md5 for regular file +function parse_dir() +{ + while read line + do + array=3D(${line//\ / }); + f_type=3D${array[0]}; + cur_f=3D${array[1]}; + cur_info=3D"" + if [[ "$f_type" =3D~ "block" ]] + then + major=3D`stat -c %t $cur_f` + minor=3D`stat -c %T $cur_f` + cur_info=3D"block $cur_f $major $minor" + elif [[ "$f_type" =3D~ "char" ]] + then + major=3D`stat -c %t $cur_f` + minor=3D`stat -c %T $cur_f` + cur_info=3D"char $cur_f $major $minor" + elif [[ "$f_type" =3D~ "symlink" ]] + then + link=3D`stat -c %N $cur_f` + cur_info=3D"symlink $cur_f $link" + elif [[ "$f_type" =3D~ "sock" ]] + then + cur_info=3D"sock $cur_f" + elif [[ "$f_type" =3D~ "fifo" ]] + then + cur_info=3D"fifo $cur_f" + elif [[ "$f_type" =3D~ "reg" ]] + then + sz=3D`stat -c %s $cur_f` + if [[ "$1" !=3D "md5sum" ]]; then + cur_info=3D"reg $cur_f $sz" + else + md5=3D`md5sum $cur_f | awk '{print $1}'` + cur_info=3D"reg $cur_f $md5 $sz" + fi + elif [[ "$f_type" =3D~ "dir" ]] + then + cur_info=3D"dir $cur_f" + else + fatal "parse unknown file type $cur_f" + fi + if [[ "$cur_info" !=3D "$line" ]] + then + fatal "current info $cur_info, but expect $line" + fi + done < $TMP_FILE +} + +function authentication() +{ + keyctl clear @s + res=3D$? + if [[ $res !=3D 0 ]]; then + fatal "keyctl is not found" + fi + keyctl add logon ubifs:foo 12345678901234567890123456789012 @s +} + +function encryption_gen_key() +{ + # CONFIG_FS_ENCRYPTION=3Dy + head -c 64 /dev/urandom > $KEY_FILE + cmd=3D`fscryptctl -h | grep "set_policy"` + if ! [[ "$cmd" =3D~ "set_policy" ]]; then + fatal "fscryptctl is not found" + fi +} + +function encryption_set_key() +{ + mnt=3D$1 + # https://github.com/google/fscryptctl + key=3D$(fscryptctl add_key $mnt < $KEY_FILE) + fscryptctl set_policy $key $mnt + #fscryptctl get_policy $mnt + ret=3D$? + if [[ $ret !=3D 0 ]]; then + fatal "set encryption policy failed" + fi +} + +function mount_ubifs() +{ + local dev=3D$1; + local mnt=3D$2; + local auth=3D$3; + local noatime=3D$4; + local option=3D""; + if [[ "$noatime" =3D=3D "noatime" ]]; then + option=3D"-o noatime" + fi + if [[ "$auth" =3D=3D "authentication" ]]; then + authentication + if [[ "$option" =3D=3D "" ]]; then + option=3D"-o auth_key=3Dubifs:foo,auth_hash_name=3Dsha256" + else + option=3D"$option,auth_key=3Dubifs:foo,auth_hash_name=3Dsha256" + fi + fi + mount -t ubifs $option $dev $mnt +} + +function enable_chkfs() +{ + echo 1 > /sys/kernel/debug/ubifs/chk_fs + echo 1 > /sys/kernel/debug/ubifs/chk_general + echo 1 > /sys/kernel/debug/ubifs/chk_index + echo 1 > /sys/kernel/debug/ubifs/chk_lprops + echo 1 > /sys/kernel/debug/ubifs/chk_orphans +} + +function disable_chkfs() +{ + echo 0 > /sys/kernel/debug/ubifs/chk_fs + echo 0 > /sys/kernel/debug/ubifs/chk_general + echo 0 > /sys/kernel/debug/ubifs/chk_index + echo 0 > /sys/kernel/debug/ubifs/chk_lprops + echo 0 > /sys/kernel/debug/ubifs/chk_orphans +} + +function inject_mem_err() +{ + # CONFIG_FAILSLAB=3Dy + # CONFIG_FAIL_PAGE_ALLOC=3Dy + local pid=3D$1; + + if ! [ -f /sys/kernel/debug/failslab/probability ]; then + fatal "failslab is not enabled, injection failed" + fi + if ! [ -f /sys/kernel/debug/fail_page_alloc/probability ]; then + fatal "fail_page_alloc is not enabled, injection failed" + fi + + echo 1 > /proc/$pid/make-it-fail + + echo Y > /sys/kernel/debug/failslab/task-filter + echo 1 > /sys/kernel/debug/failslab/probability # 1% failure + echo 10000 > /sys/kernel/debug/failslab/times + echo 1 > /sys/kernel/debug/failslab/verbose + echo N > /sys/kernel/debug/failslab/ignore-gfp-wait + + echo Y > /sys/kernel/debug/fail_page_alloc/task-filter + echo 1 > /sys/kernel/debug/fail_page_alloc/probability + echo 10000 > /sys/kernel/debug/fail_page_alloc/times + echo 0 > /sys/kernel/debug/fail_page_alloc/verbose + echo N > /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait +} + +function cancel_mem_err() +{ + echo 0 > /sys/kernel/debug/failslab/probability + echo 0 > /sys/kernel/debug/failslab/times + echo 0 > /sys/kernel/debug/failslab/verbose + echo N > /sys/kernel/debug/failslab/task-filter + echo Y > /sys/kernel/debug/failslab/ignore-gfp-wait + + echo 0 > /sys/kernel/debug/fail_page_alloc/probability + echo 0 > /sys/kernel/debug/fail_page_alloc/times + echo 1 > /sys/kernel/debug/fail_page_alloc/verbose + echo N > /sys/kernel/debug/fail_page_alloc/task-filter + echo Y > /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait +} + +function inject_io_err() +{ + if ! [ -f /sys/kernel/debug/ubi/ubi$UBI_NUM/tst_emulate_io_failures ]; th= en + fatal "tst_emulate_io_failures is not enabled, skip injection" + fi + + echo 1 > /sys/kernel/debug/ubi/ubi$UBI_NUM/tst_emulate_io_failures +} + +function cancel_io_err() +{ + echo 0 > /sys/kernel/debug/ubi/ubi$UBI_NUM/tst_emulate_io_failures +} + +if ! [ -d $MNT ]; then + mkdir -p $MNT +fi + +modprobe ubi || fatal "common.sh: cannot load ubi" +modprobe ubifs || fatal "common.sh: cannot load ubifs" +modprobe -r ubifs +modprobe -r ubi --=20 2.13.6