Table of Contents
  • Home
  • /
  • Blog
  • /
  • How to Partition And Format the Hard Drives on Raspberry Pi?
November 21, 2023
|
9m

How to Partition And Format the Hard Drives on Raspberry Pi?


Partition And Format The Hard Drives On Raspberry Pi

New Raspberry Pis are shipped with powerful computing and faster USB 3.0 ports. It is possible to connect the large-size USB storage drives and share them over the network to create your own file server or NAS. Well, all these are good to know. As thriving technology over time, it gives plenty of storage drive options and several supported formats. In this article, we are going to cover how to partition and format the hard drive on raspberry pi.

Filesystem Types:

There are more than 100 types of file systems that have developed over the years. We have selected only two main types of file systems. NTFS and EXT4. It is much needed to know as these are very close to the universe.

  1. NTFS: This file system is developed by Microsoft in the early 90s. All new versions of Windows operating systems will support this file system. Theoretically, NTFS can support hard drives up to just under 16 EB. The individual file size is capped at just under 256 TB, at least in Windows 8 and Windows 10, as well as in some newer Windows Server versions. When it comes to supporting, this file system is universally supported. Although it’s developed by Microsoft, it is supported by most Linux distributions and Mac.

  2. EXT4: This file system is developed based on the older Minix filesystem, A file system being used by Linux systems for ages. The higher maximum volume size it supports is 1 EB. That’s, again, a mathematical number. I know all these numbers don’t bother you like many of us. After all, who is going to use such a gigantic drive at home with Raspberry Pi!

Prerequisites for partitioning and formatting hard drives on a Raspberry Pi:

  1. Raspberry Pi hardware: You’ll need a Raspberry Pi device along with associated peripherals like a power supply, SD card, etc. The blog post suggests newer Raspberry Pi models with USB 3.0 ports to connect high-capacity drives.

  2. External USB hard drives: You’ll need one or more external USB hard drives you want to connect and format to use with your Raspberry Pi. The instructions cover both traditional hard disk drives (HDDs) as well as solid-state drives (SSDs).

  3. Basics of Linux and the command line: Many of the steps like partitioning, creating filesystems, editing configuration files etc. need familiarity with basic Linux commands and administering the system from the command line interface (CLI). Some prior experience with this would be helpful.

  4. Administrative privileges: Commands like using parted, creating mount points, editing system files like fstab etc will need you to have administrative (root/sudo) privileges on the Raspberry Pi OS. So you’ll need access to the pi user account or other account with sudo rights.

  5. Backup any data: As the blog post involves completely wiping any existing partitions and filesystems on the attached USB drives, if you have any data on them already, be sure to have backups available before proceeding. The operations detailed are destructive and will remove previous data.

  6. Understanding of filesystems: A basic background on common filesystems like NTFS, EXT4 will be helpful to understand the core concepts and select the appropriate ones to create on your external drives.

Let me know if you need any clarification or have additional questions on the prerequisites.

Which File System is Better to Use?

Now you have got some idea of the most popular file systems. The real question is, which format is better for you? The verdict is quite straight here. If you want to connect the storage drive between your Pi and Windows system quite frequently, then you might consider NTFS as your first choice. Because it is supported by most of the windows and non-windows platforms. On the other hand, if you want to stick your drive only with Raspberry Pi, EXT4 would be a better option for you. As it goes well with the Linux system. After all, Raspberry Pi is a custom variant of Debian Linux. We hope this feels you much better in selecting the file system for your file server. Gather up your drive, and let’s get started.

Partitioning and Formatting the Hard Drive on Raspberry Pi:

When you connect a fresh drive that you picked up from the factory. Even though it has nothing on it. It is going to be mounted under /media/pi by default. Again if we remount the drive, it will be mounted again under the same path. This nature is just fine for removable drives. The convention is to mount the drive under /mnt. Let’s see how this work. Note: This is just a demonstration; you may get different results when you try this on your PI and drives; it all depends on the version of hardware and software used.

Partitioning and Formatting the Hard Drive on Raspberry Pi

Step 1. Unmount the drive:

Unmount the drive from its current location by hitting the eject button.

Step 2. List out the connected drives:

Open the terminal and write the ‘parted’ command to see what drives are connected and how they are partitioned.$ sudo parted

Step 3. Use ‘print all’ to read the drive information:

Type ‘print all’ to see all the drives and their partition Information. If you see the information inside the red box, there are two drives mounted. 120 GB Hard Drive is mounted under /dev/sda with an NTFS file system with a single partition. And, you should see there is an SD card of size 8 GB mounted under /dev/mmcblk0 with two partitions. The first one is the 256 MB boot partition with the FAT32 file system, and the second one is the root EXT4 partition.
(parted) print all

Step 4. Select the drive to partition:

