其他分享
首页 > 其他分享> > 第十节

第十节

作者:互联网

添加硬盘设备

向机器中新增一块硬盘后,需要分区、格式化、挂载操作。其中分区也可以省略,表示不对硬盘进行分区。

1.添加一块硬盘

建议在关机状态下添加/更换硬盘,否则可能会造成盘符漂移。按照udev设备管理器,通常按照sda~z(SATA/SAS)的顺序对识别到的硬盘进行排序。

添加硬盘后,可以使用fdisk -l命令来查看新添加的硬盘是否被识别到。

[root@superwu ~]# df -h   
Filesystem Size Used Avail Use% Mounted on
devtmpfs 969M 0 969M 0% /dev
tmpfs 984M 0 984M 0% /dev/shm
tmpfs 984M 9.6M 974M 1% /run
tmpfs 984M 0 984M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 17G 3.9G 14G 23% /
/dev/sr0 6.7G 6.7G 0 100% /media/cdrom
/dev/sda1 1014M 152M 863M 15% /boot
tmpfs 197M 16K 197M 1% /run/user/42
tmpfs 197M 3.4M 194M 2% /run/user/0
[root@superwu ~]# fdisk -l      //可以查看新增的硬盘是否被识别到。
Disk /dev/sda: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x12b989df

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 41943039 39843840 19G 8e Linux LVM


Disk /dev/sdb: 20 GiB, 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/rhel-root: 17 GiB, 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/rhel-swap: 2 GiB, 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

2.分区(fdisk)

对新增硬盘进行分区

[root@linuxprobe ~]# fdisk /dev/sdb 

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0x88b2c2b0.

Command (m for help): p   //查看硬盘内已有的分区信息,其中包括了硬盘的容量大小、扇区个数等信息
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x88b2c2b0
Command (m for help): n  //参数n添加新的分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  //参数p创建主分区,输入参数e来创建扩展分区。
Partition number (1-4, default 1): 1  //输入主分区的编号,主分区的编号范围是1~4.
First sector (2048-41943039, default 2048): 此处敲击回车即可  //定义起始的扇区位置,敲击回车键保留默认设置即可,系统会自动计算出最靠前的空闲扇区的位置。
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +2G  //创建出一个容量为2GB的硬盘分区.
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): w   //保存分区信息并退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

在上述步骤执行完毕之后,Linux系统会自动把这个硬盘主分区抽象成/dev/sdb1设备文件。可以使用file命令查看该文件的属性,有些时候系统并没有自动把分区信息同步给Linux内核,可以输入partprobe命令手动将分区信息同步到内核,推荐连续两次执行该命令,效果会更好。如果使用这个命令都无法解决问题,那么就重启计算机吧,这个“杀手锏”百试百灵,一定会有用的。

