[LTP] [PATCH] commands/mkswap: Added new testcase to test mkswap(8).

Lei Li rocklee_104@outlook.com
Wed Nov 11 04:45:37 CET 2015


> Hi!
> Thanks for review!
>
> On 2015/11/09 10:00, Lei Li wrote:
>>> +setup()
>>> +{
>>> +    tst_require_root
>>> +
>>> +    tst_check_cmds losetup free mkswap
>>> +
>>> +    tst_tmpdir
>>> +
>>> +    TST_CLEANUP=cleanup
>>> +
>>> +    tst_acquire_device
>>> +
>>> +    local path=`losetup -a | grep "$TMPDIR"  | awk '{print $3}' | \
>>> +        cut -d '(' -f2 | cut -d ')' -f1`
>>> +    DEVICE_SIZE=$((`ls -l $path | awk '{print $5}'`/1024))
>> Hi!
>>      This testcase uses a loop device instead of a block device, but tst_acquire_device may offer you a block device, /opt/ltp/runltp -b /dev/sdb1 for example, then, "path" may be invilidate, and syntax error occurs in the line :
>>
>> DEVICE_SIZE=$((`ls -l $path | awk '{print $5}'`/1024))
>>
>> So, examination whether the "path" is validate is necessary.
>>
>
> Indeed, I missed the condition of block device.
> I think I could use DEVICE_SIZE=$((`blockdev --getsize64 $TST_DEVICE`/1024)) instead of via path.
>
>>> +
>>> +    PAGE_SIZE=`getconf PAGE_SIZE`
>>> +}
>>> +
>>> +cleanup()
>>> +{
>>> +    tst_release_device
>>> +
>>> +    tst_rmdir
>>> +}
>>> +
>>> +mkswap_verify()
>>> +{
>>> +    local ret=0
>>> +
>>> +    local before=`free | grep "Swap" | awk '{print $2}'`
>>> +
>>> +    if [ -z "$4" ]; then
>>> +        local swapsize=$DEVICE_SIZE
>>> +    else
>>> +        local swapsize=$4
>>> +    fi
>>> +
>>> +    if [ "$1" = "-p" ]; then
>>> +        local pagesize=$2
>>> +    else
>>> +        local pagesize=$PAGE_SIZE
>>> +    fi
>>> +
>>> +    if [ "$1" = "-L" ]; then
>>> +        local swap_op="-L"
>>> +        local swapfile=$2
>>> +    elif [ "$1" = "-U" ]; then
>>> +        local swap_op="-U"
>>> +        local swapfile=$2
>>> +    else
>>> +        local swap_op=""
>>> +        local swapfile=$3
>>> +    fi
>>> +
>>> +    swapon $swap_op $swapfile 2>/dev/null
>>> +    if [ $? -ne 0 ]; then
>>> +        tst_resm TINFO "can not do swapon on $swapfile."
>>> +        if [ $pagesize -ne $PAGE_SIZE ]; then
>>> +            return $ret
>>> +        fi
>>> +
>>> +        if [ $swapsize -gt $DEVICE_SIZE ]; then
>>> +            return $ret
>>> +        fi
>>> +    fi
>>> +
>>> +    local after=`free | grep "Swap" | awk '{print $2}'`
>>> +
>>> +    local est=16
>>> +    if [ $((after-before)) -lt $((swapsize-pagesize/1024-est)) ] || \
>>> +        [ $((after-before)) -gt $((swapsize-pagesize/1024+est)) ]; then
>>> +        ret=1
>>> +    fi
>>
>> How do you get this formula? I'm a newbie here :-)
>>
>
> I just compare device size used for swap with the actual increment of swap.
> The increment should be equal to the device size minus a page size, but in some cases, the result fluctuates.

Hi
   I read the code of mkswap, find nothing about the fluctuation but
the exact value(as you mentioned above, device size minus a page size).
And the fluctuation didn't show up in all of my test result.Would you
please give me some proof about the fluctuation ?

Thanks.
-- 
Rock Lee
rocklee_104@outlook.com


More information about the Ltp mailing list