محاضرة 7
Disk Partitioning (fdisk)
فهم تقسيم الهارد ديسك (Partitioning) وأنواعه، وكيفية إنشاء بارتشن جديد باستخدام `fdisk`.
ملخص المحاضرة
🐧 Lecture 7: Disk Partitioning (fdisk)
This lecture covers the process of disk partitioning, the different types of partitions, and the step-by-step procedure for creating, formatting, and mounting a new partition using the fdisk command-line tool.
Partitioning Concepts
- Disk Partitioning: The process of dividing a single physical disk into one or more logical areas.
- HDD vs. SSD:
- HDD (Hard Disk Drive): Uses mechanical spinning disks. Best for long-term storage at a low cost.
- SSD (Solid State Drive): Uses flash memory with no moving parts. It's more expensive but provides rapid boot times and fast file access.
- Partition Types:
- Primary: A bootable partition. A disk can have a maximum of four primary partitions.
- Extended: A special type of primary partition that cannot hold data itself but can be subdivided.
- Logical: The partitions created inside an extended partition. This is used to overcome the 4-partition limit.
The 3-Step Process for Using a New Partition
To use a new disk, you must perform three steps:
- Step 1: Create the Partition: Use a tool like
fdiskto define the partition's size and type. - Step 2: Format the Partition: Create a filesystem on the partition so the OS can store data on it (e.g., ext4).
- Step 3: Mount the Partition: Attach the formatted partition to a directory in the filesystem so you can access it.
Using the fdisk Utility
fdisk is a powerful command-line tool for partition management.
fdisk -l: Lists all disks and their current partition tables.fdisk /dev/sdb: Enters thefdiskediting tool for the disksdb.
Inside the fdisk tool, you use single-letter commands:
m: Prints the help menu.p: Prints the current partition table for the disk you are editing.n: Adds a new partition. It will ask you to choose:pfor primaryefor extended- It will then ask for the partition number, first sector (press Enter for default), and last sector or size (e.g.,
+2048Mfor 2GB).
d: Deletes a partition.w: Writes all changes to the disk and exits. This is the save-and-quit command.q: Quits the tool without saving any changes.
Formatting and Mounting
partprobe /dev/sdb: After usingfdisk, this command tells the kernel to re-read the partition table to see the new changes.mkfs -t ext4 /dev/sdb1: Makes a filesystem of typeext4on the newsdb1partition.mkdir -p /mt/sdb1: Creates a directory to be used as the "mount point".mount /dev/sdb1 /mt/sdb1: Attaches thesdb1partition to the/mt/sdb1directory.df hT: Verifies that the partition is successfully mounted and shows its usage.
🐧 Lecture 8: Comprehensive Review and Practical Scenarios
This lecture serves as a final review of all topics and applies them to practical, real-world scenarios.
Topic 1: Core Linux Concepts (Recap)
- Linux: An operating system based on the Linux Kernel.
- Kernel: The core component that communicates with hardware (RAM, CPU, etc.).
- Distribution (Distro): A complete OS made from the Kernel plus a software collection, including GNU tools, a Package Manager, and a Graphical Environment (like Gnome).
- Shell: The program that takes text commands and gives them to the OS.
- Terminal: The window you type commands into.
- Why Linux?: It is Costless, Stable, Reliable, and Highly Secure.
Topic 2: User and Group Management (Recap)
- User Types: Root (full admin), Privileged (uses
sudo), and Normal (limited). - GUI Management: Done via
Settings > Users > Unlock. - CLI User Commands:
sudo useradd -m name: Creates user with a home directory.sudo useradd name: Creates user without a home directory.sudo passwd name: Sets the user's password.sudo userdel name: Deletes user but keeps their files.sudo userdel -r name: Deletes user AND their files.
- CLI Group Commands:
sudo groupadd name: Creates a new group.sudo usermod -aG group user: Adds a user to a group.groups user: Lists a user's groups.gpasswd -d user group: Removes user from a group.cat /etc/passwd: Lists all users.cat /etc/group: Lists all groups.
Topic 3: File Permissions (Recap)
- Structure:
[File Type] [User (rwx)] [Group (rw-)] [Other (r--)]. - File Type:
-for file,dfor directory. - Command:
chmod. - Symbolic Mode: Uses
u, g, oand+, -, =.- Example:
chmod g+x file(Adds Execute for Group). - Example:
chmod u-x file(Removes Execute from User). - Example:
chmod o-rwx file(Removes All from Other).
- Example:
- Absolute Mode: Uses numbers (r=4, w=2, x=1).
rwx= 4+2+1 = 7rw-= 4+2+0 = 6r-x= 4+0+1 = 5r--= 4+0+0 = 4---= 0+0+0 = 0- Example:
chmod 755 filemeansrwxr-xr-x. - Example:
chmod 670 filemeansrw-rwx---.
Topic 4: Partitioning & Advanced File Commands (Recap)
- Partitioning: Dividing a disk. Max 4 primary partitions. An Extended partition holds Logical partitions (e.g., sda5, sda6).
- 3 Steps: 1) Create (
fdisk /dev/sdb), 2) Format (mkfs -t ext4 /dev/sdb1), 3) Mount (mount /dev/sdb1 /data). fdiskcommands:n(new),p(print),w(write/save),q(quit).find: Searches for files.find . -name "Capsules": Finds a file named "Capsules" in the current directory (.).
grep: Searches inside files.grep Panadol Capsules: Searches for the text "Panadol" inside the "Capsules" file.
tail -f: Follows a file and shows new lines as they are added.echo >>: Appends text to a file.uniq: Removes consecutive duplicate lines from a file.tar: Archives (zips) and extracts (unzips) files.tar -cvf Books.Rar ...: Creates a verbose file (archive).tar -xvf Books.Rar: Extracts a verbose file (archive).