Monday 16 April 2018

partitioning - How do you create and partition a raw disk image?


I need to create a raw disk image for use in a virtual machine, but I need to create the partitions before creating the file system or installing the operating system.


Can anyone describe a process to do this?



Answer



First create a blank raw disk image (25GB in this case):


    # dd if=/dev/zero of=disk.img bs=1024k seek=25600 count=0
0+0 records in
0+0 records out
0 bytes (0 B) copied, 2.7301e-05 s, 0.0 kB/s
# ls -lh
total 2.0G
-rw-r--r-- 1 root root 25G Dec 13 11:13 disk.img

Give it a partition table:


    # parted disk.img mklabel msdos

Mount it as a loopback device (the simplest way to define cylinders, heads, and sectors):


    # losetup -f disk.img
# losetup -a
/dev/loop0: [0801]:12059103 (/path/to/disk.img)

Check that it seems to be a valid block device:


    # fdisk /dev/loop0

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): p

Disk /dev/loop0: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000db005

Device Boot Start End Blocks Id System

Then use fdisk to partition it as you wish:


    # fdisk /dev/loop0

When you're done you need to unloop the device (it will need unmapping and unmounting first):


# losetup -d /dev/loop0

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...