Jump to content

Ubuntu:Format Drive CLI: Difference between revisions

From Wiki
No edit summary
 
Line 6: Line 6:


It’s important to note that formatting is a destructive process, and it will erase all the existing data. If you have data on the UDB drive or the SD card, make sure you back it up.
It’s important to note that formatting is a destructive process, and it will erase all the existing data. If you have data on the UDB drive or the SD card, make sure you back it up.
== Installing Parted ==
== Installing Parted ==
GNU Parted is a tool for creating and managing partition tables. The parted package is pre-installed on most Linux distros nowadays. You can check if it is installed on your system by typing:
GNU Parted is a tool for creating and managing partition tables. The parted package is pre-installed on most Linux distros nowadays. You can check if it is installed on your system by typing:
parted --version


parted (GNU parted) 3.2
<syntaxhighlight lang="bash">
Copyright (C) 2014 Free Software Foundation, Inc.
parted --version
...
</syntaxhighlight>
 
<syntaxhighlight lang="bash">
parted (GNU parted) 3.2
Copyright (C) 2014 Free Software Foundation, Inc.
...
</syntaxhighlight>
 
If <code>parted</code> is not installed on your system, you can install it using your distribution package manager.
If <code>parted</code> is not installed on your system, you can install it using your distribution package manager.
=== Install <code>parted</code> on Ubuntu and Debian ===
=== Install <code>parted</code> on Ubuntu and Debian ===
sudo apt updatesudo apt install parted
 
<syntaxhighlight lang="bash">
sudo apt updatesudo apt install parted
</syntaxhighlight>
 
=== Install <code>parted</code> on CentOS and Fedora ===
=== Install <code>parted</code> on CentOS and Fedora ===
sudo yum install parted
 
<syntaxhighlight lang="bash">
sudo yum install parted
</syntaxhighlight>
 
== Identifying the USB or SD Card Name ==
== Identifying the USB or SD Card Name ==
Insert the USB flash drive or SD card into your Linux machine and find the device name using the <code>lsblk</code> command:
Insert the USB flash drive or SD card into your Linux machine and find the device name using the <code>lsblk</code> command:
lsblk
 
<syntaxhighlight lang="bash">
lsblk
</syntaxhighlight>
 
The command will print a list of all available block devices:
The command will print a list of all available block devices:
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
 
...
<syntaxhighlight lang="bash">
sdb      8:16  1  14.4G  0 disk  
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
└─sdb1  8:17  1  1.8G  0 part /media/data
...
...
sdb      8:16  1  14.4G  0 disk  
└─sdb1  8:17  1  1.8G  0 part /media/data
...
</syntaxhighlight>
 
In the example above, the name of the SD device is <code>/dev/sdb</code>, but this may vary on your system.
In the example above, the name of the SD device is <code>/dev/sdb</code>, but this may vary on your system.


You can also use the <code>dmesg</code> command to find the device name:
You can also use the <code>dmesg</code> command to find the device name:
lsblk
 
<syntaxhighlight lang="bash">
lsblk
</syntaxhighlight>
 
Once you attach the device, <code>dmesg</code> will show the device name:
Once you attach the device, <code>dmesg</code> will show the device name:
...
 
