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

匿名 (未验证) 提交于 2019-12-02 21:53:52
  • 磁盘管理基础知识
  • 分区
    • 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!