作业5 深入文件系统


文章目录

  • 作业5 深入文件系统
  • 5.0 实验原理
  • 5.1 分区
  • 5.2格式化成ext4+三种方式挂载
  • 5.3 开机自动挂载
  • 5.4开机挂载光盘到指定目录
  • 5.5 parted命令进行GPT分区
  • 5.6 FTP下载镜像并挂载到本地目录
  • 5.7问题


5.0 实验原理

  • MBR分区表与GPT分区表的对比

对比

MBR

GPT

名称

主引导记录,存在于磁盘开头的一个用于系统启动的扇区,在Linux系统,这段MBR扇区主要存放GRUB加载程序,Grub用来引导Linux操作系统内核的初始化启动。

全局唯一标识磁盘(GUID)分区表。另一种更新颖的磁盘组织方式。

BIOS类型

传统即Legacy模式,使用到的INT13中断读取磁盘数据,每次只能读取64KB

UEFI模式,使用到的INT13中断读取硬盘,每次可以读取,1024KB,载入速度快

支持最大容量

2.2TB,如果需要更多分区,需要创建扩展分区,在这个基础上创建逻辑分区

18EB(1EB=2^30GB)

分区限制

最多划分四个主分区

GPT会为每一个分区分配一个全局唯一的标识符,没有个数,大小限制

安全性

分区和启动信息保存在一起,这部分数据被破坏,系统无法正常启动

整个磁盘有多个分区和启动信息副本,健壮性强,一经CRC校验发现数据被破坏,可以恢复被破坏的信息。

5.1 分区

利用MBR方式添加分区最多只能添加四个主分区,或者是三个主分区合一个扩展分区(可以是多个)。当操作系统已经添加了三个主分区,存量剩下一个,那么为了添加更多的分区以满足操作系统的需要,我们就只能添加拓展分区,在这个扩展分区里我们就可以添加任意个逻辑分区。

  1. 查看当前分区状态
[root@ ~ 10:25:56]#fdisk -l /dev/sda
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: 0x000cdaf0
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    21383167    10485760   83  Linux
/dev/sda3        21383168    25577471     2097152   82  Linux swap / Solaris

由上面已有分区情况可知,已知sda有21.5GB,/dev/sda1占据约204MB磁盘空间,/dev/sda2占据约10GB,/dev/sda3占据约2GB空间,因此剩余足够的磁盘空间(约7.8G)供使用。

  1. 创建扩展分区
[root@ ~ 11:05:49]#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): n #添加分区
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e  #选择扩展分区
Selected partition 4
First sector (25577472-41943039, default 25577472): 
Using default value 25577472
Last sector, +sectors or +size{K,M,G} (25577472-41943039, default 41943039): +1G  #创建1G的扩展分区
Partition 4 of type Extended and of size 1 GiB is set
  1. 添加逻辑分区(五次)
Command (m for help): n
All primary partitions are in use
Adding logical partition 9
First sector (26406912-27674623, default 26406912): 
Using default value 26406912
Last sector, +sectors or +size{K,M,G} (26406912-27674623, default 27674623): +100M
Partition 9 of type Linux and of size 100 MiB is set

4.查看分区情况

Command (m for help): p
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: 0x000cdaf0
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    21383167    10485760   83  Linux
/dev/sda3        21383168    25577471     2097152   82  Linux swap / Solaris
/dev/sda4        25577472    27674623     1048576    5  Extended
/dev/sda5        25579520    25784319      102400   83  Linux
/dev/sda6        25786368    25991167      102400   83  Linux
/dev/sda7        25993216    26198015      102400   83  Linux
/dev/sda8        26200064    26404863      102400   83  Linux
/dev/sda9        26406912    26611711      102400   83  Linux
//可以看到有一个扩展分区和五个100M的逻辑分区

5.保存分区操作

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks. //提示你现在系统还在使用旧的分区表,需要更新

6.更新分区表

[root@ ~ 11:07:09]#partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-3
[root@ ~ 11:25:04]#partx -a /dev/sda  //连续输入partx命令
partx: /dev/sda: error adding partitions 1-9 
[root@ ~ 11:25:05]#ls /dev/sda*    
/dev/sda   /dev/sda2  /dev/sda4  /dev/sda6  /dev/sda8
/dev/sda1  /dev/sda3  /dev/sda5  /dev/sda7  /dev/sda9

5.2格式化成ext4+三种方式挂载

拓展分区无法被格式化,我们需要格式化的是逻辑分区。

  1. 制作ext4文件系统
[root@ ~ 11:36:08]#mkfs -t ext4 /dev/sda9  //格式化为ext4文件系统,这里只举例对sda9操作,其他分区操作类似
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
  1. 查看所有分区是否被格式化
