菜鳥教你如何磁盤分區與創建文件系統以及掛載(2)

一、文件系統簡介與創建

1、什么是文件系統?

   文件系統是操作系統用于明確存儲設備或分區上的文件的方法和數據結構;即在存儲設備上組織文件的方法。操作系統中負責管理和存儲文件信息的軟件結構稱為文件管理系統,簡稱文件系統。

2、文件系統的作用

   從系統角度來看,文件系統是對文件存儲設備的空間進行組織和分配,負責文件存儲并對存入的文件進行保護和檢索的系統。具體地說,它負責為用戶建立文件,存入、讀出、修改、轉儲文件,控制文件的存取,安全控制,日志,壓縮,加密等。

3、文件系統的類型

Linux文件系統 ext2,ext3,ext4,xfs,btrfs,reiserfs,jfs,swap
光盤 iso9660
windos fat32,ntfs
unix FFS(fast),UFS(unix),JFS2
網絡文件系統 NFS,CIFS
集群文件系統 GFS2,OCF32(oracle)
分布式文件系統 ceph,moosefs,mogilefs,glusterfs
RAW 未經處理或者未經格式化產生的文件系統

4、創建文件系統(以centos7為參考)

mkfs 命令

簡介:
mkfs - build a Linux filesystem
格式:
mkfs [options] [-t type] [fs-options] device [size]
選項:
-L 'LABEL': 設定卷標
實例1:創建任何一個分區為ext4類型的文件系統
[root@centos7 ~]# echo '- - -' > /sys/class/scsi_host/host2/scan     #添加一塊200G的硬盤,并用此類方法讓系統掃描顯示該硬盤,無需重啟系統
[root@centos7 ~]# fdisk -l     #查看硬盤分區,已顯示添加新的硬盤sdb

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x00007ead

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648   205211647   102400000   83  Linux
/dev/sda3       205211648   225691647    10240000   83  Linux
/dev/sda4       225691648   419430399    96869376    5  Extended
/dev/sda5       225693696   234082303     4194304   82  Linux swap / Solaris

Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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

[root@centos7 ~]# fdisk /dev/sdb      #對新添加的硬盤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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9c73d1e5.

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-419430399, default 2048):      #回車選擇默認值為2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +80G     #以容量size分給sdb1分區80G
Partition 1 of type Linux and of size 80 GiB is set

Command (m for help): w     #保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos7 ~]# cat /proc/partitions     #內核已自動掃描添加,無需做其它操作 
major minor  #blocks  name

  11        0    7587840 sr0
   8        0  209715200 sda
   8        1     204800 sda1
   8        2  102400000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5    4194304 sda5
   8       16  209715200 sdb
   8       17   83886080 sdb1
[root@centos7 ~]# blkid /dev/sdb1     #查看分區sdb1無文件系統類型
[root@centos7 ~]# mkfs -t ext4 -L "MYDATA" /dev/sdb1     #創建分區sdb1為ext4類型的文件系統,并命名為“MYDATA”
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYDATA
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
5242880 inodes, 20971520 blocks
1048576 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2168455168
640 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

[root@centos7 ~]# blkid /dev/sdb1     #查看分區sdb1屬性
/dev/sdb1: LABEL="MYDATA" UUID="f595a2dc-59e4-4327-8f0e-4a08d050603b" TYPE="ext4"
實例2:將分區sdb1更改類型為btrfs
[root@centos7 ~]# mkfs -t btrfs /dev/sdb1     #更改分區sdb1更改類型為btrfs
/dev/sdb1 appears to contain an existing filesystem (ext4).
Error: Use the -f option to force overwrite.     #報錯,需要加選項-f(強制)
[root@centos7 ~]# mkfs -t btrfs -f /dev/sdb1     #強制將分區sdb1更改類型為btrfs
btrfs-progs v3.19.1
See http://btrfs.wiki.kernel.org for more information.

Turning ON incompat feature 'extref': increased hardlink limit per file to 65536
Turning ON incompat feature 'skinny-metadata': reduced-size metadata extent refs
fs created label (null) on /dev/sdb1
	nodesize 16384 leafsize 16384 sectorsize 4096 size 80.00GiB
[root@centos7 ~]# blkid /dev/sdb1     #查看分區sdb1的屬性
/dev/sdb1: UUID="2962a44a-9520-471a-8fdc-71cc087480ad" UUID_SUB="7afc3a9c-4777-41c9-a8bc-c6236c51e096" TYPE="btrfs"      #已顯示文件系統類型為btrfs,已更改成功
[root@centos7 ~]#

