1. 程式人生 > >Linux下格式化sd卡和重新分割槽

Linux下格式化sd卡和重新分割槽

Linux下格式化sd卡和重新分割槽

寫這篇文章的最初是因為手邊有張sd卡被人分了區。結果在windows上8G的卡只能認出512M。為了把“被吞掉”的部分找回來,我們就要使用linux下強大的分割槽工具fdisk。

在進行下述操作前,先獲取root許可權再操作會比較方便。之後的命令就可不輸入sudo了。

sudo -i

  
  • 1

準備工作


把你的sd卡插入linux系統。你就會看到原本在windows下只能看到1個512M的空間,
而在linux下你就能看到那個第2分割槽了。

檢視掛載點

df -h

  
  • 1

你會看到下述資訊,告訴你裝置名稱掛載點
可能你的裝置名稱為mmcxxxp1或者mmcxxxp2的。
請注意,他們是一家。。。

Filesystem    size   Used Avail Use% Mounted on
...(其他fs)
/dev/sdb1     512M   4K   512M    1% /media/LABLE1
/dev/sdb2     6.8G   4K   6.8G    1% /media/LABLE2

  
  • 1
  • 2
  • 3
  • 4

解除安裝SD卡

sudo umount /dev/sdb1 /dev/sdb2
  • 1

Fdisk


接下來就是強大的liunx工具fdisk出場了。

sudo fdisk /dev/sdb

  
  • 1

輸入m會出現命令類表,簡潔而強大

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

輸入p檢視當前sd卡的分割槽(partition)資訊

Command (m for help): p

Disk /dev/sdb: 7948 MB, 7948206080 bytes
245 heads, 62 sectors/track, 1021 cylinders, total 15523840 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     1048576      523264+   c  W95 FAT32 (LBA)
/dev/sdb2         1048577    15523839     7237631+  83  Linux

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

其中,需要記住的是他的容量7948206080 bytes之後會用到。
同時也留意他的heads和setcors/track

刪除分割槽


輸入d刪除(delete)所有sd卡分割槽。因為有2個分割槽,需要刪除2次

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

  
  • 1
  • 2
  • 3
  • 4
  • 5

建立扇區


如果SD卡不是255 heads, 63 sectors/track,按照以下步驟建立扇區

輸入x進入擴充套件命令列表

Command (m for help): x

Expert command (m for help): 

  
  • 1
  • 2
  • 3

輸入n顯示擴充套件命令列表

Command action
   b   move beginning of data in a partition
   c   change number of cylinders
   d   print the raw data in the partition table
   e   list extended partitions
   f   fix partition order
   g   create an IRIX (SGI) partition table
   h   change number of heads
   i   change the disk identifier
   m   print this menu
   p   print the partition table
   q   quit without saving changes
   r   return to main menu
   s   change number of sectors/track
   v   verify the partition table
   w   write table to disk and exit

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

輸入h設定heads = 255

Expert command (m for help): h
Number of heads (1-256, default 245): 255

  
  • 1
  • 2

輸入s設定sectors/track = 63

Expert command (m for help): s
Number of sectors (1-63, default 62): 63

  
  • 1
  • 2

輸入c設定cylinder = 966

Expert command (m for help): c
Number of cylinders (1-1048576, default 1021): 966

  
  • 1
  • 2

cylinder計算方法

cylinder = SD卡容量(bytes) / heads / sectors / 512(bytes)
cylinder = 7948206080 / 255 /63 / 512 = 966.3 = 966 

  
  • 1
  • 2

輸入r返回(return)普通命令

Expert command (m for help): r

Command (m for help):

  
  • 1
  • 2
  • 3

輸入p再次檢視當前sd卡的分割槽資訊。
此時你會看到heads, sectors/track, cylinder都已經更新了

Command (m for help): p

Disk /dev/sdb: 7948 MB, 7948206080 bytes
255 heads, 63 sectors/track, 966 cylinders, total 15523840 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

建立分割槽


輸入n新建(new)一個分割槽

輸入p選擇新建一個主分割槽

輸入1建立分割槽1

輸入分割槽1的‘First sector’和‘Last sector’

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

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

輸入t改變分割槽系統ID,
一般windows的用,則輸入分割槽型別碼cFAT32,
而linux的為預設的分割槽型別碼83EXT3

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (W95 FAT32 (LBA))

  
  • 1
  • 2
  • 3
  • 4

輸入a啟用分割槽的bootable標誌

Command (m for help): a
Partition number (1-4): 1

  
  • 1
  • 2

輸入w將建立完成對分割槽表寫入sd卡,並退出

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

格式化分割槽


windows的分割槽格式化命令(其中LABEL為卷標)

sudo mkfs.msdos -F32 /dev/sdb1 -n LABEl1

  
  • 1

linux的分割槽格式化命令

sudo mkfs.ext3 /dev/sdb2 -L LABEL2

  
  • 1

完成


需要注意的是,有時需要在Windows再次格式化第一個引導分割槽LABEL1才能成功引導Linux。(不要使用快速格式化)

參考資料


http://blog.sina.com.cn/s/blog_68e596750100jx9m.html


樹莓派-找回丟失的空間

原本的文章到上面就結束了,這只是個“課外閱讀”的簡單實踐章節而不單獨另開一篇。
通過使用上述的fdisk的功能來找回安裝完樹莓派後部分磁碟空間無法使用的問題。
fdisk細節問題請見上文,下面就直接列出步驟了:

背景: 將樹莓派安裝到了一張8G的TF卡上。裝完df -h後發現只有一半的磁碟空間。

找回方法:

進入fdisk

sudo fdisk /dev/sdb

  
  • 1

輸入p檢視當前sd卡的分割槽(partition)資訊。

會發現有2個分割槽。
一個是boot的windows下可認的FAT32的分割槽。
另一個是root的linux的分割槽。但只有3.xG

  
  • 1
  • 2
  • 3

輸入d刪除(delete)第二個分割槽。

Command (m for help): d
Partition number (1-4): 2

  
  • 1
  • 2

輸入n新建(new)一個分割槽

輸入p選擇新建一個主分割槽

輸入1建立分割槽1

輸入分割槽1的‘First sector’和‘Last sector’

此時使用預設的開始和結束的sector值即可。你也可以看到預設就是幫你擴充套件到最大。

  
  • 1

輸入w將建立完成對分割槽表寫入sd卡,並退出

此時會提醒你無法當場寫入,需要你reboot

  
  • 1

terminal輸入sudo reboot重啟系統

sudo reboot

  
  • 1

待重啟完成後,resize修復分割槽。其中p2即指partition 2

sudo resize2fs /dev/mmcblk0p2

  
  • 1

完成後,重新檢視磁碟資訊

df -h

  
  • 1

至此,“丟失”的磁碟空間已經找回來了。:)

Linux下格式化sd卡和重新分割槽