Question 0: Disk Structure
The hard disk is divided into sectors (each sector is 512b or 4kb). The first sector records: the starting sector number of the partition, the ending sector number of the partition, the type of partition (linux/swap), and the status of the partition (whether it is bootable).
Question 1: How are disks partitioned in Linux servers?
In Linux systems, disk devices typically exist as files under /dev. 1. SATA/SCSI/SAS hard disks are named /dev/sda, /dev/sdb, etc.; 2. NVMe solid-state drives are named /dev/nvme0n1, /dev/nvme0n2, etc.; 3. Virtual machine disks are named /dev/vda, /dev/xvda, etc.
Question 2: Partitioning – Formatting – Mounting: What tools are available for partitioning?
1. For less than 2GB: use fdisk 2. For more than 2GB: use parted
Question 3: How to partition?
fdisk is used to directly operate on the newly added hard disk; under the /dev directory, for example: fdisk /dev/sdb: this enters an interactive environment. Common interactive commands are as follows: n: new partition (new partition) p: print partition list (print) d: delete partition (delete) t: change partition type (type) w: write and exit (write) q: quit without saving (quit) m: view help (menu)
After entering the interactive environment, how to operate:
root@localhost /]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.35.2). Changes will remain in memory until you decide to write them to disk. Think twice before using the write command. The device does not contain a recognizable partition table. Created a new DOS disk label with identifier 0x2275ff5b. Command (enter m for help): n Partition type p: primary partition (0 primary, 0 extended, 4 free) e: extended partition (logical partition container) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-20971519, default 2048): Last sector, +/-sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +5G Created a new partition 1 of type 'Linux', size 5 GiB. Command (enter m for help): w The partition table has been altered. Calling ioctl() to re-read the partition table. Syncing disks.
Question 4: Formatting:
1. Formatting is necessary because the partition is empty, and the operating system does not know how to store files. 2. Formatting creates a file system on the partition, such as ext4, xfs, etc. 3. The file system is responsible for managing: How files are stored Directory structure Metadata (permissions, timestamps, etc.) mkfs.ext4 /dev/sdb1 mke2fs 1.45.6 (20-Mar-2020) Creating a file system with 1,310,720 blocks (each block 4k) and 327,680 inodes. File system UUID: 6e2b9980-7482-4b2d-ad6c-36a90ec31cc6. Backup superblock stored at the following blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736. Allocating group table: Done. Writing inode table: Done. Creating journal (16,384 blocks): Done. Writing superblock and filesystem accounting information: Done.
Question 5: Mounting – Mounting to /mnt/data
mount /dev/sdb1 /mnt/data [root@localhost mnt]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 445M 0 445M 0% / devtmpfs 467M 0 467M 0% /dev shm tmpfs 467M 6.8M 460M 2% /run tmpfs 467M 0 467M 0% /sys/fs/cgroup dev/mapper/klas-root 27G 3.6G 24G 14% / tmpfs 467M 0 467M 0% /tmp dev/sda1 1014M 256M 759M 26% /boot tmpfs 94M 0 94M 0% /run/user/0 dev/sdb1 4.9G 20M 4.6G 1% /mnt/data
Question 6: Setting up automatic mounting at boot
vim /etc/fstab Add this line /dev/sdb1 /mnt/data ext4 defaults 00 # Here is for verification mount -a