2010年11月16日 星期二

[Tips] fdisk & fsck & mount usb stick in Ubuntu

隨手記...

1.先確定usb裝置是否有偵測到
$ lsusb
Bus 001 Device 042: ID 090c:6000 Feiya Technology Corp. SD/SDHC Card Reader (SG365 / FlexiDrive XC+)
如果沒有的話,就用dmesg查看Ubuntu把隨身碟辨認成什麼裝置
$ dmesg |grep usb

2.查詢目前掛載到的disk與裝置(/dev)的連結
$ ls -al /dev/disk/by-id/
lrwxrwxrwx 1 root root   9 2010-11-26 17:37 usb-Generic_USB2.0_Card_Reader_12345678901234567890-0:0 -> ../../sdb
查詢隨身碟的partition tables(非必要)
$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 2006 MB, 2006974464 bytes
62 heads, 62 sectors/track, 1019 cylinders
Units = cylinders of 3844 * 512 = 1968128 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
3.切成只有一個partition
$ sudo fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
查詢指令
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)
4.建立新的partition(如果不是全新的disk)
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
新增primary partition,起始磁柱為1,最後的磁柱為1019(default)
p
Partition number (1-4): 1
First cylinder (1-1019, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1019, default 1019): 
Using default value 1019
確認partition table
Command (m for help): p
Disk /dev/sdb: 2006 MB, 2006974464 bytes
62 heads, 62 sectors/track, 1019 cylinders
Units = cylinders of 3844 * 512 = 1968128 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               1        1019     1958487   83  Linux
5.建立完成後寫入partition table
Command (m for help): w
如果順利的話,會出現這樣的訊息
The partition table has been altered!

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

如果出現
WARNING: Re-reading the partition table failed with error 22: Invalid argument.
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.

解決方法如以下步驟
$ sudo fdisk /dev/sdb
Command (m for help): p

Disk /dev/sdb: 4005 MB, 4005560320 bytes
124 heads, 62 sectors/track, 1017 cylinders
Units = cylinders of 7688 * 512 = 3936256 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0f60e47c

   Device Boot      Start         End      Blocks   Id  System
建一個空的DOS partition table到隨身碟
Command (m for help): o

Building a new DOS disklabel with disk identifier 0x38e86b45.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
再次存檔
Command (m for help): w
寫入成功了
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
就再重切一次吧
$ sudo fdisk /dev/sdb
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1019, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1019, default 1019): 
Using default value 1019

Command (m for help): p

Disk /dev/sdb: 2006 MB, 2006974464 bytes
62 heads, 62 sectors/track, 1019 cylinders
Units = cylinders of 3844 * 512 = 1968128 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               1        1019     1958487   83  Linux
建立完成後寫入partition table
Command (m for help): w
這次就寫入成功
The partition table has been altered!

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

6.接著是格式化隨身碟
假設掛載在/dev/sdb1的裝置希望格式化成ext2格式
$ sudo mkfs.ext2 /dev/sdb1
mke2fs 1.41.11 (14-Mar-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
122640 inodes, 489952 blocks
24497 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
 32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Writing superblocks and filesystem accounting information: 
改變隨身碟的系統標籤(非必要)
$ sudo e2label /dev/sdb1 {label}
建立掛載點
$ mkdir {path/to/dir}
將隨身碟掛上
$ mount -t ext2 /dev/sdb1 {/path/to/dir}


有時候在資料讀取/寫入過程中,如果I/O沒有順利結束,就會發生錯誤,例如看到這樣的訊息
$ sudo mount /dev/sdb1 {/path/to/dir}
$ cd {/path/to/dir}
$ sudo rm -rf *
rm: cannot remove `data/system/batterystats.bin': Input/output error
rm: cannot remove `data/system/packages.xml': Input/output error
這時候就先umount隨身碟,用fsck進行修復,然後一路y到底
$ sudo umount /dev/sdb1
$ sudo fsck.ext2 /dev/sdb1
e2fsck 1.41.11 (14-Mar-2010)
/dev/sdb1 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Entry 'batterystats.bin' in /data/system (24504) has deleted/unused inode 24604.  Clear? yes

Entry 'packages.xml' in /data/system (24504) has deleted/unused inode 24608.  Clear? yes

Pass 3: Checking directory connectivity
/lost+found not found.  Create? yes

Pass 4: Checking reference counts
Pass 5: Checking group summary information
Block bitmap differences:  -(16385--16733) -26624 -(110592--110600) -124933 -(151552--151553) -153601 -(278528--279183)
Fix? yes

Free blocks count wrong for group #0 (31663, counted=32013).
Fix? yes

Free blocks count wrong for group #3 (32004, counted=32014).
Fix? yes

Free blocks count wrong for group #4 (32253, counted=32256).
Fix? yes

Free blocks count wrong for group #8 (31600, counted=32256).
Fix? yes

Free blocks count wrong (959025, counted=960044).
Fix? yes

Inode bitmap differences:  -(15--16) -24602 -24608 -(65281--65299)
Fix? yes

Free inodes count wrong for group #0 (8147, counted=8149).
Fix? yes

Free inodes count wrong for group #3 (8156, counted=8158).
Fix? yes

Free inodes count wrong for group #8 (8141, counted=8160).
Fix? yes

Directories count wrong for group #8 (1, counted=0).
Fix? yes

Free inodes count wrong (244764, counted=244787).
Fix? yes


/dev/sdb1: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdb1: 13/244800 files (0.0% non-contiguous), 17285/977329 blocks

沒有留言: