磁盘分区
Fdisk工具
介绍:应用于硬盘空间小于等于2TB。(大于2TB使用:)
添加一个新硬盘
[root@www ~]# ls -l /dev/sd*
brw-rw---- 1 root disk 8, 0 Nov 25 21:06 /dev/sda
brw-rw---- 1 root disk 8, 1 Nov 25 21:06 /dev/sda1
brw-rw---- 1 root disk 8, 2 Nov 25 21:06 /dev/sda2
brw-rw---- 1 root disk 8, 16 Nov 25 21:06 /dev/sdb -----> 新添加的硬盘
开始分区:
[root@www ~]# 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 0x94829cba.
Command (m for help): p # 打印硬盘状态
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x94829cba
Device Boot Start End Blocks Id System # 分区表(新硬盘没有信息)
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 #1 (最多可以分4个主分区,从第一个开始分区)
First sector (2048-41943039, default 2048): # 扇区 默认 (从起始位置开始分区,默认就可以)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G # 2G分区 (无法计算扇区的数值,可以直接输入容量单位)
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): p # 检查一下分区表,看分区是否产生
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x94829cba
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 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): 2 # 分第二个分区
First sector (4196352-41943039, default 4196352): # 起始扇区默认 (系统会自动从上一个分区末尾开始计算)
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): # 结束扇区默认表示到整个扇区结束 (这样操作就是为了把整个扇区都用到,否则剩下的就不能用了)
Using default value 41943039
Partition 2 of type Extended and of size 18 GiB is set
Command (m for help): p # 检查分区表状态
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x94829cba
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 41943039 18873344 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 # 选逻辑分区
Adding logical partition 5
First sector (4198400-41943039, default 4198400): # 默认是从5开始 (1-4编号都是给主分区的,5开始是逻辑分区的)
Using default value 4198400
Last sector, +sectors or +size{K,M,G} (4198400-41943039, default 41943039): +1G # 设置分区1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): p # 查看分区表
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x94829cba
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 41943039 18873344 5 Extended
/dev/sdb5 4198400 6295551 1048576 83 Linux
Command (m for help): w # 保存分区
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
让内核刷新一下分区表 让内核知道有了新的分区出现了
[root@www ~]# partprobe /dev/sdb # kpartx -a /dev/sdb
[root@www ~]# fdisk -l /dev/sdb # 查看分区状态
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x94829cba
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 41943039 18873344 5 Extended
/dev/sdb5 4198400 6295551 1048576 83 Linux
设置文件系统
[root@www ~]# mkfs.xfs /dev/sdb1 # 设置xfs文件系统 指定第一个分区sdb1 (centos7开始默认是xfs文件系统)
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
挂载分区
[root@www ~]# mkdir /sdb1 # 创建任意名字的文件夹作为挂载点
[root@www ~]# mount /dev/sdb1 /sdb1 # 挂载分区
[root@www ~]# cd /sdb1
[root@www sdb1]# ls
[root@www sdb1]# touch haha
[root@www sdb1]# mkdir /s1 # 再次创建一个挂载点,进行挂载操作
[root@www sdb1]# mount /dev/sdb1 /s1
[root@www sdb1]# cd /s1
[root@www s1]# ls # ,查看结果表明是访问的同一个分区
haha
检查挂载情况
[root@www s1]# mount |grep /dev/sdb1
/dev/sdb1 on /sdb1 type xfs (rw,relatime,attr2,inode64,noquota)
/dev/sdb1 on /s1 type xfs (rw,relatime,attr2,inode64,noquota)
自动挂载
设置自动挂载
这样重启也不怕了
[root@www s1]# vi /etc/fstab # 设置自动挂载
UUID=0ced8e0d-af50-4d45-bc37-9bb0e13d43a7 xfs defaults 0 0
查看磁盘的uuid
[root@www s1]# blkid /dev/sdb1 # blkid 加设备的名字,可以查询到设备的uuid
/dev/sdb1: UUID="efb3e845-7a39-4c28-aaef-4c6b096f1d73" TYPE="xfs"
卸载分区:
[root@www s1]# umount /dev/sdb1 # 卸载分区
umount: /s1: target is busy. # 当前在s1属于挂载点,所以无法卸载分区
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@www s1]# cd .. # 退出挂载点
[root@www /]# umount /sdb1 # 卸载挂载点
[root@www /]# cd s1
[root@www s1]# ls
haha
[root@www s1]# cd
[root@www ~]# umount /s1 # 卸载另一个挂载点
[root@www ~]# cd /s1
[root@www s1]# ls # 卸载掉了,没有数据了
[root@www s1]#
Parted 分区工具
[root@www ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)