15.新增硬體裝置
新加一塊硬碟,按照udev 服務命名規則,第二個被識別的SCSI 裝置應該會被儲存為/dev/sdb,
1.fdisk:管理磁碟分割槽,格式為“fdisk [磁碟名稱]”
fdisk命令的引數是互動式的,可以根據需求動態調整。
舉例:使用fdisk命令來管理dev/sdb1.看到提示資訊後輸入引數p 來檢視硬碟裝置內已有的分割槽資訊,其中包括了硬碟的容量大小、扇區個數等資訊:
[root@Centos ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, untilyou decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 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 label type: dos Disk identifier:0xdb6e91a1 Device Boot Start End Blocks Id System
輸入引數 n 嘗試新增新的分割槽。系統會要求是選擇繼續輸入引數p 來建立主分割槽,還是輸入引數e 來建立擴充套件分割槽。這裡輸入引數p 來建立一個主分割槽:
在確認建立一個主分割槽後,系統要求先輸入主分割槽的編號。主分割槽的編號範圍是1~4,因此這裡輸入預設的1 就可以了。接下來系統會提示定義起始的扇區位置,這不需要改動,敲擊回車鍵保留預設設定即可,系統會自動計算出最靠前的空閒扇區的位置。
最後,系統會要求定義分割槽的結束扇區位置,這其實就是要去定義整個分割槽的大小是多少。不用去計算扇區的個數,只需要輸入+2G 即可創建出一個容量為2GB 的硬碟分割槽。
再次使用引數p 來檢視硬碟裝置中的分割槽資訊。可以看到一個名稱為/dev/sdb1、起始扇區位置為2048、結束扇區位置為4196351 的主分割槽了。敲擊引數w 後回車,這樣分割槽資訊才是真正的寫入成功。
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): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G Partition 1 of type Linux and of size 2 GiB is set Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 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 label type: dos Disk identifier: 0xdb6e91a1 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
在上述步驟執行完畢之後,Linux 系統會自動把這個硬碟主分割槽抽象成/dev/sdb1 裝置檔案。可以使用file 命令檢視該檔案的屬性,有些時候系統並沒有自動把分割槽資訊同步給Linux 核心,而且這種情況似乎還比較常見(但不能算作是嚴重的bug)。
我們可以輸入partprobe 命令手動將分割槽資訊同步到核心,而且一般推薦連續兩次執行該命令,效果會更好。如果使用這個命令都無法解決問題,那麼就重啟計算機吧,這個殺手鐗百試百靈。。。
刪除分割槽:
[root@Centos ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 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 label type: dos Disk identifier: 0xdb6e91a1 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux Command (m for help): d Selected partition 1 Partition 1 is deleted Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@Centos ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 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 label type: dos Disk identifier: 0xdb6e91a1 Device Boot Start End Blocks Id System
如果硬體儲存裝置沒有進行格式化,則Linux 系統無法得知怎麼在其上寫入資料。因此,在對儲存裝置進行分割槽後還需要進行格式化操作。
2.mkfs:格式化
在Shell 終端中輸入mkfs 名後再敲擊兩下用於補齊命令的Tab,會有如下所示的效果
[root@Centos ~]# mkfs
mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
mkfs 命令地把常用的檔案系統名稱用字尾的方式儲存成了多個命令檔案,用起來也非常簡單—mkfs.檔案型別名稱。
[root@Centos ~]# mkfs.xfs /dev/sdb1 mkfs.xfs: /dev/sdb1 appears to contain a partition table (dos). mkfs.xfs: Use the -f option to force overwrite. [root@Centos ~]# [root@Centos ~]# mkfs.xfs -f /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=524288, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
上述完成了儲存裝置的分割槽和格式化操作,掛載後即可使用:首先是建立一個用於掛載裝置的掛載點目錄;然後使用mount 命令將儲存裝置與掛載點進行關聯;最後使用df -h 命令來檢視掛載狀態和硬碟使用量資訊。
[root@Centos ~]# mkdir /netFS [root@Centos ~]# mount /dev/sdb1 /netFS/ [root@Centos ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos_centos-root 17G 4.3G 13G 25% / devtmpfs 897M 0 897M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.9M 903M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 179M 836M 18% /boot tmpfs 183M 12K 183M 1% /run/user/42 tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb1 2.0G 33M 2.0G 2% /netFS