6

The parted output is as below:

$> parted /dev/sda print free
Model: ATA Hitachi HUA72302 (scsi)
Disk /dev/sda: 2000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  33.6GB  33.6GB  primary               raid
 2      33.6GB  67.1GB  33.6GB  primary               raid
 3      67.1GB  67.7GB  524MB   primary  ext4         boot, raid
        67.7GB  2000GB  1933GB           Free Space

What would be the steps on CentOS 7.1 to create an empty, non-formatted partition on those free 1933GB? (using terminal tools only)

4
  • 2
    You can just crete a partition in that space... and then not write anything into it. I am not sure I understand your question. Do you need help with the parted command for creating a partition (or fdisk for that matter)?
    – Celada
    Commented Apr 30, 2015 at 15:27
  • This is a MBR table so you can still create an extended partition and then create a logical partition inside of that. It sounds like you actually understand the tool. There's not too much to this part of whatever process you're going through.
    – Bratchley
    Commented Apr 30, 2015 at 15:33
  • @Celada, yes! pardon my lack of knowledge but I've only just found out on how to query the disks/partitions. Need to create one now and not sure how to specify that it should take that 1933GB free space exactly etc…
    – msciwoj
    Commented Apr 30, 2015 at 16:19
  • fdisk is easier due to its dialogue structure imho.
    – Björn
    Commented Apr 2, 2019 at 14:05

2 Answers 2

9

I'll put an answer here since I've figured it out.

First I query the disk layout using command similar to above but request sectors a units:

> parted /dev/sda unit s print free
Model: ATA Hitachi HUA72302 (scsi)
Disk /dev/sda: 3907029168s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start       End          Size         Type     File system  Flags
        63s         2047s        1985s                 Free Space
 1      2048s       65570815s    65568768s    primary               raid
 2      65570816s   66594815s    1024000s     primary  ext4         boot, raid
 3      66594816s   132163583s   65568768s    primary               raid
 4      132163584s  3907029167s  3774865584s  primary

note 132163584s, the starting sector of the free space (previous partition end +1, if you don't use free switch). Then the rest is as simple as:

parted /dev/sda mkpart primary ext4 132163584s -- -1s

-1s is the ending sector being the end of disk. -- is to use -1s in batch mode and prevent parted: invalid option -- '1' error (running parted in interactive mode doesn't need this).

5

Just to add another way (using fdisk command) to create a partition out of unallocated space. fdisk is available across several Linux distros, at least Centos, Ubuntu and Debian have this command.

IMPORTANT NOTE: before executing the following commands, make sure you identify the disk you want to work with, in my case it is /dev/sda but yours could be different. Using the wrong disk can break your disk and data.

First, check where there is the unallocated space on your disk.

sudo parted /dev/sda unit s print free

The output is shown in the picture below, where we can see that there is a 2GB (aprox.) unallocated space. The unallocated space corresponds to the line which has "Free Space" under the file system column.

       enter image description here

Then, run fdisk in interactive mode.

sudo fdisk /dev/sda

Afterfdisk is running, go through the following steps:

  • Type n for creating a new partition.
  • Type p for a primary partition (this is my case, yours can be a different one).
  • Type the value of the first sector of your partition (you can get this value from the "Start" column of the sudo parted /dev/sda unit s print free command, i.e. from the previous picture). In my case this would be the value 308582400 (the trailing s is not needed)
  • Type the value of the final sector of your partition (analogous to the previous step, you can get this value from the "End" column of the sudo parted /dev/sda unit s print free command, i.e. from the previous picture). In my case this would be the value 312581807 (again, the trailing s is not needed)
  • Type w to write the changes to disk

After this, you will have to reboot your system or run the sudo partprobe command to make the partition changes available for the kernel.

You can end at this point and you will have your partition created. In the case that you want to tag your partition because it will have a specific use, e.g. a swap partition, you can follow the steps below before typing the w letter into fdisk command (i.e. before writing changes to disk, i.e. this means that the steps below substitute to the previous step "Type w to write...").

  • Type t for tagging the newly created partition
  • Type the number of the partition to be tagged, the one that we just created, in my case it would be 3 (fdisk command will inform you the number assigned to the newly created partition)
  • Type the partition type (82 in my case for a swap partition, but you can type L to list all the types and select the type that suits you)
  • Type w to write the changes to disk

Below is a screenshot that shows all the previous steps executed and the output.

enter image description here

The final result, after creating the new partition is shown below. You can see yours by executing the following command

sudo fdisk -l /dev/sda

       enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .