I had done a script whose goal was to build a custom Raspbian distribution for the SamplerBox project.

It worked. But now launching this a few months/updates later:

fdisk image.img <<EOF



makes it fail:

Command (m for help): Created a new DOS disklabel with disk identifier 0x2bd95ecc.

Command (m for help): Partition type
    p   primary (0 primary, 0 extended, 4 free)    
    e   extended (container for logical partitions) 
Select (default p): 
Using default response p. 
Partition number (1-4, default 1): First sector (2048-2457599, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-2457599, default 2457599): 
Created a new partition 1 of type 'Linux' and of size 64 MiB. 

Command (m for help): Selected partition 1 
The bootable flag on partition 1 is enabled now. 

Command (m for help): 1: unknown command 

Any idea what could solve the problem?


When you get to the a command you only have 1 partition, so fdisk auto selects it since you could not possibly mean anything else:

Command (m for help): Selected partition 1 
The bootable flag on partition 1 is enabled now. 

You are now on the main prompt again so the next char you input 1 is invalid:

Command (m for help): 1: unknown command

Simply drop the 1 after the a and it should work. Alternatively move the a\n1 to after you have created the second partition.

Note that fdisk is really meant to be an interactive program. sfdisk or parted are better suited to scripting. Here is what I generally use to set up raspberry pi images:

parted --script ${device} mklabel msdos
parted --script ${device} mkpart primary fat32 0% 64M
parted --script ${device} mkpart primary ext4 100M 100%

This is much easier to read/understand and less likely to break randomly if you edit it slightly. You may be interested in this blog post on the subject of scripting the creation of raspberry pi images.

| improve this answer | |
  • Dear G-d. Second using parted. Please, for the love of everything holy and good in the world, don't script fdisk. – BJ Black Sep 2 '16 at 11:52

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.