Linux磁盘管理及文件系统使用

  • 磁盘管理基础知识
  • 分区
    • 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...]:列出指定磁盘设备上的分区情况;
      [[email protected] ~]# 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>

      [[email protected] ~]# 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]
      [[email protected] ~]# 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

      [[email protected] ~]# 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类似
      [[email protected] ~]# 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.创建文件系统的工具:
      [[email protected] ~]# 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.查看主机总线号
    [email protected] /]# ls /sys/class/scsi_host/
    host0 host1 host2
    2.重新扫描SCSI总线来添加设备
    [[email protected] /]# echo "- - -" > /sys/class/scsi_host/host0/scan
    [[email protected] /]# echo "- - -" > /sys/class/scsi_host/host1/scan
    [[email protected] /]# echo "- - -" > /sys/class/scsi_host/host2/scan
    3.重新查看

    [[email protected] ~]# 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

    原文地址:http://blog.51cto.com/12580678/2340391

    时间: 2024-08-27 19:44:34

    Linux磁盘管理及文件系统使用的相关文章

    分析Linux磁盘管理与文件系统专题三

    1.前言 紧接着我的上一篇博客进行磁盘管理操作: http://zhangfengzhe.blog.51cto.com/8855103/1430531 我们已经对磁盘进行了分区,信息如下: [[email protected] ~]# fdisk -l /dev/sdb Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 5

    分析Linux磁盘管理与文件系统专题二

    1.关于设备文件 A 我们知道常见的设备文件有:字符设备文件(character),块设备文件(block). B 块设备,简写b,随机访问,比如硬盘. C 字符设备,简写c,线性访问,比如键盘,鼠标,显示器. D 我们的设备文件常常在/dev目录下,并且没有大小.因为设备文件只是作为设备访问   的入口. E 设备文件,一般用major(主设备号),minor(次设备号)进行标示.主设备号标示设备类型,次设备号标示同一种类型设备下的不同设备. 2.创建设备文件实例 [[email protec

    Linux磁盘管理及文件系统

    Linux磁盘管理及文件系统 1.Linux磁盘管理 识别硬盘设备:/dev/sd 标记不同的硬盘设备:/dev/sd[a-z] 标记同一设备上的不同分区:/dev/sd[a-z][1-] 1-4: 主或扩展分区标识 5+:逻辑分区标识 2.设备文件:特殊文件 设备号: major, minor major: 设备类型 minor: 同一类型下的不同设备 "块":block,随机设备 "字符":character,线性设备 3.分区工具: (1)fdisk, par

    Linux磁盘管理——日志文件系统与数据一致性

    参考:Linux磁盘管理——Ext2文件系统 数据不一致 上图是Ext2结构图,其他FS结构类似. 一般来说,我们将 inode table 与 data block 称为数据区:至于其他例如 superblock. block bitmap 与 inode bitmap 等称为 metadata (元数据). 以新增一个文件为例,看看FS操作流程 1. 先确定使用者对于欲新增文件的目录是否具有 w 与 x 的权限,若有的话才能新增:2. 根据 inode bitmap 找到没有使用的 inod

    Linux磁盘管理和文件系统

    前言分区的概念:分区从实质上说就是对硬盘的一种格式化.当我们创建分区时,就已经设置好了硬盘的各项物理参数,指定了硬盘主引导记录(即 MasterBootRecord,一般简称为 MBR)和引导记录备份的存放位置.而对于文件系统以及其他操作系统管理硬盘所需要的信息则是通过以后的高级格式化,即 Format 命令来实现.面.磁道和扇区硬盘分区后,将会被划分为面(Side).磁道(Track)和扇区(Sector).需要注意的是,这些只是个虚拟的概念,并不是真正在硬盘上划轨道MBR简介:MBR(Mai

    Linux磁盘管理、文件系统及其相关命令的使用方法

    Linux磁盘管理 硬盘: 机械硬盘 SSD固态硬盘 track sector: 512bytes cylinder: 分区的基本单位: MBR:Master Boot Record 512bytes 446: bootloader 64: filesystem allocation table 16: 标识一个分区  2:55AA 磁盘接口类型: IDE (ATA):133MB/s,/dev/hd SCSI: 640MB/s SATA:6Gbps SAS:6Gbps USB:480MB/s 识

    Linux磁盘管理与文件系统(精简理论部分,浓缩的精华!)

    今天和大家分享的是Linux操作系统中的磁盘管理与文件系统的相关知识,将会从以下几点和大家进行说明: 磁盘基础 检测并确认新磁盘 规划硬盘中的分区 创建文件系统 挂载.卸载文件系统 首先先和大家说一下磁盘的结构1.硬盘的物理结构 盘片:硬盘有多个盘片.每盘片2面 磁头:每面一个磁头2.硬盘的数据结构 扇区:盘片被分为多个扇形区域,每个扇区存放512字节的数据(磁盘的最小组成单元通常是512字节,部分厂商设定的是4096个字节) 磁道:同一盘片不同半径的同心圆 柱面:不同盘片相同半径构成的圆柱面(

    linux磁盘管理和文件系统创建

    1      磁盘管理 1.1    硬盘的构造原理 硬盘分类: 机械式硬盘,固态硬盘 硬盘出厂会进行低级格式化,分磁盘,再分扇区,硬盘的第一个磁道的一个扇区就是MBR 512Bytes Master boot record 446 bytes bootloader 主引导程序 64bytes :主分区存储 16bytes表示一个主分区,最多4个主分区 2bytes:magic number 表示mbr是否有效 硬盘的注意事项: a)                1.硬盘需要绝对的无尘环境,生

    详解Linux磁盘管理与文件系统

    磁盘基础 硬盘结构 物理结构 盘片:硬盘有多个盘片,每盘片 2 面. 磁头:每面一个磁头. 数据结构 扇区:磁盘上的每个磁道被等分为若干个弧段,这些弧段便是硬盘的扇区. 硬盘的第一个扇区,叫做引导扇区. 磁道:当磁盘旋转时,磁头若保持在一个位置上,则每个磁头都会在磁盘表面划出一个 圆形轨迹,这些圆形轨迹就叫做磁道. 柱面:在有多个盘片构成的盘组中,由不同盘片的面,但处于同一半径圆的多个磁道组 成的一个圆柱面. 储存容量 硬盘存储容量=磁头数×磁道(柱面)数×每道扇区数×每扇区字节数. 可以用柱面