• 第十节


    添加硬盘设备

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

    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)
    

    接下来创建一个用于检查quota磁盘容量配额效果的用户tom,并针对/boot目录增加其他人的写权限,保证用户能够正常写入数据:

    [root@linuxprobe ~]# useradd tom
    [root@linuxprobe ~]# passwd tom
    [root@linuxprobe ~]# chmod -R o+w /boot
    

    xfs_quota命令用于管理设备的磁盘容量配额,语法格式为“xfs_quota [参数] 配额 文件系统”。

    这是一个专门针对XFS文件系统来管理quota磁盘容量配额服务而设计的命令。其中,-c参数用于以参数的形式设置要执行的命令;-x参数是专家模式,让运维人员能够对quota服务进行更多复杂的配置。接下来使用xfs_quota命令来设置用户tom对/boot目录的quota磁盘容量配额。具体的限额控制包括:硬盘使用量的软限制和硬限制分别为3MB和6MB;创建文件数量的软限制和硬限制分别为3个和6个。 

    [root@linuxprobe ~]# xfs_quota -x -c 'limit bsoft=3m bhard=6m isoft=3 ihard=6 tom' /boot
    [root@linuxprobe ~]# xfs_quota -x -c report /boot
    User quota on /boot (/dev/sda1)
                                   Blocks                     
    User ID          Used       Soft       Hard    Warn/Grace     
    ---------- -------------------------------------------------- 
    root           114964          0          0     00 [--------]
    tom                 0       3072       6144     00 [--------]
    

    在Linux系统中每个文件都会使用一个独立的inode信息块来保存属性信息,一个文件对应一个inode信息块,所以isoft和ihard就是通过限制系统最大使用的inode个数来限制了文件数量。bsoft和bhard则是代表文件所占用的block大小,也就是文件占用的最大容量的总统计。  

    soft是软限制,超过该限制后也只是将操作记录写到日志中,不对用户行为进行限制。而hard是硬限制,一旦超过系统就会马上禁止,用户再也不能创建或新占任何的硬盘容量。

    [root@linuxprobe ~]# su - tom
    [tom@linuxprobe ~]$ cd /boot
    [tom@linuxprobe boot]$ dd if=/dev/zero of=/boot/tom bs=5M count=1  //创建5M的文件时,成功
    1+0 records in
    1+0 records out
    5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.00298178 s, 1.8 GB/s
    [tom@linuxprobe boot]$ dd if=/dev/zero of=/boot/tom1 bs=8M count=1    //创建8M的文件时,失败(已超出配额)
    dd: error writing '/boot/tom': Disk quota exceeded
    1+0 records in
    0+0 records out
    4194304 bytes (4.2 MB, 4.0 MiB) copied, 0.00398607 s, 1.1 GB/s
    

    edquota命令用于管理系统的磁盘配额,英文全称为“edit quota”,语法格式为“edquota [参数] 用户名”。

    在为用户设置了quota磁盘容量配额限制后,可以使用edquota命令按需修改限额的数值。其中,-u参数表示要针对哪个用户进行设置;-g参数表示要针对哪个用户组进行设置,如表所示。

     edquota命令中可用的参数以及作用

    参数 作用
    -u 对某个用户进行设置
    -g 对某个用户组进行设置
    -p 复制原有的规则到新的用户/组
    -t 限制宽限期限
    [tom@linuxprobe ~]$ exit
    [root@linuxprobe ~]# edquota -u tom  //vim编辑后保存退出(wq)
    Disk quotas for user tom (uid 1001):
      Filesystem                   blocks       soft       hard     inodes     soft     hard
      /dev/sda1                      4096       3072      10240          1        3        6
    [root@linuxprobe ~]# su - tom
    [tom@linuxprobe ~]$ cd /boot
    [tom@linuxprobe boot]$ dd if=/dev/zero of=/boot/tom1 bs=8M count=1   //创建8M文件,成功
    1+0 records in
    1+0 records out
    8388608 bytes (8.4 MB, 8.0 MiB) copied, 0.0185476 s, 452 MB/s
    

      

  • 相关阅读:
    强烈推荐好用的文本编辑器Notepad++
    MSP430学习笔记9PS2键盘解码
    MSP430学习笔记8ST7920 12864液晶显示并行接口
    MSP430学习笔记74*4键盘的对应数码管显示
    MSP430学习笔记6动态数码管的显示
    MSP430寄存器中文注释
    MSP430学习小结3MSP430基本时钟模块
    ST7920 12864液晶图片取模,显示你想显示的图片。
    MSP430学习小结2程序主体结构安排及低功耗转载
    Windows Phone开发之路(4) XAML基础(上)
  • 原文地址:https://www.cnblogs.com/wushuai2018/p/15865789.html
Copyright © 2020-2023  润新知