Linux使用fdisk命令新建分区详细步骤
前言:
本文将演示在LINUX系统中如何添加主分区,SWAP分区,逻辑分区
1.VMware添加硬盘
#我们添加 SCSI 磁盘
2.重启或者扫描主机更新磁盘信息
[16:46:02 root@c7-1 ~]#fdisk -l #此时新加的 sdb 磁盘还没出现,如果不想重启虚拟机执行下条命令 Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000f2378 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 10487807 4194304 82 Linux swap / Solaris /dev/sda3 10487808 209715199 99613696 83 Linux [16:51:11 root@c7-1 ~]#echo "- - -" > /sys/class/scsi_host/host0/scan #全盘扫描主机更新信息,这样就不需要重启虚拟机了 [16:51:28 root@c7-1 ~]#fdisk -l #再次查看 sdb 已经出现 Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000f2378 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 10487807 4194304 82 Linux swap / Solaris /dev/sda3 10487808 209715199 99613696 83 Linux Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
3.开始分区
#分区规划,sdb1主分区10G,sdb2 SWAP分区10G,sdb3扩展分区20G,sdb5逻辑分区10G
#步骤:主分区 -- SWAP分区 -- 扩展分区 -- 逻辑分区
#逻辑分区是在扩展分区的基础上分的
[16:51:29 root@c7-1 ~]#fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x1976e323. Command (m for help): n #开始分主分区 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p #主分区 Partition number (1-4, default 1): 1 #分区号可以默认也可手动选 First sector (2048-125829119, default 2048): #扇区默认 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-125829119, default 125829119): +10G #主分区设定10G Partition 1 of type Linux and of size 10 GiB is set Command (m for help): p #显示分区信息 Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x1976e323 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux Command (m for help): n #开始分 SWAP 分区 Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p #选择主分区 Partition number (2-4, default 2): 2 First sector (20973568-125829119, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-125829119, default 125829119): +10G #大小设定10G Partition 2 of type Linux and of size 10 GiB is set Command (m for help): t #设定分区系统号码,82为 SWAP Partition number (1,2, default 2): 2 #我们将 sdb2 设定为 swap 分区,别写成 1 了 Hex code (type L to list all codes): 82 #设定分区类型 Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): p #查看分区情况 Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x1976e323 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux /dev/sdb2 20973568 41945087 10485760 82 Linux swap / Solaris Command (m for help): n #开始添加扩展分区 Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): e # e 添加扩展分区 Partition number (3,4, default 3): First sector (41945088-125829119, default 41945088): Using default value 41945088 Last sector, +sectors or +size{K,M,G} (41945088-125829119, default 125829119): +20G #大小设定20G Partition 3 of type Extended and of size 20 GiB is set Command (m for help): n #开始添加逻辑分区 Partition type: p primary (2 primary, 1 extended, 1 free) l logical (numbered from 5) Select (default p): l # l 添加逻辑分区 Adding logical partition 5 First sector (41947136-83888127, default 41947136): Using default value 41947136 Last sector, +sectors or +size{K,M,G} (41947136-83888127, default 83888127): +10G #大小设定10G Partition 5 of type Linux and of size 10 GiB is set Command (m for help): p #查看分区情况 Disk /dev/sdb: 64.4 GB, 64424509440 bytes, 125829120 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x1976e323 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux /dev/sdb2 20973568 41945087 10485760 82 Linux swap / Solaris /dev/sdb3 41945088 83888127 20971520 5 Extended /dev/sdb5 41947136 62918655 10485760 83 Linux Command (m for help): w #保存并退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
[16:54:55 root@c7-1 ~]#partprobe /dev/sdb #更新分区表信息
4.格式化文件系统并挂载
(1)格式化主分区
mkdir /opt/data{1,2} #创建两个挂载目录给主分区和逻辑分区 mkfs.xfs /dev/sdb1 #格式化主分区为 xfs 格式
(2)格式化SWAP分区
mkswap /dev/sdb2 #将 sdb2 设置为 SWAP swapon /dev/sdb2 #开启 sdb2 交换功能
(3)格式化逻辑分区
mkfs.xfs /dev/sdb5 #将 sdb5 设置为 xfs 格式
(4)将分区信息写进 /etc/fstab 配置文件中实现自动挂载
[17:05:50 root@c7-1 ~]#vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jul 23 15:33:12 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=adf79f65-0a7d-4159-adde-099e05b62786 / xfs defaults 0 0
UUID=e269bb76-c9d1-499b-a0d3-9bb86dbbb72d /boot xfs defaults 0 0
UUID=61978a53-09e9-4404-96d1-7b3e2bfe7bba swap swap defaults 0 0
/dev/sdb1 /opt/data1 xfs defaults 0 0
/dev/sdb2 swap swap defaults 0 0
/dev/sdb5 /opt/data2 xfs defaults 0 0
[17:06:07 root@c7-1 ~]#mount -a #重载 /etc/fstab 文件,不需要重启即可生效
5.验证
[17:36:44 root@c7-1 ~]#free -h #SWAP增加了10G
total used free shared buff/cache available
Mem: 1.8G 709M 126M 23M 982M 938M
Swap: 13G 0B 13G
[17:36:52 root@c7-1 ~]#cat /proc/partitions
major minor #blocks name
8 0 104857600 sda
8 1 1048576 sda1
8 2 4194304 sda2
8 3 99613696 sda3
11 0 4554752 sr0
8 16 62914560 sdb
8 17 10485760 sdb1
8 18 10485760 sdb2
8 19 1 sdb3
8 21 10485760 sdb5
[17:37:01 root@c7-1 ~]#df -h #sdb1和sdb5已经挂载成功
Filesystem Size Used Avail Use% Mounted on
devtmpfs 895M 0 895M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 11M 900M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda3 95G 5.3G 90G 6% /
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 182M 4.0K 182M 1% /run/user/42
tmpfs 182M 0 182M 0% /run/user/0
tmpfs 182M 36K 182M 1% /run/user/1000
/dev/sr0 4.4G 4.4G 0 100% /run/media/syhj/CentOS 7 x86_64
/dev/sdb1 10G 33M 10G 1% /opt/data1
/dev/sdb5 10G 33M 10G 1% /opt/data2
[17:37:13 root@c7-1 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 95G 0 part /
sdb 8:16 0 60G 0 disk
├─sdb1 8:17 0 10G 0 part /opt/data1
├─sdb2 8:18 0 10G 0 part [SWAP]
├─sdb3 8:19 0 1K 0 part
└─sdb5 8:21 0 10G 0 part /opt/data2
sr0 11:0 1 4.4G 0 rom /run/media/syhj/CentOS 7 x86_64
[17:37:28 root@c7-1 ~]#blkid -o list
device fs_type label mount point UUID
------------------------------------------------------------------------------------------------------------------------------------------------------------
/dev/sda1 xfs /boot e269bb76-c9d1-499b-a0d3-9bb86dbbb72d
/dev/sda2 swap <swap> 61978a53-09e9-4404-96d1-7b3e2bfe7bba
/dev/sda3 xfs / adf79f65-0a7d-4159-adde-099e05b62786
/dev/sr0 iso9660 CentOS 7 x86_64 /run/media/syhj/CentOS 7 x86_64 2019-09-11-18-50-31-00
/dev/sdb1 xfs /opt/data1 be41ef53-be5a-4d67-89ac-541b594a4d85
/dev/sdb2 swap <swap> bb50bc18-b642-4917-b552-e25bdda74a1d
/dev/sdb5 xfs /opt/data2 69d1003d-3049-4e7a-9e31-bf1cbf4efaab