iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

HDD Formatting on Ubuntu Server 24.04

に公開

Overview

This is a memo about formatting a disk to ext4 while migrating an HDD previously used in Windows to Ubuntu Server.

Environment

  • Ubuntu Server 24.04.3 LTS
  • Source HDD file system: NTFS
  • Destination HDD file system: ext4

Steps

Connecting the HDD

Connect the HDD to the Ubuntu Server.

Checking the Device File

You can check it with the following command:

sudo fdisk -l

The device file was found to be sdb (the HDD connected this time is 2 TB).
By the way, we can see that the partitions are divided into two: sdb1 and sdb2.
Using the lsblk command makes it easier to visualize the partitions.

...
Disk /dev/sdb: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
...
Device      Start        End    Sectors  Size Type
/dev/sdb1      34     262177     262144  128M Microsoft reserved
/dev/sdb2  264192 3907026943 3906762752  1.8T Microsoft basic data
...

Deleting Partitions

From here, we will manage the disk using parted.
Execute the following to enter interactive mode:

sudo parted /dev/sdb

Check the current partition numbers:

(parted) print

You can see the current partition numbers.
Additionally, you can see that the partition table is GPT.

...
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name                          Flags
 1      17.4kB  134MB   134MB                Microsoft reserved partition  msftres
 2      135MB   2000GB  2000GB  ntfs         Basic data partition          msftdata

Delete partitions 1 and 2:

(parted) rm 1
(parted) rm 2

Creating Partitions

Just in case, change the disk label to GPT.
A warning will be displayed, so enter "yes" if it's okay.

(parted) mklabel 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

Create a partition.
You will be asked for the partition name, file system type, start position, and end position.

(parted) mkpart
Partition name?  []? HDD_2TB # Partition name (optional, you don't have to specify it)
File system type?  [ext2]? ext4 # File system type
Start? 0% # Start position
End? 100% # End position

Check if the partition was created correctly.

(parted) p

It has been created correctly.

Model: ***
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  2000GB  2000GB  ext4         HDD_2TB

Exit interactive mode.

(parted) q

Formatting the Partition

Check the device file name again.

sudo fdisk -l

You can see that the device file name for the partition created this time is sdb1.

...
Disk /dev/sdb: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
...
Device     Start        End    Sectors  Size Type
/dev/sdb1   2048 3907028991 3907026944  1.8T Linux filesystem

Execute the following to format it to ext4.

sudo mkfs.ext4 /dev/sdb1
# Various information will be displayed below
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 488378368 4k blocks and 122101760 inodes
Filesystem UUID: 1ba1037f-f799-4d6c-bd32-6427d1b3d42d
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000, 214990848

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done # The default value will be entered after waiting for a while
Writing superblocks and filesystem accounting information: done # The default value will be entered after waiting for a while

Mounting to a Mount Point

Finally, mount the partition to an arbitrary mount point.
In this case, we will mount it to ~/hdd.

mkdir ~/hdd
sudo mount /dev/sdb1 ~/hdd

You can check whether it is mounted with the following command:

df -h

It will be displayed as follows:

...
/dev/sdb1                          1.8T   28K  1.7T   1% /home/me/hdd

References

https://blog.hn-pgtech.com/2024-03-20/
https://qiita.com/pollenjp/items/d5924016a6c88e4a9172

Discussion