一、硬盘的接口类型

        从整体的角度上,硬盘接口分为IDE、SATA、SCSI、光纤通道和SAS五种,IDE接口硬盘多用于家用产品中,也部分应用于服务器,SCSI接口的硬盘则主要应用于服务器市场,而光纤通道只在高端服务器上,价格昂贵。SATA是种新生的硬盘接口类型,还正处于市场普及阶段,在家用市场中有着广泛的前景。


二、linux的硬盘分区

        linux磁盘的分区分为: primary(主分区)、extended(扩展分区)、Logical (逻辑分区)且主分区加上扩展分区的个数小于等于4个。且扩展分区最多只有一个,扩展分区是不能直接在里面写入数据的,扩展分区里面新建逻辑分区才能读写数据。如果看见一个硬盘有很多分区,则其实是在扩展分区里面新建的逻辑分区。 其中主分区从 sdb1--sdb4,而逻辑分区是从 sdb5--sdbN 


三、linux下分区工具及使用

1、linux的常用分区工具简介

        linux下常用的分区工具有fdisk 和parted两种,这两种最主要的区别为fdisk分区只能分2T以下的硬盘,而parted分区工具能分大于2T的硬盘,本篇使用的为fdisk分区工具。


2、fdisk分区工具使用前准备

        在机器上装块硬盘或者在虚拟机上自己手动操作添加一块虚拟硬盘,之后在终端上用fdisk -l 查看系统上所有硬盘的信息,同时根据输出的信息可以分析出哪块硬盘还未使用,本例的硬盘信息为/dev/sdb,大小20G。


3、使用fdisk分区工具进行分区 

[root@hpf-linux~]# fdisk /dev/sdb        //对/dev/sdb进行分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xb7c76144.
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): m                    //输入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): p         //输入p可以查看此块硬盘还未分区

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0xb7c76144

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n             //添加分区
Command action
   e   extended                     //扩展分区
   p   primary partition (1-4)      //主分区
p
Partition number (1-4): 1           //一般主分区为1-3、扩展分区为4
First cylinder (1-2610, default 1):            
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G           

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0xb7c76144

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux

        通过上面的步骤现已可以自己创建分区了,为减少篇幅继续添加的就不列出了,下面是自己创建后的分区,其中1-3为主分区,4为扩展分区,5为逻辑分区同时还可以继续添加分区直到把逻辑分区用完为止。

[root@hpf-linux~]# fdisk -l /dev/sdb     //列出/dev/sdb的分区信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0xb7c76144
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655         916     2104515   83  Linux
/dev/sdb3             917        1309     3156772+  83  Linux
/dev/sdb4            1310        2610    10450282+   5  Extended
/dev/sdb5            1310        1571     2104483+  83  Linux


4、查看linux内核识别的分区信息

