ADB SHELL DD的祕密

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:\Users\zhongyaqi>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:\Users\zhongyaqi>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:\Users\zhongyaqi>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\n")

腳本執行結果如下

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章