複数パーティーション情報を含むディスクイメージの作成からフォーマット,マウントまでのメモ

SD card などの実デバイスで作成して dd で書き出すという方法もありますが遅いし面倒なのでちょっと調べてみました.
以下は debian-jessie-raspberrypi.img というイメージファイルを 500MB の容量で作成し,fat と ext4 の2つのファイルシステムを作ってマウントするまでのメモです.

イメージの作成

% ionice -c 2 -n 0 dd if=/dev/zero of=./debian-jessie-raspberrypi.img bs=1M count=500

パーテションの作成(ここでは1つ目のプライマリパーティションに32Mのfat領域を,2つ目のプライマリパーティションに残り全ての容量でlinux領域を作成)

% /sbin/fdisk  ./debian-jessie-raspberrypi.img
 
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
 
 
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-1023999, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-1023999, default 1023999): +32M
 
Created a new partition 1 of type 'Linux' and of size 32 MiB.
 
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): e
Changed type of partition 'Linux' to 'W95 FAT16 (LBA)'.
 
Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (67584-1023999, default 67584):
Last sector, +sectors or +size{K,M,G,T,P} (67584-1023999, default 1023999):
 
Created a new partition 2 of type 'Linux' and of size 467 MiB.
 
Command (m for help): p
Disk ./debian-jessie-raspberrypi.img: 500 MiB, 524288000 bytes, 1024000 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: 0x00000000
 
Device                           Boot Start     End Sectors  Size Id Type
./debian-jessie-raspberrypi.img1       2048   67583   65536   32M  e W95 FAT16 (LBA)
./debian-jessie-raspberrypi.img2      67584 1023999  956416  467M 83 Linux
 
Command (m for help): w
The partition table has been altered.
Syncing disks.
 

現在利用している loop デバイスを確認

% /sbin/losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0         0      0         1  1 /home/mk/Downloads/db410c_sd_install_ubuntu.img
/dev/loop1         0      0         0  1 /ubuntu/rootfs_1.img
/dev/loop2         0      0         0  1 /ubuntu/rootfs_2.img

オフセットを指定して loop デバイスに接続(上で確認したデバイスを避ける,オフセットはfdisk で確認できる開始セクタ /sbin/fdisk -l -u ./debian-jessie-raspberrypi.img で確認できる )

% sudo losetup -o2048 /dev/loop4 ./debian-jessie-raspberrypi.img
% sudo losetup -o67584 /dev/loop5 ./debian-jessie-raspberrypi.img

フォーマット

% sudo mkfs.fat /dev/loop4
mkfs.fat 3.0.28 (2015-05-16)
Loop device does not match a floppy size, using default hd params
% sudo mkfs.ext4 /dev/loop5
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done
Creating filesystem with 511932 1k blocks and 128016 inodes
Filesystem UUID: b4d1c469-c057-4839-b412-86486f586d9b
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
 
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
 

マウント

% sudo mount /dev/loop4 ./mnt/boot
% sudo mount /dev/loop5 ./mnt/rootfs
% mount |grep /dev/loop
/dev/loop4 on /var/tmp/debian_pi/mnt/boot type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=utf8,shortname=mixed,errors=remount-ro)
/dev/loop5 on /var/tmp/debian_pi/mnt/rootfs type ext4 (rw,relatime,data=ordered)

アンマウントと loop デバイスの切断

% sudo umount /dev/loop4 /dev/loop5
% sudo losetup -d /dev/loop4 /dev/loop5

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)