-
Notifications
You must be signed in to change notification settings - Fork 0
Disk Management
In Linux, managing your disks is an important task to ensure that your data is stored properly, and your system works efficiently. Just like organizing files in your bedroom, managing disks and partitions helps organize your data so that your computer can find everything easily and quickly.
- A disk is your physical storage, where data is stored (like your hard drive or SSD).
- Partitions are sections or divisions on that disk, each acting as a separate storage unit.
- Think of a disk like a book, and the partitions are like the chapters in that book. Each chapter (partition) can contain different types of information.
💡 A partition is a part of your disk that’s separated from the rest. It’s like a section of your bookshelf where you keep different types of books: one for your homework, another for novels, and one more for magazines.
- Organize data: You can separate your operating system files from your personal files.
- Better performance: Organizing data into separate partitions can make your system faster.
- Data protection: If something goes wrong with one partition, others can still be safe.
- Primary Partition: These are the main partitions where your data and operating system are stored.
- Extended Partition: This is a special partition that allows you to create more than four partitions.
- Logical Partition: These are partitions inside an extended partition, used to organize your data further.
There are several tools in Linux that you can use to manage your disks and partitions. Each one has its purpose, and knowing which one to use helps you do the job faster and more efficiently.
fdisk is a popular tool to manage disks and partitions. It allows you to create, delete, and modify partitions.
📚 Example: Listing Partitions
sudo fdisk -lThis will list all the disks and partitions on your system.
lsblk is a tool that shows your disk partitions in a tree-like structure. It’s a simple way to understand how your disk is divided.
lsblk📚 Example Output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 200G 0 part /home
└─sda3 8:3 0 200G 0 partThis shows that sda is a 500GB disk, with three partitions:
-
sda1 (100GB) for the root system (
/) -
sda2 (200GB) for the home directory (
/home) - sda3 (200GB) is unmounted.
parted is used for more complex partitioning, like resizing partitions or creating partitions with different file systems.
📚 Example: Resize a Partition
sudo parted /dev/sda resizepart 2 400GBThis command resizes partition sda2 to 400GB.
Creating partitions involves breaking up your disk into sections, like dividing a bookshelf into smaller spaces. You can use tools like fdisk or parted to do this.
-
Open
fdiskwith your disk (e.g.,/dev/sda):
sudo fdisk /dev/sda-
Create a new partition: Press
nto create a new partition. -
Select the partition type: You can choose whether it’s a primary (p) or extended (e) partition.
-
Set the partition size: Type the size you want for your new partition.
-
Write changes to disk: Press
wto save the changes.
Once a partition is created, it needs to be formatted before it can store data. This means putting a file system on it, like ext4 (common in Linux), NTFS (for Windows), or FAT32.
Let’s say you just created a partition called /dev/sda3, and you want to format it with the ext4 file system.
sudo mkfs.ext4 /dev/sda3This command formats the partition and makes it ready for storing files.
After formatting, the partition needs to be mounted so the system can access it. Mounting a partition means telling your system where to put it so you can use it.
- Create a mount point (a directory where the partition will be mounted):
sudo mkdir /mnt/data- Mount the partition:
sudo mount /dev/sda3 /mnt/data-
Access your partition: Now you can access your new partition by going to
/mnt/data.
If you want the system to automatically mount the partition every time it boots up, you need to add it to the /etc/fstab file.
- Open the
fstabfile for editing:
sudo nano /etc/fstab- Add the following line at the end:
/dev/sda3 /mnt/data ext4 defaults 0 0This tells the system to mount /dev/sda3 to /mnt/data every time it starts.
Sometimes, you might want to increase or decrease the size of a partition. This is especially helpful when your system or data grows.
You can resize partitions using tools like gparted (a graphical tool) or parted.
📚 Example (using gparted):
- Open gparted:
sudo gparted- Select the partition you want to resize.
- Drag the slider or enter the new size and apply the changes.
If you no longer need a partition, you can delete it using fdisk or gparted.
- Open
fdisk:
sudo fdisk /dev/sda- Press
dto delete a partition. - Select the partition number you want to delete.
- Press
wto save the changes.