Linux文件系统

目录

1 Linux磁盘分区表

1.1 MBR

1.2 GTP

2 Linux磁盘管理

2.1 lsblk

2.2 fdisk

2.3 格式化

2.4 mount

2.5 umount

2.6 swapon

2.8 swapoff


1 Linux磁盘分区表

1.1 MBR

在早期,linux系统为了兼容Windows的磁盘,使用了支持Windows的MBR(Master Boot Record,主引导记录)的方式来处理启动引导和分区表。

MBR结构的分区,第一个扇区大小是512字节(旧的磁盘扇区都是512字节),包含最多446字节的启动代码、4个硬盘分区表项(每个表项16字节,共64字节)、2个签名字节(0x55,0xAA)。

MBR分区有以下特点:

  • 支持的硬盘最大容量是2TB(超出部分无法被识别)
  • 最多有4个主分区或3个主分区、一个扩展分区
1.2 GTP

GPT分区模式使用GUID分区表,是源自EFI标准的一种较新的磁盘分区表结构的标准。与普遍使用的主引导记录(MBR)分区方案相比,GPT提供了更加灵活的磁盘分区机制。

GTP分区有以下优点:

  • 支持容量在2TB以上的硬盘
  • 每个磁盘的分区个数几乎没有限制(Windows仅支持128个分区)
  • 分区大小几乎没有限制(GPT分区提供了64位来记录开始/结束的扇区号)

2 Linux磁盘管理

2.1 lsblk

lsblk命令是"list block"的缩写,用于列出所有系统识别到的可用块设备的信息(不包括RAM)

Usage:
 lsblk [options] [<device> ...]

Options:
 -a, --all            打印所有设备
 -b, --bytes          以字节为单位显示分区大小
 -d, --nodeps         不打印从属或持有者
 -z, --zoned          print zone model
 -e, --exclude <list> exclude devices by major number (default: RAM disks)
 -f, --fs             输出有关文件系统的信息
 -i, --ascii          仅使用ASCII字符
 -I, --include <list> 仅显示有指定主要编号的设备
 -n, --noheadings     不打印标题行
 -S, --scsi           输出有关SCSI设备的信息
 -t, --topology       输出有关拓扑的信息

