Fdisk 创建分区、进行格式化、最后挂载目录

磁盘进行分区

什么是分区

分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用。分区表是一个硬盘分区的索引,分区的信息都会写进分区表。

为什么要有多个分区

  • 分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用。分区表是一个硬盘分区的索引,分区的信息都会写进分区表。
  • 数据激增到极限不会引起系统挂起:将用户数据和系统数据分开,可以避免用户数据填满整个硬盘,引起的系挂起。
  • 增加磁盘空间使用效率:可以用不同的区块大小来格式化分区,如果有很多1K的文件,而硬盘分区区块大小为4K,那么每存储一个文件将会浪费3K空间。这时我们需要取这些文件大小的平均值进行区块大小的划分。

使用工具开始分区

有关详细介绍请看博客主页的 Linux 基础命令分栏中 “Linux 基础命令 – file

在这里,我们用到的相关命令:-l

# 列出所有的分区表
[root@fp-21 ~]# fdisk -l

Disk /dev/sda: 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: 0x000d4f7a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

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 /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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: 2147 MB, 2147483648 bytes, 4194304 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

通过查看,现在有两块硬盘,需要分区的为:/dev/sdb

# 对某块硬盘进行分区
[root@fp-21 ~]# fdisk /dev/sdb
……
# 进入之后,先查看有什么命令
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition	# 删除一个分区
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types	# 列出一个分区类型
   m   print this menu	# 帮助信息
   n   add a new partition	# 创建新分区
   o   create a new empty DOS partition table
   p   print the partition table	# 列出分区表
   q   quit without saving changes	# 不保存退出
   s   create a new empty Sun disklabel
   t   change a partition's system id	# 改变分区类型
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit	# 保存退出	
   x   extra functionality (experts only)

首先,我们先查看一下这块硬盘

# 列出分区表
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: 0xd4a8ecfc

   Device Boot      Start         End      Blocks   Id  System

可以看到,无任何的分区,接下来,开始分区

# 创建新分区
Command (m for help): n
Partition type:
# 在一块硬盘上,它的主分区最多只能有4个,或者3个主分区和1个扩展分区;在扩展分区上我们可以创建无数个逻辑分
   p   primary (0 primary, 0 extended, 4 free)	# 主分区
   e   extended	# 扩展分区
# 默认创建主分区,逻辑分区必须建立在扩展分区之上
Select (default p): p
# 分区编号:主分区1-4 ,逻辑分区5开始
Partition number (1-4, default 1): 1
# 创建分区的开始大小
First sector (2048-41943039, default 2048): 2048
# 创建分区的结束大小,可以家加单位
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 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: 0xd4a8ecfc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

已经有了一块5G大小的分区为:/dev/sdb1,保存退出

# 保存退出
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

同步磁盘,获取新的分区列表,查看分区表

[root@fp-21 ~]# partprobe  /dev/sdb
[root@fp-21 ~]# fdisk -l
……
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: 0xd4a8ecfc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

……

也可以通过查看目录判断是否创建分区成功

[root@fp-21 ~]# ll /dev/sd*
brw-rw----. 1 root disk 8,  0 Feb 28 20:27 /dev/sda
brw-rw----. 1 root disk 8,  1 Feb 28 20:27 /dev/sda1
brw-rw----. 1 root disk 8,  2 Feb 28 20:27 /dev/sda2
brw-rw----. 1 root disk 8, 16 Feb 28 20:28 /dev/sdb
brw-rw----. 1 root disk 8, 17 Feb 28 20:28 /dev/sdb1

创建文件系统

对分区进行格式化

centos 7 的文件系统:xfs,centos 6:的文件系统: ext4

[root@fp-21 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, 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

挂载虚拟目录

手动挂载

使用 mount 命令进行挂载

[root@fp-21 ~]# mkdir /opt/sdb1
[root@fp-21 ~]# mount /dev/sdb1 /opt/sdb1

查看是否挂载成功

[root@fp-21 ~]# df -h
# 文件系统                 容量  已用  可用 已用% 挂载点
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.9G   16G  12% /
devtmpfs                 979M     0  979M   0% /dev
tmpfs                    991M     0  991M   0% /dev/shm
tmpfs                    991M  9.6M  981M   1% /run
tmpfs                    991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                    199M     0  199M   0% /run/user/0
/dev/sdb1                5.0G   33M  5.0G   1% /opt/sdb1

这里需要注意一个问题

[root@fp-21 sdb1]# umount /opt/sdb1/
umount: /opt/sdb1: target is busy.
        (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))

提示说:在某些情况下,有关使用该设备的进程的有用信息由 lsof(8)或 fuser(1)找到,简而言之,就是:你要拆房子,可是你却站在房子内,肯定不能拆啊

[root@fp-21 sdb1]# cd
[root@fp-21 ~]# umount /opt/sdb1/
[root@fp-21 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.9G   16G  12% /
devtmpfs                 979M     0  979M   0% /dev
tmpfs                    991M     0  991M   0% /dev/shm
tmpfs                    991M  9.6M  981M   1% /run
tmpfs                    991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                    199M     0  199M   0% /run/user/0

退出挂载目录,在进行卸载挂载目录即可完成卸载

自动挂载

在配置文件 /etc/fstab 中进行添加

# 在文件末尾添加即可
[root@fp-21 ~]# vim /etc/fstab
# 要挂载的分区设备 挂载点 文件系统类型 挂载选项 是否备份 是否检测
/dev/sdb1 	    /opt/sdb1     xfs  defaults    0      0

使用 mount 命令进行挂载

# 自动挂载将/etc/fstab 的所有内容重新加载
[root@fp-21 ~]# mount -a
[root@fp-21 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.9G   16G  12% /
devtmpfs                 979M     0  979M   0% /dev
tmpfs                    991M     0  991M   0% /dev/shm
tmpfs                    991M  9.6M  981M   1% /run
tmpfs                    991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                    199M     0  199M   0% /run/user/0
/dev/sdb1                5.0G   33M  5.0G   1% /opt/sdb1

总结

只有注入思想的博客才是好的博客

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值