1,硬盘查看分区情况(虚拟机先手动添加一块硬盘)
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 9.8G 0 part
├─centos-root 253:0 0 9.3G 0 lvm /
└─centos-swap 253:1 0 512M 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
sr0 11:0 1 1024M 0 rom
[root@centos7 ~]#
可以看到我们新增加的硬盘为sdb,还未分区
2,分区:看出sdb磁盘空间未启动;接下来进行磁盘分区,fdisk /dev/sdb
[root@centos7 ~]# 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 0x1f9fd57b.
Command (m for help): p 注:列出当前操作硬盘的分区情况,用p
Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f9fd57b
Device Boot Start End Blocks Id System
Command (m for help): n 注:添加分区
Partition type:
p primary (0 primary, 0 extended, 4 free) 注:1个主分区,3个空闲分区总共四个主分区
e extended
Select (default p): p 注:添加主分区,默认主分区
Partition number (1-4, default 1): 1 注:添加主分区1
First sector (2048-6291455, default 2048): 注:直接回车,主分区1的结束位置;默认为2048,默认就好;
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-6291455, default 6291455): +1G 注:指定分区大小,用+1G来指定大小为1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): w 注:保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos7 ~]#
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 9.8G 0 part
├─centos-root 253:0 0 9.3G 0 lvm /
└─centos-swap 253:1 0 512M 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
└─sdb1 8:17 0 1G 0 part
sr0 11:0 1 1024M 0 rom
[root@centos7 ~]#
可以看到我们为sdb分了一个区为1G
3,接下来要对sdb的每一个分区进行格式化(swap分区格式化要使用:mkswap /dev/sdb5)
[root@centos7 ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@centos7 ~]#
4,在使用硬盘之前必须对其分区进行格式化,并挂载。
先创建分区挂载的文件夹
[root@centos7 ~]# mkdir /aa
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 9.3G 1.6G 7.8G 17% /
devtmpfs 982M 0 982M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 8.6M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 197M 103M 95M 53% /boot
tmpfs 199M 0 199M 0% /run/user/0
[root@centos7 ~]#
[root@centos7 ~]# mount /dev/sdb1 /aa
[root@centos7 ~]#
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 9.3G 1.6G 7.8G 17% /
devtmpfs 982M 0 982M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 8.6M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda1 197M 103M 95M 53% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 976M 1.3M 924M 1% /aa
[root@centos7 ~]#
注:这个挂载只是临时的,重启服务器之后又需要重新挂载,通过修改/etc/fstab文件使挂载永久有效
要想开机重启能自动挂载还需要如下:添加UUID
查看分区的 UUID
[root@centos7 ~]# blkid
/dev/sda1: UUID="1ea4931d-ac82-4095-bcc4-215fdb164920" TYPE="xfs"
/dev/sda2: UUID="GVpeR0-0W85-CBHy-e7eh-aIiW-QlcC-7Lj6b0" TYPE="LVM2_member"
/dev/sdb1: UUID="100a0a62-5aff-4837-b1fa-635efa1b2463" TYPE="ext3"
/dev/mapper/centos-root: UUID="e935a619-9aca-4b73-b98c-8c59e84ab6d5" TYPE="xfs"
/dev/mapper/centos-swap: UUID="492cfe22-df1e-4ddc-b7b4-41f406fe3219" TYPE="swap"
[root@centos7 ~]#
添加UUID:
[root@centos7 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat May 18 03:21:31 2019
#
# 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
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=1ea4931d-ac82-4095-bcc4-215fdb164920 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID=100a0a62-5aff-4837-b1fa-635efa1b2463 /aa ext3 defaults 0 0
[root@centos7 ~]#
5,添加一个扩展分区如下:
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 9.8G 0 part
├─centos-root 253:0 0 9.3G 0 lvm /
└─centos-swap 253:1 0 512M 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
└─sdb1 8:17 0 1G 0 part /aa
sr0 11:0 1 1024M 0 rom
[root@centos7 ~]#
查看到sdb硬盘还有2G空间未分区。
接下来我们添加一块扩展分区
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 9.8G 0 part
├─centos-root 253:0 0 9.3G 0 lvm /
└─centos-swap 253:1 0 512M 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
└─sdb1 8:17 0 1G 0 part /aa
sr0 11:0 1 1024M 0 rom
[root@centos7 ~]#
[root@centos7 ~]# 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.
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f9fd57b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2):
First sector (2099200-6291455, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-6291455, default 6291455): +500M
Partition 2 of type Extended and of size 500 MiB is set
Command (m for help): p\
Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f9fd57b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 3123199 512000 5 Extended
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos7 ~]#
6,成功创建扩展分区,但是扩展分区是不可以直接使用的,想要使用就要在扩展分区上创建逻辑分区。(有了扩展分区才可以创建逻辑分区)
[root@centos7 ~]# 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.
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f9fd57b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 3123199 512000 5 Extended
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f9fd57b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 3123199 512000 5 Extended
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l 注:输入l知道创建类型为逻辑分区
Adding logical partition 5
First sector (2101248-3123199, default 2101248):
Using default value 2101248
Last sector, +sectors or +size{K,M,G} (2101248-3123199, default 3123199): +200M
Partition 5 of type Linux and of size 200 MiB is set
Command (m for help):
7,查看一下逻辑分区是否创建成功。
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 9.8G 0 part
├─centos-root 253:0 0 9.3G 0 lvm /
└─centos-swap 253:1 0 512M 0 lvm [SWAP]
sdb 8:16 0 3G 0 disk
└─sdb1 8:17 0 1G 0 part /aa
sr0 11:0 1 1024M 0 rom
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000bc854
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 20971519 10279936 8e Linux LVM
Disk /dev/sdb: 3221 MB, 3221225472 bytes, 6291456 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: 0x1f9fd57b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 3123199 512000 5 Extended
/dev/sdb5 2101248 2510847 204800 83 Linux
/dev/sdb6 2512896 3123199 305152 83 Linux
Disk /dev/mapper/centos-root: 9986 MB, 9986637824 bytes, 19505152 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 /dev/mapper/centos-swap: 536 MB, 536870912 bytes, 1048576 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
[root@centos7 ~]#
8,可以看出从/dev/sdb2 扩展分区,分出了2个逻辑分区sdb5,sdb6,创建完逻辑分区,格式化,直接输入命令后等待完成即可。
[root@centos7 ~]# mkfs.ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# mkfs.ext4 /dev/sdb6
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
76304 inodes, 305152 blocks
15257 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33947648
38 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@centos7 ~]#
[root@centos7 ~]# mkdir /bb
[root@centos7 ~]# mkdir /cc
[root@centos7 ~]#
[root@centos7 ~]# mount /dev/sdb5 /bb
[root@centos7 ~]# mount /dev/sdb6 /cc
[root@centos7 ~]#
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 9.3G 1.6G 7.8G 17% /
devtmpfs 982M 0 982M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 8.6M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sdb1 976M 1.3M 924M 1% /aa
/dev/sda1 197M 103M 95M 53% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb5 190M 1.6M 175M 1% /bb
/dev/sdb6 281M 2.1M 260M 1% /cc
[root@centos7 ~]#
磁盘容量与主分区、扩展分区、逻辑分区的关系:
硬盘的容量=主分区的容量+扩展分区的容量
扩展分区的容量=各个逻辑分区的容量之和