Select the drive to format and create new partitions. Type the ‘select’ command with the drive path.
(parted) select /dev/sda

Step 5. Create a fresh GPT partition table:

Create a fresh GPT partition table by typing ‘mklabel gpt’ command. You will get a warning to wipe out all your drive. Type ‘yes’ to continue. Please bear in mind, that it’s just a partition table, not the partitions.
(parted) mklabel gpt

Step 6. Type ‘print’ to make sure the new GPT partition is created.

(parted) print

Step 7. Create new partitions:

For demonstration purposes, we are going to create three partitions on this drive using ‘mkpart’ command: os, data-ntfs, and data-ext4. Later we will show you how to install the Raspberry Pi OS on the hard drive and boot from USB.
Type ‘mkpart’ command to create a new partition. It asks four simple questions to create a partition. Your partition will be created upon supplying the answers. Just pay attention to the commands we used to create three partitions. You can create partitions in a single line command as well, as we show in the below screenshot.
(parted) mkpart data-ntfs ntfs 8gb 50%
(parted) mkpart data-ext4 ext4 50% 100%

Step 8. Exit from parted:

All right, everything looks fine. Type ‘q’ to exit from the parted. You can ignore the ‘fstab’ at this point in time. We will look at this in later sessions.
(parted) q

Step 9. Format the partitions:

You can’t use the partitions until you format them. Let’s use mkfs commands to format the partitions. Different versions of mkfs commends are there to format NTFS and EXT4 file systems. In this command -L specifies the label of the drive, and -Q specifies quick format, which takes the partition name as a parameter. Note: EXT4 doesn’t take -Q as it doesn’t support the quick format.
$ sudo mkfs.ntfs -L data-ntfs -Q /dev/sda2$ sudo mkfs.ext4 -L data-ext4 /dev/sda3

Step 10. Reboot the Raspberry Pi:

Sometimes, the GUI desktop doesn’t pick this partition information. We recommend rebooting the Pi once.After reboot, you will see the partitions on your GUI desktop. But, these drives are mounted under /mdia/pi.

Step 11. Remount these partitions under /mnt:

To remount these partitions under /mnt.
At first, Unmount the partitions by clicking on their eject button. Refer to the first section to see how to unmount the drive.Open the terminal and change the directory to /mnt.$ cd /mnt
Create two directories named ‘data-ntfs’ and ‘data-ext4’ under /mnt.$ sudo mkdir data-ntfs$ sudo mkdir data-ext4
Mount the partitions using the mount command. Note: This is just a temp mount. It is not going to work after reboot.$ sudo mount /dev/sda2 /mnt/data-ntfs$ sudo mount /dev/sda3 /mnt/data-ext4
Do you remember the fstab? It’s a file system table. This is where you can mount a partition forever. Open the /etc/fstab file and see how it looks. You can only see the SD card at this time.$ cat /etc/fstab

Step 12. Take the PARTUUID value of the two partitions:

You need to add those two partitions to /etc/fstab to mount permanently. Before that, make a note of PARTUUID value of the two partitions.
$ sudo blkid

Step 13. Create a permanent mount:

Use your choice of a text editor to edit and add the partition information in the /etc/fstab. You can add the two lines representing each line for a partition. Write this information separated by TAB.
PARTUUID=VALUEMount pathFile Systemdefault or default, notime: The word ‘notime’ just tells us to keep track of the access time along with created and modified time.00

Step 14. Reboot the Raspberry Pi

Reboot to save all the changes and mount the partitions forever. This is how you can partition and format the hard drives on Raspberry Pi

Once the drive is ready to use on the Pi, you can use it locally as extended storage or share it over the network to feel the effect of a personal file server or a NAS. We have shown how to set up a NAS on the Raspberry Pi in a different blog post. Please read the post and leave your comments there.

Thank you for reading this article. Please visit the below links to read more such interesting articles. Peace leave your comments here below and let us know your feedback. This helps us to bring more such articles.

You may also like these articles:

Arun KL

Arun KL is a cybersecurity professional with 15+ years of experience in IT infrastructure, cloud security, vulnerability management, Penetration Testing, security operations, and incident response. He is adept at designing and implementing robust security solutions to safeguard systems and data. Arun holds multiple industry certifications including CCNA, CCNA Security, RHCE, CEH, and AWS Security.

Recently added

Tutorials

View All

Learn More About Cyber Security Security & Technology

“Knowledge Arsenal: Empowering Your Security Journey through Continuous Learning”

Cybersecurity All-in-One For Dummies - 1st Edition

"Cybersecurity All-in-One For Dummies" offers a comprehensive guide to securing personal and business digital assets from cyber threats, with actionable insights from industry experts.

Tools

Featured

View All

Learn Something New with Free Email subscription

Subscribe

Subscribe