阿里雲ECS掛載磁盤

服務器系統是 Ubuntu 14.04 Trusty LTS 64bit,下面是掛載磁盤的筆記,僅供參考,注意參數要根據具體狀況修改。node

Step 1: 檢查磁盤,是否有未加載的磁盤

# fdisk -l
root@BBY-Dev02:~# fdisk -l
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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: 0x000da36d
    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048    41940991    20969472   83  Linux
Disk /dev/xvdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders, total 419430400 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
Disk /dev/xvdb doesn't contain a valid partition table

最後一句 Disk /dev/xvdb doesn't contain a valid partition table 說明有未掛載的磁盤linux

Step 2: 分區

root@BBY-Dev02:~# fdisk /dev/xvdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xc9a280eb.
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)
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-419430399, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): 
Using default value 419430399
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

貌似分區成功,用 fdisk 命令檢查一下:服務器

root@BBY-Dev02:~# fdisk -l
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 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: 0x000da36d
    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048    41940991    20969472   83  Linux
Disk /dev/xvdb: 214.7 GB, 214748364800 bytes
86 heads, 25 sectors/track, 195083 cylinders, total 419430400 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: 0xc9a280eb
    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1            2048   419430399   209714176   83  Linux

Disk /dev/xvdb: 214.7 GB, 214748364800 bytes 至少說明掛載成功了。async

Step 3: 格式化分區

使用 mksf.ext3 格式化分區ide

root@BBY-Dev02:~# mkfs.ext3 /dev/xvdb
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
13107200 inodes, 52428800 blocks
2621440 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
1600 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
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Step 4: 掛載分區

mount /dev/xvdb /projects

幹了一件錯事,因爲 /projects 目錄下有文件,mount 後 /projects 文件消失了。。。我暈!
掛載點必須是一個已經存在的目錄,這個目錄能夠不爲空,但掛載後這個目錄下之前的內容將不可用,umount之後會恢復正常ui

Step 5: 加入開機啓動

重啓後發現掛載的磁盤不見了,原來 mount 命令只是臨時的,因此把它寫入開機啓動
注意一下 /etc/fstab 和 /etc/mtab 的區別:this

etc/fstab: 記錄了計算機上硬盤分區的相關信息,啓動 Linux 的時候,檢查分區的 fsck 命令,和掛載分區的 mount 命令,都須要 fstab 中的信息,來正確的檢查和掛載硬盤。 
/etc/mtab: This changes continuously as the file /proc/mount changes. In other words, when filesystems are mounted and unmounted, the change is immediately reflected in this file.

記載的是如今系統已經裝載的文件系統,包括操做系統創建的虛擬文件等;而/etc/fstab是系統準備裝載的。 
每當 mount 掛載分區、umount 卸載分區,都會動態更新 mtab,mtab 老是保持着當前系統中已掛載的分區信息,fdisk、df 這類程序,必需要讀取 mtab 文件,才能得到當前系統中的分區掛載狀況。
固然咱們本身還能夠經過讀取/proc/mount也能夠來獲取當前掛載信息

附一篇參考資料: 詳解/etc/fstab文件

在linux中 /etc/fstab 的數據項以下所示:操作系統

設備名稱    掛載點          分區的類型   掛載選項     dump選項    fsck選項 
LABEL=/    /               ext3       defaults          1        1
tmpfs      /dev/shm        tmpfs      defaults          0        0 
devpts     /dev/pts        devpts     gid=5,mode=620    0        0
sysfs      /sys            sysfs      defaults          0        0 
proc       /proc           proc       defaults          0        0
  1. 設備名稱項,能夠是實際的設備名稱/dev/sda1,也能夠是實際設備的標籤例如LABLE=/,咱們能夠知道 LABEL=/1是/dev/sda1的標籤;那咱們用什麼命令來爲實際的設備設置標籤的方法,對於ext3和ext2文件系統,咱們能夠用 e2label 來設置 e2label device [newlabel],好比咱們想把文件系統爲ext3 的分區/dev/hda1的標籤設備爲 /1 ,咱們應該執行以下的命令:[root@localhost beinan]# e2label /dev/hda1 /1code

  2. 掛載點,沒什麼多說的,/、 /usr、 swap 都是系統安裝時分區的默認掛載點。方法以下:
    root@localhostbeinan]# mkdir /mnt/hda1 注:建立掛載/dev/hda1分區的目錄;orm

    [root@localhost beinan]# chmod 777 /mnt/hda1 注:打開權限,全部用戶可讀可寫可執行,執行完了以上兩步就能夠再/etc/fstab裏      使用這個掛載點了
  3. 文件系統類形:

    Linux file systems: ext2, ext3, jfs, reiserfs, reiser4, xfs, swap. 
     Windows: 
     vfat = FAT 32, FAT 16 
     ntfs= NTFS 
     Note: For NTFS rw ntfs-3g 
     CD/DVD/iso: iso9660 
     Network file systems: 
     nfs: server:/shared_directory /mnt/nfs nfs <options> 0 0 
     smb: //win_box/shared_folder /mnt/samba smbfs rw,credentials=/home/user_name/winbox-credentials.txt 0 0 
     auto: The file system type (ext3, iso9660, etc) it detected automatically. Usually works. Used for removable devices   
     (CD/DVD, Floppy drives, or USB/Flash drives) as the file system may vary on thesedevices.
  4. 掛載選項,下面列舉幾個經常使用的:

    • auto: 系統自動掛載,fstab默認就是這個選項

    • defaults: rw, suid, dev, exec, auto, nouser, and async.

    • noauto 開機不自動掛載

    • nouser 只有超級用戶能夠掛載

    • ro 按只讀權限掛載

    • rw 按可讀可寫權限掛載

    • user 任何用戶均可以掛載
      請注意光驅和軟驅只有在裝有介質時才能夠進行掛載,所以它是noauto

  5. dump 選項,設置是否讓備份程序dump備份文件系統,0爲忽略,1爲備份,若是上次用dump備份,將顯示備份至今的天數。

  6. fsck 選項,告訴fsck程序以什麼順序檢查文件系統,爲0就表示不檢查,(/)分區永遠都是1,其它的分區只能從2開始,當數字相同就同時檢查(但不能有兩1), 注意:當你修改了/etc/fstab後,必定要從新引導系統纔會有效。

相關文章
相關標籤/搜索