Linux入门真经-032文件系统的创建与管理

时间:2024-05-24 07:00:04

Linux入门真经-032文件系统的创建与管理

本节介绍文件系统的创建与一些文件系统管理的相关知识。

 

1、创建文件系统

 

新建文件系统常用的是mkfs命令。

mkfs说全了其实是:make filesystem

用法:

mkfs.FILESYSTEM [options] device

mkfs.之后连续按两次tab可以补全所支持的文件系统

 

 

[[email protected] ~]# mkfs.       #双击两次tab

mkfs.btrfs  mkfs.cramfs  mkfs.ext2    mkfs.ext3   mkfs.ext4    mkfs.minix   mkfs.xfs

[[email protected] ~]# mkfs.

 

举例:

首先我们可以看一下目前系统的分区状况,有一个简单的命令:lsblk

 

[[email protected] ~]# lsblk

NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda               8:0    0  20G  0 disk

├─sda1            8:1    0   1G  0 part /boot

└─sda2            8:2    0  19G  0 part

  ├─centos-root253:0    0   17G  0lvm  /

  └─centos-swap253:1    0    2G  0lvm  [SWAP]

sdb               8:16   0  20G  0 disk

├─sdb1            8:17   0 9.3G  0 part

└─sdb2            8:18   0 4.7G  0 part

sr0              11:0    1 906M  0 rom 

[[email protected] ~]#

 

我们还可以指定命令的输出条目(细则请参考man手册),如:

 

[[email protected] ~]# lsblk

[[email protected] ~]# lsblk -o NAME,FSTYPE,TYPE,SIZE

NAME            FSTYPE      TYPE SIZE

sda                         disk   20G

├─sda1          xfs         part   1G

└─sda2          LVM2_memberpart   19G

  ├─centos-rootxfs         lvm    17G

  └─centos-swapswap        lvm     2G

sdb                         disk   20G

├─sdb1                     part  9.3G

└─sdb2                      part 4.7G

sr0             iso9660     rom  906M

 

这里对输出作简要讲解:sda1分区上是xfs格式的文件系统,sda2做了lvm(后续会讲到),上面有两个分区,xfs格式的根和swap分区。

sdb上有两个分区,sdb1和sdb2都没有文件系统

 

我们将在sdb1上创建xfs格式的文件系统,sdb2上创建ext4格式的文件系统(如果你的虚拟机没有sdb或未分区,请根据前几节的教程自行添加硬盘并分区)

 

[[email protected] ~]# mkfs.xfs /dev/sdb1

meta-data=/dev/sdb1              isize=512    agcount=4, agsize=610304 blks

        =                      sectsz=512   attr=2, projid32bit=1

        =                       crc=1        finobt=0, sparse=0

data    =                       bsize=4096   blocks=2441216, imaxpct=25

        =                      sunit=0      swidth=0 blks

naming  =version 2             bsize=4096   ascii-ci=0 ftype=1

log     =internal log          bsize=4096   blocks=2560,version=2

        =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

[[email protected] ~]# mkfs.ext4 /dev/sdb2

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

305216 inodes, 1220608 blocks

61030 blocks (5.00%) reserved for the superuser

First data block=0

Maximum filesystem blocks=1249902592

38 block groups

32768 blocks per group, 32768 fragments pergroup

8032 inodes per group

Superblock backups stored on blocks:

         32768,98304, 163840, 229376, 294912, 819200, 884736

 

Allocating group tables: done                           

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystemaccounting information:  2/38

done

 

[[email protected] ~]#

[[email protected] ~]# lsblk -o NAME,FSTYPE,TYPE,SIZE

NAME            FSTYPE      TYPE SIZE

sda                         disk   20G

├─sda1          xfs        part    1G

└─sda2          LVM2_memberpart   19G

  ├─centos-rootxfs         lvm    17G

  └─centos-swapswap        lvm     2G

sdb                         disk   20G

├─sdb1          xfs         part 9.3G

└─sdb2          ext4        part 4.7G

sr0             iso9660     rom  906M

[[email protected] ~]#

 

不知道你是否注意到,sdb1大小是9.3G,sdb2是4.7G。但是sdb1格式化的时候似乎比sdb2快一些。没错,xfs的格式化速度确实比ext4要快一点,尤其是格式化大容量分区的时候,要明显得多。

 

ext4最大能支持1EB的文件系统,这个已经足够大,不过单个文件大小不能超过16T。是Centos6默认的文件系统

 

xfs能支持16EB的文件系统,尤其擅长处理大文件,在大文件的读写效率上要明显高于ext4,小文件的效率则和ext4相差无几。目前是Centos7默认的文件系统。

 

其实文件系统还有很多很深的知识,但是我觉得暂时没必要讲太多。

比如ext系列一些专用的管理工具:可以调整文件系统参数的tune2fs、查看超级块信息的dumpe2fs等等,用到再去查即可。

 

因进程意外终止或者系统崩溃导致写入操作非正常终止时,可能导致文件系统损坏,此时可以使用fsck来试图修复,读者可以自行了解。

 

xfs还可以进行文件系统级别的数据备份(xfsdump),了解即可,用到再去查。其实,数据备份的解决方案很多很成熟,数据库厂商提供了很多数据库级别的备份、回滚策略,存储厂商基于自己的存储设备也提供了很多的解决方案,所以极有可能用不着xfsdump的。因此暂不作详细介绍了。

 

