You do not need sudo
in your dd
command, the following is OK:
dd if=/dev/zero of=backup.img bs=1M count=100
Again, to create/edit the partition table in there, you do not need sudo
:
$ gdisk backup.img
GPT fdisk (gdisk) version 1.0.8
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-204766, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-204766, default = 204766) or {+-}size{KMGTP}: 102400
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8300
Changed type of partition to 'Linux filesystem'
Command (? for help): c
Using 1
Enter name: Test_1
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-204766, default = 104448) or {+-}size{KMGTP}:
Last sector (104448-204766, default = 204766) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): c
Partition number (1-2): 2
Enter name: Test_2
Command (? for help): p
Disk backup.img: 204800 sectors, 100.0 MiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 61A000F6-7F51-4615-B1B6-82D4C9E305B3
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 204766
Partitions will be aligned on 2048-sector boundaries
Total free space is 4061 sectors (2.0 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 102400 49.0 MiB 8300 Test_1
2 104448 204766 49.0 MiB 8300 Test_2
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to backup.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
And the same goes for showing the partition table, no sudo
:
$ fdisk -l backup.img
Disk backup.img: 100 MiB, 104857600 bytes, 204800 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: gpt
Disk identifier: 61A000F6-7F51-4615-B1B6-82D4C9E305B3
Device Start End Sectors Size Type
backup.img1 2048 102400 100353 49M Linux filesystem
backup.img2 104448 204766 100319 49M Linux filesystem
Now you want some file system on those partitions, ok, let me create both ext4
, type of file system I believe does not matter at this point.
$ sudo kpartx -a backup.img
$ losetup --list | grep backup.img
/dev/loop19 0 0 0 0 /home/vlastimil/partition-experiments/backup.img 0 512
$ sudo mkfs.ext4 -L Test_ext4_1 -m 0 -E lazy_itable_init=0,lazy_journal_init=0 /dev/mapper/loop19p1
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done
Creating filesystem with 12544 4k blocks and 12544 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
# The same command for /dev/mapper/loop19p2
After that, I mean after creating an image of a disk, I mounted it always using sectors multiplied by, but as we used kpartx
above, it simplified it to:
$ mkdir mntpoint
$ sudo mount /dev/mapper/loop19p1 mntpoint1/
$ sudo mount /dev/mapper/loop19p2 mntpoint2/
Now, you shall have both partitions mounted.
file sda2.img
agree this is a file system containing image?losetup
, does it just try to replicate a system similar to the/dev/sda2
when I mount the usb drive, or is it doing something different?400000
is just approximate