怎樣在linux下對U盤進行格式化和分區


說明,為了不做無用功,首先必須卸載要分區的設備,分區才能執行成功。通過命令umount /media/?? 或者umount /mnt/???
看你的實際情況,這一步必不可少。
1、首先通過命令fdisk -l  在root命令下查看U盤的分區信息。

2、通過fdisk命令對掛載的設備進行分區操作,命令如下:fdisk /dev/sdb (可能不一定是這樣,按照實際情況而定)。

3、輸入fdisk /dev/sdb 會有提示命令,輸入m就會列出來,如下所示:

root@samarxie:~# fdisk /dev/sdb

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)

Command (m for help): 
4、輸入p可以瀏覽當前設備的分區信息,如下所示:
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: 0x13561963

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      268287      133120   83  Linux
/dev/sdb2          268288    15523839     7627776   83  Linux

Command (m for help): 

5、可以看到上面有兩個分區,現在我刪除這兩個分區,d命令:
Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

Command (m for help): 

6、確定是否分區被刪除了,在通過p命令:
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: 0x13561963

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 
可以看出沒有了分區信息,刪除分區成功。

7、現在新建分區,兩個分區:
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-15523839, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15523839, default 15523839): +200M  //設定此分區的大小

Command (m for help): 
第一個分區已經新建完畢。
現在第二個分區:
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 2): 
Using default value 2
First sector (411648-15523839, default 411648): 
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-15523839, default 15523839):     //沒有寫,就表示剩下的空間都是此分區的。
Using default value 15523839

Command (m for help): 
按照默認的就可以了,這樣分區就完成了。

8、退出分區操作:
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

這一步有可能出錯(如下),出錯的原因是現有設備掛載了。首先必須卸載此設備,上面的操作才能執行成功。
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 設備或資源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.



上面分區成功,但是我們想要自己的文件系統,比如分區1是fat格式的,分區2是ext2格式的,這樣怎樣處理呢?非常重要的命令mkfs
其有很多種命令:
mkfs          mkfs.cramfs   mkfs.ext3     mkfs.ext4dev  mkfs.msdos    mkfs.vfat     
mkfs.bfs      mkfs.ext2     mkfs.ext4     mkfs.minix    mkfs.ntfs    

1、分區1為fat格式:

mkfs.vfat -F 32 -n vfat /dev/sdb1   //后面的vfat是自己起的設備的命令
root@samarxie:~# mkfs.vfat -F 32 -n fat /dev/sdb1
mkfs.vfat 3.0.12 (29 Oct 2011)


2、分區2為ext2格式:

mkfs.ext2 -F -L ext2 /dev/sdb2    //同上,這個會格式化,時間依據空間的大小不定
root@samarxie:~# mkfs.ext2 -F -L ext2 /dev/sdb2 
mke2fs 1.42 (29-Nov-2011)
文件系統標簽=ext2
OS type: Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
472352 inodes, 1889024 blocks
94451 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=1937768448
58 block groups
32768 blocks per group, 32768 fragments per group
8144 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: 完成                            
正在寫入inode表: 完成                            
Writing superblocks and filesystem accounting information:      
完成

root@samarxie:~# 
如果命令不知道,通過man mkfs.vfat查看,一目了然。
 
完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM