1-14 文件系统的特性与磁盘加密技术

本节所讲内容:

1-1 硬盘,文件系统的结构组成

硬盘结构详解

文件系统结构的组成

实战-怎样创建硬链接和软链接

 

1-2 真正的了解xfs与ext文件系统的特性与区别

xfs和ext 文件系统的区别

实战-磁盘的加密技术.

 

============================================

1-1 硬盘,文件系统的结构组成

MBR(主引导记录(Master Boot Record))

位于硬盘第一个物理扇区柱面0,磁头0,扇区1处

 

硬盘结构详解

硬盘主要包括:盘片、磁头、盘片主轴、控制电机马达、磁头控制器、数据转换器、接口、缓存等几个部份

 

磁盘的存储顺序是:由外到内存储的

 

 

磁头

磁头是硬盘中最昂贵的部件,也是硬盘技术中最重要和最关键的一环。硬盘的读、写操作.

磁道:在盘片上,一圈一圈的那些就叫做磁道,(由很多扇区连接在一起,组成的一圈叫做磁道)

当磁盘旋转时,磁头若保持在一个位置上,则每个磁头都会在磁盘表面划出一个圆形轨迹,这些圆形轨迹就叫做磁道。

磁道由外向内从“0”开始顺序编号,柱面编号与磁道号相同;磁道上的扇区从“1”开 始编号;盘面从上到下从“0”开始依次编号,磁头号与盘面号相同

扇区:磁盘上的磁道,被分割成若干个扇区,每个扇区的大小为512字节,最小读写单元

柱面:

 

(这点没懂,一个磁盘有几个盘面?!)

硬盘的内部是不能沾染灰尘的,否则立即报废。

所谓硬盘的CHS,即Cylinder(柱面)、Head(磁头)、Sector(扇区),只要知道了硬盘的CHS的数目,即可确定硬盘的容量,

硬盘的容量=柱面数*磁头数*扇区数*512B。

 

文件系统结构的组成

Linux系统中,文件系统由三部分构成:文件名、inode、block

在创建文件时:

首先创建文件名,然后,这个文件名会随机生成一个inode号,inode号,对应磁盘存储数据的block。(只有block中才会存有数据)

 

#新建一个文件,查看其信息

[root@xiaogan ~]# touch a.txt
[root@xiaogan ~]# stat a.txt #查看文件信息
 File: ‘a.txt’
 Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 34081997    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-08-16 22:51:46.420038018 +0800
Modify: 2016-08-16 22:51:46.420038018 +0800
Change: 2016-08-16 22:51:46.420038018 +0800
Birth: -
[root@xiaogan ~]#

 

SIZE=文件的大小(字节数)

Blocks=文件的大小 (占用块数量)

UID=文件拥有者的UID

GID=文件的GID

Access=文件的权限(读写执行)

Access=atime :访问的时间

Ctime=ctime:发生变动的时间

Mtime=Modify:文件修改的时间

df -i  #查看所有分区inode结点数量

查看每个硬盘分区的inode总数和已经使用的数量,可以使用df命令。

 

 

ls -i  #查看文件结点号

 

 

ls -di #查看目录结点号

 

 

ls -lc filename #ctime 查看文件发生变动的时间 

ctime (time of last modification of file status information)

ls -lu filename # atime  查看文件访问时间

ls -l  filename  #mtime 查看文件修改时间

[root@xiaogan ~]# ls -lc /etc/passwd
-rw-r--r-- 1 root root 2362 8月  14 00:26 /etc/passwd
[root@xiaogan ~]# ls -lu /etc/passwd
-rw-r--r-- 1 root root 2362 8月  16 22:57 /etc/passwd
[root@xiaogan ~]# ls -l /etc/passwd
-rw-r--r-- 1 root root 2362 8月  14 00:26 /etc/passwd

 

 

RHEL7.2 如何设置block

在文件系统中,文件是存储在block中的,每个block大小是固定的,

比如,block=2M,那么一个9M的文件存储在这个设备中,就需要占用5个block,实际占用空间为10M,那么就存在1M的空间浪费。

所以,block设置过大,会导致磁盘空间浪费,但是设置过小,会导致文件拷贝过慢。所以应根据需求,合理设置磁盘块大小

在对分区进行格式化时,使用-b选项进行设置:

默认:

[root@xiaogan ~]# ls /dev/sdb
/dev/sdb
[root@xiaogan ~]# fdisk /dev/sdb
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.
 
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x8225fe63.
 
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): +2G
Partition 1 of type Linux and of size 2 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: 0x8225fe63
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@xiaogan ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1
[root@xiaogan ~]# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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
[root@xiaogan ~]# 
 
