Linux添加硬盘设备及交换分区

技术分享-添加硬盘设备及交换分区
2020.02.09

一、添加硬盘设备

1、插入新硬盘

lsblk 命令列出所有可用块设备,可以找到我们刚才插入的 20G 新硬盘 sdc:

[root@centos-7-master ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
├─sdb1            8:17   0    2G  0 part /mnt/heleiNewFS
└─sdb2            8:18   0    5G  0 part [SWAP]
sdc               8:32   0   20G  0 disk 
2、分区

使用 fdisk 命令对新硬盘进行分区:

fdisk 命令的主要参数及作用如下:
参数		作用
m		查看全部可用的参数
n		添加新的分区
d		删除某个分区信息
l		列出所有可用的分区类型
t		改变某个分区的类型
p		查看分区信息
w		保存并退出
q		不保存直接退出
[root@centos-7-master ~]# fdisk /dev/sdc
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 0xbd9ec732.

Command (m for help): p		#查看分区信息,因为是新硬盘,所以无分区信息

Disk /dev/sdc: 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: 0xbd9ec732

   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
First sector (2048-41943039, default 2048):		#定义起始的扇区位置,按回车默认即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G	#定义整个分区的大小
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): p		#再次查看分区信息,2G的分区已创建

Disk /dev/sdc: 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: 0xbd9ec732

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

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

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

执行完以上步骤之后,Linux系统会自动将这个硬盘主分区抽象成 /dev/sdc1 设备文件,可以使用 file 命令查看该文件的属性:

[root@centos-7-master ~]# file /dev/sdc1
/dev/sdc1: block special
#如果系统没有自动将分区信息同步给Linux内核,则输入 partprobe 命令手动同步(推荐连续两次执行该命令):
[root@centos-7-master ~]# partprobe
3、格式化

使用 mkfs 命令对新分区进行格式化:

[root@centos-7-master ~]# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1              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
4、挂载

挂载刚创建的分区:

[root@centos-7-master ~]# mkdir /mnt/helei-test
[root@centos-7-master ~]# mount /dev/sdc1 /mnt/helei-test/
[root@centos-7-master ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   13M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  4.2G   13G  25% /
/dev/sdb1               xfs       2.0G   76M  2.0G   4% /mnt/heleiNewFS
/dev/sda1               xfs      1014M  240M  775M  24% /boot
tmpfs                   tmpfs     378M   24K  378M   1% /run/user/1000
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdc1               xfs       2.0G   33M  2.0G   2% /mnt/helei-test
5、验证

现在向新分区里写入数据,验证以上添加硬盘动作是否成功:

[root@centos-7-master ~]# cp -rf /tmp/* /mnt/helei-test/
[root@centos-7-master ~]# ll /mnt/helei-test/
total 48
drwx------. 2 root root    21 Feb  9 13:01 tmux-0
......省略部分内容......
drwx------. 2 root root     6 Feb  9 13:01 tracker-extract-files.0
[root@centos-7-master ~]# du -sh /mnt/helei-test/	#du 命令查看文件夹占用多大的硬盘空间
56K	/mnt/helei-test/
6、重启自动挂载

新硬盘创建成功之后,将挂载信息写入 /etc/fstab 文件中,系统重启之后可自动挂载:

/etc/fstab文件,用于挂载信息的指定填写格式中,各字段所表示的意义如下:
字段			意义
设备文件	一般为设备的路径+设备名称,也可以写唯一识别码(UUID,Universally Unique Identifier)
挂载目录	指定要挂载到的目录,需在挂载前创建好
格式类型	指定文件系统的格式,比如 Ext3、Ext4、XFS、SWAP、iso9660(此为光盘设备)等
权限选项	若设置为 defaults,则默认权限为:rw, suid, dev, exec, auto, nouser, async 
自检     若为 1 则开机后进行磁盘自检,为 0 则不自检
优先级    若“自检”字段为 1,则可对多块硬盘进行自检优先级设置
#这里要改一下,blkid命令查看磁盘UUID,使用UUDI挂载。后面的开机自挂载也都改成UUID。
[root@centos-7-master ~]# vim /etc/fstab 
# /etc/fstab
# Created by anaconda on Mon Jan 13 14:02:49 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/centos-root /                       xfs     defaults        0 0
UUID=91279967-40de-45d0-876f-33c582a30122 /boot xfs     defaults,uquota 0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1               /mnt/heleiNewFS         xfs     defaults        0 0 
/dev/sdb2               swap                    swap    defaults        0 0
/dev/sdc1               /mnt/helei-test         xfs     defaults        0 0

二、添加交换分区

1、分区

使用 fdisk 命令创建分区:

[root@centos-7-master ~]# fdisk /dev/sdc
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 (4196352-41943039, default 4196352):		#定义起始的扇区位置,默认即可
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +5G		#设置分区大小
Partition 2 of type Linux and of size 5 GiB is set

Command (m for help): p		#再次查看分区信息,5G的新分区已创建

Disk /dev/sdc: 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: 0xbd9ec732

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     4196351     2097152   83  Linux
/dev/sdc2         4196352    14682111     5242880   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.
2、格式化

使用SWAP分区专用的格式化命令 mkswap 对新建的主分区进行格式化操作:

[root@centos-7-master ~]# partprobe		#重读分区表,手动向Linux内核同步分区信息
[root@centos-7-master ~]# mkswap /dev/sdc2
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=8c2cbb26-1ebf-42f3-bfa6-05124039560f
3、挂载

使用 swapon 命令把准备好的SWAP分区设备正式挂载到系统中。使用 free -m 命令查看交换分区的大小变化:

[root@centos-7-master ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3770         715        2364          20         690        2812
Swap:          7167           0        7167
[root@centos-7-master ~]# swapon /dev/sdc2 
[root@centos-7-master ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3770         719        2360          20         690        2808
Swap:         12287           0       12287
4、重启自动挂载

将交换分区 /dev/sdc2 信息写入 /etc/fstab 文件中,系统重启之后自动生效:

[root@centos-7-master ~]# vim /etc/fstab 

# /etc/fstab
# Created by anaconda on Mon Jan 13 14:02:49 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/centos-root /                       xfs     defaults        0 0
UUID=91279967-40de-45d0-876f-33c582a30122 /boot xfs     defaults,uquota        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1               /mnt/heleiNewFS         xfs     defaults        0 0 
/dev/sdb2               swap                    swap    defaults        0 0
/dev/sdc1               /mnt/helei-test         xfs     defaults        0 0
/dev/sdc2               swap                    swap    defaults        0 0

你可能感兴趣的:(计算机人生,linux,运维,服务器,centos,debian)