I seldom add and remove disks on my Linux machine but I added a new disk on my Linux desktop machine just now, so I am going to summarize what I am doing here now. The commands I am showing in this blog can applied to any cloud environment as well.
I added a Kensington SSD (500 GB) disk. I want to list the disks attached to my Linux. Here is what you can do.
lsblk
Here is the result:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 447.1G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 446.6G 0 part /
sdb 8:16 0 447.1G 0 disk
└─sdb1 8:17 0 447.1G 0 part
You can alternatively list disks and their partitions with fdisk.
sudo fdisk -l
Disk /dev/sdb: 447.13 GiB, 480103981056 bytes, 937703088 sectors
Disk model: KINGSTON SA400S3
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc979445b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 * 2048 937701375 937699328 447.1G 83 Linux
lsblk shows the disks and the partitions. The disk I just added is sdb and it has sdb1 partition. I want to remove the partition, recreate it and format it from the terminal. Here is how you can remove the partition sdb1.
Though fdisk
shows more detailed information, lsblk
seems to be easier to read.
To remove the sdb1 partition, we’ll select the disk first with fdisk.
sudo fdisk /dev/sdb
You will see a prompt like the following:
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
By entering m, you get the pretty good list of what you can do with it.
If you enter ‘d’, you get to remove the partition. As you may have seen, sdb disk that I have has only 1 partition, so partition 1 is automatically selected. If the disk has multiple partitions, you can select a partition by entering its number.
When I enter ‘i’, I get to print information about a partition but since I removed the only partition, it cannot print anything.
Command (m for help): i
No partition is defined yet!
Now you can save the change you just made by entering ‘w’. When you do that, it exits fdisk. Just to see it, you can run lsblk again and you don’t see any partition anymore.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 447.1G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 446.6G 0 part /
sdb 8:16 0 447.1G 0 disk
Creating 2 New Partitions
Select sdb disk with fdisk.
sudo fdisk /dev/sdb
Now I am dividing the disk into 2 partitions.
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (34-937703054, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-937703054, default 937703054): 468851527
Created a new partition 1 of type 'Linux filesystem' and of size 223.6 GiB.
Command (m for help): n
Partition number (2-128, default 2): 2
First sector (468851528-937703054, default 468852736):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (468852736-937703054, default 937703054):
Created a new partition 2 of type 'Linux filesystem' and of size 223.6 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
I calculated the last sector number for the partition 1 by dividing the last number of the sector by 2.
Here is the result of lsblk.
❯ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 447.1G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
└─sda2 8:2 0 446.6G 0 part /
sdb 8:16 0 447.1G 0 disk
├─sdb1 8:17 0 223.6G 0 part
└─sdb2 8:18 0 223.6G 0 part
sdb is now partitioned into 2 now. Now we have format them and mount them to be able to start to use them. Let’s format them!
❯ sudo mkfs -t ext4 /dev/sdb1
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done
Creating filesystem with 58606185 4k blocks and 14655488 inodes
Filesystem UUID: 8c060f92-bf34-4473-993d-da8b52474da3
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
❯ sudo mkfs -t ext4 /dev/sdb2
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done
Creating filesystem with 58606289 4k blocks and 14655488 inodes
Filesystem UUID: 3c31b2dc-409d-49ec-a9c2-e23748c49bbe
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
Now create a directory you can mount the partitions.
mkdir ~/extra1
mkdir ~/extra2
Now mount it.
sudo mount /dev/sdb1 /home/hiriumi/extra1
sudo mount /dev/sdb2 /home/hiriumi/extra2
Now I can access those partitions from those directories in my home directory.
However, the mount does not survive restarts. What to do? Let’s take a look at the disk data a little closer with lsblk.
lsblk -fs
We will use UUID to retain the mounts in /etc/fstab
file. Be very careful editing the file. If there is an error, your system might not start up correctly and you might get into a rabbit hole of fixing it.
NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
sda1 vfat 8FE2-835D 505.8M 1% /boot/efi
└─sda
sda2 ext4 226cfcfc-711c-4b8d-b0f8-67210dfbf8b4 390G 6% /
└─sda
sdb1 ext4 8c060f92-bf34-4473-993d-da8b52474da3 207.8G 0%
└─sdb
sdb2 ext4 3c31b2dc-409d-49ec-a9c2-e23748c49bbe 207.8G 0%
└─sdb
Now add the 2 lines in /etc/fstab
file.
UUID=8c060f92-bf34-4473-993d-da8b52474da3 /home/hiriumi/extra1 ext4
UUID=3c31b2dc-409d-49ec-a9c2-e23748c49bbe /home/hiriumi/extra2 ext4
Now you can constantly access those partitions.
❯ ll | grep extra
drwxr-xr-x 3 root root 4.0K Jan 30 13:16 extra1
drwxr-xr-x 3 root root 4.0K Jan 30 13:16 extra2
So many steps huh… If you can use a desktop environment, you can use disks utility to configure disk partitions pretty much visually and as for Linux Mint, it automatically edits /etc/fstab automatically. So what are these steps for? Well, if you are en engineer having to deal with disks and partitions in cloud environments, you cannot escape from these issues.