[root@ ~ 03:24:58]#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  211MB   210MB   primary   ext4            boot
 2      211MB   10.9GB  10.7GB  primary   ext4
 3      10.9GB  13.1GB  2147MB  primary   linux-swap(v1)
 4      13.1GB  14.2GB  1074MB  extended
 5      13.1GB  13.2GB  105MB   logical   ext4
 6      13.2GB  13.3GB  105MB   logical   ext4
 7      13.3GB  13.4GB  105MB   logical   ext4
 8      13.4GB  13.5GB  105MB   logical   ext4
 9      13.5GB  13.6GB  105MB   logical   ext4
...
  1. 在/mnt目录下创建挂载目录
[root@ /mnt 03:28:23]#mkdir disk{a,b,c,d,e}  //创建目录
[root@ /mnt 03:28:28]#ls   //查看创建情况
cdrom  diska  diskb  diskc  diskd  diske
  1. 分别用/目录(对sda5,sda6),UUID(sda7,sda8),UUID(sda9)方式将分区挂载到指定目录里
  • 查看sda7,sda8的UUID
[root@ ~ 08:45:48]#blkid /dev/sda7
/dev/sda7: UUID="ede09dd4-53f5-436d-8892-6dec4f9535f5" TYPE="ext4" 
[root@ ~ 08:45:55]#blkid /dev/sda8
/dev/sda8: UUID="03fb8bd3-e386-4b39-b43b-91c95fe3b818" TYPE="ext4"
  • 为sda9设置卷标,由于sda9是ext4文件系统,设置卷标选择e2label命令
[root@ ~ 08:49:12]#e2label /dev/sda9 "diskesda9"
  • 挂载
[root@ ~ 08:52:03]#mount /dev/sda5 /mnt/diska
[root@ ~ 08:52:22]#mount /dev/sda6 /mnt/diskb
[root@ ~ 08:52:36]#mount -U ede09dd4-53f5-436d-8892-6dec4f9535f5 /mnt/diskc   #使用UUID挂载 选项为-U
[root@ ~ 08:53:25]#mount -U 03fb8bd3-e386-4b39-b43b-91c95fe3b818 /mnt/diskd
[root@ ~ 08:53:47]#mount -L diskesda9 /mnt/diske
[root@ ~ 08:54:27]#df  使用卷标挂载  选项为-L
Filesystem     1K-blocks    Used Available Use% Mounted on
...#省略无关信息
/dev/sda5          95054    1550     86336   2% /mnt/diska
/dev/sda6          95054    1550     86336   2% /mnt/diskb
/dev/sda7          95054    1550     86336   2% /mnt/diskc
/dev/sda8          95054    1550     86336   2% /mnt/diskd
/dev/sda9          95054    1550     86336   2% /mnt/diske

5.3 开机自动挂载

手动挂载并不是永久性挂载,重启系统之后就挂载的痕迹消失;要实现永久性修改,满足开机自动挂载,则需要修改/etc/fstab配置文件

  1. /etc/fstab文件系统描述信息,文件中的每一行是文件系统的描述,每一行选项之间严格用tab键分割。/etc/fstab文件的每一行都遵循以下格式:
<device>	<dir>	<type>	<options>	<dump>	 <pass>
//device,指定需要加载的磁盘分区或者移动文件系统可以使用目录,UUID,卷标指定
//dir 指定挂载点的路径
//type 指定文件系统的类型
//options 指定挂载的选项 默认为defaults
//dump 挂载后文件系统能否被dump备份命令作用,0表示不能,1表示每天备份,2表示不定期
//pass 表示开机过程当中是否校验扇区,0表示不要校验,1表示优先校验,2表示1级别校验完后再进行校验
  1. 修改配置文件
#  // /etc/fstab配置文件如下
# /etc/fstab
# Created by anaconda on Mon Jun 22 18:51:04 2020
#
# 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=ea1d8d1c-3e80-4a6f-98cc-c83d8897ff86 /                       ext4    defau    lts        1 1
UUID=35521718-2203-497c-b690-d44622dc6704 /boot                   ext4    defau    lts        1 2
UUID=60cf8279-14ea-42a1-9083-0be8cbbc9f8d swap                    swap    defau    lts        0 0
UUID=d1b3797e-a45d-4687-9b5d-6207d0962149 /mnt/diska            ext4    default    s        0 0
UUID=584c74ac-ae21-4386-8aec-9b0616ad3208 /mnt/diskb            ext4    default    s        0 0
UUID=ede09dd4-53f5-436d-8892-6dec4f9535f5 /mnt/diskc            ext4    default    s        0 0
UUID=03fb8bd3-e386-4b39-b43b-91c95fe3b818 /mnt/diskd            ext4    default    s        0 0
UUID=af1a3376-f162-4ff6-8a0e-dd833bd9790c /mnt/diske            ext4    default    s        0 0
  1. 重启系统 df查看磁盘使用情况,系统启动自动挂载成功
