格式化分割區循環檔案中的檔案系統會損壞分割區表

格式化分割區循環檔案中的檔案系統會損壞分割區表

我創建了一個大約 3 GB 的文件,名為 test 並將其分區為fdisk.然後,我使用該檔案(和偏移量)設定一個循環設備,以便格式化第二個分割區中的 ext4 檔案系統。然而,在我這樣做之後,我的分區表似乎被截斷了!我很確定我已經正確計算了偏移量...

為什麼會發生這種情況?

請注意,該分區表有很多分區。我必須增加內核max_loop設定才能獲得比正常 8 個循環設備更多的數量。目前,max_loop設定為 32。

以下是捕獲的輸出,準確顯示了正在發生的情況:

steve@steve-VirtualBox:/s/src/scripted/image$ ls -l test
-rw-r--r-- 1 root root 3072000000 Jul 21 17:13 test
steve@steve-VirtualBox:/s/src/scripted/image$ fdisk -l test

Disk test: 3072 MB, 3072000000 bytes
255 heads, 63 sectors/track, 373 cylinders, total 6000000 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
Disk identifier: 0x4b8bbe0f

Device Boot      Start         End      Blocks   Id  System
 test1   *        2048       67583       32768    c  W95 FAT32 (LBA)
 test2           67584      133119       32768   83  Linux
 test3          133120     5999999     2933440    5  Extended
 test5          135168     1708031      786432   83  Linux
 test6         1710080     3282943      786432   83  Linux
 test7         3284992     3416063       65536   83  Linux
 test8         3418112     3428351        5120   83  Linux
 test9         3430400     3440639        5120   83  Linux
 test10        3442688     3452927        5120   83  Linux
 test11        3454976     3465215        5120   83  Linux
 test12        3467264     3991551      262144   83  Linux
 test13        3993600     4255743      131072   83  Linux
 test14        4257792     4268031        5120   83  Linux
 test15        4270080     4280319        5120   83  Linux
 test16        4282368     5999999      858816   83  Linux
steve@steve-VirtualBox:/s/src/scripted/image$ sudo losetup /dev/loop0 test -o 34603008
steve@steve-VirtualBox:/s/src/scripted/image$ sudo mkfs.ext4 /dev/loop0
mke2fs 1.42 (29-Nov-2011)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
185472 inodes, 741552 blocks
37077 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=763363328
23 block groups
32768 blocks per group, 32768 fragments per group
8064 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

steve@steve-VirtualBox:/s/src/scripted/image$ sudo losetup -d /dev/loop0
steve@steve-VirtualBox:/s/src/scripted/image$ fdisk -l test
Warning: invalid flag 0x0000 of partition table 5 will be corrected by w(rite)

Disk test: 3072 MB, 3072000000 bytes
255 heads, 63 sectors/track, 373 cylinders, total 6000000 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
Disk identifier: 0x4b8bbe0f

Device Boot      Start         End      Blocks   Id  System
 test1   *        2048       67583       32768    c  W95 FAT32 (LBA)
 test2           67584      133119       32768   83  Linux
 test3          133120     5999999     2933440    5  Extended
steve@steve-VirtualBox:/s/src/scripted/image$ 

答案1

嘗試在命令中新增區塊計數mkfs.ext4

sudo mkfs.ext4 /dev/loop0 32768

如果沒有它,mkfs 將建立一個具有最大大小的檔案系統,並建立一個從檔案末端losetup -o [offset]開始但結束的裝置。[offset]因此其他分區會損壞。它會損壞分區表,因為您使用的是擴展分區,該分區將剩餘的分區資訊儲存在該分區的位置。

另一點是使用 GUID 分區表 (GPT),它不限制分區數量,因此不需要擴展分區。這不會解決您的問題,但管理這些分割區可能會更容易。 (也許還有 LVM,但我不知道。)

相關內容