dd 到檔案和 dd 到實體磁碟有什麼區別

dd 到檔案和 dd 到實體磁碟有什麼區別

「openwrt-19.07.6-x86-64-combined-squashfs.img」映像檔的大小為 20MB。如果我將檔案名稱更改為“openwrt.raw”,我可以將其匯入 virt-manager 並啟動進入系統。但根分區“/”沒有空格。

如果我將「openwrt-19.07.6-x86-64-combined-squashfs.img」新增至 USB 磁碟機中,然後將 USB 磁碟機新增至新檔案中,將輸出檔案匯入 virt-manager 並啟動進入系統,根分割區的大小為“256MB”。

dd if=openwrt-19.07.6-x86-64-combined-squashfs.img of=/dev/sde
dd if=/dev/sde of=./openwrt_with_256MB_root_partition.raw

“openwrt_with_256MB_root_partition.raw”的大小與 USB 驅動器的大小相同。但如果我使用以下命令:

dd if=openwrt-19.07.6-x86-64-combined-squashfs.img of=/openwrt.raw

輸出檔案的大小與輸入檔案的大小相同。我明白這個行動是沒有意義的。但是有人可以解釋一下,當我將 256MB 根分割區新增到 USB 隨身碟時,是什麼讓其可用?有沒有辦法創建一個具有 256MB 根空間的映像文件,而無需實體 USB 驅動器或其他磁碟?

答案1

不知道 OpenWRT 現在在做什麼。

然而,您所做的只是在文件末尾添加一些垃圾資料。由於檔案大小最終是虛擬機器內部的裝置大小,因此很容易理解結果。

您也可以使用truncate

truncate -s256M openwrt-19.07.6-x86-64-combined-squashfs.img

嚴格來說,這是不一樣的,因為它會產生稀疏文件,但它更快且足夠好。

為了獲得完全相同的結果,您實際上需要將一些資料寫入文件,如下所示:

dd if=/dev/zero bs=1M count=236 >> openwrt-19.07.6-x86-64-combined-squashfs.img

您也可以在 中使用高級標誌dd。 256 - 20 = 236

與此相關的另一個工具是fallocate.


更新: 看圖,是這樣的:

$ fdisk -l openwrt-19.07.6-x86-generic-combined-squashfs.img
Disk openwrt-19.07.6-x86-generic-combined-squashfs.img: 19.5 MiB, 20450816 bytes, 39943 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf1f0223e

Device                                             Boot Start    End Sectors  Size Id Type
openwrt-19.07.6-x86-generic-combined-squashfs.img1 *      512  33279   32768   16M 83 Linux
openwrt-19.07.6-x86-generic-combined-squashfs.img2      33792 558079  524288  256M 83 Linux

(注意“尺寸”欄。)

第二個分割區應該包含壓縮的唯讀根映像一個可讀寫的 F2FS 區域,不知道它是如何運作的。也許它在第一次啟動時調整了大小或其他什麼。

相關內容