[root@ ~ 09:22:59]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       10190100 5318756   4330672  56% /
devtmpfs          488148       0    488148   0% /dev
tmpfs             498976       0    498976   0% /dev/shm
tmpfs             498976    7840    491136   2% /run
tmpfs             498976       0    498976   0% /sys/fs/cgroup
/dev/sda5          95054    1550     86336   2% /mnt/diska
/dev/sda8          95054    1550     86336   2% /mnt/diskd
/dev/sda7          95054    1550     86336   2% /mnt/diskc
/dev/sda6          95054    1550     86336   2% /mnt/diskb
/dev/sda9          95054    1550     86336   2% /mnt/diske
/dev/sda1         194235   94352     85547  53% /boot
tmpfs              99796       0     99796   0% /run/user/0

5.4开机挂载光盘到指定目录

  1. 修改/etc/fstab文件
echo "/dev/sr0 /media iso9660 defaults 0 0" >>/etc/fstab
  1. 重启系统,用df命令查看磁盘(包括可移动的)使用情况
[root@ ~ 09:44:24]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
...
/dev/sr0         4364408 4364408         0 100% /mnt/cdrom
...

5.5 parted命令进行GPT分区

删除所有的分区,并重新使用parted命令分区,分区方式使用GPT

  1. 手动删除分区(建议直接修改配置文件,)
[root@ ~ 02:49:36]#umount -v /dev/sda5 /dev/sda6 /dev/sda7 /dev/sda8 /dev/sda9   #卸载文件系统
umount: /mnt/diska (/dev/sda5) unmounted
umount: /mnt/diskb (/dev/sda6) unmounted
umount: /mnt/diskc (/dev/sda7) unmounted
umount: /mnt/diskd (/dev/sda8) unmounted
umount: /mnt/diske (/dev/sda9) unmounted
[root@ ~ 03:12:37]#fdisk /dev/sda    
Command (m for help): d    #用此操作删除五个逻辑分区
Partition number (1-5, default 5): 5
Partition 5 is deleted
....
[root@ ~ 03:15:30]#fdisk -l /dev/sda  #查看分区使用情况
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: 0x000cdaf0
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    21383167    10485760   83  Linux
/dev/sda3        21383168    25577471     2097152   82  Linux swap / Solaris

2.在虚拟机里添加一块硬盘

  • 依次点击菜单栏中的“虚拟机”->”设置“,如下图所示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TKrB7iAP-1595810871462)(assets/1.jpg)]

  • 在弹出的虚拟机设置对话框中,硬件Tab选项卡中,选择硬盘(SCSI),然后点击“添加”,如下图所示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Qhxb58A-1595810871465)(assets/2.jpg)]

  • 硬件类型选择硬盘,然后点击“下一步”,如下图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ROEkiGKY-1595810871468)(assets/3.jpg)]

  • 虚拟磁盘类型选择“SCSI(S)(推荐)”,然后点击“下一步”,如下图所示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kMIpDeQ1-1595810871473)(assets/4.jpg)]

  • 创建新虚拟机磁盘”,然后点击“下一步”,指定磁盘容量大小,并选择“将虚拟机磁盘拆分成多个文件”,然后点击“下一步”,如下图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e0FoQkCL-1595810871476)(assets/5.jpg)]

  • 点击“完成”,如下图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TtUNB4iF-1595810871478)(assets/6.jpg)]

  • 完成以上操作后,重新启动虚拟机,然后进入Linux系统,打开终端命令,输入命令
    lsblk -f,可查看系统的分区和挂载情况,我们可以看到刚才我们添加的硬盘已经出现在列表中:
[root@ ~ 03:04:51]#lsblk -f
NAME   FSTYPE  LABEL           UUID                                 MOUNTPOINT
sda                                                                 
├─sda1 ext4                    35521718-2203-497c-b690-d44622dc6704 /boot
├─sda2 ext4                    ea1d8d1c-3e80-4a6f-98cc-c83d8897ff86 /
├─sda3 swap                    60cf8279-14ea-42a1-9083-0be8cbbc9f8d [SWAP]
├─sda4                                                             
sdb                                                                 
sr0    iso9660 CentOS 7 x86_64 2018-05-03-20-55-23-00               /mnt/cdrom
[root@ ~ 03:05:01]#ls /dev/sdb
/dev/sdb
  1. 分区
