9.1 (創建文件系統 掛載 swap 卷標 鏈接ln )

1、複習本次課程所講的內容;

創建文件系統

mke2fs  [option] ……  DEVICE

mke2fs 

-t    {ext2|ext3|ext4}

-b   {1024|2048|4096}

-L  ‘LABEL’

-j     =  mke2fs -t ext3 開啓日誌功能

 

文件系統屬性查看及調整工具(ext系統)

e2label  查看或設置卷標

e2label DEVICE  [label]



tune2fs   

顯示ext系統文件系統的屬性,或調整其屬性

-l    顯示超級塊中的信息(顯示整個文件的屬性及佈局等相關信息)

-L   ‘label’  設置卷標

-m  #   調整預留給管理員的管理空間百分比(默認5%)

-j   ext2--> ext3提升,不損害數據

-O   開啓過關閉文件系統屬性(關閉使用:^屬性)   對應: filesystem features  中的屬性

-o   文件系統默認掛在選項的啓用過關閉    對應:defaultmount options

 

dumpe2fs

-h  僅顯示超級塊信息

 

文件系統檢測工具

fsck  filesystem check  使用方法類似mkfs

-t  type  

fsck.type

    -a  自動修復錯誤

    -r   交互式修復錯誤

    -f   強制檢測



e2fsck   針對ext文件系統專用檢測修復工具

-y    自動回答爲“yes”

-f    force  強制檢測



文件系統的掛載和使用

kernel-->rootfs(分區)    內核加載根分區

rootfs分區包含:

bin,sbin,etc,lib,lib64,dev,tmp,porc,sys

文件系統掛載

掛載:將額外文件系統與根文件系統某現存的目錄建立起關聯關係,此目錄作爲其他文件系統訪問的入口

卸載:解除關聯關係

 

mount命令

mount  [options]  [-o optoins] DEVICE  MOUNT_POINT

    options  命令選項

    -o optiongs 掛載選項

DEVICE  可以直接寫設備文件 /dev/sda# 、卷標-L "abel"   、-U   “UUID"

常用選項:

-t  type

-r   以只讀方式掛載

-w   以讀寫方式掛載

-n   跟個文件系統在掛載時都會自動更新/etc/mtab文件 -n用於進制此功能    

        如果想查看所有掛載的文件系統:cat /porc/mounts

-a   掛載/etc/fstab文件中所有支持自動掛載的文件系統

-B   綁定目錄至另一個目錄上

 

-o  [options]   掛載選項

async   異步模式

sync    同步模式

atime/noatime  (文件atime更新非常頻繁,可以關閉掉noatime)

exec/noexec  是否運行執行此文件系統上應用程序

dev/nodev  是否支持在此設備上使用設備文件

suid/nosuid   允許/禁止使用suid

remount    重新掛載

ro   只讀掛載

rw   讀寫掛載

use/nouser  是否允許普通掛載此設備

acl  是否支持此設備上使用facl



卸載命令

umount  DEVICE

umount   mount_point

 

查看正在訪問指定掛載點的進程

fuser -v mount_point

 

終止所有正在訪問指定的掛載點的進程

fuser -km mount_point

 

swap 交換分區

free  查看內存和swap使用情況

-m  以MB爲單位

-g   以GB爲單位

 

創建交換分區:

mkswap  [option] DEVICE   

        -L  LABEL

 

啓用交換分區

swapon [option] [device]

-a  激活所有交換分區

-p   priority  設定其優先級

 

禁用交換分區

swap  [option]  [DEVICE]

 

文件系統大、小查看

df   disk free

-h  人性化自動換算大小單位

-i    inode數量

-P  以posix兼容格式輸出

 

du   disk usage

-h    human-readable

-s    summary

 

 

文件系統掛載配置文件   /etc/fstab

要掛載的設備或僞文件系統    掛載點  文件系統類型   掛載選項  轉儲頻率   自檢次序

 

 

掛載選項defaults即可

轉儲頻頻率  0不轉儲   1每天轉儲   2每一隔天轉儲      設置0即可

自檢次序   0不自檢    1首先自檢(通常只有/才爲1)    設置爲0即可

 

 

文件系統上的鏈接文件

硬連接

兩個路徑指向同一個inode,不能對目錄進行,不能跨分區進行,刪除硬連接至少刪除其中一個訪問路徑,直至刪除最後一個才被徹底刪除

 

軟連接

相當於快捷方式,可以針對目錄進行,可以跨分區,對文件創建軟連接不會增加其引用計數,刪除原文件,軟連接也就沒有什麼用了

 

