ubuntu下格式化U盘

问题

某个U盘(4GB容量),在我制作一个U盘启动后,然后被我格式化掉了,出现了只有248MB的容量,在Windows下怎么格式化都还是248MB。于是想到在ubuntu下使用命令行将其格式化掉并重建分区表。


格式化步骤

1、确认磁盘
U盘插入ubuntu机器,查看生成的block是哪一个,使用lsblk命令,我这边查看到的这里U盘是对应/dev/sdc。后续的操作一定要指定/dev/sdc,否则有可能破坏掉其他的磁盘。

1
2
3
4
5
6
7
8
9
10
victor@victor-HP:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 931.5G 0 disk
└─sdb1 8:17 0 931.5G 0 part /home/victor/disk2
sdc 8:32 1 3.8G 0 disk
└─sdc1 8:33 1 3.8G 0 part /media/victor/3B93-C2C4
sda 8:0 0 465.8G 0 disk
├─sda2 8:2 0 464.3G 0 part /
├─sda3 8:3 0 976M 0 part [SWAP]
└─sda1 8:1 0 512M 0 part /boot/efi

2、umount磁盘
正常u盘插上去会主动挂载上,执行mount命令看挂载到哪个目录下,然后将其umount掉。

1
sudo umount /media/victor/3B93-C2C4

3、将磁盘清空
使用dd命令,往磁盘中写入0,执行以下命令,其中写入的大小为count bs = 4096 1024k = 4GB,这里需要根据自己的磁盘大小来决定。

1
sudo dd if=/dev/zero of=/dev/sdc bs=1024k count=4096 status=progress

4、格式化分区
使用fdisk命令擦除和重新创建新的分区,依次执行o(创建新的分区表),n(创建新的分区),w(写入磁盘并退出)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
victor@victor-HP:~$ sudo fdisk /dev/sdc

Welcome to fdisk (util-linux 2.27.1).
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.
Created a new DOS disklabel with disk identifier 0xd8895380.

Command (m for help): m

Help:

DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag

Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition

Misc
m print this menu
u change display/entry units
x extra functionality (experts only)

Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file

Save & Exit
w write table to disk and exit
q quit without saving changes

Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table


Command (m for help): o
Created a new DOS disklabel with disk identifier 0x6affbecc.

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-7881823, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-7881823, default 7881823):

Created a new partition 1 of type 'Linux' and of size 3.8 GiB.

Command (m for help):


Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

5、查看新的分区表
使用lsblk可以查看U盘的最新分区表。

6、创建vfat文件系统
分区和分区表创建好之后,接下来使用mkfs.vfat创建vfat文件系统。

1
sudo mkfs.vfat /dev/sdc1

参考资料

Title:ubuntu下格式化U盘

Author:Victor Huang

Time:2019-08-11 / 14:08

Link:http://wowothink.com/7a4f33a8/

License: Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)