Tag Archives: Partition

Command line create partition and format in Linux with a label

I have a Raspberry Pi with a couple of memory sticks (8/32GB SanDisk). I’d like to plug them into my Raspberry Pi, replace the partitions and format them to ext4 with a label describing the partition.

A note before I begin is that you will need to run most of the command with elevated privileges (root) so I’m prefixing the required command with sudo.

Using lsblk –fs you will be able to identify the physical drives (devices) in your system as well as any partitions they have on them.

pi@raspberrypi:~ $ sudo lsblk --fs
NAME        FSTYPE LABEL UUID                                 MOUNTPOINT
sda                                                           
└─sda1      vfat         FDB1-8F68                            
sdb                                                           
└─sdb1      vfat         2BB1-F497                            /media/pi/2BB1-F497
mmcblk0                                                       
├─mmcblk0p1 vfat   boot  7EF5-979D                            /boot
└─mmcblk0p2 ext4         139ca52c-d45e-44ca-8b8c-12c0af0c9cf5 /

Firstly ensure that any partition(s) on the drive(s) you want to work on are unmounted.

pi@raspberrypi:~ $ sudo umount /dev/sda1
pi@raspberrypi:~ $ sudo umount /dev/sdb1

Launch parted. On launch you will be shown the version number of parted as well as the device you are currently using.  You will then be left at a prompt  that reads (parted).

pi@raspberrypi:~ $ sudo parted 
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

In parted you can type help to bring up the help system overview.

(parted) help                                                             
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found
        partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted

For more detailed help on a specific command type help followed by the command you want help with.

(parted) help print                                                       
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found
        partitions, or a particular partition

        Without arguments, 'print' displays the entire partition table. However with the following arguments it performs
        various other actions.
          devices   : display all active block devices
          free      : display information about free unpartitioned space on the current block device
          list, all : display the partition tables of all active block devices
          NUMBER    : display more detailed information about this particular partition

You can get a list of all devices and their partitions by entering the print all command. As you can see from the listing below the Raspberry Pi boots from an SD card so it’s a device called mmcblk0. On most computers the device that has the boot partition on it will be sda. As sda has not already been used sda has been issued to one memory stick and sdb has been issued to the other. sda and sdb are both currently fat32 and part of the goal here is to format them as ext4.

(parted) print all                                                        
Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sda: 32.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  32.0GB  32.0GB  fat32              msftdata

Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sdb: 8004MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  8004MB  8003MB  fat32              msftdata

Model: SD SE64G (sd/mmc)
Disk /dev/mmcblk0: 63.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  70.3MB  66.1MB  primary  fat32        lba
 2      70.3MB  63.9GB  63.8GB  primary  ext4

Even though we were told that /dev/sda was the device we were using when we opened parted you can confirm or change the device you’re using with the following command.

(parted) select /dev/sda
Using /dev/sda

Next we’re going to remove the partitions from sda and sdb so let’s look up the help for the rm command.

(parted) help rm                                                          
  rm NUMBER                                delete partition NUMBER

        NUMBER is the partition number used by Linux.  On MS-DOS disk labels, the primary partitions number from 1 to 4,
        logical partitions from 5 onwards.

Since we are using the device sda and sda only has one partition (the partition number is 1) the command to remove that partition is quite short.

If you get a message saying that the device is being used then the drive will need unmounting outside of parted using umount (see above).

(parted) rm 1

If you enter the print command without any further parameters the information returned with be for the current device. In the case of the example below it is for the device /dev/sda and shows that partition 1 has now been removed.

(parted) print                                                            
Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sda: 32.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

Now let’s switch to the device sdb.

(parted) select /dev/sdb
Using /dev/sdb

From the print all output above we can see that there is also only one partition on sdb and that it is also partition 1 so we can remove this partition like we removed the partition on sda.

(parted) rm 1

To check that the partition was removed successfully we can again using the print command.

(parted) print
Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sdb: 8004MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

Next we’re going to make a partition table so let’s look up the help for that.

(parted) help mktable                                                     
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)

        LABEL-TYPE is one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop

