Creating ext4 partition from console

76

28

I have a volume /dev/sda1 (1.2 TB) containing an NTFS partition using 0.6 TB space.

I want to make an ext4 partition in the remaining 0.6 TB space.

I have not tried anything yet because I don't want to risk losing data if I hit a wrong command. (I cannot use gparted as I don't have GUI.)

Can you provide me with the correct command, or at least the right parameters for mkfs.ext4.

root@rasp:~# fdisk -l /dev/sda

Disk /dev/sda: 1500.3 GB, 1500299395072 bytes
255 heads, 63 sectors/track, 182401 cylinders, total 2930272256 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
Disk identifier: 0x000303ee

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048  2930272255  1465135104    7  HPFS/NTFS/exFAT

Daniel W.

Posted 2013-09-11T10:25:49.213

Reputation: 1 462

Thanks for the update, I am looking into the details but in the mean time, you do have a backup of your NTFS data right? If not MAKE ONE NOW! It should be possible to do this without data loss but you never know... Also, do you have full access to the machine? Can you reboot into a live session for example? – terdon – 2013-09-11T11:47:04.853

@terdon I have root access but not physical access. The /dev/sda is not the one I boot from. I cannot do backup or boot from anything else. From http://ubuntuforums.org/showthread.php?t=1244058 I read that I have to delete the partition from the table and set a new one in the range of the NTFS size, then add a new partition that I have to initialize with ext4.

– Daniel W. – 2013-09-11T11:53:14.050

Yes, exactly. Also have a look at this. Theoretically, once you have resized using ntfsresize you can delete the partition using fdisk and recreate it smaller as long as it starts on the same cylinder. So, basically, first delete the existing partition then follow the steps in my answer to create the new one. You do this at your own risk, I have never tried it personally but it should work. I would really recommend using clonezilla or a similar tool to make a backup first.

– terdon – 2013-09-11T11:57:35.403

@terdon I noticed that ntfsresize ran properly with success but it must have deleted something because I get the error of integrity fail.. Failed to load $MFT: Input/output error. – Daniel W. – 2013-09-11T12:29:16.520

Sorry, no idea about that. Perhaps this will help.

– terdon – 2013-09-11T12:32:42.653

Running testdisk now... I hope the data is not lost xDDD hahahaaaaaaa nooooooo MASTER FILE TABLE corrupt gg – Daniel W. – 2013-09-11T12:41:30.260

Answers

108

First of all you need to create a partition, then you can make the filesystem.

  1. Create a new partition

    sudo fdisk /dev/sda
    

    This will bring up the fdisk menu. You should familiarize yourself with it, search for tutorials (here's one). The basic options are:

    Command action
       a   toggle a bootable flagL
       b   edit bsd disklabel
       c   toggle the dos compatibility flag
       d   delete a partition
       l   list known partition types
       m   print this menu
       n   add a new partition
       o   create a new empty DOS partition table
       p   print the partition table
       q   quit without saving changes
       s   create a new empty Sun disklabel
       t   change a partition's system id
       u   change display/entry units
       v   verify the partition table
       w   write table to disk and exit
       x   extra functionality (experts only)
    

    If all goes well, by pressing N, you will be given the option of creating a new partition in the empty space. If the only unallocated space is the one you want to use, the default choices should be fine and you can just let fdisk choose.

  2. Since you want to create an ext partition, you don't need to do anything. If you wanted to create a different type (swap or NTFS or whatever) you would need to use t to change the partition type. I am mentioning this just in case.

  3. Check that your changes are what you expected by hitting P to print the partition table.

  4. If everything is OK, write the new partition table to the disk (W) and exit (Q).

Now that you have your new, empty partition, you can create its filesystem. So, if you just created /dev/sdaX (where X is the number of the partition you created, for example /dev/sda2), run this:

sudo mkfs.ext4 /dev/sdaX

terdon

Posted 2013-09-11T10:25:49.213

Reputation: 45 216

I am following your answer, but when I select n, I am told to create more partitions, first replace a primary with and extended. What I am looking to do is section of 2GB from SSD which has enough to spare. – dustin – 2015-04-04T22:35:55.620

@dustin that means that you are using a GPT partitioning scheme that only allows three primary partitions. You will need to convert on of the existing ones to a logical (extended) partition and create the others within that. That's a bit too complicated to answer in a comment, I'm sure it's been asked before but, if not, post a new question. – terdon – 2015-04-06T12:04:07.837

For devices larger than 1TB, you should use parted to create GPT partition table: http://www.thegeekstuff.com/2012/08/2tb-gtp-parted/

– Amir Ali Akbari – 2016-09-08T07:33:50.787

Is it compulsary to make the partition? I tried mkfs.ext4 /dev/vdb and I can mount it with mount -t ext4 /dev/vdb /mnt/external. Does it mean there's a single partition or there's no partition at all? – Sudip Bhattarai – 2019-01-07T07:43:34.697

I just shrinked the NTFS partition with the command ntfsresize -s 656511M /dev/sda1. Using your nice guide, I want to create a second partition with fdisk but on hitting n, p, 2, it sais No free sectors available – Daniel W. – 2013-09-11T11:34:47.850

Using cfdisk, it sais my NTFS is still size of 1500000 MB. I just resized it using ntfsresize?! What is wrong :,-( – Daniel W. – 2013-09-11T11:38:45.820

@DanFromGermany please add the output of sudo fdisk -l /dev/sda to your question. – terdon – 2013-09-11T11:39:49.307

22

The syntax is mkfs.ext4 *partition*

An example would be mkfs.ext4 /dev/sda2 where /dev/sda2 is the unformatted partition in question.

You can get information about your filesystem by executing lsblk or lsblk --fs for more information.

The last 2 partitions are empty and have no filesystems as yet:

root@augur:/# lsblk --fs --ascii 
NAME        FSTYPE LABEL MOUNTPOINT
mmcblk0                  
|-mmcblk0p1 vfat   boot  /boot
|-mmcblk0p2 ext4         /
|-mmcblk0p3              
`-mmcblk0p4 

headkase

Posted 2013-09-11T10:25:49.213

Reputation: 1 690

Will this command delete my existing 0.6 TB NTFS or will it just use as much space as it can without touching other partitions? – Daniel W. – 2013-09-11T10:36:09.117

The partition table must have the free space already set aside for mkfs.ext4 to operate on. If you just have one big partition then you need to resize that to create some free space for mkfs to work with. If that is the case boot into Windows and go to the disk management adminstrator tools and resize the existing partition to make some free space which you then format with Linux.

Create a partition in the free space and then format that. – headkase – 2013-09-11T10:38:27.877

I have no GUI, nor Windows, nor even physical access to the machine. I will just try partman – Daniel W. – 2013-09-11T10:41:52.403

Double check everything. Use "lsblk -fs" as given above: don't mkfs on anything that is listed as NTFS. You want to look for an empty partition. – headkase – 2013-09-11T10:43:22.413