Saturday, January 3, 2015

how to create raw image file

1- create raw file allocate 1G named myimage.raw
root@test:~/# fallocate -l 1G myimage.raw

2- use fdisk to create partition
root@test:~/# fdisk myimage.raw
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x24127375.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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

Command (m for help): w
The partition table has been altered!

Syncing disks.



3- losetup and kpartx to map it to map device
root@test:~/# losetup -f --show myimage.raw
 /dev/loop0
root@test:~/# kpartx -a /dev/loop0

4- format the partition 1 with ext4 file system
root@test:~/# mkfs.ext4 /dev/mapper/loop0p
mke2fs 1.42.9 (4-Feb-2014)
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
65536 inodes, 261888 blocks
13094 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376

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


5- mount it to a directory, myimg
root@test:~/# mount /dev/mapper/loop0p1 myimg/


No comments:

Post a Comment