Linux 磁盘分区介绍

举报
rivers 发表于 2022/01/11 19:30:53 2022/01/11
【摘要】 linux 磁盘基础,简单的介绍了Iinode、区块、 文件系统等基础知识,手把手让你明白 如何建立文件系统

一、linux 文件系统及磁盘分区

1.1 磁盘简介:

  • 硬盘是计算机主要存储媒介之一,由一个或者多个铝制或者玻璃制的碟片组成,碟片外覆盖有铁磁性材料,硬盘内部由磁道、柱面、扇区、磁头等部件组成

  • Linux系统中硬件设备相关配置文件存放在/dev/下,常见硬盘命名:/dev/hda、/dev/sda、/dev/sdb、/dev/sdc、/dev/vda。不同硬盘接口,在系统中识别的设备名称不一样。

  • IDE硬盘接口在Linux中设备名为/dev/hdaSAS、SCSI、SATA硬盘接口在Linux中设备名为sda高效云盘硬盘接口会识别为/dev/vda等。

  • 文件储存在硬盘上,硬盘的最小存储单位叫做Sector(扇区)每个Sector储存512字节。操作系统在读取硬盘的时候,不会逐个Sector的去读取,这样效率非常低,为了提升读取效率,操作系统会一次性连续读取多个Sector,即一次性读取多个Sector称为一个Block(块

  • 由多个Sector组成的Block是文件存取的最小单位。Block的大小常见的有1KB、2KB、4KB,Block在Linux中常设置为4KB,即连续八个Sector组成一个Block

  • /boot分区Block一般为1KB,而/data/分区或者/分区的Block为4K

  • linux 系统查看分区的Block大小方法:

    [root@hbs ~]# stat anaconda-ks.cfg |grep -i "block"
      Size: 1511      	Blocks: 8          IO Block: 4096   regular file
    [root@hbs ~]# 
    
    [root@hbs ~]# stat /boot/|grep "IO Block"
      Size: 4096      	Blocks: 8          IO Block: 4096   directory
    [root@hbs ~]# 
    
    
    

1.2 linux 文件系统简介

1.3 mount 挂载

  • Mount命令工具主要用于将设备或者分区挂载至Linux系统目录下,Linux系统在分区时,也是基于mount机制将/dev/sda分区挂载至系统目录,将设备与目录挂载之后,Linux操作系统方可进行文件的存储。

    mount [-Vh]
    	mount -a [-fFnrsvw] [-t vfstype]
    	mount [-fnrsvw] [-o options [,...]] device | dir
    	mount [-fnrsvw] [-t vfstype] [-o options] device dir
    	-V:			                显示mount工具版本号;
    	-l:			                    显示已加载的文件系统列表;
    	-h:			                    显示帮助信息并退出;
    	-v:			                    输出指令执行的详细信息;
    	-n:			                    加载没有写入文件/etc/mtab中的文件系统;
    	-r:			                    将文件系统加载为只读模式; 
    	-a:			                    加载文件/etc/fstab中配置的所有文件系统;
    	-o:				                指定mount挂载扩展参数,常见扩展指令:rw、remount、loop等,其中-o相关指令如下:
    	-o atime:						系统会在每次读取文档时更新文档时间;
    	-o noatime:						系统会在每次读取文档时不更新文档时间;
    	-o defaults:						使用预设的选项 rw,suid,dev,exec,auto,nouser等;
    	-o exec                         允许执行档被执行;
    	-o user、-o nouser:				使用者可以执行 mount/umount的动作;
    	-o remount:						将已挂载的系统分区重新以其他再次模式挂载;
    	-o ro:							只读模式挂载;
    	-o rw:							可读可写模式挂载;
    	-o loop                          使用loop模式,把文件当成设备挂载至系统目录。
    	-t:			                    指定mount挂载设备类型,常见类型nfs、ntfs-3g、vfat、iso9660等,其中-t相关指令如下:
    	iso9660                         光盘或光盘镜像;
    	msdos 	                        Fat16文件系统;
    	vfat  	                        Fat32文件系统;
    	ntfs	                            NTFS文件系统;
    	ntfs-3g                          识别移动硬盘格式;
    	smbfs 	                        挂载Windows文件网络共享;
    	nfs		                        Unix/Linux文件网络共享。
    
  • MOUNT 常用案例

    mount  	/dev/sdb1    		/data 				挂载/dev/sdb1分区至/data/目录
    mount 	/dev/cdrom   		/mnt 			挂载Cdrom光盘至/mnt目录;
    mount 	-t ntfs-3g   		   /dev/sdc  	/data1	挂载/dev/sdc移动硬盘至/data1目录;
    mount   -o remount,rw    	/ 		  			重新以读写模式挂载/系统;
    mount 	-t  iso9660  -o loop  centos7.iso /mnt	将centos7.iso镜像文件挂载至/mnt目录;
    mount   -t fat32   /dev/sdd1            /mnt    将U/dev/sdd1挂载至/mnt/目录;
    mount   -t nfs 192.168.10.11:/data/    	/mnt	    将远程192.168.10.11:/data目录挂载至本地/mnt目录。
    

二、案例演示

2.1 gdisk 给磁盘分区

  • 使用 gdisk 创建一个 500M 的 xfs 文件系统,并将它挂载到 /data/test目录下

    # 1.列出所有磁盘
    [admin@rivers~]$ lsblk
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0   40G  0 disk 
    ├─sda1   8:1    0    2M  0 part 
    ├─sda2   8:2    0    1G  0 part /boot
    ├─sda3   8:3    0   10G  0 part /
    ├─sda4   8:4    0    5G  0 part /home
    └─sda5   8:5    0    1G  0 part [SWAP]
    sr0     11:0    1  4.2G  0 rom  /run/media/admin/CentOS 7 x86_64
    
    
    # 2. 查看磁盘分区表类型
    [admin@rivers~]$ sudo parted /dev/sda print
    [sudo] admin 的密码:
    Model: VMware, VMware Virtual S (scsi)   # (厂商)模块名称
    Disk /dev/sda: 42.9GB                    # 磁盘总容量
    Sector size (logical/physical): 512B/512B # 物理扇区容量
    Partition Table: gpt                     # 这里显示是gpt格式
    Disk Flags: pmbr_boot
    
    Number  Start   End     Size    File system     Name  标志
     1      1049kB  3146kB  2097kB                        bios_grub
     2      3146kB  1077MB  1074MB  xfs
     3      1077MB  11.8GB  10.7GB  xfs
     4      11.8GB  17.2GB  5369MB  xfs
     5      17.2GB  18.3GB  1074MB  linux-swap(v1)
    
    [admin@rivers~]$ 
    
    
    # 3.使用gdisk 创建分区
    
    [admin@rivers~]$ sudo  gdisk /dev/sda
    GPT fdisk (gdisk) version 0.8.6
    
    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
    
    Found valid GPT with protective MBR; using GPT.
    
    Command (? for help): ?               # ? 命令帮助,想到fdisk中的m
    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): p
    Disk /dev/sda: 83886080 sectors, 40.0 GiB               # 磁盘文件名/扇区总数与总容量
    Logical sector size: 512 bytes                          # 单一扇区大小为512 bytes 
    Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5  #磁盘的GPT标识码
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 83886046
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 48230333 sectors (23.0 GiB)          # 下面为完整的分区信息
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048            6143   2.0 MiB     EF02     # 第一个分区信息
       2            6144         2103295   1024.0 MiB  0700  
       3         2103296        23074815   10.0 GiB    0700  
       4        23074816        33560575   5.0 GiB     0700  
       5        33560576        35657727   1024.0 MiB  8200  
    
    Command (? for help): n                                 # 新增加一个
    Partition number (6-128, default 6):                    # 标识符
    First sector (34-83886046, default = 35657728) or {+-}size{KMGTP}: # 开始扇区
    Last sector (35657728-83886046, default = 83886046) or {+-}size{KMGTP}: +500M 
    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/sda: 83886080 sectors, 40.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 83886046
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 47206333 sectors (22.5 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048            6143   2.0 MiB     EF02  
       2            6144         2103295   1024.0 MiB  0700  
       3         2103296        23074815   10.0 GiB    0700  
       4        23074816        33560575   5.0 GiB     0700  
       5        33560576        35657727   1024.0 MiB  8200  
       6        35657728        36681727   500.0 MiB   8300  Linux filesystem
    
    
    # 8300 linux 文件系统
    # 8200 swap 文件系统
    Command (? for help): w  # 保存
    
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): Y  # 按Y 退出
    OK; writing new GUID partition table (GPT) to /dev/sda.
    Warning: The kernel is still using the old partition table.
    The new table will be used at the next reboot.
    The operation has completed successfully.
    [admin@rivers~]$ 
    [admin@rivers~]$ lsblk
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0   40G  0 disk 
    ├─sda1   8:1    0    2M  0 part 
    ├─sda2   8:2    0    1G  0 part /boot
    ├─sda3   8:3    0   10G  0 part /
    ├─sda4   8:4    0    5G  0 part /home
    └─sda5   8:5    0    1G  0 part [SWAP]
    sr0     11:0    1  4.2G  0 rom  /run/media/admin/CentOS 7 x86_64
    
    
    # 4. 更新linux 内核的分区表信息
     [admin@rivers~]$ lsblk 
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0   40G  0 disk 
    ├─sda1   8:1    0    2M  0 part 
    ├─sda2   8:2    0    1G  0 part /boot
    ├─sda3   8:3    0   10G  0 part /
    ├─sda4   8:4    0    5G  0 part /home
    ├─sda5   8:5    0    1G  0 part [SWAP]
    └─sda6   8:6    0  500M  0 part 
    sr0     11:0    1  4.2G  0 rom  /run/media/admin/CentOS 7 x86_64
    [admin@rivers~]$ 
    
    
    
    # 5.格式化刚建立的分区
    [admin@rivers~]$ sudo mkfs.xfs /dev/sda6
    meta-data=/dev/sda6              isize=512    agcount=4, agsize=32000 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=128000, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=855, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    [admin@rivers~]$ 
    
    
    # 6.查看/dev/sda6的UUID
    [admin@rivers~]$ sudo blkid|grep "/dev/sda6"
    /dev/sda6: UUID="fc584ce9-b4ff-4c54-8c2c-9ed6cf54dc1d" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="1aa87728-47cd-428d-b5bd-32a076c8f537" 
    [admin@rivers~]$ 
    
    
    # 7.创建挂载点,且挂载
    [admin@rivers~]$ sudo mount  UUID="fc584ce9-b4ff-4c54-8c2c-9ed6cf54dc1d" /data/test
    [admin@rivers~]$ df -h
    文件系统        容量  已用  可用 已用% 挂载点
    /dev/sda3        10G  4.6G  5.5G   46% /
    devtmpfs        1.8G     0  1.8G    0% /dev
    tmpfs           1.8G     0  1.8G    0% /dev/shm
    tmpfs           1.8G  9.1M  1.8G    1% /run
    tmpfs           1.8G     0  1.8G    0% /sys/fs/cgroup
    /dev/sda4       5.0G   37M  5.0G    1% /home
    /dev/sda2      1014M  158M  857M   16% /boot
    tmpfs           367M  4.0K  367M    1% /run/user/42
    tmpfs           367M   36K  367M    1% /run/user/1000
    /dev/sr0        4.3G  4.3G     0  100% /run/media/admin/CentOS 7 x86_64
    /dev/sda6       497M   26M  472M    6% /data/test
    [admin@rivers~]$ 
    
    # 8.删除刚建立的 6号分区,先卸载,再说删除
    [admin@rivers~]$ sudo umount /data/test/
    
    [admin@rivers~]$ sudo gdisk /dev/sda
    GPT fdisk (gdisk) version 0.8.6
    
    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
    
    Found valid GPT with protective MBR; using GPT.
    
    Command (? for help): p  # 打印下以存在的分区
    Disk /dev/sda: 83886080 sectors, 40.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 83886046
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 47206333 sectors (22.5 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048            6143   2.0 MiB     EF02  
       2            6144         2103295   1024.0 MiB  0700  
       3         2103296        23074815   10.0 GiB    0700  
       4        23074816        33560575   5.0 GiB     0700  
       5        33560576        35657727   1024.0 MiB  8200  
       6        35657728        36681727   500.0 MiB   8300  Linux filesystem
    
    Command (? for help): d   # 按 d 删除分区
    Partition number (1-6): 6
    
    Command (? for help): p
    Disk /dev/sda: 83886080 sectors, 40.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 83886046
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 48230333 sectors (23.0 GiB)
    
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048            6143   2.0 MiB     EF02  
       2            6144         2103295   1024.0 MiB  0700  
       3         2103296        23074815   10.0 GiB    0700  
       4        23074816        33560575   5.0 GiB     0700  
       5        33560576        35657727   1024.0 MiB  8200  
    
    Command (? for help): w  # 保存
    
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    
    Do you want to proceed? (Y/N): Y
    OK; writing new GUID partition table (GPT) to /dev/sda.
    Warning: The kernel is still using the old partition table.
    The new table will be used at the next reboot.
    The operation has completed successfully.
    
    
    [admin@rivers~]$ sudo lsblk
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0   40G  0 disk 
    ├─sda1   8:1    0    2M  0 part 
    ├─sda2   8:2    0    1G  0 part /boot
    ├─sda3   8:3    0   10G  0 part /
    ├─sda4   8:4    0    5G  0 part /home
    └─sda5   8:5    0    1G  0 part [SWAP]
    sr0     11:0    1  4.2G  0 rom  /run/media/admin/CentOS 7 x86_64
    [admin@rivers~]$ 
    
    

2.2 parted 创建文件系统

  • 使用parted 创建一个swap 分区

    # 0.检查是否有parted 工具,没有就安装一个这里我有,不需安装
    [admin@rivers~]$ which parted  
    /usr/sbin/parted
    #[admin@rivers~]$ yum -y install parted
    
    # 1. 先列出 磁盘分区表类型,及分区情况
    [admin@rivers~]$ sudo parted /dev/sda print
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt   #2. 这里看到的是GPT类型,使用gdisk或者parted分区
    Disk Flags: pmbr_boot
    
    Number  Start   End     Size    File system     Name  标志
     1      1049kB  3146kB  2097kB                        bios_grub
     2      3146kB  1077MB  1074MB  xfs
     3      1077MB  11.8GB  10.7GB  xfs
     4      11.8GB  17.2GB  5369MB  xfs
     5      17.2GB  18.3GB  1074MB  linux-swap(v1)
     
     # 2. 将 原本gpt格式的 磁盘变为 mbr 分区表。(很危险,请勿乱用!无法恢复!)
     @ 这条命令我变了一个磁盘/sdb ,新添加的磁盘,请不要用/sda 去弄。!
     [admin@rivers~]$ sudo parted /dev/sdb mklabel msdos
     [admin@rivers~]$parted /dev/sdb print
     Model: VMware, VMware Virtual S (scsi)
     Disk /dev/sdb: 10.7GB
     Sector size (logical/physical): 512B/512B
     Partition Table: msdos   # 现在这里变了,变成MBR了。
     Disk Flags: 
    
    Number  Start  End  Size  Type  File system  Flags
     使用parted 分区,格式化为 vfat 类型大小为 500M
     
     
     [admin@rivers~]$ sudo parted /dev/sda mkpart primary ext4 18.3GB 18.8GB
    [sudo] admin 的密码:
    信息: You may need to update /etc/fstab.
    
    [admin@rivers~]$ sudo partprobe /dev/sda    
    
    parted  [设备名]  [命令]  [参数]
    
    # 固定以 MB 格式显示
    parted   /dev/sdb  unit  mb  print  
    
    
    # 将GPT格式的分区表,将它改变为 MBR格式 
     parted /dev/sdb mklabel msdos
     
    # 在建立一个 500M的 ext4 格式的
    parted /dev/sda mkpart primary ext4 18.3GB 18.8GB
    

2.3 创建大文件做 swap 分区

  • 使用dd 命令,创建一个大文件,格式化做swap分区,并挂载

    # 1. 创建一个 大文件,块大小为1M ,共计512个块
    
    [admin@rivers~]$ dd if=/dev/zero of=/tmp/dvd bs=1M count=512
    记录了512+0 的读入
    记录了512+0 的写出
    536870912字节(537 MB)已复制,30.7426 秒,17.5 MB/秒
      
      # 2. 查看,并格式化文件
      [admin@rivers~]$ sudo ls -lih /tmp/dvd 
      16838973 -rw-rw-r--. 1 admin admin 512M 1229 20:09 /tmp/dvd
      
      [admin@rivers~]$ sudo mkswap /tmp/dvd 
      mkswap: /tmp/dvd: warning: wiping old swap signature.
      正在设置交换空间版本 1,大小 = 524284 KiB
      无标签,UUID=79a3af17-1c7a-4025-a596-c34782619b7a
      [admin@rivers~]$ 
      
      
      #3. 启动swap ,利用swapon 将/tmp/dvd 启动(swapoff 关闭 swap file)
      [admin@rivers~]$ sudo swapon /tmp/dvd
      
      # 4. 使用swapoff 关闭,并设置自启动
      [admin@rivers~]$ sudo swapoff /tmp/dvd 
      
      [admin@rivers~]$ tail -1 /etc/fstab 
      /tmp/dvd    swap  swap  defaults 0 0
      [admin@rivers~]$ 
    

总结

  • 一个 可以被挂载的数据 通常 称为 文件系统,不是硬盘分区

  • linux 传统文件系统为 ext2,该文件系统主要包含信息有:

    • 超级区块:包含inode/区块总量,使用量、剩余量,文件系统格式等信息
    • inode:文件属性,一个文件一个inode,同时记录文件数据所在区块号码
    • 数据区块:实际记录文件的内容,若文件太大,会占用多个数据区块
  • ext2 文件系统的数据存取为 索引式文件系统

  • 硬链接:只是多了一个文件名 对该inode 号码的连接,不能跨文件连接、不能连接目录

  • 符号链接:快捷方式。

  • 磁盘使用 必须经过 分区、格式化与挂载。常用命令 为 parted、gdisk、fdisk、mount、partproe等

  • 启动自动挂载,可以编写/etc/fstab 文件,然后使用 mount -a测试

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200