[root@hpf-linux ~]# partx -a /dev/sdb     //让内核重新读取硬件分区表
[root@hpf-linux ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdb3  /dev/sdb4  /dev/sdb5


四、linux下格式化

1、格式化命令mke2fs介绍

常用选项:

   -t:指定文件类型 {ext2|ext3|ext4}

   -j:相当于格式化为ext3文件系统

   -L label: 指定卷标,

   -b{1024|2028|4096}:指定块大小

   -i # :#个字节给指定一个indone

   -N # : 直接指定预留多少个indone

   -I # : 指定Inode大小

   -m # :预留给管理员的空间百分比,默认为5

   -O :指定分区特性

配置文件:/etc/mke2fs.conf 


2、mke2fs命令的应用

[root@hpf-linux ~]# mke2fs -t ext3 -b 2048 -m 3 /dev/sdb5   //格式化为ext3系统、分区块为2048字节、分区预留的空间改为所占总空间的3%,默认为5%,
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=2048 (log=1)
分块大小=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
131560 inodes, 1052240 blocks
31567 blocks (3.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=538968064
65 block groups
16384 blocks per group, 16384 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@hpf-linux~]# mke2fs -t ext3 -L DATE -m 3 /dev/sdb1  //文件系统为ext3、卷标为DATE、
分区预留的空间改为所占总空间的3%
mke2fs 1.41.12 (17-May-2010)
文件系统标签=DATE
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328656 inodes, 1313305 blocks
39399 blocks (3.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments per group
8016 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736

正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

        在格式化时可能会遇到/dev/sdb5 is apparently in use by the system; will not make a 文件系统 here!的错误,解决方法链接:

http://cuchadanfan.blog.51cto.com/9940284/1671337


3、扩展命令:

查看更改卷标命令e2label以及简单使用:

[root@hpf-linux~]# e2label /dev/sdb1          //查看卷标
DATE
[root@hpf-linux~]# e2label /dev/sdb1 MYDATE   //更改卷标
[root@hpf-linux~]# e2label /dev/sdb1
MYDATE


查看分区的UUID和TYPE命令blkid 以及简单使用:

[root@hpf-linux ~]# blkid /dev/sdb2   //不显示信息是因为/dev/sdb2还未格式化
[root@hpf-linux ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="MYDATE" UUID="c15e1494-00b0-4e67-bd9b-f575bee8824d" SEC_TYPE="ext2" TYPE="ext3"


dumpe2fs 查看超级块和是否有碎片:

   dumpe2fs -h /dev/sdb1     //  -h:仅显示超级块中保存的信息

调整文件系统的信息(块大小不能被调整)命令tune2fs:

常用选项

  -l:查看超级块中的信息   //和dumpe2fs -h 类似

  -L: 设定卷标

  -m:预留管理员的空间百分比

  -j:如果原来的文件系统为ext2,-j能够将其提升为ext3,不会影响数据

  -o:[^]mount-options[,...] 指定默认挂载选项

  -O:[^]feature[,...] 调整分区特性  加^表示关闭,不加表示开启

     tune2fs -o 挂载选项 设备

     tune2fs -o ^设备选项 取消


文件系统检测命令fsck

    -t 文件类型 设备

    -f 强行检测

    -a 自动修复错误

    -r 交互式修复错误


ext系统专用的检测文件命令e2fsck :

       -t 指定时间

       -y 自动回答为yes

       -f 强行检测


五、linux下挂载硬盘

手动挂载

1、mount挂载的选项

mount [options] -t 文件类型 -o option 设备 挂载点 (系统的空闲目录)

            [options] : 命令的选项

            -o option:挂载时启用分区特性

   -n:不更新/etc/mtab文件

   --bind:dir1 dir2 将目录挂载到目录上,使得dir2也能访问dir1的文件

   -t fstype

   -r 只读挂载

   -w:读写挂载

   -L lable 以卷标指定,也可以使用LABLE="lable"

   -U UUID:使用UUID挂载,也可以使用 UUID="uuid"

   -o options:挂载时启动分区特性

     async:异步I/O

     sync:同步I/O

     noatime/atime 是否更新文件时间戳,不是特别重要的文件,建议noatime

     auto: 是否能够被mount -a 自动挂载所有(/etc/fstab中)的文件自动挂载

     dev/nodev:是否能创建设备文件

     diratime/nodirtime:是否更新目录的时间戳

     exec/noexec:是否允许执行二进制程序

     _netdev:网络设备

     remount:重新挂载

     relatime/norelatime 是否实时更新

     acl 文件访问控制列表


2、应用举例

直接挂载并没有添加选项:

[root@hpf-linux~]# mkdir /mydata    
[root@hpf-linux~]# mount /dev/sdb1 /mydata/
[root@hpf-linux~]# ls /mydata/
lost+found


指定卷标进行挂载,挂载目录的变动并不影响数据,同时还可以重新指定挂载选项并挂载,在设置为只读后该目录不能进行写操作:

[root@hpf-linux ~]# mkdir /mnt/data
[root@hpf-linux ~]# blkid /dev/sdb3
/dev/sdb3: LABEL="DATA" UUID="9d50fe71-5657-40c4-84d7-b9ac4e6663ee" TYPE="ext4" 
[root@hpf-linux ~]# mount -L DATA /mnt/data/
[root@hpf-linux ~]# ls /mnt/data/
lost+found
[root@hpf-linux ~]# cd /mnt/data/
[root@hpf-linux data]# cp /etc/[a-b]* .
[root@hpf-linux data]# ls
adjtime  aliases  aliases.db  anacrontab  asound.conf  bashrc  lost+found
[root@hpf-linux ~]# umount /mnt/data/         //卸载命令
[root@hpf-linux ~]# mkdir /tmp/data
[root@hpf-linux ~]# mount -L DATA /tmp/data/
[root@hpf-linux ~]# ls /tmp/data/
adjtime  aliases  aliases.db  anacrontab  asound.conf  bashrc  lost+found
[root@hpf-linux ~]# mount
/dev/sdb3 on /tmp/data type ext4 (rw)
[root@hpf-linux ~]# mount -o remount,ro,acl /dev/sdb3
[root@hpf-linux ~]# mount
/dev/sdb3 on /tmp/data type ext4 (ro,acl)
[root@hpf-linux ~]# cd /tmp/data/
[root@hpf-linux data]# touch 1.txt
touch: 无法创建"1.txt": 只读文件系统


3、扩展学习:

有时在卸载时会出现下面的错误:

[root@hpf-linux~]# umount /tmp/data/
umount: /tmp/data: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@hpf-linux ~]# fuser -v /tmp/data/        //查看哪个进程占用该目录
                     用户     进程号 权限   命令
/tmp/data/:          root       1908 ..c.. bash
[root@hpf-linux ~]# fuser -km /tmp/data/      //踢掉占用该目录的进程
/tmp/data/:           1908c
[root@hpf-linux ~]# umount /tmp/data/


4、开机自动挂载:

自动挂载的配置文件

[root@hpf-linux~]# cat /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Mon May 25 00:42:34 2015
#
# 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=c066198c-a293-488b-a4fd-9497718c8acb    /              ext4    defaults        1 1
UUID=c2b2d941-4420-4a73-beed-8c519fc64537   /boot           ext4    defaults        1 2
UUID=9ef64966-018c-4bb5-a5ea-6c060541d387   /home           ext4    defaults        1 2
UUID=aa95be7d-9863-4782-9ca7-4fd62e601d15   swap            swap    defaults        0 0
tmpfs                   /dev/shm                            tmpfs   defaults        0 0
devpts                  /dev/pts                            devpts  gid=5,mode=620  0 0
sysfs                     /sys                              sysfs   defaults        0 0
proc                    /proc                               proc    defaults        0 0


每一项的意义:

要挂载的设备:可以用设备文件、LABEL=”卷标名” 、UUID 来指定

挂载点:有的文件系统没有挂载点  swap没有挂载点,挂载点为swap

文件系统类型:

挂载选项:多个选项间使用逗号分隔

转储频率:

    0:从不备份

    1:每日备份

    2:每隔一天备份

自检次序

    1:首先自检,通常只能被/使用

    2-9:顺序

    0:从不自检


使用fstab配置文件挂载硬盘:

[root@hpf-linux ~]# vim + /etc/fstab    //在最后一行添加以下内容
/dev/sdb3               /tmp/data               ext4    defaults,ro,acl 0 0 
[root@hpf-linux ~]# mount             //发现配置并没有生效,要生效可以重启服务器或者使用mount -a 命令
/dev/sda5 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/sda2 on /home type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /mydata type ext3 (rw)
[root@hpf-linux ~]# mount -a          //自动挂载还未被挂载的硬盘信息
[root@hpf-linux ~]# mount 
.........
/dev/sdb3 on /tmp/data type ext4 (ro,acl)