红帽学习笔记[RHCSA] 第九课[文件归档、硬盘、分区以及自动挂载、Swap、链接]

2022-11-19,,,,

文件归档

tar是什么

通过tar命令可以将大型文件汇集成一个文件(归档),注意没有压缩功能。

压缩方式

gzip

通过gzip过滤文档,使用最广泛

bzip2

通常比gzip压缩小,但是不如gzip广泛

xz

比较新,压缩率比较高

tar命令参数介绍

-A 追加文件至归档
-c 创建一个新的归档
-v 列出处理文件的过程
-f 要操作的归档的名字
-t 列出归档内容
-x 从归档文件中解压出内容
-a 使用文档后缀名来决定压缩程序
-j bzip2 压缩方式
-J xz压缩方式
-z gzip压缩方式

tar命令操作

# 创建tar
[root@master kane]# tar cf 1.tar ./tar_test/
# 查看tar内容
[root@master kane]# tar -tf 1.tar
./tar_test/
./tar_test/11.q
./tar_test/22.q
./tar_test/44.q
./tar_test/33.q
# 解压出来
[root@master test]# ls
1.tar
[root@master test]# tar -xvf 1.tar
./tar_test/
./tar_test/11.q
./tar_test/22.q
./tar_test/44.q
./tar_test/33.q
[root@master test]# ls
1.tar tar_test
# 创建一个使用gzip压缩的文件
[root@master kane]# tar -zcf 2.tar ./tar_test/
# 比较一下 1.tar 与 2.tar 。发现2.tar确实小了很多
[root@master test]# ll
total 16
-rw-r--r--. 1 root root 10240 Oct 22 14:57 1.tar
-rw-r--r--. 1 root root 178 Oct 22 15:01 2.tar
drwxr-xr-x. 2 root root 50 Oct 22 14:57 tar_test

硬盘

分类

/dev/sd      表示串口设备
/dev/hd 表示并口设备(现在很难找并口设备)
/dev/vd 虚拟出来的

硬盘设备的命名规则

# 按设备
第一块 /dev/sda
第二款 /dev/sdb
# 按分区
第一个分区 /dev/sda1
第二个分区 /dev/sda2

硬盘结构

# 物理结构
机械硬盘与固态硬盘(寿命不如机械硬盘)
# 逻辑结构
1. 磁道:以盘片为中心,半径大小不同的同心圆组成的环形区域。
2. 扇区:磁道上的一段圆弧
3. 柱面:虚的,类似于筒壁

分区创建、使用

为什么要分区

1. 便于管理
2. 不同类型的数据分开放
3. 分隔操作系统
4. 初始化硬盘

分区的类型

1. 主分区:
安装操作系统,启动和引导操作系统。最多只能有`4`个主分区。MBR(Master Boot Recorder) 主要开机扇区,放置硬盘的信息。一共512B,前面446B字节是程序,`64B`表示分区每个分区是16B。所以一共只能有4个主分区。最后两个字节是保留位。
2. 扩展分区:
为了解除只有4个主分区的限制就有了扩展分区。但是扩展分区占用主分区的编号,最多只能有一个扩展分区。可以在扩展分区上创建逻辑分区供我们来使用
3. 逻辑分区:
在扩展分区上创建逻辑分区来使用。
4. 注意:
分区不是无限的,对于串口设备,最多15个。`1~4`主分区或扩展分区`5-15`逻辑分区。即使主分区只用了一个分区号,逻辑分区也不能使用前面四个分区号。

创建分区的步骤

1. 进入到分区操作
2. 通知内核读取分区表
3. 创建文件系统即格式化
4. 挂载使用

创建主分区 操作

1. 操作在虚拟机上操作,需要添加一块硬盘给虚拟机。
2. 查看硬盘情况
[root@localhost Desktop]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─rhel-root 253:0 0 17.5G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 1024M 0 rom
3. 使用fdisk 进入到 sda硬盘的分区
[root@master Desktop]# fdisk /dev/sda
4. 查看分区(此时还没有分区)使用命令 p
命令(输入 m 获取帮助):p 磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0000220c 设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 41943039 20458496 8e Linux LVM
5. 使用fdisk进入到sdb开始分区
[root@localhost Desktop]# fdisk /dev/sdb
6. 创建一个主分区
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1000M
Partition 1 of type Linux and of size 1000 MiB is set 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 / 512 bytes
Disk label type: dos
Disk identifier: 0xb43d3ed1 Device Boot Start End Blocks Id System
/dev/sdb1 2048 2050047 1024000 83 Linux
7. 注:关于扇区大小,精确计算扇区位置比较复杂直接使用 +{K,M,G}比较方便
8. 保存 w 自动退出
9. 通知内核
[root@localhost Desktop]# partprobe
10. 创建文件系统即格式化
[root@localhost Desktop]# mkfs.ext4 /dev/sdb1
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
64000 inodes, 256000 blocks
12800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=262144000
8 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376 Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
11. 挂在到目录上
[root@localhost Desktop]# mount /dev/sdb1 /first
12. 查看分区
[root@localhost Desktop]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─rhel-root 253:0 0 17.5G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 1000M 0 part /first
sr0 11:0 1 1024M 0 rom

