How to create and modify ramdisk img

From HyperSecurity Wiki
Jump to: navigation, search

Calculate count size:

1024x1500 = 1.5Gb img file

Create 1.5GB ramdisk file:

dd if=/dev/zero of=target.img bs=1k count=1536000

Clone partition table from source to target:

sfdisk -d source.img | sfdisk target.img

Check partition table:

fdisk -l target.img

Automatically add partition mappings(s) from source partition table:

kpartx -av source.img

Automatically add partition mappings(s) from target partition table:

kpartx -av target.img

DD partitions from source to target

dd bs=4M if=/dev/mapper/loop1p1 of=/dev/mapper/loop2p1
dd bs=4M if=/dev/mapper/loop1p2 of=/dev/mapper/loop2p2
dd bs=4M if=/dev/mapper/loop1p3 of=/dev/mapper/loop2p3

Mount source partition:

mount /dev/mapper/loop1p4 /mnt/source

Create file system on target partition to be resized:

mkfs.ext4 /dev/mapper/loop2p4

Mount empty target partition

mount /dev/mapper/loop2p4 /mnt/target

Copy files and folders from source to target:

cp -pR /mnt/source/* /mnt/target/

Unmount source:

umount /mnt/source/

Unmount target:

umount /mnt/target/

Delete partition mappings of source:

kpartx -d source.img

Delete partition mappings of source:

kpartx -d target.img