mke2fs 命令

簡介:
mke2fs - create an ext2/ext3/ext4 filesystem
格式:
mke2fs  [  -c  | -l filename ] [ -b block-size ] [ -D ] [ -f fragment-size ] [ -g blocks-per-group ] [ -G number-of-groups ] [ -i bytes-per-inode ] [ -I inode-
       size ] [ -j ] [ -J journal-options ] [ -N number-of-inodes ] [ -n ] [ -m reserved-blocks-percentage ] [ -o creator-os ] [ -O feature[,...]  ] [ -q ] [  -r  fs-
       revision-level  ] [ -E extended-options ] [ -v ] [ -F ] [ -L volume-label ] [ -M last-mounted-directory ] [ -S ] [ -t fs-type ] [ -T usage-type ] [ -U UUID ] [
       -V ] device [ blocks-count ]
mke2fs -O journal_dev [ -b block-size ] [ -L volume-label ] [ -n ] [ -q ] [ -v ] external-journal [ blocks-count ]
選項:
-t {ext2|ext3|ext4}:文件類型
-b {1024|2048|4096}:塊大小
-L 'LABEL':卷標
-j: 相當于-t ext3;mkfs.ext3 = mkfs-t ext3 = mke2fs -j = mke2fs -t ext3
-i#: 為數據空間中每多少個字節創建一個inode;此大小不應該小于block的大小
-N #:為數據空間創建個多少個inode
-I 一個inode記錄大小128---4096
-m #: 默認5%,為管理人員預留空間占總空間的百分比
-O FEATURE[,...]:啟用指定特性
-O ^FEATURE:關閉指定特性
實例1:
[root@centos7 ~]# mke2fs -t ext3 -L 'MYDATA' -b 2048 -m 3 /dev/sdb1     #對分區sdb1進行屬性更改,更改文件系統類型為ext3,卷標為“MYDATA”,塊大小為“2048”,預留給管理員的磁盤容量為3%
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
5242880 inodes, 41943040 blocks
1258291 blocks (3.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=578813952
2560 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 
	2048000, 3981312, 5619712, 10240000, 11943936, 35831808, 39337984

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done     

[root@centos7 ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="MYDATA" UUID="bf693262-f95c-4248-b8cb-f3d6bc6fd1ad" SEC_TYPE="ext2" TYPE="ext3"

二、磁盤分區掛載

什么是掛載?

   將額外文件系統與根文件系統某現存的目錄建立起關聯關系,進而使得此目錄做為其它文件訪問入口的行為

如何掛載?

mount 命令

簡介:
mount - mount a filesystem
格式:
mount [-lhV]
mount -a [-fFnrsvw] [-t vfstype] [-O optlist]
mount [-fnrsvw] [-o option[,option]...]  device|dir
mount [-fnrsvw] [-t vfstype] [-o options] device dir
選項:
-t vsftype:指定要掛載的設備上的文件系統類型
-r: readonly,只讀掛載
-w: read and write, 讀寫掛載
-n: 不更新/etc/mtab,相當于#mount
-a:自動掛載所有支持自動掛載的設備(定義在了/etc/fstab文件中,且掛載選項中有auto功能)
-L 'LABEL': 以卷標指定掛載設備
-U 'UUID': 以UUID指定要掛載的設備
-B, --bind: 綁定目錄到另一個目錄上
查看內核追蹤到的已掛載的所有設備:cat /proc/mounts
內置選項:
-o options:(掛載文件系統的選項),多個選項使用逗號分隔
      async:異步模式
      sync:同步模式,內存更改時,同時寫磁盤
      atime/noatime:包含目錄和文件
      diratime/nodiratime:目錄的訪問時間戳
      auto/noauto:是否支持自動掛載,是否支持-a選項
      exec/noexec:是否支持將文件系統上運行應用程序
      dev/nodev:是否支持在此文件系統上使用設備文件
      suid/nosuid:不否支持suid和sgid權限
      remount:重新掛載
      ro:只讀
      rw:讀寫
      user/nouser:是否允許普通用戶掛載此設備,默認管理員才能掛載
      acl:啟用此文件系統上的acl功能
      
      Tips: Defaults,相當于rw, suid, dev, exec, auto, nouser, async
實例1:在根下創建一個目錄,并掛載光盤到該創建的目錄下
[root@centos7 ~]# mkdir -p /mydata     #在根目錄下創建目錄"mydata"
[root@centos7 ~]# mount /dev/cdrom /mydata     #將光盤掛載至目錄"mydata"下
mount: /dev/sr0 is write-protected, mounting read-only     #因為是光盤,因此只能讀取,不能寫入
[root@centos7 ~]#
實例2:創建一個主分區20G,并格式化文件系統為ext4、塊大小為1024、預留給管理員百分比為10%、命名卷標為"MYSQL",最后掛載到根下的mydata,且為只讀。
[root@centos7 ~]# fdisk /dev/sdb     #對硬盤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): n     #新增一個分區
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p     #創建一個主分區
Partition number (2-4, default 2): 2
First sector (20973568-419430399, default 20973568):      #回車默認選擇默認值
Using default value 20973568  
Last sector, +sectors or +size{K,M,G} (20973568-419430399, default 419430399): +20G     #給新增的分區添加20G容量
Partition 2 of type Linux and of size 20 GiB is set

Command (m for help): w     #分區完后,保持退出
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos7 ~]# partx -a /dev/sdb     #讓內核掃描硬盤sdb,并將分好的區寫入到內核
partx: /dev/sdb: error adding partition 1
[root@centos7 ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-2
[root@centos7 ~]# mke2fs -t ext4 -b 1024 -m 10 -L 'MYSQL' /dev/sdb2     #對分區sdb2進行添加屬性,文件系統類型為ext4,塊大小為1024,預留給管理員的百分比為10%,卷標名稱為‘MYSQL’
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYSQL
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 20971520 blocks
2097152 blocks (10.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=54525952
2560 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553, 
	1024001, 1990657, 2809857, 5120001, 5971969, 17915905, 19668993

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done     

[root@centos7 ~]# mount -o ro /dev/sdb2 /mydata     #將分區sdb2掛載到目錄‘mydata’下,并且為只讀
[root@centos7 ~]# mount |grep '/dev/sdb2'
/dev/sdb2 on /mydata type ext4 (ro,relatime,seclabel,data=ordered)     #顯示已掛載;而mount的命令顯示的內容基于/etc/mtab文件的內容
實例3:將分區sdb2重新掛載至/mydata下,并且為讀寫
[root@centos7 ~]# mount -o remount,rw /dev/sdb2 /mydata
[root@centos7 ~]# mount |grep '/dev/sdb2'
/dev/sdb2 on /mydata type ext4 (rw,relatime,seclabel,data=ordered)

原創文章,作者:Aleen,如若轉載,請注明出處:http://www.www58058.com/40886

(0)
AleenAleen
上一篇 2016-08-29 08:44
下一篇 2016-08-29 08:45

相關推薦

  • N28-第二周作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 命令:cp、mv、rm 1.1 cp命令 用法: cp [OPTIONS]…[-T] SOURCE DEST cp SRC DEST而言,SRC是文件時 如果目標不存在: 新建DEST,并將SRC中內容填充至DEST中 如果目標存在: 如果DEST是文件:將SRC中的內容覆蓋至DE…

    Linux干貨 2017-12-15
  • 基于mysql的數據庫分析系統(rsyslog)

    rsyslog:日志收集和存儲系統 1.事件在電腦中的日志記錄格式為:     日期時間 主機 進程[pid]:事件內容 2.rsyslog的特性:     多線程;     UDP,TCP,SSL/TLS,RELP; &nbsp…

    Linux干貨 2016-10-23
  • 管道重定向筆記作業

      標準i/o和管道 Vim f1 [root@centos7~]#]ps aux|grep vim root ?????10967(進程編號)??0.1 ?0.4 151196 ?4828 pts/0 ???S+ ??11:10 ??0:00 vim f1 root ?????11028 ?0.0 ?0.0 112660 ??968 pts/1 …

    Linux干貨 2017-11-20
  • N26 – 第一周作業

    一、計算機組成及其功能     如圖,計算機由硬件和軟件組成。其中硬件可劃分為控制器、運算器、存儲器,以及輸入和輸出設備;軟件則分為直接與硬件通信的系統軟件,以及運行在其上的各類應用軟件。      二、Linux發行版的不同系列     …

    Linux干貨 2016-12-28
  • linux文件管理類命令

    stat、alias、xargs命令使用;(非)字母,(非)數字的表示方法。

    2017-12-11
  • Centos 6 –grub legacy及grub修復

    一、CentOS 6 grub legacy:     1.啟動流程:             POST –> Boot Sequence(BIOS) –> Boo…

    Linux干貨 2016-09-21
欧美性久久久久