创建 扩展分区 逻辑分区

1. 直接进入sdb
[root@localhost Desktop]# fdisk /dev/sdb
2. 创建扩展分区,创建了10个G
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2):
First sector (2050048-41943039, default 2050048):
Using default value 2050048
Last sector, +sectors or +size{K,M,G} (2050048-41943039, default 41943039): +10G
Partition 2 of type Extended and of size 10 GiB is set 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 / 512 bytes
Disk label type: dos
Disk identifier: 0xb43d3ed1 Device Boot Start End Blocks Id System
/dev/sdb1 2048 2050047 1024000 83 Linux
/dev/sdb2 2050048 23021567 10485760 5 Extended
3. 创建逻辑分区
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (2052096-23021567, default 2052096):
Using default value 2052096
Last sector, +sectors or +size{K,M,G} (2052096-23021567, default 23021567): +1G
Partition 5 of type Linux and of size 1 GiB is set 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 / 512 bytes
Disk label type: dos
Disk identifier: 0xb43d3ed1 Device Boot Start End Blocks Id System
/dev/sdb1 2048 2050047 1024000 83 Linux
/dev/sdb2 2050048 23021567 10485760 5 Extended
/dev/sdb5 2052096 4149247 1048576 83 Linux
4. 保存-通知内核-格式化-挂载
[root@localhost Desktop]# partprobe
[root@localhost Desktop]# mkfs.ext4 /dev/sdb5
[root@localhost Desktop]# mount /dev/sdb5 /second
5. 查看分区
[root@localhost Desktop]#
[root@localhost Desktop]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─rhel-root 253:0 0 17.5G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 1000M 0 part /first
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 1G 0 part /second
sr0 11:0 1 1024M 0 rom
6. 查看文件系统
[root@localhost Desktop]# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 19G 3.2G 16G 17% /
devtmpfs 949M 0 949M 0% /dev
tmpfs 958M 144k 958M 1% /dev/shm
tmpfs 958M 9.3M 949M 1% /run
tmpfs 958M 0 958M 0% /sys/fs/cgroup
/dev/sda1 521M 125M 397M 24% /boot
/dev/sdb1 1.1G 2.6M 944M 1% /first
/dev/sdb5 1.1G 2.7M 951M 1% /second

分区自动挂载

查看文件/etc/fstab

[root@localhost Desktop]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Aug 15 20:39:14 2019
#
# 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
#
/dev/mapper/rhel-root / xfs defaults 1 1
UUID=a0d45f55-dcf8-4d8e-b32d-4d1eb66a07bc /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0

自动挂载的三种方式

1. 使用UUID
2. 使用label
3. 使用设备名(不建议,因为设备名有可能会更换)

UUID

