Linux 格式化磁盘,制作文件系统 => 报错: dev/sdb is NOT identified.

前言

在学习 ARM S5PV210 开发板的时候, SD 卡烧录时总是失败,报错信息:

dev/sdb is NOT identified.

经过实践,发现格式化 SD 卡可以解决该问题;或者使用命令 ls /dev/sdb 发现 sdb 块设备的文件类型不是 b(块设备),此时 rm -rf /dev/sdb 删除,然后重新插入 SD 卡,也能解决问题。


一、准备工作

1. 转换成 root 身份:

ubuntu:~$ sudo su  
# 之后输入 root 用户密码
# 如果没有 root 用户/密码,可以使用下面的命令添加 root 用户/密码
ubuntu:~$ sudo passwd root
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
ubuntu:~$ 

2. 查看当前系统的磁盘数据

root@ubuntu:/home# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 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 identifier: 0x000e00c0

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    33554431    16776192   83  Linux
/dev/sda2        33556478    41940991     4192257    5  Extended
/dev/sda5        33556480    41940991     4192256   82  Linux swap / Solaris
root@ubuntu:/home# 

一般的,sda 盘就是 Linux 操作系统文件所在的那个盘。

我们可以验证下,sda 一个扇区是 512 bytes,总共 41943040 sectors 扇区,因此 sda 硬盘的总空间就是: 512 x 41943040 = 21,474,836,480 bytes,刚好和 fdisk -l 命令统计的数据一样。

底部的 device 信息详解:

  • /dev/sda :第 a 块硬盘。
  • /dev/sda1 :第 a 块硬盘设备的第 1 个分区。
  • Boot:是不是启动分区。
  • Start:起始的扇区号。
  • End:最终的扇区号。
  • ID:分区类型的编号(83 对应 Linux)。
  • System:分区类型编号对应的文字描述。

3. 关闭虚拟机,在虚拟机控制台增加一个硬盘。

在这里插入图片描述


4. 重启虚拟机,再利用 fdisk -l 命令观察数据变化。

在这里插入图片描述


二、MBR 分区

1. 为了消除 “Disk /dev/sdb doesn’t contain a valid partition table” 的警告,我们需要为 /dev/sdb 硬盘创建一个分区表。

将 /dev/sdb 硬盘设置为 MBR 分区(disklable type),并创建一个分区。


2. 使用 fdisk /dev/sdb 命令,对该硬盘做操作;输入 “m” 可以看到命令帮助信息。

在这里插入图片描述

输入"l" 列出支持的分区类型。

在这里插入图片描述

输入 “p”, 打印当前已有的分区表。

在这里插入图片描述


输入 “n”,新建一个分区。

在这里插入图片描述


最后输入 “w”,保存信息到硬盘。

在这里插入图片描述


以后输入 fdisk -l 命令,就没有那个 “Disk /dev/sdb doesn’t contain a valid partition table” 的警告了。

在这里插入图片描述


3. 为了系统地分析硬盘分区表,我们重新对 /dev/sdb 硬盘重新新建一个分区。

在这里插入图片描述

在这里插入图片描述

分区的起始扇区号为 2048,前面 0 ~ 2047 扇区为保留扇区,第 0 号扇区为 MBR。

先输入 w,保存我们设置的硬盘分区信息。

在这里插入图片描述


观察 /dev/sdb 是否符合 MBR 的特征:主引导记录(MBR)是硬盘驱动器上的第一个扇区,MBR 包含引导程序代码(440字节),可能还包含其他一些信息,紧接着是 64 字节的分区表和一个 2 字节的引导签名。64 字节的分区表有 4 个 16 字节的条目,从偏移量(446,即 1BEh )开始。下表给出了每个 16 字节条目的布局。

偏移量(十六进制)长度描述
0h1状态。80h表示活动(或可引导)的分区。
1h3分区中第一个绝对扇区的CHS(柱面-磁头-扇区)地址
4h1分区类型。
5h3分区中最后一个绝对扇区的CHS(柱面-磁头-扇区)地址
8h4分区中第一个绝对扇区的逻辑块地址(LBA)。
Ch4分区中的扇区数量
root@ubuntu:/home/aston# # 通过下面的命令,将MBR以16进制形式打印出来
root@ubuntu:/home/aston# dd if=/dev/sdb bs=512 count=1 2>/dev/null | hexdump -C
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  2a dd 85 80 00 00 00 3a  |........*......:|
000001c0  13 00 83 40 52 be 00 08  00 00 00 f8 7f 00 00 00  |...@R...........|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200
root@ubuntu:/home/aston# 

在这里插入图片描述


为什么我们在新建分区的时候,最多只能选择 1~4分区?原因就是一个分区表 16 字节,在第 0 扇区总共只有 64 字节的分区表,所以只能有 4 个分区。

在这里插入图片描述
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


使用下面的命令,只显示 64 字节的分区表信息:

由于只有1个扇区,所以除了第一行 16 字节,其他都是 0。
在这里插入图片描述


三、GPT 分区

1.

在这里插入图片描述


2.

扇区按 LBA 模式编排,第 0 号扇区存放一个叫 Protective MBR 的数据结构,是为了兼容 MBR 分区,里面的分区类型为 0xEE,不支持 GPT 的系统读到这个标志就会报错。


3.

每个分区信息占用 128 字节,因此 1 个扇区可以存放 4 个分区信息,从 2~33号扇区都是存放分区信息的,故 GPT 可以支持最大 128 个分区。


4.

扇区号使用 8 字节进行编制,因此每个分区最大支持容量为:2^64 * 512 bytes = 8 Zbytes。(注:TB->PB->ZB->YB->BB->NB->DB)


5.

实践演练。

