歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux自動掛載文件系統詳解

linux自動掛載文件系統詳解

日期:2017/3/1 13:38:56   编辑:關於Linux
Linux磁盤自動掛載必須,而且必要,不然每次重啟後都要掛載就太麻煩啦。

1. 查看系統中磁盤信息,並確定是否需要分區
首先,為了需要,新添加一塊磁盤到linux主機中,
目的是讓這個新添加的磁盤分區格式化並可以隨系統啟動自動掛載。
[root@localhost ~]# fdisk -l
【此處是sda的磁盤信息】
Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 36404 292310707+ 8e Linux LVM

【此處是sdb的磁盤信息】
Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table 【此處顯示磁盤dev/sdb尚未格式化】

如上所示,系統中有兩塊磁盤sda、sdb,並提示sdb未含有分區表,需要將其進行分區操作。

2.對磁盤進行分區操作
磁盤容量與主分區、擴展分區、邏輯分區的關系:

硬盤的容量=主分區的容量+擴展分區的容量
擴展分區的容量=各個邏輯分區的容量之和

一塊物理硬盤只能有:
一到四個主分區(但其中只能有一個是活動的主分區),
或一到三個主分區,和一個擴展分區。分別對應hda1,hda2,hda3,hda4.

Linux 中規定,每一個硬盤設備最多能有 4 個主分區(其中包含擴展分區)構成,
任何一個擴展分區都要占用一個主分區號碼,也就是在一個硬盤中,主分區和擴展分區一共最多是 4 個。

一塊硬盤可以只設主分區,這時主分區可設置4個分區號。

也可以設置成主分區+邏輯分區,這時也是最多4個分區號碼,但是變成了4 = 3 + 1.
. 其中4是主分區和擴展分區加起來最多4個;
. 3是主分區,可以小於或等於3;
. 1是擴展分區號,占用了一個主分區號。

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

The number of cylinders for this disk is set to 182024.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p 【顯示磁盤基本信息】
Disk /dev/sdb: 1497.1 GB, 1497198755840 bytes
255 heads, 63 sectors/track, 182024 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

【此處可以看出,尚未對磁盤進行分區】
Command (m for help):n    【n:add a new partition】
Command action
e extended      【選擇主分區還是擴展分區】
p primary partition (1-4)
p
Partition number (1-4): 1  【選擇分區數】
First cylinder (1-182024, default 1): 【設置分區大小。此處選擇默認值,直接回車】
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-182024, default 182024):
Using default value 182024
Command (m for help):w    【如無誤輸入w保存配置信息,如不保存輸入q退出】
The partition table has been altered!

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

【再檢查一下分區成功與否】
$ fdisk /dev/sdb
The number of cylinders for this disk is set to 182024.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p   【如下查看分區之後的磁盤信息】
Disk /dev/sdb: 1497.1 GB, 1497198755840 bytes
255 heads, 63 sectors/track, 182024 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 182024 1462107748+ 5 Extended

Command (m for help): q

【如果想劃分多個分區,可以按以下操作】
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): p   【p:print the partintion table】
Disk /dev/sdb: 6442 MB, 6442450944 bytes 【顯示磁盤基本信息】
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
此處可以看出,尚未對磁盤進行分區
Command (m for help):n    【n:add a new partition】
Command action
e extended      【選擇主分區還是擴展分區,在此選擇主分區】
p primary partition (1-4)
p
Partition number (1-4): 3 【選擇分區數】
First cylinder (1-783, default 1): 【設置分區大小。此處定義1000M。就是最接近1000M的柱面號碼】
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-783, default 783): +1000M
Command (m for help):w    【如無誤輸入w保存配置信息,如不保存輸入q退出】
Command (m for help): p   【如下查看分區之後的磁盤信息】
Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 368 783 3341520 83 Linux
/dev/sdb2 124 367 1959930 83 linux
/dev/sdb3 1 123 987966 83 Linux

3.將分區好的磁盤進行格式化操作
[root@localhost ~]# mkfs.xfs -f /dev/sdb 【將磁盤sdb1格式化為xfs文件系統】
meta-data=/dev/sdb isize=256 agcount=32, agsize=137318400 blks
= sectsz=512 attr=0
data = bsize=4096 blocks=4394188800, imaxpct=25
= sunit=0 swidth=0 blks, unwritten=1
naming =version 2 bsize=4096
log =internal log bsize=4096 blocks=32768, version=1
= sectsz=512 sunit=0 blks, lazy-count=0
realtime =none extsz=4096 blocks=0, rtextents=0

4.將分區格式化完畢的磁盤掛載,檢查確認分區格式化完畢。
[root@localhost /]# mkdir /data  【新建一個目錄data】
[root@localhost /]# mount /dev/sdb /data/ 【將磁盤sdb掛載到系統中】

[root@localhost /]# cd /data
[root@localhost data]# ls  【進行寫文件測試,檢查是否可以對磁盤進行讀寫(是否格式化)
[root@localhost data]# vim test.txt 【進行寫測試】
[root@localhost data]# cat test.txt 【進行讀測試】

5.測試無誤後將其卸載
[root@localhost mnt]#umount /dev/sdb1

6.修改fstab文件,如下所示:
[root@localhost mnt]# vim /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
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
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
【添加一行】
/dev/sdb /data xfs defaults 0 0

【如果是多個分區,則添加如下多行】
/dev/sdb1 /mnt/sdb1 ext3 defaults 0 0
/dev/sdb2 /mnt/sdb2 ext3 defaults 0 0
/dev/sdb3 /mnt/sdb3 ext3 defaults 0 0

7.重啟linux系統,檢查掛載情況
[root@localhost sdb1]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
238G 8.5G 218G 4% /
/dev/sda1 99M 13M 81M 14% /boot
tmpfs 16G 0 16G 0% /dev/shm
/dev/sdb 1.4T 5.1M 1.4T 1% /data

【多個分區時】
文件系統   1K-塊    已用     可用   已用%   掛載點
/dev/sda1  8022104  2893532  4714500 39%    /
tmpfs  127720   0      127720 0%   /dev/shm
/dev/sdb1  3288944  71328   3050540 3%    /mnt/sdb1
/dev/sdb2  1929100  35696   1795408 2%    /mnt/sdb2
/dev/sdb3  972404   17640   905368 2%   /mnt/sdb3

如上所示,將新建的磁盤的三個分區順利掛載到系統中。
Copyright © Linux教程網 All Rights Reserved