#我们可以看到,系统默认的块大小是4096字节,4K
#设置1024字节:
[root@xiaogan ~]# mkfs -t xfs -b size=1024 /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@xiaogan ~]# mkfs -t xfs -b size=1024 /dev/sdb1 -f
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=524288 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=1024   blocks=2097152, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=1024   blocks=10240, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@xiaogan ~]# 

 

 

 

 

实战-怎样创建硬链接和软链接

Linux系统中,连接存在两种模式,硬链接(Hard Link),符号连接(又叫软连接Symbolic Link)。我们使用ln命令来创建链接文件,默认情况下,ln命令创建的连接为硬链接。

【硬链接】硬链接指通过索引结点来进行的连接,即他们拥有不同的索引节点,但是他们使用的是同一块存储空间。

在删除文件时,若该文件还存在硬链接,那么不会直接删除文件,而是删除对应文件的索引结点,因此,我们可以使用这一点,对重要文件进行备份。需要注意的是,硬链接不能跨越分区。

硬链接---->创建 不支持目录

[在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。

在Linux中,多个文件名指向同一索引节点是存在的。一般这种连接就是硬连接。

硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。

其原因:因为对应该目录的索引节点有一个以上的连接。

只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。]

【软连接】符号连接,就像是windows中的快捷方式一样,是一个特殊的文件

符号连接(Symbolic Link),也叫软连接。软链接文件有类似于Windows的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。

1、创建硬链接

[root@xiaogan ~]# ls
a.txt  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  test  usr  Videos
[root@xiaogan ~]# ln a.txt b.txt
[root@xiaogan ~]# ll a.txt b.txt 
-rw-r--r-- 2 root root 0 8月  16 22:51 a.txt
-rw-r--r-- 2 root root 0 8月  16 22:51 b.txt
[root@xiaogan ~]# echo 123 > a.txt
[root@xiaogan ~]# cat a.txt 
123
[root@xiaogan ~]# cat b.txt 
123
[root@xiaogan ~]# 

 

 

2、尝试跨分区创建硬链接

[root@xiaogan ~]# ln a.txt /boot/c.txt
ln: failed to create hard link ‘/boot/c.txt’ => ‘a.txt’: Invalid cross-device link
[root@xiaogan ~]# 

#创建失败

 

 

3、尝试为目录创建硬链接

 

 

4、创建软连接

[root@xiaogan ~]# ln -s a.txt c.txt
[root@xiaogan ~]# ll a.txt c.txt 
-rw-r--r-- 2 root root 4 8月  16 23:24 a.txt
lrwxrwxrwx 1 root root 5 8月  16 23:27 c.txt -> a.txt
[root@xiaogan ~]# 

5.尝试为目录创建软连接

 

6、尝试跨分区创建软连接

在跨分区创建软连接时,必须使用绝对路径,不然会创建失败!!

 

1-2 真正的了解xfs与ext文件系统的特性与区别

 xfs和ext 文件系统的区别

 xfs文件系统比ext文件系统的强的方面:

1.数据完整性

采用XFS文件系统,当意想不到的宕机发生后,由于文件系统开启了日志功能,所以磁盘上的文件不再会意外宕机而遭到破坏,不论目前文件系统上存储的文件与数据有多少,文件系统都可以根据所记录的日志在很短的时间内迅速恢复磁盘文件内容

2.传输特性

xfs文件系统采用优化算法,日志记录对整体文件操作影响非常小。xfs查询与分配存储空间非常快。xfs文件系统能连续提供快速的反应时间。

3.可扩展性

xfs是一个全64-bit的文件系统,它可以支持上百万T字节的存储空间。对特大文件及小尺寸文件的支持都表现出众,支持特大数量的目录。最大可支持的文件大小为 9EB,最大文系统尺寸为18EB

(1EB=1024PB=1024*1024TB)

文件系统

最大文件系统(TB)

最大文件(TB)

xfs

18874368

9437184

ext4

1048576

16

ext3

16

2

4.传输带宽

XFS 能以接近裸设备I/O的性能存储数据。在单个文件系统的测试中,其吞吐量最高可达7GB每秒,对单个文件的读写操作,其吞吐量可达4GB每秒。

 

实战-磁盘的加密技术.

LUKS(Linux Unified Key Setup)为Linux硬盘加密提供了一种标准

Linux加密设置

工具:cryptsetup(默认已经安装)

常用参数:luksFormat 加密、luksOpen 打开映射、luksClose 关闭映射、luksAddKey 添加密钥

使用cryptsetup对分区进行了加密后,这个分区就不再允许直接挂载。LUKS也是一种基于device mapper 机制的加密方案。如果要使用这个分区,必须对这个分区做一个映射,映射到/dev/mapper这个目录里去,我们只能挂载这个映射才能使用。然而做映射的时候是需要输入解密密码的。

