在之前學習了磁盤的分區和格式化,格式化完後,如何去用它?這就涉及到掛載磁盤了。
格式化的磁盤其實就是一個塊設備文件,類型爲b。你也許會想,既然這個塊文件就是那個分區,那麼直接在那個文件中寫數據不就寫到那個分區了嗎,當然不行。
在掛載某個分區前需要先建立一個掛載點,這個掛載點是以目錄的形式出現的。一旦把某一個分區掛載到了這個掛載點(目錄)下,那麼再往這個目錄寫數據,則都會寫到該分區中。這就需要注意一下,在掛載該分區前,掛載點(目錄)下必須是個空目錄。其實目錄不爲空並不影響所掛載分區的使用。但是一旦掛載上了,那麼該目錄下以前的東西就不能看到了。只有卸載掉該分區後才能看到。
刪除之前的分區
[root@evan-01 ~]# 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: 10.7 GB, 10737418240 bytes, 20971520 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: 0x6e55b372
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 83 Linux
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x6e55b372
Device Boot Start End Blocks Id System
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@evan-01 ~]#
格式化分區
[root@evan-01 ~]# mkfs.xfs /dev/sdb
mkfs.xfs: /dev/sdb appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[root@evan-01 ~]# mkfs.xfs -f /dev/sdb
meta-data=/dev/sdb isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, 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
[root@evan-01 ~]#
查看是什麼格式文件系統
[root@evan-01 ~]# blkid /dev/sdb
/dev/sdb: UUID="2ad00110-fa48-46a9-9752-a6dc4ff5155c" TYPE="xfs"
[root@evan-01 ~]#
mount 掛載,用於加載文件系統到指定的加載點
左邊的是盤符,右邊的是掛載點
掛載點是磁盤的入口,沒有掛載點就沒辦法往磁盤裏寫東西。
[root@evan-01 ~]# mount /dev/sdb /mnt
[root@evan-01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1022M 16G 6% /
devtmpfs 482M 0 482M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/sdb 10G 33M 10G 1% /mnt
[root@evan-01 ~]#
用 mount 查看下系統以及掛載的所在分區,在最後一行有了 /dev/sdb
[root@evan-01 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=493004k,nr_inodes=123251,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_prio,net_cls)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=31,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=12192)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=100816k,mode=700)
/dev/sdb on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@evan-01 ~]#
umount 卸載掛載點
往掛載點裏創建文件和目錄
[root@evan-01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1022M 16G 6% /
devtmpfs 482M 0 482M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 99M 0 99M 0% /run/user/0
/dev/sdb 10G 33M 10G 1% /mnt
[root@evan-01 ~]# cd /mnt
[root@evan-01 mnt]# ll
total 0
[root@evan-01 mnt]# touch 1.txt 2.txt
[root@evan-01 mnt]# mkdir 123
[root@evan-01 mnt]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 19 10:22 123
-rw-r--r--. 1 root root 0 Aug 19 10:22 1.txt
-rw-r--r--. 1 root root 0 Aug 19 10:22 2.txt
[root@evan-01 mnt]#
[root@evan-01 mnt]# umount /dev/sdb
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@evan-01 mnt]# pwd
/mnt
[root@evan-01 mnt]#
因爲我們在 mnt 目錄裏,所以報錯了
退出來
[root@evan-01 mnt]# cd
[root@evan-01 ~]# umount /dev/sdb
[root@evan-01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1022M 16G 6% /
devtmpfs 482M 0 482M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 99M 0 99M 0% /run/user/0
[root@evan-01 ~]#
還可以繼續掛載
[root@evan-01 ~]# mount /dev/sdb /mnt/
[root@evan-01 ~]# cd /mnt
[root@evan-01 mnt]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 19 10:22 123
-rw-r--r--. 1 root root 0 Aug 19 10:22 1.txt
-rw-r--r--. 1 root root 0 Aug 19 10:22 2.txt
[root@evan-01 mnt]#
不退出 mnt 目錄直接卸載
-l (lazy) 懶,不退出,直接卸載
[root@evan-01 mnt]# umount -l /mnt/
[root@evan-01 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1022M 16G 6% /
devtmpfs 482M 0 482M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.8M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 99M 0 99M 0% /run/user/0
[root@evan-01 mnt]#