Eg:
[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0          11:0    1 1024M  0 rom  
nvme0n1     259:0    0   20G  0 disk 
├─nvme0n1p1 259:1    0    1G  0 part /boot
└─nvme0n1p2 259:2    0   19G  0 part 
  ├─rl-root 253:0    0   17G  0 lvm  /
  └─rl-swap 253:1    0    2G  0 lvm  [SWAP]
[root@localhost ~]# 
2.2 fdisk

该命令用于创建和维护分区表的程序,兼容 DOS 类型的分区表、BSD 或者 SUN 类型的磁盘列表 

Usage:
 fdisk [options] <disk>      change partition table
 fdisk [options] -l [<disk>] list partition table(s)

 选择指定磁盘开始磁盘分区

[root@localhost ~]# fdisk /dev/sda

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 0x7e8ba37e.

Command (m for help): 

常用功能:

Command (m for help): m

Help:

  DOS (MBR)
   a   活动分区标记/引导分区

  Generic
   d   删除分区
   l   显示分区类型
   n   新建分区
   p   显示分区信息
   t   设置分区号
   v   新建分区

  Misc
   m   显示菜单和帮助信息

  Save & Exit
   w   保存并推出
   q   不保存退出


Command (m for help): 

 建立磁盘分区

Command (m for help): n    #创建一个1G的主分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +1G

Created a new partition 1 of type 'Linux' and of size 1 GiB.

Command (m for help): n    #创建一个2G的主分区
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 (2099200-10485759, default 2099200): 
Last sector, +sectors or +size{K,M,G,T,P} (2099200-10485759, default 10485759): +2G                              

Created a new partition 2 of type 'Linux' and of size 2 GiB.

Command (m for help): n    #将剩余空间划分给扩展分区
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (3,4, default 3): 
First sector (6293504-10485759, default 6293504): 
Last sector, +sectors or +size{K,M,G,T,P} (6293504-10485759, default 10485759): 

Created a new partition 3 of type 'Extended' and of size 2 GiB.

Command (m for help): n    #将扩展分区划分为一个逻辑分区
All space for primary partitions is in use.
Adding logical partition 5
First sector (6295552-10485759, default 6295552): 
Last sector, +sectors or +size{K,M,G,T,P} (6295552-10485759, default 10485759): 

Created a new partition 5 of type 'Linux' and of size 2 GiB.

Command (m for help): 

 保存

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0    5G  0 disk 
├─sda1        8:1    0    1G  0 part 
├─sda2        8:2    0    2G  0 part 
├─sda3        8:3    0    1K  0 part 
└─sda5        8:5    0    2G  0 part 
sr0          11:0    1 1024M  0 rom  
nvme0n1     259:0    0   20G  0 disk 
├─nvme0n1p1 259:1    0    1G  0 part /boot
└─nvme0n1p2 259:2    0   19G  0 part 
  ├─rl-root 253:0    0   17G  0 lvm  /
  └─rl-swap 253:1    0    2G  0 lvm  [SWAP]
[root@localhost ~]# 
2.3 格式化
  1. mkfs.ext2/3/4        将磁盘格式化为ext2/3/4文件系统
  2. mkfs.fat                 将磁盘格式化为fat文件系统
  3. mkfs.xfs                将磁盘格式化为xfs文件系统
  4. mkswap                将磁盘格式化为交换分区
#以ext4为例
[root@localhost ~]# mkfs.ext4 /dev/sda1 
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 0f9eeb23-f3ad-47fc-b160-5c842bb23d2b
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

[root@localhost ~]# mkswap /dev/sda2 
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=724edf7f-378f-4557-a2a7-d1869d09b888
[root@localhost ~]# 
2.4 mount

用于挂载Linux系统外的文件

Usage: mount [-a] 分区 挂载点

Eg:
[root@localhost ~]# mount /dev/sda1 /data1/        
[root@localhost ~]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
devtmpfs               893976       0    893976   0% /dev
tmpfs                  914100       0    914100   0% /dev/shm
tmpfs                  914100    8932    905168   1% /run
tmpfs                  914100       0    914100   0% /sys/fs/cgroup
/dev/mapper/rl-root  17811456 2118068  15693388  12% /
/dev/nvme0n1p1        1038336  216296    822040  21% /boot
tmpfs                  182820       0    182820   0% /run/user/0
/dev/sda1              999320    2564    927944   1% /data1
[root@localhost ~]# 
2.5 umount

卸除目前挂在Linux目录中的文件系统

Usage: umount 挂载点

Eg:
[root@localhost ~]# umount /data1 
[root@localhost ~]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
devtmpfs               893976       0    893976   0% /dev
tmpfs                  914100       0    914100   0% /dev/shm
tmpfs                  914100    8932    905168   1% /run
tmpfs                  914100       0    914100   0% /sys/fs/cgroup
/dev/mapper/rl-root  17811456 2118048  15693408  12% /
/dev/nvme0n1p1        1038336  216296    822040  21% /boot
tmpfs                  182820       0    182820   0% /run/user/0
[root@localhost ~]# 
2.6 swapon

Linux系统的内存管理必须通过交换区来建立虚拟内存,这条命令用于开启或查看交换分区

Usage: swapon [options] [<spec>]

Options:
 -a, --all                启用/etc/fstab中的所有交换分区
 -p, --priority <prio>    指定交换设备的优先级
 -s, --summary            显示有关已用交换设备的摘要

Eg:
[root@localhost ~]# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2
[root@localhost ~]# swapon /dev/sda2 
[root@localhost ~]# swapon 
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2
/dev/sda2 partition   2G   0B   -3
[root@localhost ~]# 
2.8 swapoff

有开启自然就会有关闭嘛

Usage:
 swapoff [options] [<spec>]

Options:
 -a, --all     将/etc/fstab文件中所有设置为swap的设备关闭

Eg:
[root@localhost ~]# swapoff /dev/sda2
[root@localhost ~]# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2
[root@localhost ~]# 
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值