菜鸟教你如何磁盘分区与创建文件系统以及挂载(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.178linux.com/40886

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

相关推荐

  • 文本处理工具三剑客之awk

    文本处理工具:grep,sed,awk awk:报告生成器,格式化文本输出 AWK: Aho ,Weinberger,Kernighan gawk:GNU awk gawk – pattren  scanning  and  processing  language 基本语法:gawk  [opt…

    Linux干货 2016-09-22
  • N23-卡卡琦-第一周

    1、描述计算机的组成及其功能 硬件: 控制器:是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算机各部分工作及内存与外设的访问等;运算器:运算器的功能是对数据进行各种算术运算和逻辑运算,即对数据进行加工处理;存储器:存储器的功能是存储程序、数据和各种信号、命令等信息,并在需要时提供这些信息;输入输…

    Linux干货 2016-11-16
  • 程序员的相关笑话(二)

    从前,有一个牧羊人,他有很多的羊。一天他赶着他的那群羊到了一条公路边上。突然,有一辆保时洁急驶过来,上面坐着一个年轻人人,穿着Armani的衣服,和Cerutti的皮鞋,Ray-Ban的太阳眼镜,TAG-Heuer的手表,以前Versace的领带。 他走到牧羊人面前问牧羊人:“如果我能说出你有多少只羊,你能给我一只吗?” 牧羊人看了看他那一大群数都数不过来的…

    Linux干货 2016-07-10
  • 浅谈Openssl与私有CA搭建

        随着网络技术的发展、internet的全球化,信息共享程度被进一步提高,各种基于互联网的应用如电子政务、电子商务日益增多并愈加被人们工作和生活依赖。但是,由于互联网的开放性和通用性,网络上的信息是对所有人公开的,这就使网络上的数据传输过程中存在被窃听、篡改等安全隐患,并极有可能给用户带来不可估量的损失。为此,各种保证数据在互联网上…

    Linux干货 2015-06-04
  • 一键获取MySQL账户及相关的权限

    有时候,运维同学可能在做grant授权时候,给的用户权限太高了,有风险。因此,我们接手服务器后有必要检查一遍用户权限。 #!/bin/bash# 一键获取MySQL账户及相关的权限 user=’root’ password=’123456′ MYSQL_CMD=’/usr/local/mysql/b…

    Linux干货 2017-05-07
  • 马哥教育网络班22期+第2周课程练习

    1、Linux上文件/目录相关命令有哪些? 1)目录管理类命令:mkdir、rmdir (1)mkdir:make directorise 创建文件夹  使用格式:      mkdir [OPTION]… DIRECTORY…    &nb…

    Linux干货 2016-09-08