掛載硬碟

September 15, 2018

事由

那年是我剛進實驗室的第 2 個月,因為我對 linux 系統較熟,所以我接了管理的這一部分。沒想到此系統沒幾天後硬碟竟然壞了,於是我呼叫我朋友幫我處裡硬碟部分(硬碟還原),我負責掛載。

分割區切割

Linux 的分割區有 90 多種,但實際只會用到四種,數字表分割代碼

新增硬碟至 ubuntu

檢查第二顆是否有偵測到

$ dmesg |grep hd

再次確認

$ sudo fdisk -l /dev/sdb # 確認硬碟分割區狀態
[sudo] password for itachi:

Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders, total 3907029168 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
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

接著開始分割

進入 fdisk

$ sudo fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x5b5b43aa.
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)

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

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): 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-3907029167, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-3907029167, default 3907029167):
Using default value 3907029167

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

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

首先我們要新增一個分割區,步驟如下

  1. 新增分割區,輸入 n 按 Enter。
  2. 選擇要建立 extended 還是 primary partition,因為我的硬碟全部只要一個分割區,所以我選 primary,輸入 p 按 Enter。
  3. 選擇 Partition number,primary 分割區最多可以有四個,隨便選都可以,不過建議選 1,免得以後看起來很奇怪,輸入 1 按 Enter。
  4. 輸入開始的 sector,用預設值就可以了,直接按 Enter。
  5. 輸入結束的 sector,若是要用最大的容量,就直接按 Enter,若是要指定分割區的大小,就用 +size{K,M,G} 的形式指定,例如指定為 100G 的大小就輸入 +100G 再按 Enter。
  6. 最後將分割表寫入硬碟,輸入 w 再按 Enter。

檢查

$ sudo fdisk -l /dev/sdb

Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes
81 heads, 63 sectors/track, 765633 cylinders, total 3907029168 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
Disk identifier: 0x5b5b43aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  3907029167  1953513560   83  Linux

格式化(Format)硬碟

分割區劃分後,需格式化後才能使用

$ sudo mkfs -t ext4 /dev/sdb1
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
122101760 inodes, 488378390 blocks
24418919 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
14905 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000, 214990848

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

查詢目前檔案系統

查看 UUID

$ sudo blkid
/dev/sda1: UUID="87231b42-c88b-4111-ae5b-9650f743eafd" TYPE="ext4"
/dev/sda5: UUID="d2ba3a6d-7a41-4eaf-ad01-bc81c279d82a" TYPE="swap"
/dev/sdb1: UUID="59af9c6e-a7e8-4f05-99c0-492e16ec5df9" TYPE="ext4"

/etc/fstab 資訊

# /etc/fstab: static file system information.
  2 #
  3 # Use 'blkid' to print the universally unique identifier for a
  4 # device; this may be used with UUID= as a more robust way to name devices
  5 # that works even if disks are added and removed. See fstab(5).
  6 #
  7 # <file system> <mount point>   <type>  <options>       <dump>  <pass>
  8 proc            /proc           proc    nodev,noexec,nosuid 0       0
  9 # / was on /dev/sda1 during installation
 10 UUID=87231b42-c88b-4111-ae5b-9650f743eafd /               ext4    errors=remount-ro 0       1
 11 # swap was on /dev/sda5 during installation
 12 UUID=d2ba3a6d-7a41-4eaf-ad01-bc81c279d82a none            swap    sw              0       0
 13 # The Data Hardisk
 14 #UUID=f1f0d3c0-7789-4bd5-98a0-52ea86076bdd /home/home1    ext3    defaults        0
 15
 16 UUID=bec08759-3ca6-4982-b8e0-6b00522038b9 /home/home1     ext4    defaults        0
 17 UUID=FC9E-C961 /mnt/usb   vfat    defaults        0
~

格式化檔案系統介紹

ext2

ext3

硬碟最大支援和單一檔案最大支援是一樣的。

ext4

和前兩個比

xfs

btrfs

安裝的套件為 btrfs-tools

掛載與卸載

磁碟格式化完後,需使用就要掛載。掛載是一種建立鏈結的關係,不會去動到分割區內原始資料

掛載指令使用 mount

參數功能
-a掛載所有列在 /etc/fstab 的檔案系統
-L掛載特定名稱的檔案系統
-t指定檔案系統類型,支援 ext2 ext3 ext4
-r掛載唯獨模式
-w掛載可讀寫模式
-oasync:所有存取不同步進行
atime:每次存取自動更新存取時間
auto:自動掛載
noatime:不即時更新檔案系統存取時間
ro:掛載為唯獨檔案系統
rw:掛載為可讀寫檔案系統
default:預設選項

卸載指令使用 unmount

參數功能
-a/etc/fstab 內以掛載的檔案系統全部卸載
-r卸載失敗,以唯獨模式掛載
-t只卸載指定檔案系統

掛載 exfat 方式

因為我朋友是在 windows 進行硬碟操作,我記得好像我是要複製東西(從 win 到 Liunx)。因此要掛載需要使用 exfat 方式。

$ sudo apt-get install exfat-fuse -y
$ sudo mount -t exfat /dev/sdg1 /mnt/usb

後續還原的問題與處裡

網卡問題

ubuntu 12.0 ver 82579v 需要安裝驅動

強制更換 MAC

hwaddress ether AA:BB:CC:DD:EE:FF

apache 問題

2.2 up to 2.4 在 Apache 2.2 是這樣寫:

Order allow, deny
Allow from all

在 Apache 2.4 要改成:

Require all granted

還原遇到的問題

move_uploaded_file 有問題的函示 /home/home1/143GB/…../upload/ 這為上傳檔案的資料夾必須賦予 www-data 權限

$ sudo chown www-data:www-data /home/home1/143GB/...../upload/

sudo 處裡

$ sudo vi /etc/sudoers
[sudo] password for jni:
jni is not in the sudoers file.  This incident will be reported.
itachi@ubuntu:~$ sudo vi /etc/sudoers
 # User privilege specification
 username     ALL=(ALL:ALL) ALL