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,所以對他的功能瞭解的也不是很全面,只是在遇到問題時上網搜索了一下,所以會有一些不對的地方,或者繞遠了的地方,大家有什麼問題或者更好的方法可以在評論區告訴我呀!:)

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