2、SWAP文件系统

 

Linux上的交换分区必须使用独立的文件系统;

创建swap设备:mkswap命令

mkswap [OPTIONS]  device

-L LABEL:指明卷标      

-f:强制创建

创建完之后,使用swapon对swap分区进行扩容

swapon device

 

也可以用swapoff将对应的交换分区临时卸载

swapoff

 

举例:

我们在sdb上再创建一个4G的分区,来演示如何创建交换分区

我这台虚拟机上的sdb,由于之前演示的关系,是gpt格式的,所以我使用parted继续分区(fdisk新版本也支持gpt了,但是仍然处于实验阶段)

 

[[email protected] ~]# parted /dev/sdb print

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdb: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags:

 

Number Start   End     Size   File system  Name     Flags

 1     1049kB  10.0GB  9999MB xfs          primary

 2     10.0GB  15.0GB  5000MB ext4         primary

 

[[email protected] ~]# parted /dev/sdb mkpartprimary linux-swap 15G 19G

Information: You may need to update/etc/fstab.

 

查看当前的swap大小

 

[[email protected] ~]# free -m | grep -i swap

Swap:          2047           0        2047

 

[[email protected] ~]# mkswap /dev/sdb3

Setting up swapspace version 1, size =3906556 KiB

no label,UUID=776feabb-c248-43e9-a6e3-9ccc28630681

[[email protected] ~]# free -m | grep -i swap

Swap:          2047           0        2047

 

[[email protected] ~]# swapon /dev/sdb3

[[email protected] ~]# free -m | grep -i swap

Swap:          5862           0        5862

 

再用fdisk演示一遍。(我这边暂时不修改分区表格式了,新版本fdisk对gpt的支持仍然处于实验阶段,所以不建议在生产环境对gpt分区格式的磁盘进行操作)

 

[[email protected] ~]# swapoff /dev/sdb3

[[email protected] ~]# parted /dev/sdb print

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdb: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags:

 

Number Start   End     Size   File system     Name     Flags

 1     1049kB  10.0GB  9999MB xfs             primary

 2     10.0GB  15.0GB  5000MB ext4            primary

 3     15.0GB  19.0GB  4000MB linux-swap(v1)  primary

 

[[email protected] ~]# parted /dev/sdb rm 3

Information: You may need to update/etc/fstab.

 

[[email protected] ~]# parted /dev/sdbprint                                

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdb: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags:

 

Number Start   End     Size   File system  Name     Flags

 1     1049kB  10.0GB  9999MB xfs          primary

 2     10.0GB  15.0GB  5000MB ext4         primary

 

分区已经删除。

 

[[email protected] ~]# fdisk /dev/sdb

WARNING: fdisk GPT support is currentlynew, and therefore in an experimental phase. Use at your own discretion.

Welcome to fdisk (util-linux 2.23.2).

 

Changes will remain in memory only, untilyou decide to write them.

Be careful before using the write command.

 

可以看到警告:fdisk对gpt的支持仍然处于实验阶段。

 

Command (m for help): n

Partition number (3-128, default 3):

First sector (34-41943006, default29296640):

Last sector, +sectors or +size{K,M,G,T,P}(29296640-41943006, default 41943006): +4G

Created partition 3

 

 

Command (m for help): p

 

Disk /dev/sdb: 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 / 512bytes

Disk label type: gpt

Disk identifier:8EB60DAF-1A44-4428-9132-B030D8696F48

 

 

#        Start          End    Size Type            Name

 1        2048     19531775    9.3G Microsoft basic primary

 2    19531776     29296639    4.7G Microsoft basic primary

 3    29296640     37685247      4G Linux filesyste

 

这里的type是Linux filesyste我们要改成linux swap

 

Command (m for help): t

Partition number (1-3, default 3): 3

Partition type (type L to list all types):

这个时候你按L可以显示出全部的类型编号,可以看到默认是20 Linux filesystem,把他改成19 Linux swap即可。(注意:GPT的system id和mbr的完全不同,mbr默认是83 linux,改成82  Linux swap即可)

Partition type (type L to list all types):19

Changed type of partition 'Linuxfilesystem' to 'Linux swap'

 

Command (m for help): p

 

Disk /dev/sdb: 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 / 512bytes

Disk label type: gpt

Disk identifier:8EB60DAF-1A44-4428-9132-B030D8696F48

 

 

#        Start          End    Size Type            Name

 1        2048     19531775    9.3G Microsoft basic primary

 2    19531776     29296639    4.7G Microsoft basic primary

 3    29296640     37685247      4G Linux swap     

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

 

后面的关于swap的创建和挂载就都一样了:

 

[[email protected] ~]# mkswap /dev/sdb3

mkswap: /dev/sdb3: warning: wiping old swapsignature.

Setting up swapspace version 1, size =4194300 KiB

no label,UUID=e3077b1c-6c86-4696-9033-fe129308f11f

[[email protected] ~]# swapon /dev/sdb3

[[email protected] ~]# free -m | grep -i swap

Swap:          6143          0        6143

[[email protected] ~]#

 

好的,创建完文件系统之后,分区就处于可读写状态了,可是我们如何开始使用呢。我们需要把这个分区挂载到我们现有文件系统的某个目录下。下一节给大家介绍。

 

关注本公众号获取每日更新

Linux入门真经-032文件系统的创建与管理