0

Usually we would use parted or fdisk to create a new partition, but in my case I want to do this non-interactively in a script. An interactive fdisk session would look like this:

# fdisk /dev/sdb
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1): [[ENTER]]
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): [[ENTER]]
Using default value 13054

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
# fdisk -l /dev/sdb

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x7a0ce571

    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1       13054   104856223+  83  Linux

The closest I can get is with the following where the cylinders don't line up to 13054, the specified maximum:

# parted -s -- /dev/sdb mklabel msdos
# parted -s -- /dev/sdb mkpart primary ext3 1 -1
# fdisk -l /dev/xvdb

Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x000d3807

    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1       13055   104856576   83  Linux

So the standard ways to do this are using parted, echoing commands to fdisk or using a here document passed to sfdisk. Are there other ways to create a single partition using the maximum amount of cylinders?

EDIT: It looks like passing '-a cylinder' causes parted to behave exactly like fdisk. But passing '-a optimal' is...based on disk topology optimally? Can anyone explain this? Why does fdisk align to cylinders and not 'optimally'?

atx
  • 1,281
  • 1
  • 9
  • 25

1 Answers1

0

You can do it via input redirection. Put all the instructions in a file, typed as if you were in fdisk, then run fdisk like

fdisk "$DISK" < instructions_file

I should warn that this has great potential to go terribly wrong, take extreme care.

Robbie Mckennie
  • 1,083
  • 1
  • 8
  • 21