2

I'm creating an image in memory (/tmp is a tmpfs):

$ dd if=/dev/zero of=/tmp/sdcard.img bs=512 count=3147775

It's supposed to hold a partition table and the first 3 partitions of a device

$ losetup /dev/loop0 /tmp/sdcard.img
$ dd if=bootloader.img of=/dev/loop0 bs=512

The first 2048 sectors contain a partition table.

$ fdisk -l /tmp/sdcard.img
Disk /dev/loop0: 1.5 GiB, 1611660800 bytes, 3147775 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: 0x0000de21

Device       Boot   Start     End Sectors  Size Id Type
/dev/loop0p1         2048 1050623 1048576  512M  c W95 FAT32 (LBA)
/dev/loop0p2      1050624 2099199 1048576  512M 83 Linux
/dev/loop0p3      2099200 3147775 1048576  512M 83 Linux

But I have a problem.

I want to add a fourth partition /dev/loop0p4, that starts at 3147775 and ends at 37748724.

I don't want to physically create the partition, but I want to modify the partition table so that it thinks this drive exists.

However, when I use fdisk for this purpose it complains Value out of range.

How can I force fdisk to just do it. I don't care that the partition table is invalid. I'm going to be dding this to a larger disk and then formatting it later. I'd like the parition table to be part of what I dd to that larger disk (there are reason for this, would rather not delve into those details). All I want to know is how I can write a partition table with these arbitrary values without pulling out the hex editor.

  • 1
    One of the tags you used is sfdisk. Is using that an option? – Mark Plotnick Dec 23 '15 at 21:39
  • Yes, it is, and after looking at the man page further it's exactly what I need. From the disk I want, I do sfdisk --dump /dev/sdc > part-table.out, then to the loopback device, I can do sfdisk /dev/loop0 < part-table.out – peskal Dec 25 '15 at 3:05
  • I take it back, sfdisk did not work for my purposes, but the answer below did – peskal Dec 29 '15 at 0:35
  • Your deleted answer looks OK; I would've given the second sfdisk the -f option. What didn't work? – Mark Plotnick Dec 29 '15 at 1:43
3

You can make a file as big or as small as you want - especially on a linux tmpfs.

df -h /tmp

Filesystem      Size  Used Avail Use% Mounted on
tmpfs            12G  472K   12G   1% /tmp

We can just make a sparse file.

for cmd in  \
       'dd bs=1024k seek=20k of=' \
       'ls -slh '
do      eval "$cmd/tmp/file"
        echo
done    </dev/null

0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000152051 s, 0.0 kB/s

0 -rw-r--r-- 1 mikeserv mikeserv 20G Dec 24 20:19 /tmp/file

See? It's using 0 blocks of disk space, but its apparent size is 20 gigabytes.

You can then just fdisk /tmp/file. I just created a partition table on it. Here's fdisk -l:


Disk /tmp/file: 20 GiB, 21474836480 bytes, 41943040 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: 0x057d787a

Device     Boot    Start      End  Sectors Size Id Type
/tmp/file1          2048 20973567 20971520  10G 83 Linux
/tmp/file2      20973568 31459327 10485760   5G  5 Extended
/tmp/file3      31459328 41943039 10483712   5G 83 Linux

After the table is written it does use a little bit of space:

ls -lsh /tmp/file

8.0K -rw-r--r-- 1 mikeserv mikeserv 20G Dec 24 20:21 /tmp/file

You wouldn't know, though.

df -h /tmp

Filesystem      Size  Used Avail Use% Mounted on
tmpfs            12G  480K   12G   1% /tmp

And you can sparsely extend a file in the same way:

for cmd in  \
       'dd bs=1024k seek=30k of=' \
       'ls -slh '  'fdisk -l '
do      eval "$cmd/tmp/file"
        echo
done    </dev/null

0+0 records in
0+0 records out
0 bytes (0 B) copied, 9.8239e-05 s, 0.0 kB/s

8.0K -rw-r--r-- 1 mikeserv mikeserv 30G Dec 26 14:24 /tmp/file

Disk /tmp/file: 30 GiB, 32212254720 bytes, 62914560 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: 0x057d787a

Device     Boot    Start      End  Sectors Size Id Type
/tmp/file1          2048 20973567 20971520  10G 83 Linux
/tmp/file2      20973568 31459327 10485760   5G  5 Extended
/tmp/file3      31459328 41943039 10483712   5G 83 Linux
  • Thank you! The sparse file works perfectly for this. I did not know they could be created with dd – peskal Dec 29 '15 at 0:35
0

As I see it you have 3 choices:

1) Use another tool which does not make sanity checks. You did mention a hex editor yourself...

2) Modify the tool (fdisk) that you are using and remove the sanity check. The source for fdisk is out there, maybe it is already included in your distribution.

3) Modify your image so that the sanity check becomes valid. You could create a bigger image, create your partitions and then use dd to only copy the beginning of the image to a new file with the size you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.