硬盘设备是由大量的扇区组成,每个扇区容量512字节,第一扇区最为重要,保存主引导记录和分区表信息。分别是主引导记录占用446,分区表占用64字节,结束符2字节,其中分区表中每记录一个信息占用16字节,最多只有四个分区,为了解决分区数不够,衍生出扩展分区。扩展分区可以用来创建逻辑分区,逻辑分区最多128个。
硬件设备 | 文件名称 |
---|---|
IDE设备 | /dev/hd[a-d] |
SCSI/SATA/U盘/SAS | /dev/sd[a-p] |
软驱 | /dev/fd[0-1] |
打印机 | /dev/lp[0-15] |
光驱 | /dev/cdrom |
鼠标 | /dev/mouse |
磁带机 | /dev/st0或/dev/ht0 |
IDE硬盘接口在Linux中设备名为/dev/hda,SAS、SCSI、SATA硬盘接口在Linux中设备名为sda,高效云盘硬盘接口会识别为/dev/vda等。
硬盘的最小存储单位叫做Sector(扇区),每个Sector储存512字节。操作系统在读取硬盘的时候,不会逐个Sector的去读取,这样效率非常低,为了提升读取效率,操作系统会一次性连续读取多个Sector,即一次性读取多个Sector称为一个Block(块)。
由多个Sector组成的Block是文件存取的最小单位。Block的大小常见的有1KB、2KB、4KB,Block在Linux中常设置为4KB,即连续八个Sector组成一个Block。
硬盘分区分别分成(superblock)、inode、block,
Superblock:文件系统的整体信息,包含inode值和block块的使用的大小、剩余大小,相关的格式及信息。(记录inode和block的对应关系)
Inode:记录文件的属性和信息,同时会把文件的存储的位置生成一个编号(inode值:记录数据的存储位置(block在分区中所在位置))
Block块是存储文件的内容。因为每个 inode 与 block 都有编号,而每个文件都会占用一个 inode ,inode 内则有文件数据放置的 block 号码。如果能够找到文件的 inode,就可以找到该文件所放置数据的block号码,从而读取该文件内容。
Parted 分区格式:gpt
1)将硬盘所有内容分到一个分区
将2.4T 硬盘分区操作
-1 参数:代表剩下的所有空间。
Parted工具中的mklabel 和mkpart 参数
Mount常用案例演示如下:
mount /dev/sdb1 /data 挂载/dev/sdb1分区至/data/目录
mount /dev/cdrom /mnt 挂载Cdrom光盘至/mnt目录;
mount -t ntfs-3g /dev/sdc /data1 挂载/dev/sdc移动硬盘至/data1目录;
mount -o remount,rw / 重新以读写模式挂载/系统;
mount -t iso9660 -o loop centos7.iso /mnt 将centos7.iso镜像文件挂载至/mnt目录;
mount -t fat32 /dev/sdd1 /mnt 将U盘/dev/sdd1挂载至/mnt/目录;
mount -t nfs 192.168.1.11:/data/ /mnt 将远程192.168.1.11:/data目录挂载至本地/mnt目录。
1.3 文件系统详解
dd 命令创建一个空文件,if指定一个来源/dev/zero(/dev/zero文件提供源源不断的“0”数据),of指定存储文件,bs=定义块的大小,count=定义块的数量
[root@localhost ~]# dd if=/dev/zero of=/root/test bs=100M count=20
20+0 records in
20+0 records out
2097152000 bytes (2.1 GB) copied, 42.9902 s, 48.8 MB/s
[root@localhost ~]# mkswap /root/test
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=13db4d1a-eb96-49fc-8e29-33e51c660ea5
[root@localhost ~]# free -hm
total used free shared buff/cache available
Mem: 976M 113M 170M 6.7M 692M 686M
Swap: 2.0G 0B 2.0G
[root@localhost ~]# swapon /root/test
swapon: /root/test: insecure permissions 0644, 0600 suggested.
[root@localhost ~]# free -hm
total used free shared buff/cache available
Mem: 976M 115M 168M 6.7M 692M 684M
Swap: 4.0G 0B 4.0G
[root@localhost ~]#
[root@localhost ~]# swapoff /root/test
[root@localhost ~]#
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 976 114 168 6 693 685
Swap: 2047 0 2047
1. 创建一个XFS文件系统
[root@localhost ~]# mkfs.xfs -f /dev/sdb1meta-data=/dev/sdb1
[root@localhost ~]# cat /etc/fstab |grep sdsb1/dev/sdb1 /test xfs defaults 0 0
[root@localhost ~]# mount -a[root@localhost ~]# df -h
/dev/sdb1 30G 610M 30G 2% /test
2. 卸载文件系统/test, 并且破坏superblock
[root@localhost ~]# umount /test
[root@localhost ~]# dd if=/dev/zero of=/dev/sdb1 bs=512 count=1
1+0 records in
1+0 records out512 bytes (512 B) copied, 0.00251096 s, 204 kB/s
3. 再次挂载报错
[root@localhost ~]# mount -a
mount: wrong fs type, bad option, bad superblock on /dev/sdb1,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so
[root@localhost ~]# dmesg
[22597379.623449] XFS (sdb1): bad magic number[22597379.624372] XFS (sdb1): SB validate failed
4. 恢复superblock
[root@localhost ~]# xfs_repair /dev/sdb1
Phase 1 - find and verify superblock...bad primary superblock -
bad magic number !!!
attempting to find secondary superblock......
......found candidate secondary superblock...
verified secondary superblock...
writing modified primary superblock
sb realtime bitmap inode 18446744073709551615 (NULLFSINO)
inconsistent with calculated value 129
resetting superblock realtime bitmap ino pointer to 129
sb realtime summary inode 18446744073709551615 (NULLFSINO)
inconsistent with calculated value 130
resetting superblock realtime summary ino pointer to 130
Phase 2 - using internal log
- zero log...
- scan filesystem freespace and inode maps...
sb_icount 0, counted 64
sb_ifree 0, counted 60
sb_fdblocks 7860552, counted 7712768
- found root inode chunkPhase 3 - for each AG...
- scan and clear agi unlinked lists...
- process known inodes and perform inode discovery...
- agno = 0
- agno = 1
- agno = 2
- agno = 3
- process newly discovered inodes...
Phase 4 - check for duplicate blocks...
- setting up duplicate extent list...
- check for inodes claiming duplicate blocks...
- agno = 0
- agno = 1
- agno = 2
- agno = 3
Phase 5 - rebuild AG headers and trees...
- reset superblock...Phase 6 - check inode connectivity...
- resetting contents of realtime bitmap and summary inodes
- traversing filesystem ...
- traversal finished ...
- moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
Note - stripe unit (0) and width (0) fields have been reset.
Please set with mount -o sunit=<value>,swidth=<value>done
5. 确认是否已经恢复
[root@localhost ~]# mount -a
[root@localhost ~]# df -h
/dev/sdb1 30G 610M 30G 2% /test
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.7G 16G 10% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 127M 888M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb 20G 33M 20G 1% /test
/dev/sdc1 20G 44M 19G 1% /test1
[root@localhost ~]# umount /test1
[root@localhost ~]# !dd
dd if=/dev/zero of=/dev/sdc1 bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 1.72432 s, 608 MB/s
[root@localhost ~]# mount -a
mount: wrong fs type, bad option, bad superblock on /dev/sdc1,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
[root@localhost ~]# fsck -t ext4 /dev/sdc1
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
ext2fs_open2: Bad magic number in super-block
fsck.ext4: Superblock invalid, trying backup blocks...
fsck.ext4: Bad magic number in super-block while trying to open /dev/sdc1
The superblock could not be read or does not describe a correct ext2
filesystem. If the device is valid and it really contains an ext2
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck -b 8193 <device>
[root@localhost ~]# mke2fs -S /dev/sdc1
-S 仅写入superblock与group descriptors
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
1310720 inodes, 5242631 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/sdc1
mount: mount /dev/sdc1 on /test1 failed: Stale file handle
[root@localhost ~]#
[root@localhost ~]# fsck -t ext4 -y /dev/sdc1
检查特定类型的文件系统(ext4),-y检测完成之后并自动修复
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
/dev/sdc1 contains a file system with errors, check forced.
Resize inode not valid. Recreate? yes
Pass 1: Checking inodes, blocks, and sizes
Root inode is not a directory. Clear? yes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Root inode not allocated. Allocate? ye
/lost+found not found. Create? yes
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences: +(0--1539) +(32768--34306) +(65536--66049) +(98304--99842) +(131072--131585) +(163840--165378) +(196608--197121) +(229376--230914)
Fix? yes
Free blocks count wrong for group #0 (31227, counted=31226).
Fix? yes
Free blocks count wrong (5149114, counted=5149113).
Fix? yes
Inode bitmap differences: +1 +(3--10)
Fix? yes
Free inodes count wrong for group #0 (8191, counted=8181).
Fix? yes
Directories count wrong for group #0 (1, counted=2).
Fix? yes
Free inodes count wrong (1310719, counted=1310709).
Fix? yes
/dev/sdc1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdc1: 11/1310720 files (0.0% non-contiguous), 93518/5242631 block
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.7G 16G 10% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 127M 888M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb 20G 33M 20G 1% /test
/dev/sdc1 20G 44M 19G 1% /test1
努力学习,勤奋工作,让青春更加光彩
再长的路,一步步也能走完,再短的路,不迈开双脚也无法到达