zoukankan      html  css  js  c++  java
  • ADB SHELL DD的秘密


    问题

    在工作中会遇到需要模拟存储空间不足的场景,以验证某功能点或复现某些特殊bug。常规做法是通过手工拷贝大文件到测试设备,模拟存储空间不足,当涉及机型适配时得在N个设备重复操作N次,手工操作非常繁琐,该如何高效解决这个问题呢?

    解决方案

    是否有方法可以模拟设备存储空间不足的场景,此时联想到adb shell dd命令就可以很好地实现我们的需求,它支持生成指定大小的块文件,具体命令和参数解释如下

    adb shell dd命令

    adb shell dd if=/dev/zero of=/sdcard/tmp/auto_dd_file bs=1024 count=1

    ###参数含义
    if=文件:读取内容,而非标准输入的数据
    of=文件:将数
    据写入的文件,而不在标准输出显示
    bs=bytes:同时设置读入/输出的块大小为bytes字节,未填写时默认512 bytes
    ibs:一次读入bytes个字节,即指定一个块大小为bytes字节
    obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes字节。
    count :创建/拷贝blocks个数,块大小等于bs指定的字节数。

    注:单位是byte,如需要1000M大文件,bs=1024*1024*100
    ,单位不支持M或Kb

    如果要生成一个10M和20M文件,可以这样使用

    C:Userszhongyaqi>adb shell dd if=/dev/zero of=/sdcard/tmp/auto_dd_file1 bs=102
    4*1024*10 count=1
    1+0 records in
    1+0 records out
    10485760 bytes transferred in 0.042 secs (249660952 bytes/sec)

    C:Userszhongyaqi>adb shell dd if=/dev/zero of=/sdcard/tmp/auto_dd_file2 bs=102
    4*1024*10 count=2
    2+0 records in
    2+0 records out
    20971520 bytes transferred in 0.081 secs (258907654 bytes/sec)

    验证执行结果,在/sdcard/tmp/目录下生成了10M和20M的文件

    C:Userszhongyaqi>adb shell
    PD1709:/ $ cd /sdcard/tmp/
    cd /sdcard/tmp/
    PD1709:/sdcard/tmp $ ls -l -h
    ls -l -h
    total 131M
    -rw-rw---- 1 root sdcard_rw 10M 2020-04-25 17:11 auto_dd_file1
    -rw-rw---- 1 root sdcard_rw 20M 2020-04-25 17:11 auto_dd_file2


    Python脚本实现

    在上一篇文章中,实现批量复制文件前,进行了检查adb连接状态等操作,同样该该操作也适用于本篇文章使用的场景,有兴趣的请参考Python脚本葵花宝典之一键批量复制文件

     

    def run_create_file(self, byte, count, path):
    """
    :param byte: The unit is byte
    :param count: Number of blocks created
    :param path: THE path of file that includes file name
    :return:
    for example
    byte = 1024*1024*100
    count = 1
    path = '/sdcard/tmp/test_dd'
    """
    print("step2 Create file and check status")
    ADB_SHELL_DD = "adb shell dd if=/dev/zero of=%s bs=%d count=%d " % (path,byte,count)
    os.system(ADB_SHELL_DD)
    adbfile = os.popen(ADB_SHELL_DD, "r")
    result = adbfile.read()
    if "%s bytes transferred in" % str(byte) in result:
    print("Create file success,file size is %sM ,and file path is %s " % (str(byte/1024/1024),path))
    else:
    print("Create file failed ")

    脚本执行结果如下

    step1 Check adb status
    adb connect success

    step2 Create file and check status
    1+0 records in
    1+0 records out
    104857600 bytes transferred in 0.179 secs (585796648 bytes/sec)
    Create file success,file size is 100.0M ,and file path is /sdcard/tmp/test_dd

    adb shell 其他命令

     

    adb shell dd还有复制文件的功能,如下

    adb shell dd if=/sdcard/DCIM/camera/timg.jpg of=/sdcard/tmp/timg_cp.jpg
    #以上命令将/sdcard/DCIM/camera/timg.jpg文件复制到/sdcard/tmp/timg_cp.jpg

    如果想了解adb shell 还支持哪些命令,adb shell 进入/system/bin目录,执行ls -l即可。

    由于文章长度限制,仅截取部分结果如下

    lrwxr-xr-x 1 root shell       6 2009-01-01 00:00 chown -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 chroot -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 chrt -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 cksum -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 clear -> toybox
    -rwxr-xr-x 1 root shell 479432 2009-01-01 00:00 climax
    -rwxr-xr-x 1 root shell 65208 2009-01-01 00:00 cmd
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 cmp -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 comm -> toybox
    -rwxr-xr-x 1 root shell 207 2009-01-01 00:00 content
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 cp -> toybox
    -rwxr-xr-x 1 root shell 137224 2009-01-01 00:00 cp_log
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 cpio -> toybox
    -rwxr-xr-x 1 root shell 102080 2009-01-01 00:00 crash_dump32
    -rwxr-xr-x 1 root shell 123360 2009-01-01 00:00 crash_dump64
    -rwxr-xr-x 1 root shell 410384 2009-01-01 00:00 curl
    -rwxr-xr-x 1 root shell 11040 2009-01-01 00:00 custtest
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 cut -> toybox
    lrwxr-xr-x 1 root shell 10 2009-01-01 00:00 dalvikvm -> dalvikvm64
    -rwxr-xr-x 1 root shell 24648 2009-01-01 00:00 dalvikvm32
    -rwxr-xr-x 1 root shell 19488 2009-01-01 00:00 dalvikvm64
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 date -> toybox
    lrwxr-xr-x 1 root shell 7 2009-01-01 00:00 dd -> toolbox
    -rwxr-xr-x 1 root shell 11184 2009-01-01 00:00 debuggerd
    -rwxr-xr-x 1 root shell 141940 2009-01-01 00:00 dex2oat
    -rwxr-xr-x 1 root shell 27784 2009-01-01 00:00 dexdiag
    -rwxr-xr-x 1 root shell 104384 2009-01-01 00:00 dexdump
    -rwxr-xr-x 1 root shell 15416 2009-01-01 00:00 dexlist
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 df -> toybox
    -rwxr-xr-x 1 root shell 11152 2009-01-01 00:00 dhrystone64_bit.elf
    -rwxr-xr-x 1 root shell 19664 2009-01-01 00:00 diag_command
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 diff -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 dirname -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 dmesg -> toybox
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 dos2unix -> toybox
    -rwxr-xr-x 1 root shell 156 2009-01-01 00:00 dpm
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 du -> toybox
    -rwxr-xr-x 1 root shell 40336 2009-01-01 00:00 dumpsys
    -rwxr-xr-x 1 root shell 6904 2009-01-01 00:00 ebtables
    lrwxr-xr-x 1 root shell 6 2009-01-01 00:00 echo -> toybox

    猜你喜欢

  • 相关阅读:
    python 命令行传参
    chardet模块下载&安装
    【解决方案】编码问题:UnicodeDecodeError: 'XXX' codec can't decode byte 0xad in position...的错误
    [解决方案]Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
    perl笔记
    字符串操作
    c++错误及解决方案随记
    本地Java程序访问HTTPs遇到的问题
    学习笔记 : 异常处理
    学习笔记 : python 文件操作
  • 原文地址:https://www.cnblogs.com/echoqi/p/12775805.html
Copyright © 2011-2022 走看看