[root@ ~ 03:18:48]#parted /dev/sdb        #对sdb磁盘进行分区
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p   							  #打印分区信息                                                      
Error: /dev/sdb: unrecognised disk label  #一开始提示没有设定分区方式
Model: VMware, VMware Virtual S (scsi)                             
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:     						   #没有分区
(parted) mklabel gpt  					 #设定GPT分区方式(而不是MBR)
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart 					        #对sda1分区             
Partition name?  []? sdb1
File system type?  [ext2]? 1                                      
parted: invalid token: 1
File system type?  [ext2]? ext4                                     
Start? 1                                                           
End? 101

(parted) mkpart      					  #对sda2分区               
Partition name?  []? sdb2                                           
File system type?  [ext2]? ext4                                     
Start? 101                                                         
End? 201   

(parted) mkpart        					 #对sda3分区                 
Partition name?  []? sdb3
File system type?  [ext2]? ext4                                     
Start? 201                                                         
End? 301                                                                 
(parted) mkpart       					 #对sda4分区                 
Partition name?  []? sdb4                                           
File system type?  [ext2]? ext4                                     
Start? 301                                                         
End? 401                                                                  
(parted) mkpart        					 #对sda5分区                 
Partition name?  []? sdb5                                           
File system type?  [ext2]? ext4                                     
Start? 401                                                         
End? 501                                                                  
(parted) p            					 #打印分区情况            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start   End    Size    File system  Name  Flags
 1      1049kB  101MB  99.6MB               sdb1
 2      101MB   201MB  101MB                sdb2
 3      201MB   301MB  99.6MB               sdb3
 4      301MB   401MB  99.6MB               sdb4
 5      401MB   501MB  101MB                sdb5

(parted) q            #退出                            
Information: You may need to update /etc/fstab.
  1. 查看硬盘使用情况(挂载成功)
[root@ ~ 03:32:40]#fdisk -l                                         
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
....
Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 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: gpt
Disk identifier: 3235A4FC-E8B1-4A7E-8E48-7A5B0F22A545
#         Start          End    Size  Type            Name
 1         2048       196607     95M  Microsoft basic sdb1
 2       196608       393215     96M  Microsoft basic sdb2
 3       393216       587775     95M  Microsoft basic sdb3
 4       587776       782335     95M  Microsoft basic sdb4
 5       782336       978943     96M  Microsoft basic sdb5

5.6 FTP下载镜像并挂载到本地目录

  • 本地搭建FTP服务器/地址:ftp://192.168.124.37/用户名:hehe;密码:hehe
[root@ / 08:04:29]#ftp 192.168.124.37  					 //连接
Connected to 192.168.124.37 (192.168.124.37).
220 Microsoft FTP Service
Name (192.168.124.37:root): hehe						//输入用户名
331 Password required
Password:												//输入密码
230 User logged in.
Remote system type is Windows_NT.
ftp> cd /pub/test										//访问目录
250 CWD command successful.
ftp> ls											  //查看目录下文件
227 Entering Passive Mode (192,168,124,37,27,27).
125 Data connection already open; Transfer starting.
07-26-20  11:53PM               563200 test.iso
226 Transfer complete.
ftp> get test.iso /bin/test01.iso					//下载文件到本地
local: /bin/test01.iso remote: test.iso
227 Entering Passive Mode (192,168,124,37,27,33).
125 Data connection already open; Transfer starting.
WARNING! 10284 bare linefeeds received in ASCII mode
File may not have transferred correctly.
226 Transfer complete.
563200 bytes received in 0.0107 secs (52571.64 Kbytes/sec)
ftp> bye												//关闭ftp
221 Goodbye.			

[root@ / 08:06:12]#ll -lh /bin/test01.iso				//查看文件
-rw-r--r--. 1 root root 550K Jul 27 08:05 /bin/test01.iso
  • 挂载
[root@ / 08:08:59]#mount -t iso9660 /bin/test01.iso /mnt/ftp/
mount: /dev/loop0 is write-protected, mounting read-only
[root@ / 08:09:27]#ll /mnt/ftp/     					//查看挂载情况
total 499
-r-xr-xr-x. 1 root root 510762 Jul 22 14:35 fhs-2.3.pdf

5.7问题

  1. /etc/fstab中每一行信息的最后的两个参数: dump,和pass主要是做什么用的,不是很了解。
  2. patred 命令进行GPT分区方式分配100MB,最终只有96-95M可以用?
  3. ftp文件下载成功但在本地找不到文件?
  • 已解决:先关闭防火墙+查看并修改“下载到指定目录”的权限设置