1. 查看UUID
[root@localhost Desktop]# blkid /dev/sdb5
/dev/sdb5: UUID="1eb4fbaa-5744-48e1-809d-90181843eddb" TYPE="ext4"
/* 在没创建文件系统的时候是没有UUID的*/
2. 编辑/etc/fstab文件
UUID=1eb4fbaa-5744-48e1-809d-90181843eddb /third xfs defaults 0 0
设备或分区或镜像名 挂载点 文件系统类型 挂载选项 是否备份 是否开机检测
3. 重启虚拟机
4. 查看目录情况,可以看到已经自动挂载上了
[root@localhost Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 84K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sdb5 190M 1.6M 175M 1% /third
/dev/sda1 497M 119M 379M 24% /boot

标签

1. 重新创建一个逻辑分区db6
1. 查看分区标签(默认为空)
[root@localhost Desktop]# e2label /dev/sdb6 2. 设置一个标签
[root@localhost Desktop]# e2label /dev/sdb6 game
[root@localhost Desktop]# e2label /dev/sdb6
game
3. 编辑 /etc/fstab 添加以标签为开始的自动挂载
LABEL=game /forth ext4 defaults 0 0
4. 重启后查看文件系统
[root@localhost Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 152K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sdb6 190M 1.6M 175M 1% /forth
/dev/sdb5 190M 1.6M 175M 1% /third
/dev/sda1 497M 119M 379M 24% /boot

注:上述编辑完 /etc/fstab后先mount -a测试一下是否正确,否则可能导致虚拟机启动不了。

Swap虚拟内存

虚拟内存是一种计算机内存管理的一种结束,可以把不常用的数据缓存到硬盘上。
两种方式

1. 交换分区
2. 交换文件

查看虚拟内存

[root@localhost Desktop]# free -h
total used free shared buffers cached
Mem: 1.8G 809M 1.0G 9.6M 912K 268M
-/+ buffers/cache: 539M 1.3G
Swap: 2.0G 0B 2.0G

使用文件作为Swap

1. 创建文件/swap/swapfile,使用dd用指定大小的块拷贝到问价中
[root@localhost Desktop]# dd if=/dev/zero of=/swap/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 7.59467 s, 141 MB/s
2. 将文件转成swap
root@localhost Desktop]# mkswap /swap/swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=7df01b33-7874-4ae9-b275-1306427bcf82
3. 激活swap
[root@localhost Desktop]# swapon /swap/swapfile
swapon: /swap/swapfile: insecure permissions 0644, 0600 suggested.
4. 查看发现多了1个G
[root@localhost Desktop]# free -h
total used free shared buffers cached
Mem: 1.8G 1.7G 77M 9.7M 168K 1.0G
-/+ buffers/cache: 711M 1.1G
Swap: 3.0G 0B 3.0G
5. 配置开机生效
vi /etc/fstab
/swap/swapfile swap swap defaults 0 0
6. 关闭swap
[root@localhost Desktop]# swapoff /swap/swapfile
[root@localhost Desktop]# free -h
total used free shared buffers cached
Mem: 1.8G 1.7G 75M 9.8M 168K 1.0G
-/+ buffers/cache: 711M 1.1G
Swap: 2.0G 0B 2.0G
7. 重启一次看看开机是否挂载上
root@localhost Desktop]# free -h
total used free shared buffers cached
Mem: 1.8G 818M 1.0G 9.8M 916K 266M
-/+ buffers/cache: 551M 1.2G
Swap: 3.0G 0B 3.0G

使用分区作为swap

1. 创建一个 1G swap 分区
[root@localhost Desktop]# fdisk /dev/sdb
Command (m for help): n
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 8
First sector (2926592-20973567, default 2926592):
Using default value 2926592
Last sector, +sectors or +size{K,M,G} (2926592-20973567, default 20973567): +1G
Partition 8 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1,5-8, default 8): 8
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris' 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 / 512 bytes
Disk label type: dos
Disk identifier: 0xed07c356 Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 5 Extended
/dev/sdb5 4096 413695 204800 83 Linux
/dev/sdb6 415744 825343 204800 83 Linux
/dev/sdb7 827392 2924543 1048576 82 Linux swap / Solaris
/dev/sdb8 2926592 5023743 1048576 82 Linux swap / Solaris
/* 创建后需要用t命令去修改分区类型为 82( Linux swap / Solaris ) */
2. 保存退出后通知内核
3. 格式化成 交换分区
[root@localhost Desktop]# mkswap /dev/sdb8
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=0cf98e08-cfe7-4818-8a39-2c51987c0677
4. 激活后查看又多了1G
[root@localhost Desktop]# swapon /dev/sdb8
[root@localhost Desktop]# free -h
total used free shared buffers cached
Mem: 1.8G 830M 996M 9.8M 1.7M 273M
-/+ buffers/cache: 555M 1.2G
Swap: 4.0G 0B 4.0G

软连接与硬链接

硬链接:基本不用,硬链接与cp类似,但是硬链接不占用内存。硬连接不更新文件时间。创建时不能跨分区

软连接:符号连接,类似windows快捷方式,占用i接待你,创建的时候没有其他限制

touch testfile
#创建硬链接
ln testfile testfile.hard
# 创建软连接
ln -s testfile testfile.hard # 硬链接在原来的文件删除时还是好用的,软连接不好用
# 开机自启其实就是创建软连接

红帽学习笔记[RHCSA] 第九课[文件归档、硬盘、分区以及自动挂载、Swap、链接]的相关教程结束。

《红帽学习笔记[RHCSA] 第九课[文件归档、硬盘、分区以及自动挂载、Swap、链接].doc》

下载本文的Word格式文档,以方便收藏与打印。