1. 程式人生 > >Linux磁盤管理及文件系統使用

Linux磁盤管理及文件系統使用

tin can sun btrfs display lis 配置 pes 默認

  • 磁盤管理基礎知識
  • 分區
    • fdisk
    • parted
    • gdisk
  • 格式化
  • 掛載

    • Linux中磁盤命名:
      IDE類型:/dev/hd[a-z]
      SCSI類型:/dev/sd[a-z]
    • 一塊磁盤經過哪些步驟才能夠使用:分區(非必需)-->格式化-->掛載;磁盤分區的類型有MBR(master boot record)和GPT(GUID Partition Table),主要區別在MBR只能分4個主分區超過需要分為邏輯分區,磁盤容量最大不超過2T,超過部分無法識別。

    • MBR 也就是主引導記錄,位於硬盤的 0 磁道、0 柱面、1 扇區中,主要記錄了啟動引導程序和磁盤的分區表:
      技術分享圖片
      由於分區表大小固定:最多只能分4個,超過需要使用擴展分區來劃分邏輯分區,即使主分區再分配一個,邏輯分區命名從/dev/sda5開始。
    • 分區
    • fdisk:不適用於GPT分區。
      1.查看磁盤分區信息:
      用法:fdisk -l [-u] [device...]:列出指定磁盤設備上的分區情況;

      [root@xt ~]# fdisk -l
      Disk /dev/sda: 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: 0x00036ca6
      Device Boot      Start         End      Blocks   Id  System
      /dev/sda1            2048    20973567    10485760   83  Linux
      /dev/sda2        20973568    25167871     2097152   82  Linux swap / Solaris
      /dev/sda3        25167872    25692159      262144   83  Linux

      2.分區管理,fdisk提供了交互接口來管理分區
      用法:fdisk [options] <disk>

      [root@xt ~]# fdisk /dev/sda 
      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): m
      Command action
      a   toggle a bootable flag
      b   edit bsd disklabel
      c   toggle the dos compatibility flag
      d   delete a partition:刪除分區
      g   create a new empty GPT partition table
      G   create an IRIX (SGI) partition table
      l   list known partition types
      m   print this menu
      n   add a new partition:創建新分區
      o   create a new empty DOS partition table
      p   print the partition table:列出現有分區
      q   quit without saving changes :不保存退出
      s   create a new empty Sun disklabel
      t   change a partition‘s system id:修改分區類型
      u   change display/entry units
      v   verify the partition table
      w   write table to disk and exit:保存退出
      x   extra functionality (experts only)

      3.在已經分區並且已經掛載其中某個分區的磁盤設備上創建的新分區,內核可能無法直接識別,通知內核重讀分區表
      查看:cat /proc/partitions
      CentOS 5:partprobe [device]
      CentOS 6,7:partx, kpartx
      partx -a [device]
      kpartx -af [device]

    • parted 、gdisk能夠使用GPT模式
      1.查看磁盤信息
      parted [options] [device]
      [root@xt ~]# parted -l 
      Model: VMware, VMware Virtual S (scsi)
      Disk /dev/sda: 21.5GB
      Sector size (logical/physical): 512B/512B
      Partition Table: msdos
      Disk Flags: 
      Number  Start   End     Size    Type     File system     Flags
      1      1049kB  10.7GB  10.7GB  primary  xfs
      2      10.7GB  12.9GB  2147MB  primary  linux-swap(v1)
      3      12.9GB  13.2GB  268MB   primary  btrfs
      Error: /dev/sdb: unrecognised disk label
      Model: VMware, VMware Virtual S (scsi)                                    
      Disk /dev/sdb: 3221GB
      Sector size (logical/physical): 512B/512B
      Partition Table: unknown
      Disk Flags: 

      2.分區管理
      用法: parted device

      [root@xt ~]# parted /dev/sdb
      GNU Parted 3.1
      Using /dev/sdb
      Welcome to GNU Parted! Type ‘help‘ to view a list of commands.
      (parted) help         #查看幫助                                                    
      align-check TYPE N                        check partition N for TYPE(min|opt) alignment
      help [COMMAND]                           print general help, or help on COMMAND
      mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
      mkpart PART-TYPE [FS-TYPE] START END     make a partition
      name NUMBER NAME                         name partition NUMBER as NAME
      print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found
          partitions, or a particular partition
      quit                                     exit program
      rescue START END                         rescue a lost partition near START and END
      rm NUMBER                                delete partition NUMBER
      select DEVICE                            choose the device to edit
      disk_set FLAG STATE                      change the FLAG on selected device
      disk_toggle [FLAG]                       toggle the state of FLAG on selected device
      set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
      toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
      unit UNIT                                set the default unit to UNIT
      version                                  display the version number and copyright information of GNU Parted
      (parted) print                                                            
      Error: /dev/sdb: unrecognised disk label
      Model: VMware, VMware Virtual S (scsi)                                    
      Disk /dev/sdb: 3221GB
      Sector size (logical/physical): 512B/512B
      Partition Table: unknown
      Disk Flags: 
      (parted) mklabel gpt  #修改磁盤結構類型:"aix", "amiga", "bsd", "dvh", "gpt", "loop", "mac", "msdos", "pc98", or "sun"
      Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want
      to continue?
      Yes/No? yes                                                               
      (parted) mkpart    #交互式分區
      Partition name?  []? primary #分區名稱: primary", "logical", or "extended"                            File system type?  [ext2]? ext4   #文件系統類型                                        
      Start? 0
      End? 1000    #分配分區大小                                                             
      Warning: The resulting partition is not properly aligned for best performance.
      Ignore/Cancel? Ignore                                                     
      (parted) print                                                            
      Model: VMware, VMware Virtual S (scsi)
      Disk /dev/sdb: 3221GB
      Sector size (logical/physical): 512B/512B
      Partition Table: gpt
      Disk Flags: 
      Number  Start   End     Size    File system  Name     Flags
      1      17.4kB  1000MB  1000MB               primary
      (parted) rm 1    #刪除分區1                                                         
      (parted) p                                                                
      Model: VMware, VMware Virtual S (scsi)
      Disk /dev/sdb: 3221GB
      Sector size (logical/physical): 512B/512B
      Partition Table: gpt
      Disk Flags: 
      Number  Start  End  Size  File system  Name  Flags
      ==============================================================
      (parted) mkpart primary 0 2000G    # 方法2分區 mkpart part-type [fs-type] start end
      Warning: The resulting partition is not properly aligned for best performance.
      Ignore/Cancel? i                                                          
      (parted) p                                                                
      Model: VMware, VMware Virtual S (scsi)
      Disk /dev/sdb: 3221GB
      Sector size (logical/physical): 512B/512B
      Partition Table: gpt
      Disk Flags: 
      Number  Start   End     Size    File system  Name     Flags
      1      17.4kB  2000GB  2000GB               primary
      (parted) q                                                                
      Information: You may need to update /etc/fstab.
    • centos7有更簡便工具:gdisk 與fdisk類似

      [root@xt ~]# gdisk /dev/sdb
      GPT fdisk (gdisk) version 0.8.10
      Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
      Found valid GPT with protective MBR; using GPT. #默認是GPT模式
      Command (? for help): ?#查看幫助
      b   back up GPT data to a file
      c   change a partition‘s name
      d   delete a partition
      i   show detailed information on a partition
      l   list known partition types
      n   add a new partition
      o   create a new empty GUID partition table (GPT)
      p   print the partition table
      q   quit without saving changes
      r   recovery and transformation options (experts only)
      s   sort partitions
      t   change a partition‘s type code
      v   verify disk
      w   write table to disk and exit
      x   extra functionality (experts only)
      ?   print this menu
      ================================================
      Command (? for help): n 添加新分區
      Partition number (1-128, default 1): 
      First sector (34-6291455966, default = 2048) or {+-}size{KMGTP}: +3T
      First sector (34-6291455966, default = 2048) or {+-}size{KMGTP}: 
      Last sector (2048-6291455966, default = 6291455966) or {+-}size{KMGTP}: +3T
      Last sector (2048-6291455966, default = 6291455966) or {+-}size{KMGTP}: 
      Current type is ‘Linux filesystem‘
      Hex code or GUID (L to show codes, Enter = 8300): 8300
      Changed type of partition to ‘Linux filesystem‘
      ======查看分區信息
      Command (? for help): p
      Disk /dev/sdb: 6291456000 sectors, 2.9 TiB
      Logical sector size: 512 bytes
      Disk identifier (GUID): FA9BB121-FD84-4955-95C3-D4F21890A508
      Partition table holds up to 128 entries
      First usable sector is 34, last usable sector is 6291455966
      Partitions will be aligned on 2048-sector boundaries
      Total free space is 2014 sectors (1007.0 KiB)
      Number  Start (sector)    End (sector)  Size       Code  Name
      1            2048      6291455966   2.9 TiB     8300  Linux filesystem
    • 格式化:低級格式化(分區之前進行,劃分磁道)、高級格式化(分區之後對分區進行,創建文件系統)
      1.創建文件系統的工具:

      [root@xt ~]# mkfs
      mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.xfs     
      mkfs.cramfs  mkfs.ext3    mkfs.minix   

      2.ext系列文件系統專用管理工具:mke2fs

      mke2fs [OPTIONS]  device
                  -t {ext2|ext3|ext4}:指明要創建的文件系統類型
                      mkfs.ext4 = mkfs -t ext4 = mke2fs -t ext4
                  -b {1024|2048|4096}:指明文件系統的塊大小;
                  -L LABEL:指明卷標;
                  -j:創建有日誌功能的文件系統ext3;
                      mke2fs -j = mke2fs -t ext3 = mkfs -t ext3 = mkfs.ext3
                  -i #:bytes-per-inode,指明inode與字節的比率;即每多少字節創建一個Indode; 
                  -N #:直接指明要給此文件系統創建的inode的數量;
                  -m #:指定預留的空間,百分比;               
                  -O [^]FEATURE:以指定的特性創建目標文件系統; 

      3.檢測及修復文件系統工具 fsck.type e2fsck xfs_repair,修復文件建議離線修復

      ext系列文件系統的專用工具:
      e2fsck : check a Linux ext2/ext3/ext4 file system
                          e2fsck [OPTIONS]  device
                              -y:對所有問題自動回答為yes; 
                              -f:即使文件系統處於clean狀態,也要強制進行檢測;
                              -b:superblock 超級快                           
                      fsck:check and repair a Linux file system
                          -t fstype:指明文件系統類型;
                              fsck -t ext4 = fsck.ext4
                          -a:無須交互而自動修復所有錯誤;
                          -r:交互式修復;   
              eg: e2fsck -v -y -b 163840 /dev/sdb7    當fsck修復不了時,使用該命令修復

      4.查看文件系統屬性
      dumpe2fs, tune2fs ,xfs_info

    • 掛載
      創建好文件系統後要使用先得掛載:根文件系統這外的其它文件系統要想能夠被訪問,都必須通過“關聯”至根文件系統上的某個目錄來實現,此關聯操作即為“掛載”;此目錄即為“掛載點”;
      1.掛載點:
      事先必須存在
      該使用未被或不會被其它進程使用到的目錄
      工作目錄非空,原有文件會被覆蓋隱藏
      2.查看當前掛載情況
      mount
      cat /etc/mtab
      cat /proc/mounts

      3.mount掛載命令使用
      mount [-nrw] [-t vfstype] [-o options] device dir

      命令選項:
                      -r:readonly,只讀掛載; 
                      -w:read and write, 讀寫掛載; 
                      -n:默認設備掛載或卸載的操作會同步更新至/etc/mtab文件中;-n用於禁止此特性;
                      -a:讀取/etc/fstab中沒有掛載的設備, mount all filesystems mentioned in fstab
                      -t vfstype:指明要掛載的設備上的文件系統的類型;可省略,會通過blkid來判斷掛載設備的文件系統類型;            
      -o options:掛載選項
                      sync/async:同步/異步操作;
                      atime/noatime:文件或目錄在被訪問時是否更新其訪問時間戳;
                      diratime/nodiratime:目錄在被訪問時是否更新其訪問時間戳;
                      remount:重新掛載; 
                      acl:支持使用facl功能;
                          # mount -o acl  device dir 
                          # tune2fs  -o  acl  device 
                          mount -o remount,acl /dev/sda3 /mnt
                      ro:只讀 
                      rw:讀寫 
                      dev/nodev:此設備上是否允許創建設備文件;
                      exec/noexec:是否允許運行此設備上的程序文件;
                      auto/noauto:
                      user/nouser:是否允許普通用戶掛載此文件系統;
                      suid/nosuid:是否允許程序文件上的suid和sgid特殊權限生效;      
                      defaults:Use default options: rw, suid, dev, exec, auto, nouser, async, and relatime.

      4.umount卸載命令
      umount device|mount_point
      註:當正在被某進程訪問時無法卸載
      查看被誰占用
      #lsof mount_point
      #fuser -v mount_point
      終止進程 訪問:
      fuser -km mount_point

    • 如何開機自動掛載:
      把設備信息寫入到配置文件:/etc/fstab
      mount -a:可自動掛載定義在此文件中的所支持自動掛載的設備
      # /etc/fstab
      # Created by anaconda on Fri Jun  1 22:10:52 2018
      #
      # 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
      #
      UUID=9633c392-6fa0-4e3b-814d-9a2c30c60085 /                       xfs     defaults        0 0
      UUID=402d2bec-0b46-4654-8c96-6d2ca74e5668 /boot                   btrfs   subvol=boot     0 0
      UUID=9bf9acd8-1001-4eb1-b3a6-464afb90645d swap                    swap    defaults        0 0
      一共6個字段:
      1.要掛載的設備,最好使用設備的UUID
      2.掛載點,swap類型特殊
      3.文件系統類型
      4.掛載選項,如同mount中options
      5.轉儲頻率
      6.自檢次序:0不檢查,1首先檢查,2次級檢查一般對系統盤做檢查,業務盤不檢查即可,若檢查異常導致系統無法啟動

      blkid device #查看設備uuid,類型


    • VMware添加一塊新磁盤,不重啟,fdisk -l沒有顯示。

    1.查看主機總線號
    root@node /]# ls /sys/class/scsi_host/
    host0 host1 host2
    2.重新掃描SCSI總線來添加設備
    [root@node /]# echo "- - -" > /sys/class/scsi_host/host0/scan
    [root@node /]# echo "- - -" > /sys/class/scsi_host/host1/scan
    [root@node /]# echo "- - -" > /sys/class/scsi_host/host2/scan
    3.重新查看

    [root@xt ~]# fdisk -l
    Disk /dev/sda: 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: 0x00036ca6
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            2048    20973567    10485760   83  Linux
    /dev/sda2        20973568    25167871     2097152   82  Linux swap / Solaris
    /dev/sda3        25167872    25692159      262144   83  Linux
    Disk /dev/sdb: 3221.2 GB, 3221225472000 bytes, 6291456000 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

    Linux磁盤管理及文件系統使用