Ubuntu下给U盘分区

1. 查看当前所有分区

1.1 方法1

cat /proc/partitions

使用上述命令查看当前系统上所有的分区,将会得到如下的输出

major    minor  #blocks  name

   8        0  976762584 sda
   8        1     498688 sda1
   8        2          1 sda2
   8        5  976261120 sda5
  11        0    1048575 sr0
 252        0  976259072 dm-0
 252        1  959586304 dm-1
 252        2   16670720 dm-2
   8       16   30375936 sdb

我们主要用到最后一行,在name前面加上dev目录 -> 变成/dev/sdb用于表示我们要操作的磁盘。

1.2 方法2

sudo fdisk -l

使用上述命令,将会得到类似如下一大片的输出

Disk /dev/sda: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x93a259d7

Device     Boot   Start        End    Sectors  Size Id Type
/dev/sda1  *       2048     999423     997376  487M 83 Linux
/dev/sda2       1001470 1953523711 1952522242  931G  5 Extended
/dev/sda5       1001472 1953523711 1952522240  931G 83 Linux

主要看Disk后面那个,Disk后面是磁盘的位置,上面的磁盘为/dev/sda,后面的表中sda1~3表示该磁盘上的三个分区。sda1应该是Linux的主分区,sda2为扩展分区,sda5为扩展分区上的逻辑分区。(对,我的Ubuntu没分盘,就一个哈哈哈)

2. 使用fdisk进行分区

进行操作前,需要先备份好u盘中的重要文件!
进行操作前,需要先备份好u盘中的重要文件!
进行操作前,需要先备份好u盘中的重要文件!
重要的事情说三遍。

上面1查看分区的作用是,找到你需要分区的那个磁盘,对u盘进行一下插拔的操作,再使用命令,对比一下,就知道u盘所对应的位置是哪儿。比如我的u盘对应的位置是/dev/sdb。

首先,使用命令进入fdisk的操作台,注意后面不要加分区号,如/dev/sdb1。

sudo fdisk /dev/sdb  // 后面的路径要替换为第一步所得到的u盘路径

之后你会看到如下输出,先受到欢迎,然后告诉你输入m可以看到帮助菜单,那当然是要寻求帮助啦!

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.


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

fdisk的功能还是很强大的,用于分区,我们用到以下几个命令:

2.1 p – 列出当前所有分区

Command (m for help): p
Disk /dev/sdb: 29 GiB, 31104958464 bytes, 60751872 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
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdb1           2048 60751871 60749824  29G  5 Extended
/dev/sdb5           4096  8388608  8384513   4G  7 HPFS/NTFS/exFAT
/dev/sdb6        8392704 16777216  8384513   4G  7 HPFS/NTFS/exFAT
/dev/sdb7       16781312 25165824  8384513   4G  c W95 FAT32 (LBA)
/dev/sdb8       25169920 33554432  8384513   4G  c W95 FAT32 (LBA)
/dev/sdb9       33558528 41943040  8384513   4G  c W95 FAT32 (LBA)
/dev/sdb10 *    41947136 60751871 18804736   9G 83 Linux

这是我分好区之后的,这个命令可以列出当前设备的分区表,格式和sudo fdisk -l的输出一致。

2.2 d – 删除分区

Command (m for help): d
Partition number (1,5-10, default 10): 

输入d后会要求输入分区号,对应着sdb后面的号码。我在备份后将所有分区都删除了。

2.3 n – 创建分区

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

Created a new partition 1 of type 'Extended' and of size 4 GiB.

如果不是用作系统盘的话,一般不需要创建主分区,也就是primary分区。
如果只是想把U盘分为几个盘符的话,可以将所有空间创建为一个扩展分区,之后再次使用n命令在扩展分区上创建逻辑分区。
关于分区类型的介绍,请看这里–>主分区、扩展分区、逻辑分区、活动分区有什么不同?

2.4 w或者q – 保存(放弃)当前修改并退出

最后也是最关键的一步骤,如果你的分区出现了问题,你不想保存本次修改,那么使用q,则不会保存本次修改,并退出到终端。
如果分区已经完成(使用p查看当前分区表,已经满意),那么使用w保存退出。不知道为何,使用w退出出现了设备忙的问题,但是查看分区表,实际的分区已经完成了。

1.3 挂载U盘

不知道标题这么叫是否合适,但是使用上述方法创建分区后,在分区表中可以看到,但是,可能因为设备忙之类的原因,在文件中看不到挂载的u盘,上网查了一下,使用如下命令可进行修复。

sudo mkfs.vfat /dev/sdb1 -I  // 需要注意的是这个命令后面的目录需要是分区的目录而不是磁盘的

之后应该就能看到分区后的磁盘带着他的乳名出现在了你的挂载磁盘的列表里。
之后可以右击选择Format对磁盘进行格式化,有四种格式FAT,NFTS,Ext4,LUKS+Ext4,还可以用户自定义,于此同时你可以给他起个你喜欢的名字,比如什么你好世界之类的:)。

到此你的U盘就分区结束啦,我也是第一次使用fdisk,所以对他的功能了解的也不是很全面,只是在遇到问题时上网搜索了一下,所以会有一些不对的地方,或者绕远了的地方,大家有什么问题或者更好的方法可以在评论区告诉我呀!:)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章