If you type mktable without any parameters you enter interactive mode and will be prompted for the inputs required.

(parted) mktable 
New disk label type? gpt
Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to
continue?
Yes/No? yes

Next let’s add a partition. The partition will be a primary partition using the file system type ext4. The partition will start after 1MB and use 100% of the rest of the device.

(parted) mkpart                                                           
Partition name?  []? primary                                              
File system type?  [ext2]? ext4
Start? 1MB
End? 100%

If you use print now you’ll be able to see the new partition has been created.

(parted) print                                                            
Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sda: 32.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  32.0GB  32.0GB  ext4         primary

Now we do the same for sdb, let’s switch to sdb.

(parted) select /dev/sdb
Using /dev/sdb

Make the partition table.

(parted) mktable                                                          
New disk label type? gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
continue?
Yes/No? yes

Now let’s make the next partition using the same settings as with sda.

(parted) mkpart 
Partition name?  []? primary
File system type?  [ext2]? ext4
Start? 1MB                                                                
End? 100%

Now using print we can confirm that the partition has been made.

(parted) print                                                            
Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sdb: 8004MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  8004MB  8003MB  ext4         primary

Entering the command q will quit parted. There is no need to save your changes as they are saved as you go.

(parted) q                                                                
Information: You may need to update /etc/fstab.

Now we want to format the ext4 file system onto the partition, we will use mkfs.ext4. You can look up the help for this using the man command.

pi@raspberrypi:~ $ man mkfs.ext4

The command starts with the -L parameter to specify the partitions label, next you pass the device followed immediately by the partition number.

pi@raspberrypi:~ $ sudo mkfs.ext4 -L SanDisk_32GB /dev/sda1
mke2fs 1.42.12 (29-Aug-2014)
/dev/sda1 contains a vfat file system
Proceed anyway? (y,n) y
Creating filesystem with 7815936 4k blocks and 1954064 inodes
Filesystem UUID: 6661f044-c533-4eb0-9a23-89aa6356a8a3
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Let’s do the same for sdb.

pi@raspberrypi:~ $ sudo mkfs.ext4 -L SanDisk_8GB /dev/sdb1
mke2fs 1.42.12 (29-Aug-2014)
/dev/sdb1 contains a vfat file system
Proceed anyway? (y,n) y
Creating filesystem with 1953792 4k blocks and 488640 inodes
Filesystem UUID: 3f51678b-b2b6-465a-b464-1e461058b0b9
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Finally we can use lsblk –fs to see the new partitions with their file systems, labels and UUID’s. If you were to eject and reinsert the USB device or restart the machine they would mount themselves.

pi@raspberrypi:~ $ sudo lsblk --fs
NAME        FSTYPE LABEL        UUID                                 MOUNTPOINT
sda                                                                  
└─sda1      ext4   SanDisk_32GB 6661f044-c533-4eb0-9a23-89aa6356a8a3 
sdb                                                                  
└─sdb1      ext4   SanDisk_8GB  3f51678b-b2b6-465a-b464-1e461058b0b9 
mmcblk0                                                              
├─mmcblk0p1 vfat   boot         7EF5-979D                            /boot
└─mmcblk0p2 ext4                139ca52c-d45e-44ca-8b8c-12c0af0c9cf5 /

Restarting the machine.

pi@raspberrypi:~ $ sudo init 6

After a reboot you can now see the USB devices mounted.

pi@raspberrypi:~ $ sudo lsblk --fs
NAME        FSTYPE LABEL        UUID                                 MOUNTPOINT
sda                                                                  
└─sda1      ext4   SanDisk_32GB 6661f044-c533-4eb0-9a23-89aa6356a8a3 /media/pi/SanDisk_32GB
sdb                                                                  
└─sdb1      ext4   SanDisk_8GB  3f51678b-b2b6-465a-b464-1e461058b0b9 /media/pi/SanDisk_8GB
mmcblk0                                                              
├─mmcblk0p1 vfat   boot         7EF5-979D                            /boot
└─mmcblk0p2 ext4                139ca52c-d45e-44ca-8b8c-12c0af0c9cf5 /