imgファイル内でパーティションをフォーマットする方法は?

imgファイル内でパーティションをフォーマットする方法は?

img次のコマンドでファイルを作成しました。

dd if=/dev/zero bs=2M count=200 > binary.img

ゼロのファイルだけですが、それを使用してfdiskパーティションテーブルを作成できます。

# fdisk binary.img

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x51707f21.

Command (m for help): p
Disk binary.img: 400 MiB, 419430400 bytes, 819200 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: 0x51707f21

パーティションがあるとします。

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-819199, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-819199, default 819199): 

Created a new partition 1 of type 'Linux' and of size 399 MiB.

Command (m for help): w
The partition table has been altered.
Syncing disks.

パーティションテーブルを確認すると、次の結果が表示されます。

Command (m for help): p
Disk binary.img: 400 MiB, 419430400 bytes, 819200 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: 0x7f3a8a6a

Device      Boot Start    End Sectors  Size Id Type
binary.img1       2048 819199  817152  399M 83 Linux

だからパーティションが存在します。 gpartedでパーティションをフォーマットしようとすると、次のエラーが発生します。

ここに画像の説明を入力してください。

なぜ見えるのかわからず、binary.img1ライブコマンドでパーティションをフォーマットする方法もわかりません。

ext4ファイルシステムを使ってフォーマットする方法を知っている人はいますか?

ベストアンサー1

ループバック機能により、ディスクイメージと個々のパーティションにアクセスできます。特定のディスクユーティリティがディスクイメージで(合理的に)スムーズに実行されることを発見しました。しかし、mkfsその一つではありません(まだ奇妙なことにmount)。

出力は次のとおりですfdisk -lu binary.img

Disk binary.img: 400 MiB, 419430400 bytes, 819200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
...

Device           Boot Start    End Sectors  Size Id Type
binary.img1            2048 819199  817152  399M 83 Linux

作成したパーティションにアクセスするには、いくつかのオプションがあります。

  1. 明確なパス

    losetup --offset $((512*2048)) --sizelimit $((512*817152)) --show --find binary.img
    /dev/loop0
    

    出力は/dev/loop0割り当てられたループ装置の名前です。この--offsetパラメータは、単にパーティションのオフセット(Start)とセクタサイズ(512)を掛けたものです。--sizelimitむしろ、パーティションのサイズは次のように計算できます。 End-Start+1(819199-2048+1=817152)、この数にセクタのサイズを掛ける必要があります。

    /dev/loop0その後、パーティショニングの参照として使用できます。

    mkfs -t ext4 -L img1 /dev/loop0
    mkdir -p /mnt/img1
    mount /dev/loop0 /mnt/img1
    ...
    umount /mnt/img1
    losetup -d /dev/loop0
    
  2. 暗黙的なルーティング

    losetup --partscan --show --find binary.img
    /dev/loop0
    

    出力は、/dev/loop0割り当てられたメインループデバイスの名前です。この--partscanオプションは、カーネルにパーティションテーブルからデバイスを検索し、セカンダリループデバイスを自動的に割り当てるように指示します。パーティションを使用している場合は、/dev/loop0p1そのパーティションへの参照として使用することもできます。

    mkfs -t ext4 -L img1 /dev/loop0p1
    mkdir -p /mnt/img1
    mount /dev/loop0p1 /mnt/img1
    ...
    umount /mnt/img1
    losetup -d /dev/loop0
    

おすすめ記事