Linux系統(tǒng)磁盤如何掛載?
發(fā)表時(shí)間:2023-09-02 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]對(duì)于Linux系統(tǒng)來說,掛載磁盤的方法其實(shí)都大同小異,所以本文以CentOS系統(tǒng)為例,介紹下Linux系統(tǒng)磁盤掛載方法,前面大部分內(nèi)容源于天翼云的論壇。1.查看磁盤情況使用命令fdisk -l # 列出全部磁盤信息[root@qxyw ~]# fdisk -lDisk /dev/xvda: 42....
對(duì)于Linux系統(tǒng)來說,掛載磁盤的方法其實(shí)都大同小異,所以本文以CentOS系統(tǒng)為例,介紹下Linux系統(tǒng)磁盤掛載方法,前面大部分內(nèi)容源于天翼云的論壇。
1.
查看磁盤情況使用命令fdisk -l # 列出全部磁盤信息
[root@qxyw ~]# fdisk -l
Disk /dev/xvda: 42.9 GB, 42949672960 bytes255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008a9a5Device Boot Start End Blocks Id System/dev/xvda1 1 523 4194304 82 Linux swap / Solaris
Partition 1 does not end on cylinder boundary./dev/xvda2 * 523 5222 37747712 83 Linux
Disk /dev/xvde: 322.1 GB, 322122547200 bytes255 heads, 63 sectors/track, 39162 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
2.創(chuàng)建分區(qū)
fdisk /dev/xvde # 對(duì)xvde硬盤進(jìn)行分區(qū)
n # 創(chuàng)建一個(gè)新分區(qū)
p # 創(chuàng)建一個(gè)主分區(qū)(e為擴(kuò)展分區(qū))
1 # 1表示第一個(gè)主分區(qū)
回車 # 從第1柱面開始分區(qū)
回車 # 表示整個(gè)xvde全寫入第1分區(qū)(若需要將sdb分為多個(gè)分區(qū),此時(shí)輸入+分區(qū)大。
P # 查看下已創(chuàng)建好的分區(qū)
w # 保存并退出
[root@qxyw ~]# fdisk /dev/xvde
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x590ca8b1.
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): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1First cylinder (1-39162, default 1):
Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-39162, default 39162):
Using default value 39162Command (m for help): p
Disk /dev/xvde: 322.1 GB, 322122547200 bytes255 heads, 63 sectors/track, 39162 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x590ca8b1Device Boot Start End Blocks Id System/dev/xvde1 1 39162 314568733+ 83 Linux
Command (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
3.創(chuàng)建分區(qū)文件系統(tǒng)并格式化磁盤
# mkfs.ext4 /dev/xvde1 # 將xvde1以ext4格式進(jìn)行格式化
[root@qxyw ~]# mkfs.ext4 xvde1
mke2fs 1.41.12 (17-May-2010)
Could not stat xvde1 --- No such file or directory
The device apparently does not exist; did you specify it correctly?[root@qxyw ~]# mkfs.ext4 /dev/xvde1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks19660800 inodes, 78642183 blocks3932109 blocks (5.00%) reserved for the super user
First data block=0Maximum filesystem blocks=4294967296
2400 block groups32768 blocks per group, 32768 fragments per group8192 inodes per group
Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.
4.在home目錄下面創(chuàng)建文件夾mysql,并且將設(shè)備掛載至/home/mysql。
[root@qxyw home]# mount /dev/xvde1 /home/mysql
[root@qxyw home]# df -h
Filesystem Size Used Avail Use% Mounted on/dev/xvda2 36G 2.4G 32G 7% /tmpfs 3.9G 0 3.9G 0% /dev/shm/dev/xvde1 296G 191M 281G 1% /home/mysql
假如原來文件夾中有文件掛載之后文件會(huì)消失并且出現(xiàn)lost+found文件夾,這時(shí)不用擔(dān)心,可以用umount命令進(jìn)行取消掛載。
原因是這樣的。
這個(gè)牽扯到linux的VFS(虛擬文件系統(tǒng))機(jī)制。登錄以后,你看到的各個(gè)目錄,文件都是內(nèi)核在加載時(shí)候構(gòu)造在內(nèi)存中的VFS目錄樹,而不是直接看到硬盤上的實(shí)際目錄樹。
按照我對(duì)這篇文章所描述的粗淺理解,當(dāng)你掛載某個(gè)設(shè)備到一個(gè)VFS掛載點(diǎn)上時(shí)(比如/home),系統(tǒng)就把VFS中的這個(gè)掛載點(diǎn)/home指向你最后所掛載的那個(gè)設(shè)備上。那么你現(xiàn)在訪問該掛載點(diǎn)時(shí),就會(huì)看到你最后掛載在此處的設(shè)備。而之前所掛載的設(shè)備依然在那里,只不過掛載點(diǎn)/home已經(jīng)不再指向之前的設(shè)備。你可以把原來的設(shè)備卸載以后掛載到一個(gè)新的掛載點(diǎn)上來訪問。
5.將磁盤UUID寫入fstab
對(duì)于Linux系統(tǒng)來說,磁盤xvda和xvde的分區(qū)是根據(jù)系統(tǒng)啟動(dòng)時(shí)識(shí)別的先后順序來排列,這樣就會(huì)出現(xiàn)一個(gè)當(dāng)主機(jī)重啟后,出現(xiàn)跳盤的情況,為了避免此情況的發(fā)生,就需要我們將磁盤的UUID信息寫入fstab文件,將分區(qū)與磁盤綁定。
1)通過blkid獲得磁盤分區(qū)的UUID
[root@qxyw ~]# blkid
/dev/xvda1: UUID="5546dd0c-ef40-451b-b266-df8ef3a49967" TYPE="swap"
/dev/xvda2: UUID="77fc0962-b3cf-400a-8903-0632e077fa09" TYPE="ext3"
/dev/xvde1: UUID="1d17945b-c369-42de-85a8-47217d24d3e5" TYPE="ext4"
2)通過vim /etc/fstab在最后一行添加xvde1的磁盤信息
#
# /etc/fstab
# Created by anaconda on Thu Jan 12 02:47:21 2017#
# Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#
UUID=77fc0962-b3cf-400a-8903-0632e077fa09 / ext3 defaults 1 1UUID=5546dd0c-ef40-451b-b266-df8ef3a49967 swap swap defaults 0 0tmpfs /dev/shm tmpfs defaults 0 0devpts /dev/pts devpts gid=5,mode=620 0 0sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
UUID=d17945b-c369-42de-85a8-47217d24d3e5 /home/mysql ext4 defaults 0 0
6.將/var/lib/mysql/下的內(nèi)容轉(zhuǎn)移至/home/mysql/,并且在/var/lib/下創(chuàng)建mysql的鏈接,實(shí)際目錄其實(shí)為/home/mysql
[root@qxyw /]# cd home[root@qxyw home]# mkdir mysql
[root@qxyw home]# mv /var/lib/mysql/* /home/mysql[root@qxyw home]# ln /home/mysql /var/lib/mysql
7.驗(yàn)證一下,通過df命令查看磁盤的使用情況。然后進(jìn)入mysql命令,創(chuàng)建數(shù)據(jù)庫test1...,執(zhí)行之后會(huì)在/home/mysql/文件夾下出現(xiàn)數(shù)據(jù)庫test1,另外使用df命令查看磁盤使用情況。會(huì)發(fā)現(xiàn)/dev/xvde1使用量在增長。
[root@qxyw mysql]# df
Filesystem 1K-blocks Used Available Use% Mounted on/dev/xvda2 37155392 2417608 32850400 7% /tmpfs 4018012 0 4018012 0% /dev/shm/dev/xvde1 309633052 217092 293687524 1% /home/mysql
以上就是Linux系統(tǒng)磁盤如何掛載?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。