linux系统中对硬盘进行磁盘分区

linux系统中的硬盘如何利用?

硬盘磁盘分区(可选)→格式化→挂载→使用

硬盘磁盘分区的目的是为了将大的硬盘划分为大小不同的存储单元。

 

1、在虚拟中添加一块硬盘

 

[root@PC1linuxprobe dev]# pwd
/dev
[root@PC1linuxprobe dev]# find sd*
sda
sda1
sda2
sdb

 

2、使用fdisk命令对硬盘/dev/sdb进行磁盘分区

fdisk命令采用交互式命令对硬盘进行磁盘分区,fdisk命令中的常用参数如下表:

 

 

[root@PC1linuxprobe dev]# fdisk /dev/sdb   ## fdisk + 硬盘
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 0xe287b3a9.

Command (m for help): p  ## 此处输入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: 0xe287b3a9

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n   ## 输入n添加磁盘分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p  ## 输入p表示创建主分区
Partition number (1-4, default 1): 1   ## 输入主分区编号
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: 0xe287b3a9

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

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

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

 

 3、在/dev目录中查看磁盘分区

[root@PC1linuxprobe dev]# pwd
/dev
[root@PC1linuxprobe dev]# find sd*
sda
sda1
sda2
sdb
sdb1

 

4、对磁盘分区进行格式化(指定文件系统)

[root@PC1linuxprobe dev]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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

 

5、挂载

[root@PC1linuxprobe dev]# mkdir /devide
[root@PC1linuxprobe dev]# mount /dev/sdb1 /devide/
[root@PC1linuxprobe dev]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  2.9G   15G  17% /
devtmpfs               985M     0  985M   0% /dev
tmpfs                  994M  140K  994M   1% /dev/shm
tmpfs                  994M  8.8M  986M   1% /run
tmpfs                  994M     0  994M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot
/dev/sr0               3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
/dev/sdb1              2.0G   33M  2.0G   2% /devide

 

6、设为开机自动启动

[root@PC1linuxprobe dev]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Nov  5 15:23:01 2020
#
# 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/rhel-root   /                       xfs     defaults        1 1
UUID=0ba20ae9-dd51-459f-ac48-7f7e81385eb8 /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@PC1linuxprobe dev]# echo -e "/dev/sdb1\t/devide\txfs\tdefaults\t0\t0" >> /etc/fstab 
[root@PC1linuxprobe dev]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Nov  5 15:23:01 2020
#
# 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/rhel-root   /                       xfs     defaults        1 1
UUID=0ba20ae9-dd51-459f-ac48-7f7e81385eb8 /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1    /devide    xfs    defaults    0    0

 

总结,磁盘分区应用:

  • 使用fdisk采用交互式命令进行磁盘分区,示例:fdisk /dev/sdb
  • 对磁盘分区格式化,制动文件系统类型,示例:mkfs.xfs /dev/sdb1
  • 挂载使用,示例:mount /dev/sdb1 /mountpoint
  • 设为开机自动挂载
posted @ 2020-11-09 22:08  小鲨鱼2018  阅读(1371)  评论(0编辑  收藏  举报