設置鏈接

ln  [option]  SRC  DEST

-s   symbolic  link  軟連接

-v   verbose

不加選項爲硬連接

 

2、總結整理磁盤管理及文件系統管理中設計的各種命令的使用,並附註一定的示例;


fdisk 命令

[root@localhost ~]# fdisk /dev/sda


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): m

Command action

   a   toggle a bootable flag

   b   edit bsd disklabel

   c   toggle the dos compatibility flag

   d   delete a partition

   l   list known partition types

   m   print this menu

   n   add a new partition

   o   create a new empty DOS partition table

   p   print the partition table

   q   quit without saving changes

   s   create a new empty Sun disklabel

   t   change a partition's system id

   u   change display/entry units

   v   verify the partition table

   w   write table to disk and exit

   x   extra functionality (experts only)


Command (m for help): 


其實fdisk常用的參數:m幫助  n新建分區  d刪除分區  p列出個分區  l列出支持的分區類型  t修改分區id  q不保存退出  w保存退出


partx 講分區加載到內核(如果分區後沒有被內核識別),可以cat /proc/partitions 

[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   83886080 sda
   8        1     204800 sda1
   8        2   20971520 sda2
   8        3   20971520 sda3
   8        4          1 sda4
   8        5    1048576 sda5
   8       16   41943040 sdb
[root@localhost ~]# partx -a -n 1 /dev/sdb
[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   83886080 sda
   8        1     204800 sda1
   8        2   20971520 sda2
   8        3   20971520 sda3
   8        4          1 sda4
   8        5    1048576 sda5
   8       16   41943040 sdb
   8       17    5253223 sdb1

上面是我們創建了一個/dev/sdb1分區(內核自動識別,手動刪除掉以後演示),或者使用[root@localhost ~]# kpartx -af /dev/sdb 


格式化文件系統、設置卷標

mkfs  或者  mkfs.type

其實mkfs -t ext4就是調用mkfs.ext4

在做實驗的過程中遇到了錯誤:

[root@localhost ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
/dev/sdb1 is apparently in use by the system; will not make a filesystem here!

解決方法:

[root@localhost ~]# dmsetup status
sdb1: 0 10506447 linear 
[root@localhost ~]# dmsetup remove sdb1    解除綁定

然後就正常了

[root@localhost ~]# mkfs.ext4 -L test /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=test
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655776 inodes, 2622603 blocks
131130 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

可以在格式化的時候指明卷標,不指明也沒有關係,可以用其他工具進行修改(不格式化)


e2label tune2fs  dumpe2fs 都可以修改

e2label專門用來查看或者修改分區卷標,另外兩個工具的功能更多些

[root@localhost ~]# e2label /dev/sdb1 'mytest'
[root@localhost ~]# e2label /dev/sdb1
mytest

tune2fs、dumpe2fs 可以顯示ext系列文件系統的屬性

[root@localhost ~]# tune2fs -l /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   mytest
Last mounted on:          <not available>
Filesystem UUID:          18235df4-58e9-49ce-b653-b03833a26a4e
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
……
[root@localhost ~]# dumpe2fs -h /dev/sdb1 
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name:   mytest
Last mounted on:          <not available>
Filesystem UUID:          18235df4-58e9-49ce-b653-b03833a26a4e
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash

tune2fs也能修改分區的卷標

[root@localhost ~]# tune2fs -L testaaa /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
[root@localhost ~]# e2label /dev/sdb1
testaaa

fsck 、 e2fsck

[root@localhost ~]# e2fsck /dev/sdb1
e2fsck 1.41.12 (17-May-2010)
testaaa: clean, 11/655776 files, 79700/2622603 blocks
[root@localhost ~]# fsck -t ext4 /dev/sdb1
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
testaaa: clean, 11/655776 files, 79700/2622603 blocks


fsck  filesystem check  使用方法類似mkfs

-t  type  

fsck.type (fsck.ext3  fsck.ext4)

    -a  自動修復錯誤

    -r   交互式修復錯誤

    -f   強制檢測


e2fsck   針對ext文件系統專用檢測修復工具

-y    自動回答爲“yes”

-f    force  強制檢測



文件系統掛載、卸載  mount

[root@localhost ~]# mount /dev/sdb1 /mnt
[root@localhost ~]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sda3 on /usr type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /mnt type ext4 (rw)
[root@localhost ~]# umount /dev/sdb1
[root@localhost ~]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sda3 on /usr type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

注意在卸載的時候,如果有進程佔用磁盤,那麼卸載時會出現錯誤

可以使用fuser  -vm  DEVICE 進程查看那些進程佔用了磁盤

[root@localhost ~]# fuser -vm /dev/sdb1    #查看佔用
                     USER        PID ACCESS COMMAND
/dev/sdb1:           root       2124 ..c.. bash
[root@localhost ~]# umount /dev/sdb1
umount: /mnt: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@localhost ~]# fuser -vm /dev/sdb1
                     USER        PID ACCESS COMMAND
/dev/sdb1:           root       2124 ..c.. bash
[root@localhost ~]# fuser -km /dev/sdb1   #結束佔用
/dev/sdb1:            2124c
[root@localhost ~]# umount /dev/sdb1
[root@localhost ~]#


mkswap swapon swapoff

刪除原來的/dev/sdb1,重新劃分分區,過程略

[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           988        143        845          0         17         49
-/+ buffers/cache:         76        912 
Swap:         1023          0       1023

目前swap 1G

[root@localhost ~]# mkswap  /dev/sdb1
Setting up swapspace version 1, size = 5253216 KiB
no label, UUID=c38f12b3-a796-4d8e-ba8c-6b31c0c0b0d8
[root@localhost ~]# swapon /dev/sdb1
[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           988        147        841          0         17         49
-/+ buffers/cache:         80        908 
Swap:         6154          0       6154 
[root@localhost ~]#

可以看到swap分區變成了6154M

卸載也比較簡單,swap DEVICE  即可

[root@localhost ~]# swapoff /dev/sdb1
[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           988        143        845          0         17         49
-/+ buffers/cache:         76        912 
Swap:         1023          0       1023 
[root@localhost ~]#

若想永久生效需修改/etc/fstab

[root@localhost ~]# blkid /dev/sdb1

/dev/sdb1: UUID="c38f12b3-a796-4d8e-ba8c-6b31c0c0b0d8" TYPE="swap" 

[root@localhost ~]# 

記住/dev/sdb1的UUID 如下格式添加到/etc/fstab

UUID=c38f12b3-a796-4d8e-ba8c-6b31c0c0b0d8   swap  swap   defaults 0 0 


文件大小查看  df (disk free)  du(disk usage)

[root@localhost ~]# df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext4    20G  422M   19G   3% /
tmpfs          tmpfs  495M     0  495M   0% /dev/shm
/dev/sda1      ext4   190M   36M  145M  20% /boot
/dev/sda3      ext4    20G  3.0G   16G  17% /usr
[root@localhost ~]# du -sh /usr
3.0G/usr

軟連接&硬連接

[root@localhost tmp]# ln fstab fstab.hl
[root@localhost tmp]# ln -s fstab fstab.sl
[root@localhost tmp]# ll -i
total 32
917550 -rw-r--r-- 2 root root   899 Sep  2 17:19 fstab
917550 -rw-r--r-- 2 root root   899 Sep  2 17:19 fstab.hl
917506 lrwxrwxrwx 1 root root     5 Sep  6 18:47 fstab.sl -> fstab
917553 -rw-r--r-- 1 root root 19697 Sep  6 10:30 functions
917552 -rw------- 1 root root   771 Sep  6 10:15 grub.conf

可以看出硬連接的引用數增加,並且inode號相同,如果刪除原文件,硬連接可以繼續使用,軟連接則不可用


3、創建一個10G的文件系統,類型爲ext4,要求開機可自動掛載至/mydata目錄

[root@localhost tmp]# fdisk /dev/sdb
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
   l   logical (5 or over)
   p   primary partition (1-4)
l                                #選擇邏輯分區
First cylinder (1961-5221, default 1961): 
Using default value 1961
Last cylinder, +cylinders or +size{K,M,G} (1961-5221, default 5221): +10G        #指明分區大小爲10G
Command (m for help): w                #保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost tmp]# mkfs.ext4 /dev/sdb5 
[root@localhost tmp]# mount /dev/sdb5 /mydata/
[root@localhost tmp]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/sda3 on /usr type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb5 on /mydata type ext4 (rw)

[root@localhost tmp]# blkid /dev/sdb5
/dev/sdb5: UUID="d5a3054c-f00d-4be1-a286-fa0037671bd2" TYPE="ext4" 

[root@localhost tmp]# tail -1 /etc/fstab        #修改/etc/fstab永久生效
UUID=d5a3054c-f00d-4be1-a286-fa0037671bd2   /mydata   ext4 defaults 0 0



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章