分区概念

  1. 1. 什么是分区: 起始扇区和结束扇区形成的区域就是分区(扇区是磁盘的最小单位)
  2. 2. 分区信息:记录起始扇区和结束扇区的记录就是分区信息
  3. 3. 主分区:
  4. ①分区信息存在于0磁柱0磁道0扇区的位置
  5. ②能被格式化、能被挂载、能读写数据
  6. 4. 扩展分区:
  7. ①分区信息存在于0磁柱0磁道0扇区的位置
  8. ②不能被格式化、不能被挂载、不能读写数据
  9. ③主要的作用是划分逻辑分区,原因是电脑的分区最多只能划分4
  10. 5. 逻辑分区
  11. ①分区编号从5开始
  12. 6. MBR分区:可以划分主分区、拓展分区、逻辑分区,最多划分4个主分区
  13. 7. GPT分区:只能划分主分区,最多可以划分128个主分区

查看分区

  1. lsblk
  2. or
  3. disk -l
  4. output:
  5. root@archiso ~ # fdisk -l
  6. Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 sectors
  7. Disk model: VMware Virtual S
  8. Units: sectors of 1 * 512 = 512 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/O size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk /dev/loop0: 556.19 MiB, 583208960 bytes, 1139080 sectors
  12. Units: sectors of 1 * 512 = 512 bytes
  13. Sector size (logical/physical): 512 bytes / 512 bytes
  14. I/O size (minimum/optimal): 512 bytes / 512 bytes

建立硬盘分区

  1. fdisk /dev/sda
  2. # 第一步:创建一个空的分区表
  3. g: create a new empty GPT partition table
  4. # 第二步:创建分区, 需要注意序号、First sector和Last sector
  5. Command (m for help): n
  6. Partition number (1-128, default 1):
  7. First sector (2048-268435422, default 2048):
  8. Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-268435422, default 268435422): +1G
  9. # 第三步:查看分区表
  10. Command (m for help): p
  11. Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 sectors
  12. Disk model: VMware Virtual S
  13. Units: sectors of 1 * 512 = 512 bytes
  14. Sector size (logical/physical): 512 bytes / 512 bytes
  15. I/O size (minimum/optimal): 512 bytes / 512 bytes
  16. Disklabel type: gpt
  17. Disk identifier: 701EBB8C-25D8-624E-8EE6-70D36D4AEF90
  18. Device Start End Sectors Size Type
  19. /dev/sda1 2048 2099199 2097152 1G Linux filesystem
  20. /dev/sda2 18876416 268435422 249559007 119G Linux filesystem
  21. /dev/sda3 2099200 18876415 16777216 8G Linux filesystem
  22. Partition table entries are not in disk order.
  23. # 第四步:将分区的类型改成正确的,
  24. # 例如efi分区的类型是EFI System, swap的分区是类型是Linux swap
  25. Command (m for help): t
  26. Partition number (1-3, default 3): 1
  27. Partition type or alias (type L to list all): L
  28. 1 EFI System C12A7328-F81F-11D2-BA4B-00A0C93EC93B
  29. 2 MBR partition scheme 024DEE41-33E7-11D3-9D69-0008C781F39F
  30. 3 Intel Fast Flash D3BFE2DE-3DAF-11DF-BA40-E3A556D89593
  31. ... ...
  32. 43 FreeBSD ZFS 516E7CBA-6ECF-11D6-8FF8-00022D09712B
  33. Partition type or alias (type L to list all): 1
  34. # 第五步:再次查看分区类型
  35. Command (m for help): p
  36. Disk /dev/sda: 128 GiB, 137438953472 bytes, 268435456 sectors
  37. Disk model: VMware Virtual S
  38. Units: sectors of 1 * 512 = 512 bytes
  39. Sector size (logical/physical): 512 bytes / 512 bytes
  40. I/O size (minimum/optimal): 512 bytes / 512 bytes
  41. Disklabel type: gpt
  42. Disk identifier: 701EBB8C-25D8-624E-8EE6-70D36D4AEF90
  43. Device Start End Sectors Size Type
  44. /dev/sda1 2048 2099199 2097152 1G EFI System
  45. /dev/sda2 18876416 268435422 249559007 119G Linux filesystem
  46. /dev/sda3 2099200 18876415 16777216 8G Linux swap
  47. # 第六步:检查无错,即可保存分区
  48. Command (m for help): w
  49. The partition table has been altered.
  50. Calling ioctl() to re-read partition table.
  51. Syncing disks.

格式化分区

# 根分区 /dev/root_partition 上创建一个 Ext4 文件系统
130 root@archiso ~ # mkfs.ext4 /dev/sda2
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 31194875 4k blocks and 7798784 inodes
Filesystem UUID: e6bc7b6c-1a5f-4818-a244-2a1a163f04e0
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 (131072 blocks): done
Writing superblocks and filesystem accounting information: done

# 交换分区 (例如 /dev/sda3),请使用 mkswap(8) 将其初始化
130 root@archiso ~ # mkswap /dev/sda3
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=0907097c-ab50-4f82-ad9d-5d34ad84186b

# EFI 格式化
root@archiso ~ # mkfs.fat -F 32 /dev/sda1
mkfs.fat 4.1 (2017-01-24)

挂载

root@archiso ~ # df -h
Filesystem      Size  Used Avail Use% Mounted on
dev             2.4G     0  2.4G   0% /dev
run             2.4G  151M  2.2G   7% /run
/dev/sr0        679M  679M     0 100% /run/archiso/bootmnt
cowspace        256M  736K  256M   1% /run/archiso/cowspace
/dev/loop0      557M  557M     0 100% /run/archiso/sfs/airootfs
airootfs        256M  736K  256M   1% /
tmpfs           2.4G     0  2.4G   0% /dev/shm
tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
tmpfs           2.4G     0  2.4G   0% /tmp
tmpfs           2.4G  2.1M  2.4G   1% /etc/pacman.d/gnupg
tmpfs           479M     0  479M   0% /run/user/0

# 挂载sda2
root@archiso ~ # mount /dev/sda2 /mnt

root@archiso ~ # df -h
Filesystem      Size  Used Avail Use% Mounted on
dev             2.4G     0  2.4G   0% /dev
run             2.4G  151M  2.2G   7% /run
/dev/sr0        679M  679M     0 100% /run/archiso/bootmnt
cowspace        256M  736K  256M   1% /run/archiso/cowspace
/dev/loop0      557M  557M     0 100% /run/archiso/sfs/airootfs
airootfs        256M  736K  256M   1% /
tmpfs           2.4G     0  2.4G   0% /dev/shm
tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
tmpfs           2.4G     0  2.4G   0% /tmp
tmpfs           2.4G  2.1M  2.4G   1% /etc/pacman.d/gnupg
tmpfs           479M     0  479M   0% /run/user/0
/dev/sda2       117G   61M  111G   1% /mnt

# 挂载sda1
mount /dev/sda1 /mnt/boot

# 开启swap,相当于挂载
130 root@archiso ~ # swapon /dev/sda3