Linux 添加硬盘设备

fdisk命令用于管理磁盘分区,格式为:“fdisk [磁盘名称]”。

管理Linux系统中的硬盘设备最常用的方法就当属是用fdisk命令了,这条命令提供了添加、删除、转换分区等等功能于一身的“一站式分区服务”,不过这条命令的参数是交互式的,而不是像咱们以前直接写到命令后面的参数一样,因此在管理硬盘设备的时候特别方便,可以根据需求动态的调整。

参数 作用
m 查看全部可用的参数
n 添加新的分区
d 删除某个分区信息
l 列出所有可用的分区类型
t 改变某个分区的类型
p 查看分区表信息
w 保存并退出
q 不保存直接退出

一、添加磁盘分区

查看当前磁盘及分区情况:

[root@rhel7 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root  8.5G  3.1G  5.5G  36% /
devtmpfs               986M     0  986M   0% /dev
tmpfs                 1001M   84K 1001M   1% /dev/shm
tmpfs                 1001M  8.7M  992M   1% /run
tmpfs                 1001M     0 1001M   0% /sys/fs/cgroup
/dev/sda1              497M  140M  358M  29% /boot
tmpfs                  201M   16K  201M   1% /run/user/42
tmpfs                  201M     0  201M   0% /run/user/0
[root@rhel7 ~]# 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: 0x0006c4ff

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20971519     9972736   8e  Linux LVM

Disk /dev/mapper/rhel-root: 9093 MB, 9093251072 bytes, 17760256 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/rhel-swap: 1073 MB, 1073741824 bytes, 2097152 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/sdb: 5368 MB, 5368709120 bytes, 10485760 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@rhel7 ~]# 

从上面可以看出,第二块磁盘/dev/sdb共5368MB,并未使用。现在我们使用 fdisk 命令来将/dev/sdb划分出500MB,挂载到/lxjtest目录;然后再划分出200MB当作swap分区使用。

第1步:首先使用fdisk命令尝试管理/dev/sdb硬盘设备,看到提示信息后输入参数p来查看硬盘设备内已有的分区信息,这其中包括了硬盘的容量大小,扇区个数等等信息:

[root@rhel7 ~]# 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 0x148e18c9.

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x148e18c9

   Device Boot      Start         End      Blocks   Id  System

第2步:输入参数n来尝试新建分区信息,系统会要求选择继续输入参数p来创建主分区,或者输入参数e来创建扩展分区,因此输入参数p来创建一个主分区:

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p

第3步:确认创建一个主分区后,系统要求您先输入分区的编号,既然已经知道主分区的编号范围是1-4,因此默认输入1就可以了,接下来系统会提示定义下起始的扇区,这一项默认不需要改动,系统会自动计算出最靠前空闲的扇区位置,咱们敲击一下回车就可以,最后系统会要求定义分区的结束扇区位置,这其实就是想要去定义下整个分区的大小是多少,但其实不用去算扇区的个数,只需要输入+500M即可创建出一个容量为500MB的硬盘分区。

Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +500M
Partition 1 of type Linux and of size 500 MiB is set

第4步:再次使用参数p来查看下硬盘设备中的分区信息,果然就能看到一个名称为/dev/sdb1,起始扇区为2048,结束扇区是1026047的分区啦,这时候千万不要直接关闭窗口,而应该敲击参数w后回车,这样分区信息才是真正的写入成功啦。

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x148e18c9

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@rhel7 ~]# 

第5步:当做完上面所说的所有步骤后,Linux系统会自动把这个硬盘分区抽象成/dev/sdb1这个设备文件,可以用file命令来看到这个文件的属性。有些时候系统并没有自动的把分区信息同步给Linux内核,也可以输入partprobe命令来手动的同步信息到内核,而且一般推荐敲击两次会效果更佳,但一旦遇到连这个命令都不能解决的时候,那么就重启计算机吧。

[root@rhel7 ~]# file /dev/sdb1
/dev/sdb1: block special

使用mkfs格式化/dev/sdb1分区。

[root@rhel7 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@rhel7 ~]# 

接下来就可以挂载使用了。

[root@rhel7 ~]# mkdir /lxjtest
[root@rhel7 ~]# mount /dev/sdb1 /lxjtest/
[root@rhel7 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root  8.5G  3.1G  5.5G  36% /
devtmpfs               986M     0  986M   0% /dev
tmpfs                 1001M   84K 1001M   1% /dev/shm
tmpfs                 1001M  8.7M  992M   1% /run
tmpfs                 1001M     0 1001M   0% /sys/fs/cgroup
/dev/sda1              497M  140M  358M  29% /boot
tmpfs                  201M   16K  201M   1% /run/user/42
tmpfs                  201M     0  201M   0% /run/user/0
/dev/sdb1              497M   26M  472M   6% /lxjtest

使用mount命令挂载的设备文件会在下一次重启的时候失效,因此如果您还想让这个设备文件的挂载永久的有效下去,需要把挂载的信息项目写入到配置文件才可以:

[root@rhel7 ~]# vi /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Wed Jul  5 03:56:47 2017
#
# 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        0 0
UUID=4f396f9a-467e-4472-a654-836f2dc7d6ca /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1       /lxjtest        xfs     defaults        0 0     
二、添加交换分区

SWAP交换分区是一种类似于Windows系统虚拟内存的功能,通过把一部分硬盘空间虚拟成内存来使用,从而解决内存容量不足的情况。但由于SWAP毕竟是用硬盘资源虚拟的,速度上比真实物理内存要慢很多,所以一般只有当真实物理内存耗尽时才会调用SWAP交换分区,把内存中暂时不常用的数据临时存放到硬盘中,腾出内存空间让更活跃的程序服务来使用。

第1步:SWAP交换分区的创建过程非常类似于上一个小节所讲到的分区设备挂载使用的方法,首先第一步就是再进行对/dev/sdb存储设备分区操作,取出一个大小为200MB的主存储分区然后保存退出即可:

[root@rhel7 ~]# 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): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (1026048-10485759, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-10485759, default 10485759): +200M          
Partition 2 of type Linux and of size 200 MiB is set

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: 0x148e18c9

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     1435647      204800   83  Linux

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@rhel7 ~]# partprobe 执行该命令将分区信息同步到内核

第2步:把新建的存储分区使用SWAP交换分区专用的格式化mkswap命令进行格式化操作:

[root@rhel7 ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 204796 KiB
no label, UUID=4746286f-4680-4a05-878d-118fff5904ac

第3步:接下来使用swapon命令把准备好的SWAP交换分区设备正式的挂载到系统中,并可以使用free -m 命令来看到交换分区大小的变化(由1023M提升至了1223M):

[root@rhel7 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           2000         259        1398           8         342        1578
Swap:          1023           0        1023
[root@rhel7 ~]# swapon /dev/sdb2
[root@rhel7 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           2000         259        1398           8         342        1578
Swap:          1223           0        1223

第4步:为了能够让新的SWAP交换分区设备在重启后依然生效,需要按照下面的格式写入到配置文件中。

[root@rhel7 ~]# vi /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Wed Jul  5 03:56:47 2017
#
# 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        0 0
UUID=4f396f9a-467e-4472-a654-836f2dc7d6ca /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
/dev/sdb1       /lxjtest        xfs     defaults        0 0
/dev/sdb2       swap    swap    defaults        0 0
原文地址:https://www.cnblogs.com/rusking/p/7156422.html