输入 fdisk /dev/sdb ,对 sdb 硬盘做改动。

输入 g, 表示创建 GPT 分区。

在这里插入图片描述


这里 sdb 硬盘是 4G大小,我将该硬盘分为两部分,每部分都是 2 G。

在这里插入图片描述


现在,我改变第 2 个分区为 Windows 类型分区。

在这里插入图片描述

查找分区类型的编码,选择 11 类型的分区。

在这里插入图片描述


最后,打印分区信息,并写入到硬盘。

在这里插入图片描述

在这里插入图片描述


四、格式化

1. 任务:构建文件系统(高级格式化)

2. 命令 mkfs

当前 sdb 硬盘有两个分区,sdb1 和 sdb2。mkfs 命令支持多种文件系统的创建。

在这里插入图片描述


在这里插入图片描述


五、挂载

1. 目的:为了让分区可用

2. 命令: mount

一般挂载的地方有两个,一个是 /mnt 下,另一个是 /media 下。

root@backvm-virtual-machine:backvm# ls /mnt/
hgfs
root@backvm-virtual-machine:backvm# ls /media/
backvm  guest-ana787  guest-mpypdy  guest-x8d6jn  root
root@backvm-virtual-machine:backvm# 

在这里插入图片描述


3. 分区挂载情况

命令: lsblk -f

在这里插入图片描述


在这里插入图片描述


可以看到,挂载后的分区能够正常使用了。

在这里插入图片描述


六、 Linux 文件系统

1. 树型目录结构

在这里插入图片描述


2. 文件占用空间的大小

在这里插入图片描述

疑问:text 文件大小只有 12 字节,但是却占用了 4k 字节的磁盘空间。

Cluster:簇。文件系统是以簇为单位进行空间分配的。簇的大小是可以调节的。


3. 查看文件的目录项

root@backvm-virtual-machine:linux# stat text 
  文件:'text'
#  实际大小       占用的扇区数量    簇的大小  
  大小:12        	块:8          IO 块:4096   普通文件
#                Inode 编号         链接数  
设备:811h/2065d	 Inode:12          硬链接:1
#                       用户编号                 组编号   
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2023-03-27 00:56:52.096207237 +0800
最近更改:2023-03-27 00:19:35.808679449 +0800
最近改动:2023-03-27 00:19:35.808679449 +0800
创建时间:-

在这里插入图片描述


4. 使用 debugfs 观察文件的扇区内容。

root@backvm-virtual-machine:linux# debugfs /dev/sdb1
debugfs 1.42.13 (17-May-2015)
debugfs:  help
Available debugfs requests:

show_debugfs_params, params
                         Show debugfs parameters
open_filesys, open       Open a filesystem
close_filesys, close     Close the filesystem
freefrag, e2freefrag     Report free space fragmentation
feature, features        Set/print superblock features
dirty_filesys, dirty     Mark the filesystem as dirty
init_filesys             Initialize a filesystem (DESTROYS DATA)
show_super_stats, stats  Show superblock statistics
ncheck                   Do inode->name translation
icheck                   Do block->inode translation
change_root_directory, chroot
                         Change root directory
change_working_directory, cd
                         Change working directory
list_directory, ls       List directory
show_inode_info, stat    Show inode information 
dump_extents, extents, ex
                         Dump extents information 
blocks                   Dump blocks used by an inode 
filefrag                 Report fragmentation information for an inode
link, ln                 Create directory link
unlink                   Delete a directory link
mkdir                    Create a directory
rmdir                    Remove a directory
rm                       Remove a file (unlink and kill_file, if appropriate)
kill_file                Deallocate an inode and its blocks
clri                     Clear an inode's contents
freei                    Clear an inode's in-use flag
seti                     Set an inode's in-use flag
testi                    Test an inode's in-use flag
freeb                    Clear a block's in-use flag
setb                     Set a block's in-use flag
testb                    Test a block's in-use flag
modify_inode, mi         Modify an inode by structure
find_free_block, ffb     Find free block(s)
find_free_inode, ffi     Find free inode(s)
print_working_directory, pwd
                         Print current working directory
expand_dir, expand       Expand directory
mknod                    Create a special file
list_deleted_inodes, lsdel
                         List deleted inodes
undelete, undel          Undelete file
write                    Copy a file from your native filesystem
dump_inode, dump         Dump an inode out to a file
cat                      Dump an inode out to stdout
lcd                      Change the current directory on your native filesystem
rdump                    Recursively dump a directory to the native filesystem
set_super_value, ssv     Set superblock value
set_inode_field, sif     Set inode field
set_block_group, set_bg  Set block group descriptor field
logdump                  Dump the contents of the journal
htree_dump, htree        Dump a hash-indexed directory
dx_hash, hash            Calculate the directory hash of a filename
dirsearch                Search a directory for a particular filename
bmap                     Calculate the logical->physical block mapping for an inode
punch, truncate          Punch (or truncate) blocks from an inode by deallocating them
symlink                  Create a symbolic link
imap                     Calculate the location of an inode
dump_unused              Dump unused blocks
set_current_time         Set current time to use when setting filesystem fields
supported_features       Print features supported by this version of e2fsprogs
dump_mmp                 Dump MMP information
set_mmp_value, smmp      Set MMP value
extent_open, eo          Open inode for extent manipulation
zap_block, zap           Zap block: fill with 0, pattern, flip bits etc.
block_dump, bdump, bd    Dump contents of a block
list_quota, lq           List quota
get_quota, gq            Get quota
inode_dump, idump, id    Dump the inode structure in hex
help                     Display info on command or topic.
list_requests, lr, ?     List available commands.
quit, q                  Leave the subsystem.
debugfs:  
debugfs:  
debugfs:  

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值