[  +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
<syntaxhighlight lang="bash">
...
...
[  +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
...
</syntaxhighlight>
 
== Securely Wipe Up the Data (Optional) ==
== Securely Wipe Up the Data (Optional) ==
Before formatting the drive, you can securely wipe out all the data on it by overwriting the entire drive with random data. This ensures that the data cannot be recovered by any data recovery tool.
Before formatting the drive, you can securely wipe out all the data on it by overwriting the entire drive with random data. This ensures that the data cannot be recovered by any data recovery tool.


Line 41: Line 74:


Be very careful before running the following command and irrevocably erase the drive data. The <code>of=...</code> part of the <code>dd</code> command must point to the target drive.
Be very careful before running the following command and irrevocably erase the drive data. The <code>of=...</code> part of the <code>dd</code> command must point to the target drive.
sudo dd if=/dev/zero of=/dev/sdb bs=4096 status=progress
 
<syntaxhighlight lang="bash">
sudo dd if=/dev/zero of=/dev/sdb bs=4096 status=progress
</syntaxhighlight>
 
Depending on the size of the drive, the process will take some time to complete.
Depending on the size of the drive, the process will take some time to complete.


Once the disk is erased, the <code>dd</code> command will print “No space left on device”:
Once the disk is erased, the <code>dd</code> command will print “No space left on device”:
15455776768 bytes (15 GB, 14 GiB) copied, 780 s, 19.8 MB/s  
 
dd: error writing '/dev/sdb': No space left on device
<syntaxhighlight lang="bash">
3777356+0 records in
15455776768 bytes (15 GB, 14 GiB) copied, 780 s, 19.8 MB/s  
3777355+0 records out
dd: error writing '/dev/sdb': No space left on device
15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s
3777356+0 records in
3777355+0 records out
15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s
</syntaxhighlight>
 
== Creating a Partition and Formatting ==
== Creating a Partition and Formatting ==
The most common file systems are exFAT and NTFS on Windows, EXT4 on Linux, and FAT32, which can be used on all operating systems.
The most common file systems are exFAT and NTFS on Windows, EXT4 on Linux, and FAT32, which can be used on all operating systems.


We will show you how to format your USB drive or SD card to FAT32 or EXT4. Use EXT4 if you intend to use the drive only on Linux systems, otherwise format it with FAT32. A single partition is sufficient for most use cases.
We will show you how to format your USB drive or SD card to FAT32 or EXT4. Use EXT4 if you intend to use the drive only on Linux systems, otherwise format it with FAT32. A single partition is sufficient for most use cases.
=== Format with FAT32 ===
=== Format with FAT32 ===
First, create the partition table by running the following command:
First, create the partition table by running the following command:
sudo parted /dev/sdb --script -- mklabel msdos
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mklabel msdos
</syntaxhighlight>
 
Create a Fat32 partition that takes the whole space:
Create a Fat32 partition that takes the whole space:
sudo parted /dev/sdb --script -- mkpart primary fat32 1MiB 100%
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mkpart primary fat32 1MiB 100%
</syntaxhighlight>
 
Format the boot partition to FAT32:
Format the boot partition to FAT32:
sudo mkfs.vfat -F32 /dev/sdb1


mkfs.fat 4.1 (2017-01-24)
<syntaxhighlight lang="bash">
sudo mkfs.vfat -F32 /dev/sdb1
</syntaxhighlight>
 
<syntaxhighlight lang="bash">
mkfs.fat 4.1 (2017-01-24)
</syntaxhighlight>
 
Once done, use the command below to print the partition table and verify that everything is set up correctly:
Once done, use the command below to print the partition table and verify that everything is set up correctly:
sudo parted /dev/sdb --script print
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script print
</syntaxhighlight>
 
The output should look something like this:
The output should look something like this:
<syntaxhighlight lang="bash">
  Model: Kingston DataTraveler 3.0 (scsi)
  Model: Kingston DataTraveler 3.0 (scsi)
  Disk /dev/sdb: 15.5GB
  Disk /dev/sdb: 15.5GB
Line 74: Line 138:
  Number  Start  End    Size    Type    File system  Flags
  Number  Start  End    Size    Type    File system  Flags
   1      1049kB  15.5GB  15.5GB  primary  fat32        lba
   1      1049kB  15.5GB  15.5GB  primary  fat32        lba
</syntaxhighlight>
That’s all! You have formatted your device.
That’s all! You have formatted your device.
=== Format with EXT4 ===
=== Format with EXT4 ===
Create a GPT partition table by issuing:
Create a GPT partition table by issuing:
sudo parted /dev/sdb --script -- mklabel gpt
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mklabel gpt
</syntaxhighlight>
 
Run the following command to create a EXT4 partition that takes the whole space:
Run the following command to create a EXT4 partition that takes the whole space:
sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100%
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100%
</syntaxhighlight>
 
Format the partition to ext4:
Format the partition to ext4:
sudo mkfs.ext4 -F /dev/sdb1


<syntaxhighlight lang="bash">
sudo mkfs.ext4 -F /dev/sdb1
</syntaxhighlight>
<syntaxhighlight lang="bash">
  mke2fs 1.44.1 (24-Mar-2018)
  mke2fs 1.44.1 (24-Mar-2018)
  /dev/sdb1 contains a vfat file system
  /dev/sdb1 contains a vfat file system
Line 94: Line 175:
  Creating journal (16384 blocks): done
  Creating journal (16384 blocks): done
  Writing superblocks and filesystem accounting information: done   
  Writing superblocks and filesystem accounting information: done   
</syntaxhighlight>
Verify it by printing the partition table:
Verify it by printing the partition table:
<code>sudo parted /dev/sdb --script print</code>
 
<syntaxhighlight lang="bash">
sudo parted /dev/sdb --script print
</syntaxhighlight>
 
The output should look something like this:
The output should look something like this:
<syntaxhighlight lang="bash">
  Model: Kingston DataTraveler 3.0 (scsi)
  Model: Kingston DataTraveler 3.0 (scsi)
  Disk /dev/sdb: 15.5GB
  Disk /dev/sdb: 15.5GB
Line 104: Line 193:
   
   
  Number  Start  End    Size    File system  Name    Flags
  Number  Start  End    Size    File system  Name    Flags
   1      1049kB  15.5GB  15.5GB  ext4        primary   
   1      1049kB  15.5GB  15.5GB  ext4        primary
</syntaxhighlight>  
 
== Source ==
== Source ==
* [https://linuxize.com/post/how-to-format-usb-sd-card-linux/ linuxize.com]
* [https://linuxize.com/post/how-to-format-usb-sd-card-linux/ linuxize.com]

Latest revision as of 08:10, 14 December 2022

Before you can use an SD card or USB drive, it needs to be formatted and partitioned. Typically most USB drives and SD cards come preformatted using the FAT file system and do not need to be formatted out of the box. However, in some cases, you may need to format the drive.

In Linux, you can use a graphical tool like GParted or command-line tools such as fdisk or parted to format the drive and create the required partitions.

This article explains how to format a USB Drive or SD Card on Linux using the parted utility.

It’s important to note that formatting is a destructive process, and it will erase all the existing data. If you have data on the UDB drive or the SD card, make sure you back it up.

Installing Parted

GNU Parted is a tool for creating and managing partition tables. The parted package is pre-installed on most Linux distros nowadays. You can check if it is installed on your system by typing:

parted --version
parted (GNU parted) 3.2
Copyright (C) 2014 Free Software Foundation, Inc.
...

If parted is not installed on your system, you can install it using your distribution package manager.

Install parted on Ubuntu and Debian

sudo apt updatesudo apt install parted

Install parted on CentOS and Fedora

sudo yum install parted

Identifying the USB or SD Card Name

Insert the USB flash drive or SD card into your Linux machine and find the device name using the lsblk command:

lsblk

The command will print a list of all available block devices:

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
...
sdb      8:16   1  14.4G  0 disk 
└─sdb1   8:17   1   1.8G  0 part /media/data
...

In the example above, the name of the SD device is /dev/sdb, but this may vary on your system.

You can also use the dmesg command to find the device name:

lsblk

Once you attach the device, dmesg will show the device name:

...
[  +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
...

Securely Wipe Up the Data (Optional)

Before formatting the drive, you can securely wipe out all the data on it by overwriting the entire drive with random data. This ensures that the data cannot be recovered by any data recovery tool.

You need to completely wipe the data only if the device is going to be given away. Otherwise, you can skip this step.

Be very careful before running the following command and irrevocably erase the drive data. The of=... part of the dd command must point to the target drive.

sudo dd if=/dev/zero of=/dev/sdb bs=4096 status=progress

Depending on the size of the drive, the process will take some time to complete.

Once the disk is erased, the dd command will print “No space left on device”:

15455776768 bytes (15 GB, 14 GiB) copied, 780 s, 19.8 MB/s 
dd: error writing '/dev/sdb': No space left on device
3777356+0 records in
3777355+0 records out
15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s

Creating a Partition and Formatting

The most common file systems are exFAT and NTFS on Windows, EXT4 on Linux, and FAT32, which can be used on all operating systems.

We will show you how to format your USB drive or SD card to FAT32 or EXT4. Use EXT4 if you intend to use the drive only on Linux systems, otherwise format it with FAT32. A single partition is sufficient for most use cases.

Format with FAT32

First, create the partition table by running the following command:

sudo parted /dev/sdb --script -- mklabel msdos

Create a Fat32 partition that takes the whole space:

sudo parted /dev/sdb --script -- mkpart primary fat32 1MiB 100%

Format the boot partition to FAT32:

sudo mkfs.vfat -F32 /dev/sdb1
mkfs.fat 4.1 (2017-01-24)

Once done, use the command below to print the partition table and verify that everything is set up correctly:

sudo parted /dev/sdb --script print

The output should look something like this:

 Model: Kingston DataTraveler 3.0 (scsi)
 Disk /dev/sdb: 15.5GB
 Sector size (logical/physical): 512B/512B
 Partition Table: msdos
 Disk Flags: 
 
 Number  Start   End     Size    Type     File system  Flags
  1      1049kB  15.5GB  15.5GB  primary  fat32        lba

That’s all! You have formatted your device.


Format with EXT4

Create a GPT partition table by issuing:

sudo parted /dev/sdb --script -- mklabel gpt

Run the following command to create a EXT4 partition that takes the whole space:

sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100%

Format the partition to ext4:

sudo mkfs.ext4 -F /dev/sdb1
 mke2fs 1.44.1 (24-Mar-2018)
 /dev/sdb1 contains a vfat file system
 Creating filesystem with 3777024 4k blocks and 944704 inodes
 Filesystem UUID: 72231e0b-ddef-44c9-a35b-20e2fb655b1c
 Superblock backups stored on blocks: 
 	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
 
 Allocating group tables: done                            
 Writing inode tables: done                            
 Creating journal (16384 blocks): done
 Writing superblocks and filesystem accounting information: done

Verify it by printing the partition table:

sudo parted /dev/sdb --script print

The output should look something like this:

 Model: Kingston DataTraveler 3.0 (scsi)
 Disk /dev/sdb: 15.5GB
 Sector size (logical/physical): 512B/512B
 Partition Table: gpt
 Disk Flags: 
 
 Number  Start   End     Size    File system  Name     Flags
  1      1049kB  15.5GB  15.5GB  ext4         primary

Source