1.   创建分区并加密分区

2.   映射分区

3.   格式化分区并挂载使用

第一步:检测工具有没有安装

rpm -qf $(which cryptsetup)

[root@xiaogan ~]# rpm -qf $(which cryptsetup)
cryptsetup-1.6.7-1.el7.x86_64
[root@xiaogan ~]# 

若没有安装可使用rpm或yum命令对其进行安装

yum install cryptsetup
or
rpm -ivh /mnt/Packages/cryptsetup-1.6.7-el7.x86_64.rpm

 

第二步:新建一个磁盘分区

[root@xiaogan ~]# fdisk /dev/sdb
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 (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (4196352-41943039, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +2G
Partition 2 of type Linux and of size 2 GiB is set
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@xiaogan ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2
[root@xiaogan ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
第三步:对新建的分区加密
设置密码,需要复杂度(>8位),YES大写
[root@xiaogan ~]# cryptsetup luksFormat /dev/sdb2
 
WARNING!
========
This will overwrite data on /dev/sdb2 irrevocably.
 
Are you sure? (Type uppercase yes): YES
Enter passphrase: 
Verify passphrase: 
[root@xiaogan ~]# 
第四步:映射磁盘分区
[root@xiaogan ~]# cryptsetup luksOpen /dev/sdb2 gan_disk
Enter passphrase for /dev/sdb2: 
[root@xiaogan ~]# ls /dev/mapper/
control  gan_disk  rhel-root  rhel-swap
[root@xiaogan ~]# 


第五步:对映射的分区进行格式化操作

[root@xiaogan ~]# mkfs.ext4 /dev/mapper/gan_disk 
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
131072 inodes, 523776 blocks
26188 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
      32768, 98304, 163840, 229376, 294912
 
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 
 
[root@xiaogan ~]# 

 

  验证磁盘分区:

[root@xiaogan ~]# cryptsetup status /dev/mapper/gan_disk 
/dev/mapper/gan_disk is active and is in use.
  type:    LUKS1
  cipher:  aes-xts-plain64
  keysize: 256 bits
  device:  /dev/sdb2
  offset:  4096 sectors
  size:    4190208 sectors
  mode:    read/write
[root@xiaogan ~]# 

 

 

第六步:挂载加密分区

[root@xiaogan ~]# mkdir /disk
[root@xiaogan ~]# mount /dev/mapper/gan_disk /disk
[root@xiaogan ~]# df | tail -1
/dev/mapper/gan_disk    2029392    6144   1902112   1% /disk
[root@xiaogan ~]# 

 

 

 到这里,我们就可以对加密分区进行读写操作了。

第七步:设置开机自动挂载

使用手动输入密码方式,设置开机自动挂载

[root@xiaogan ~]# echo "/dev/mapper/gan_disk /dev/sdb2" > /etc/crypttab  #配置crypt文件
[root@xiaogan ~]# cat /etc/crypttab 
/dev/mapper/gan_disk /dev/sdb2
[root@xiaogan ~]# echo "/dev/mapper/gan_disk /disk ext4 defaults 0 0" >> /etc/fstab #设置开机启动
[root@xiaogan ~]# tail -1 /etc/fstab 
/dev/mapper/gan_disk /disk ext4 defaults 0 0

重启,进入如下界面

 

 成功,输入密码,即可!!!

使用自动输入密码模式,设置开机自动挂载

[root@xiaogan ~]# echo "xiaogan123" > /root/passwd.txt 
[root@xiaogan ~]# cat /root/passwd.txt 
xiaogan123
[root@xiaogan ~]# vim /etc/crypttab 
[root@xiaogan ~]# cat /etc/crypttab 
gan_disk /dev/sdb2 /root/passwd.txt
[root@xiaogan ~]# cryptsetup luksAddKey /dev/sdb2 /root/passwd.txt 
Enter any passphrase: 
[root@xiaogan ~]# 

重启,看看,是不是不用输入密码了?!

[root@xiaogan ~]# cryptsetup --help
cryptsetup 1.6.7
Usage: cryptsetup [OPTION...] <action> <action-specific>
  --version                         Print package version
  -v, --verbose                     Shows more detailed error messages
  --debug                           Show debug messages
  -c, --cipher=STRING               The cipher used to encrypt the disk (see /proc/crypto)
 -h, --hash=STRING                 The hash used to create the encryption key from the
                                   passphrase
  -y, --verify-passphrase           Verifies the passphrase by asking for it twice
  -d, --key-file=STRING             Read the key from a file.
  --master-key-file=STRING          Read the volume (master) key from file.
  --dump-master-key                 Dump volume (master) key instead of keyslots info.
  -s, --key-size=BITS               The size of the encryption key
  -l, --keyfile-size=bytes          Limits the read from keyfile
  --keyfile-offset=bytes            Number of bytes to skip in keyfile
  --new-keyfile-size=bytes          Limits the read from newly added keyfile
  --new-keyfile-offset=bytes        Number of bytes to skip in newly added keyfile
  -S, --key-slot=INT                Slot number for new key (default is first free)
  -b, --size=SECTORS                The size of the device
  -o, --offset=SECTORS              The start offset in the backend device
  -p, --skip=SECTORS                How many sectors of the encrypted data to skip at the
                                    beginning
  -r, --readonly                    Create a readonly mapping
  -i, --iter-time=msecs             PBKDF2 iteration time for LUKS (in ms)
  -q, --batch-mode                  Do not ask for confirmation
  -t, --timeout=secs                Timeout for interactive passphrase prompt (in seconds)
  -T, --tries=INT                   How often the input of the passphrase can be retried
  --align-payload=SECTORS           Align payload at <n> sector boundaries - for luksFormat
  --header-backup-file=STRING       File with LUKS header and keyslots backup.
  --use-random                      Use /dev/random for generating volume key.
  --use-urandom                     Use /dev/urandom for generating volume key.
  --shared                          Share device with another non-overlapping crypt segment.
  --uuid=STRING                     UUID for device to use.
  --allow-discards                  Allow discards (aka TRIM) requests for device.
  --header=STRING                   Device or file with separated LUKS header.
  --test-passphrase                 Do not activate device, just check passphrase.
  --tcrypt-hidden                   Use hidden header (hidden TCRYPT device).
  --tcrypt-system                   Device is system TCRYPT drive (with bootloader).
  --tcrypt-backup                   Use backup (secondary) TCRYPT header.
  --veracrypt                       Scan also for VeraCrypt compatible device.
  -M, --type=STRING                 Type of device metadata: luks, plain, loopaes, tcrypt.
  --force-password                  Disable password quality check (if enabled).
  --perf-same_cpu_crypt             Use dm-crypt same_cpu_crypt performance compatibility
                                    option.
  --perf-submit_from_crypt_cpus     Use dm-crypt submit_from_crypt_cpus performance
                                    compatibility option.
 
Help options:
  -?, --help                        Show this help message
  --usage                           Display brief usage
 
<action> is one of:
      open <device> [--type <type>] [<name>] - open device as mapping <name>
      close <name> - close device (remove mapping)
      resize <name> - resize active device
      status <name> - show device status
      benchmark <name> - benchmark cipher
      repair <device> - try to repair on-disk metadata
      erase <device> - erase all keyslots (remove encryption key)
      luksFormat <device> [<new key file>] - formats a LUKS device
      luksAddKey <device> [<new key file>] - add key to LUKS device
      luksRemoveKey <device> [<key file>] - removes supplied key or key file from LUKS device
      luksChangeKey <device> [<key file>] - changes supplied key or key file of LUKS device
      luksKillSlot <device> <key slot> - wipes key with number <key slot> from LUKS device
      luksUUID <device> - print UUID of LUKS device
      isLuks <device> - tests <device> for LUKS partition header
      luksDump <device> - dump LUKS partition information
      tcryptDump <device> - dump TCRYPT device information
      luksSuspend <device> - Suspend LUKS device and wipe key (all IOs are frozen).
      luksResume <device> - Resume suspended LUKS device.
      luksHeaderBackup <device> - Backup LUKS device header and keyslots
      luksHeaderRestore <device> - Restore LUKS device header and keyslots
 
You can also use old <action> syntax aliases:
      open: create (plainOpen), luksOpen, loopaesOpen, tcryptOpen
      close: remove (plainClose), luksClose, loopaesClose, tcryptClose
 
<name> is the device to create under /dev/mapper
<device> is the encrypted device
<key slot> is the LUKS key slot number to modify
<key file> optional key file for the new key for luksAddKey action
 
Default compiled-in key and passphrase parameters:
      Maximum keyfile size: 8192kB, Maximum interactive passphrase length 512 (characters)
Default PBKDF2 iteration time for LUKS: 1000 (ms)
 
Default compiled-in device cipher parameters:
      loop-AES: aes, Key 256 bits
      plain: aes-cbc-essiv:sha256, Key: 256 bits, Password hashing: ripemd160
      LUKS1: aes-xts-plain64, Key: 256 bits, LUKS header hashing: sha1, RNG: /dev/urandom

 第八步、关闭映射,先卸载后关闭 

 

 

posted @ 2016-08-20 20:23  小甘丶  阅读(1196)  评论(0编辑  收藏  举报