[root@linuxprobe ]# file /dev/sdb1
/dev/sdb1: cannot open `/dev/sdb1' (No such file or directory)
[root@linuxprobe ]# partprobe
[root@linuxprobe ]# partprobe
[root@linuxprobe ]# file /dev/sdb1
/dev/sdb1: block special

3.格式化硬盘设备(mkfs)

如果硬件存储设备没有进行格式化,则Linux系统无法得知怎么在其上写入数据。因此,在对存储设备进行分区后还需要进行格式化操作。

[root@linuxprobe ~]# mkfs.xfs /dev/sdb1   //将/dev/sdb1格式化为xfs文件系统。
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
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.挂载(mount)

首先是创建一个用于挂载设备的挂载点目录;其次使用mount命令将存储设备与挂载点进行关联;然后使用df -h命令来查看挂载状态和硬盘使用量信息;最后将挂载信息写入/etc/fstab配置文件。

[root@linuxprobe ~]# mkdir /newFS
[root@linuxprobe ~]# mount /dev/sdb1 /newFS
[root@linuxprobe ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               969M     0  969M   0% /dev
tmpfs                  984M     0  984M   0% /dev/shm
tmpfs                  984M  9.6M  974M   1% /run
tmpfs                  984M     0  984M   0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  3.9G   14G  23% /
/dev/sr0               6.7G  6.7G     0 100% /media/cdrom
/dev/sda1             1014M  152M  863M  15% /boot
tmpfs                  197M   16K  197M   1% /run/user/42
tmpfs                  197M  3.5M  194M   2% /run/user/0
/dev/sdb1              2.0G   47M  2.0G   3% /newFS
[root@superwu ~]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Tue Jan 11 03:26:57 2022
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=d7f53471-c95f-44f2-aafe-f86bd5ecebd7 /boot xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0
/dev/sdb1 /newFS xfs defaults 0 0

 du命令用查看分区或目录所占用的磁盘容量大小,英文全称为“disk usage”,语法格式为“du -sh目录名称”。

[root@linuxprobe ~]# du -sh /newFS
39M /newFS/

 添加交换分区

交换(SWAP)分区是一种通过在硬盘中预先划分一定的空间,然后把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。

交换分区的划分建议:在生产环境中,交换分区的大小一般为真实物理内存的1.5~2倍。

例如从/dev/sdb中取出一个大小为5GB的主分区作为交换分区资源:

分区:

[root@linuxprobe ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
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 (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 敲击回车即可
First sector (4196352-41943039, default 4196352): 敲击回车即可
Last sector, +sectors or +size{K,M,G,T,P} (4196352-41943039, default 41943039): +5G

Created a new partition 2 of type 'Linux' and of size 5 GiB.

然后尝试修改硬盘的标识码,这里将其改成82(Linux swap)以方便以后知道它的作用:

Command (m for help): t  //修改分区类型
Partition number (1,2, default 2): 2   //指定修改的分区编号
Hex code (type L to list all codes): L  //查看有哪些分区类型

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): 82  //指定分区编号为82(Linux swap)

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p
Disk /dev/sdb: 20 GiB, 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
Disklabel type: dos
Disk identifier: 0x7327c2a7

Device     Boot   Start      End  Sectors Size Id Type
/dev/sdb1          2048  4196351  4194304   2G 83 Linux
/dev/sdb2       4196352 14682111 10485760   5G 82 Linux swap / Solaris

Command (m for help): w  //保存分区信息并退出
The partition table has been altered.
Syncing disks.

格式化:

mkswap命令用于对新设备进行交换分区格式化,英文全称为“make swap”,语法格式为“mkswap设备名称”。

[root@superwu ~]# mkswap /dev/sdb2   //对交换分区格式化需要使用专用命令mkswap。
Setting up swapspace version 1, size = 5 GiB (5368705024 bytes)
no label, UUID=073d9fa9-4652-4b7c-aca3-4ed62032a142

挂载:

swapon命令用于激活新的交换分区设备,英文全称为“swap on”,语法格式为“swapon设备名称”。  

[root@superwu ~]# free -m   //挂载前swap容量
              total        used        free      shared  buff/cache   available
Mem:           1966        1289          84          16         591         493
Swap:          2047           0        2047
[root@superwu ~]# swapon /dev/sdb2   //挂载
[root@superwu ~]# free -m    //挂载后swap容量
              total        used        free      shared  buff/cache   available
Mem:           1966        1294          79          16         591         488
Swap:          7167           0        7167

将挂载信息写入/etc/fstab文件。

[root@linuxprobe ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Jul 21 05:03:40 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root                        /               xfs        defaults    1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b    /boot           xfs        defaults    1 2
/dev/mapper/rhel-swap                        swap            swap       defaults    0 0
/dev/cdrom                                   /media/cdrom    iso9660    defaults    0 0 
/dev/sdb1                                    /newFS          xfs        defaults    0 0 
/dev/sdb2                                    swap            swap       defaults    0 0 

磁盘容量配额

使用磁盘容量配额服务来限制某位用户或某个用户组针对特定文件夹可以使用的最大硬盘空间或最大文件个数,一旦达到这个最大值就不再允许继续使用。

可以使用quota技术进行磁盘容量配额管理,从而限制用户的硬盘可用容量或所能创建的最大文件个数。quota技术还有软限制和硬限制的功能。

软限制:当达到软限制时会提示用户,但仍允许用户在限定的额度内继续使用。

硬限制:当达到硬限制时会提示用户,且强制终止用户的操作。

RHEL 8系统中已经安装了quota磁盘容量配额服务程序包,但存储设备却默认没有开启对quota技术的支持,此时需要手动编辑配置文件并重启一次系统,让系统中的启动目录(/boot)能够支持quota磁盘配额技术。  

[root@linuxprobe ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Jul 21 05:03:40 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root                        /             xfs        defaults         1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b    /boot         xfs        defaults,uquota  1 2
/dev/mapper/rhel-swap                        swap          swap       defaults         0 0
/dev/cdrom                                   /media/cdrom  iso9660    defaults         0 0 
/dev/sdb1                                    /newFS        xfs        defaults         0 0 
/dev/sdb2                                    swap          swap       defaults         0 0 
[root@linuxprobe ~]# reboot

在重启系统后使用mount命令查看,即可发现/boot目录已经支持quota磁盘配额技术了:

[root@linuxprobe ~]# mount | grep boot
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,usrquota)

  

  

  

 

标签:分区,bytes,dev,第十节,512,root,swap
来源: https://www.cnblogs.com/wushuai2018/p/15865789.html