磁盘管理



LINUX主要的一些知识—>基本分区、逻辑卷LVM、EXT3/4/XFS文件系统、磁盘限额、RAID


? 硬盘的分类及使用fdisk分区工具

1、认识硬盘的分类及特性

2、使用fdisk对磁盘进行操作,分区,格式化(重点)

3、开机自动挂载分区


? 实战使用parted分区工具及扩展swap分区

1、使用parted操作大于等于4T硬盘

2、扩展服务器swap内存空间


硬盘的分类


常见硬盘品牌:希捷  西数  日立   hp  dell


硬盘的类型

IDE   SCSI   SAS  STAT(1 -2 -3)SSD


个人理解:


SAS硬盘

中间是无口的 


STAT硬盘

中间是有口的


硬盘分两种:热插拔  非插拔  usb

硬盘的接口:STAT  SAS  PCIE  FC

硬盘工作原理:机械类 固态类 


硬盘尺寸分类

硬盘的尺寸和用途可分为:

? 0.85英寸,多用于手机等便携装置中

? 1英寸, 多用于数码相机

? 1.8英寸,用于部分笔记本电脑

? 2.5英寸,常用于笔记本电脑

? 3.5英寸,多用于台式电脑中。采用3.5"硬盘的外置硬盘盒需要外接电源


硬盘分区介绍

硬盘分区就是把一个硬盘驱动器划分为多个逻辑存储单元,我们把这些单元称为分区,通过这些分区,做为这个操作系统的管理员就可以通过使用不同的分区来执行不同的功能操作,例如:

限制应用或用户的可用空间

允许从同一个磁盘进行不同的操作系统多重启动

将系统文件与用户文件进行分隔

限制磁盘空间使用-

分区方案:MBR和GPT两种




硬盘分区符认识

注意:使用分区工具fdisk对磁盘进行操作,分区,格式化(重点)


购买设备?设备联机?磁盘分区?磁盘格式化(创建文件系统)?磁盘挂载?正常使用




1.查看当前系统有几个分区


[root@centos-6-1 ~]# ls /dev/sd*

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb

[root@centos-6-1 ~]# 


/dev/sda1   


dev:设备文件的目录

sd:SCSI硬盘 

a:第一块硬盘

1:分区号 


内核对不同接口的硬盘有不同的命名方式:

             RHEL5   RHEL6      KVM

IDE(并口):  /dev/hda  /dev/hdb     /dev/vda

STAT(串口): /dev/sda  /dev/sdb

SCSI:       /dev/sda  /dev/sdb



存储的方式:本地存储   外部存储(通过FC  nfs)


硬盘的分区方式:


硬盘大小        分区工具         分区表格式    分区数限制


2TB以下的硬盘 fdisk 、parted、cfdisk MSDOS(mbr)  支持4个分区或者3个主分区和1个逻辑分区


2TB以上的硬盘 parted 、gdisk          GPT      没有限制


注意:分区表格式转换,会导致数据丢失;从msdos转换到GPT,反之亦然



使用fdisk管理分区


在虚拟机新添加硬盘后,两种方法可以发现硬盘:

1、 重启服务器

2、 通过命令-A   echo "- - -" > /sys/class/scsi_host/host0/scan

3、 通过命令-B   for i in {1..32} ;do echo "- - -" > /sys/class/scsi_host/host$i/scan;done

fdisk:磁盘分区,是Linux发行版本中最常用的分区工具

用法:fdisk [选项] device

[root@centos-6-1 ~]# fdisk /dev/sda


Command (m for help): m #查看帮助

Command action

   a   toggle a bootable flag     #设定硬盘启动分区

   b   edit bsd disklabel #编辑一个BSD格式的分区

   c   toggle the dos compatibility flag #编辑一个DOS兼容分区

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



验证一下:如果看不到新划分的分区,可以通过命令partprobe /dev/sdb 刷新读取一下分区表就OK了   



常用的选项 : -l 查看硬盘分区表  



1.查看硬盘分区表  

[root@centos-6-1 ~]# fdisk -l


Disk /dev/sda: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x00035f2c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26       12794   102554624   83  Linux

/dev/sda3           12794       13055     2097152   82  Linux swap / Solaris


Disk /dev/sdb: 53.7 GB, 53687091200 bytes

255 heads, 63 sectors/track, 6527 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: 0x00000000


[root@centos-6-1 ~]# 




 

例如:给/dev/sdb分区 


2.给/dev/sdb分区 

[root@centos-6-1 ~]# fdisk /dev/sdb 

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x4f3837a9.

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




3.查看/dev/sda 硬盘下一共有几个分区



Command (m for help): p


Disk /dev/sda: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x00035f2c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26       12794   102554624   83  Linux

/dev/sda3           12794       13055     2097152   82  Linux swap / Solaris


Command (m for help): 



4.给/dev/sdb进行分区 分1个10G的主分区 


[root@centos-6-1 ~]# 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 0x191a16e6.

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): n      #创建一个新的分区

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1 #创建第一个主分区

First cylinder (1-6527, default 1): #默认从1开始,按下回车即可

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +10G    #给第一块主分区从1-10G的大小 


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): p #打印分区信息


Disk /dev/sdb: 53.7 GB, 53687091200 bytes

255 heads, 63 sectors/track, 6527 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: 0x191a16e6


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1306    10490413+  83  Linux


Command (m for help): 


Command (m for help): w #保存并退出,如果不保存的话刚才做的分区都将清空

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.

[root@centos-6-1 ~]# 



cfdisk分区划分工具


如果不指定划分那个硬盘分区,默认是第一个如:/dev/sda



                                     cfdisk (util-linux-ng 2.17.2)


                                          Disk Drive: /dev/sda

                                   Size: 107374182400 bytes, 107.3 GB

                         Heads: 255   Sectors per Track: 63   Cylinders: 13054


     Name           Flags          Part Type     FS Type               [Label]            Size (MB)

 ------------------------------------------------------------------------------------------------------

                                    Pri/Log      Free Space                                    1.05    *

     sda1           Boot            Primary Linux ext3                                  209.72    *

     sda2                           Primary Linux ext3                               105015.94    *

     sda3                           Primary Linux swap / Solaris                       2147.49    *





















      [   Help   ]  [   New    ]  [  Print   ]  [   Quit   ]  [  Units   ]  [  Write   ]



                        Write partition table to disk (this might destroy data)


mkfs工具


首先需要对分区进行格式化。格式化分区非常重要,它是用来创建文件系统的,其命令为mkfs,意为make file system。


例如:

[root@panda ~]# mkfs.xfs /dev/sdb1

[root@panda ~]# mkfs.ext4 /dev/sdb2



刚才有一个实战是关于分区的,接着用上面的例子往下做


1.把/dev/sdb1分区格式化成ext4文件系统格式的


[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# ls /dev/sd*                             #查看/dev/sdb1分区

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdb1

[root@centos-6-1 ~]# mkfs.ext4

mkfs.ext4     mkfs.ext4dev  

[root@centos-6-1 ~]# mkfs.ext4  /dev/sdb1                    #给/dev/sdb1分区进行格式化,格式化成ext4格式的

mke2fs 1.41.12 (17-May-2010)

文件系统标签=

操作系统:Linux

块大小=4096 (log=2)

分块大小=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

655776 inodes, 2622603 blocks

131130 blocks (5.00%) reserved for the super user

第一个数据块=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


正在写入inode表: 完成                            

Creating journal (32768 blocks): 完成

Writing superblocks and filesystem accounting information: 完成


This filesystem will be automatically checked every 39 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@centos-6-1 ~]# echo $?

0

[root@centos-6-1 ~]# fdisk -l #查看所有分区


Disk /dev/sda: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x00035f2c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26       12794   102554624   83  Linux

/dev/sda3           12794       13055     2097152   82  Linux swap / Solaris


Disk /dev/sdb: 53.7 GB, 53687091200 bytes

255 heads, 63 sectors/track, 6527 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: 0xc29d702e


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1306    10490413+  83  Linux

[root@centos-6-1 ~]# 







手工进行挂载


挂载的限制:

? 根目录是必须挂载的,而且一定要先于其他mount point被挂载。因为mount是所有目录的根目录,其他目录都是由根目录  / 衍生出来的。

? 挂载点必须是已经存在的目录,系统不会自己创建

? 挂载点的目录可以任意指定,但必须遵守必要的系统目录架构原则

? 所有挂载点在同一时间只能被挂载一次

? 所有分区在同一时间只能挂在一次

? 若进行卸载,必须将工作目录退出挂载点(及其子目录),也就先umount之后才可以被卸载,一个挂载点可以被多个分区设备挂载



第一步:先创建一个目录做为添加硬盘设备的挂载点

[root@xuegod72 ~]# mkdir /sdb1

第二步:进行挂载,用到命令mount

mount 设备绝对路径(中间有个空格) 你建立的挂载点目录

第三步:如何进行卸载

1) umount 挂载点

2) umount 设备路径

注意:在卸载前,你要保证你不在当前挂载目录里面,保证这个目录是没有被占用的,退出你当前的挂载点

可以通过下面这种方式查看:


1.将/dev/sdb目录挂载到 /adb目录下,在使用umount卸载 


[root@centos-6-1 ~]# mkdir /adb                          #创建挂载目录

[root@centos-6-1 ~]# mount /dev/sdb1 /adb/ #挂载到到/adb/目录下

[root@centos-6-1 ~]# 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,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

/dev/sdb1 on /adb type ext4 (rw)

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# vi /adb/aaa #编辑一个文件,ctrl+z将此命令放入后台


[1]+  Stopped                 vi /adb/aaa

[root@centos-6-1 ~]# jobs #查看后台任务

[1]+  Stopped                 vi /adb/aaa


[root@centos-6-1 ~]# umount /adb/   #卸载 /adb 目录

umount: /adb: device is busy.

        (In some cases useful info about processes that use

         the device is found by lsof(8) or fuser(1))

[root@centos-6-1 ~]# lsof /adb/                         #查看adb目录所占用的进程或程序等

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

vi      1411 root    3u   REG   8,17     4096   12 /adb/.aaa.swp

[root@centos-6-1 ~]# 


[root@centos-6-1 ~]# fg                            #切换到后台的第一个进程

vi /adb/aaa

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# lsof /adb/ #查看/adb目录是否还在被占用

[root@centos-6-1 ~]# umount /adb/ #卸载/adb

[root@centos-6-1 ~]# echo $? #验证上一个命令是否执行成功0为真,除0以外都为假

0

[root@centos-6-1 ~]# 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,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

[root@centos-6-1 ~]# 





自动挂载


/etc/fstab 是自动挂载文件,开机后会读取/etc/fstab文件进行挂载,一般所有挂载点等信息都写到/etc/fstab文件中

最好指定UUID值,指定后不会导致分区混乱,因为UUID是设备唯一编号




要挂载的分区设备 挂载点 文件系统类型 挂载选项 是否备份 是否检测

/dev/sdb1        /sdb1    ext4    defaults    0         0


其中第四列:parameters-文件系统的参数


Async/sync 设置是否为同步方式运行,默认为async

auto/noauto 当下载mount -a 的命令时,此文件系统是否被主动挂载。默认为auto

rw/ro 是否以以只读或者读写模式挂载

exec/noexe 限制此文件系统内是否能够进行"执行"的操作

user/nouser 是否允许用户使用mount命令挂载

suid/nosuid 是否允许SUID的存在

Usrquota 启动文件系统支持磁盘配额模式

Grpquota 启动文件系统对群组磁盘配额模式的支持

Defaults 同时具有rw,suid,dev,exec,auto,nouser,async等默认参数的设置


第五列:是否进行备份。通常这个参数的值为0或者1


0 代表不要做备份

1 代表要每天进行操作

2 代表不定日期的进行操作



第六列:是否检验扇区:开机的过程中,系统默认会以fsck检验我们系统是否为完整


0 不要检验

1 最早检验(一般根目录会选择)

2 1级别检验完成之后进行检验



挂载方式有下面几种:

1、 通过UUID,是设备的唯一编号

2、 通过卷标 label=zhangsan  /sdb1 xfs defaults 0 0   不推荐用这种方式进行挂载

3、 通过设备的主从设备号  mknod




  转换分区类型:


  command acton (m for help):t #转换分区类型


  partition number (1-4):2 #选择第二个主分区


  hex code (type L to list codes):82 #按L可列出分区类型所对应的编码


    可以使用 “partprobe” 命令,重新探测磁盘中分区清空,     #partprobe  /dev/sdb


  格式化与挂载: (挂载目录可以自行创建也可指定存在的空目录)


  mksf.ext3 /dev/sdb1 把第二块硬盘的第一个主分区格式化为ext3


  mkswap /dev/sdb2 初始化swap区,此区不可格式化。


  mount /dev/sdb1 /mnt/d #将第一个分区挂载到d这个目录


  重启后自动挂载:vi /etc/fstab


  添加:/dev/sdb1 /mnt/d ext3 default 0 0




1.查看/etc/fstab文件内容


[root@centos-6-1 ~]# cat /etc/fstab 


#

# /etc/fstab

# Created by anaconda on Wed Jun  7 18:00:58 2017

#

# 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=bafc5fcc-f32b-493b-93fd-304bce7ad569 /                       ext4    defaults        1 1

UUID=8d5f402e-a564-4088-ba5d-6274cfe39179 /boot                   ext4    defaults        1 2

UUID=c8371ba7-8706-41bf-a8a4-925e19f4c81c 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

[root@centos-6-1 ~]# 





使用UUID实现开机自动挂载

UUID是一个标识你系统中的存储设备的字符串,其目的是帮助使用者唯一的确定系统中的所有存储设备,全球唯一,不管它们是什么类型的。它可以标识DVD驱动器,USB存储设备以及你系统中的硬盘设备等。


1.将/dev/sdb1分区UUID等写入到/etc/fstab文件中

获取设备的UUID


[root@centos-6-1 ~]# blkid #获取UUID

/dev/sda1: UUID="8d5f402e-a564-4088-ba5d-6274cfe39179" TYPE="ext4" 

/dev/sda2: UUID="bafc5fcc-f32b-493b-93fd-304bce7ad569" TYPE="ext4" 

/dev/sda3: UUID="c8371ba7-8706-41bf-a8a4-925e19f4c81c" TYPE="swap" 

/dev/sdb1: UUID="25831e3b-0d39-4ea7-a2e4-9663b4edbb54" TYPE="ext4" 

[root@centos-6-1 ~]# 


进行挂载


[root@centos-6-1 ~]# echo "UUID="25831e3b-0d39-4ea7-a2e4-9663b4edbb54" /adb  ext4 defaults 0 0"  >> /etc/fstab 

[root@centos-6-1 ~]# echo $?

0

[root@centos-6-1 ~]# cat /etc/fstab


#

# /etc/fstab

# Created by anaconda on Wed Jun  7 18:00:58 2017

#

# 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=bafc5fcc-f32b-493b-93fd-304bce7ad569 /                       ext4    defaults        1 1

UUID=8d5f402e-a564-4088-ba5d-6274cfe39179 /boot                   ext4    defaults        1 2

UUID=c8371ba7-8706-41bf-a8a4-925e19f4c81c 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

UUID=25831e3b-0d39-4ea7-a2e4-9663b4edbb54 /adb  ext4 defaults 0 0

[root@centos-6-1 ~]# mount -a 

[root@centos-6-1 ~]# 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,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

/dev/sdb1 on /adb type ext4 (rw)

[root@centos-6-1 ~]# 




3.更改/dev/sdb1分区类型,更改成LVM 


[root@centos-6-1 ~]# 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): t 

Selected partition 1

Hex code (type L to list codes): l


 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        

 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-

 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-

 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-

 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         

 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    

 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .

 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   

 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         

 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     

 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        

 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      

 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        

 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            

 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/

10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b

11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      

12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      

14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  

16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    

17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 

18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto

1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        

1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            

1e  Hidden W95 FAT1

Hex code (type L to list codes): 8e


Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.

[root@centos-6-1 ~]# echo $?


0

[root@centos-6-1 ~]# fdisk -l


Disk /dev/sda: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x00035f2c


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              26       12794   102554624   83  Linux

/dev/sda3           12794       13055     2097152   82  Linux swap / Solaris


Disk /dev/sdb: 53.7 GB, 53687091200 bytes

255 heads, 63 sectors/track, 6527 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: 0xc29d702e


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1        1306    10490413+  8e  Linux LVM

[root@centos-6-1 ~]# mkfs

mkfs          mkfs.cramfs   mkfs.ext2     mkfs.ext3     mkfs.ext4     mkfs.ext4dev  

[root@centos-6-1 ~]# mount /dev/sdb1  /adb/

[root@centos-6-1 ~]# echo $?

0

[root@centos-6-1 ~]# cd /adb/

[root@centos-6-1 adb]# ls

lost+found

[root@centos-6-1 adb]#



Parted 磁盘分区工具


parted是Linux中磁盘分区管理的高级工具,对GPT分区方案,也可以使用gdisk工具进行




1.查看所有的磁盘状态


[root@centos-6-1 ~]# parted -l 

Model: ATA VBOX HARDDISK (scsi)

Disk /dev/sda: 107GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End    Size    Type     File system     标志

 1      1049kB  211MB  210MB   primary  ext4            启动

 2      211MB   105GB  105GB   primary  ext4

 3      105GB   107GB  2147MB  primary  linux-swap(v1)



Model: ATA VBOX HARDDISK (scsi)

Disk /dev/sdb: 53.7GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type     File system  标志

 1      32.3kB  10.7GB  10.7GB  primary  ext4         lvm



错误: /dev/sdc: unrecognised disk label                                   


[root@centos-6-1 ~]# 




2.对/dev/sdc硬盘进行分区格式化


[root@centos-6-1 ~]# parted /dev/sdc #对/dev/sdc硬盘进行分区 

GNU Parted 2.1

使用 /dev/sdc

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) mklabel gpt #更改成GPT格式

(parted) mkpart primary 0  100 #本次分区大小为0-100MB 默认是(MB)

警告: The resulting partition is not properly aligned for best performance.

忽略/Ignore/放弃/Cancel? Ignore                                 #同意(Ignore)更改并保存

(parted) print                                                  #查看分区表          

Model: ATA VBOX HARDDISK (scsi)

Disk /dev/sdc: 2199GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt


Number  Start   End    Size   File system  Name     标志

 1      17.4kB  100MB  100MB               primary


(parted) q         #退出                                                       

信息: You may need to update /etc/fstab.                                  


[root@centos-6-1 ~]#



[root@centos-6-1 ~]# mkfs -t ext4 /dev/sdc1         #格式化成ext4格式的文件系统

mke2fs 1.41.12 (17-May-2010)

文件系统标签=

操作系统:Linux

块大小=1024 (log=0)

分块大小=1024 (log=0)

Stride=0 blocks, Stripe width=0 blocks

24480 inodes, 97636 blocks

4881 blocks (5.00%) reserved for the super user

第一个数据块=1

Maximum filesystem blocks=67371008

12 block groups

8192 blocks per group, 8192 fragments per group

2040 inodes per group

Superblock backups stored on blocks: 

8193, 24577, 40961, 57345, 73729


正在写入inode表: 完成                            

Creating journal (4096 blocks): 完成

Writing superblocks and filesystem accounting information: 完成


This filesystem will be automatically checked every 28 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@centos-6-1 ~]# echo $? #验证上一个命令是否执行成功0为真,除0以外都为假

0

[root@centos-6-1 ~]# mkdir /sdc1 #创建挂载目录

[root@centos-6-1 ~]# mount /dev/sdc1  /sdc1/ #挂载到/sdc1/目录下

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# df | tail -l #查看所有分区

Filesystem     1K-blocks    Used Available Use% Mounted on

/dev/sda2      100813224 1210924  94474572   2% /

tmpfs             509956       0    509956   0% /dev/shm

/dev/sda1         194241   30723    153278  17% /boot

/dev/sdc1          90443    1550     84012   2% /sdc1

[root@centos-6-1 ~]#



参数解释:

Number:这个就是分区的号举例来说,1号代表的是 /dev/sdb1 的意思;

Start:起始的磁柱位置;

End:结束的磁柱位置;

Size: 分区有多少容量;

Type:就是分区的类型,有primary, extended, logical等类型;

File system:就如同 fdisk 的 System ID 之意。




扩展swap交换分区


Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap分区中,等到那些程序要运行时,再从Swap分区中恢复保存的数据到内存中

SWAP 分区在一些情况下可以提升 Linux 操作系统性能,但在某些情况下又会成为系统运行的瓶颈,下面我们就来详细介绍下 Linux 中的 SWAP 交换分区

SWAP 交换分区是用来存放内存溢出来的数据。

我们可以将内存想像成一个盆子,运行程序时的数据都被作为水放在内存这个盆子当中。

如果你运行太多的应用程序,导致水过多而(内存这个)盆子已经装不下时,就会将多于部分的水装到 SWAP 交换分区当中。

优点:

内存用完时,提供更多运行空间

mkswap /devices (格式化成swap格式)

swapon /swap (激活/swap,加入到swap分区中)

vim /etc/fstab (开机自启动新添加的swap分区)


—>添加

/devices swap swap defaults 0 0

如果不想使用需要删除,只需要执行

#swapoff /swap





1.服务器内存溢出,装系统时新建的swap分区不够用,想在添加两块swap分区分别为100MB并测试


[root@centos-6-1 ~]# 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): p


Disk /dev/sdb: 53.7 GB, 53687091200 bytes

255 heads, 63 sectors/track, 6527 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: 0xc29d702e


   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

First cylinder (1-6527, default 1): 

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +100M


Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 2

First cylinder (15-6527, default 15): 

Using default value 15

Last cylinder, +cylinders or +size{K,M,G} (15-6527, default 6527): +100M


Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.

[root@centos-6-1 ~]# mkswap /dev/sdb1 

Setting up swapspace version 1, size = 112416 KiB

no label, UUID=e09b5c1f-118f-4510-bd9c-1ad891104dbb

[root@centos-6-1 ~]# mkswap /dev/sdb2

Setting up swapspace version 1, size = 112448 KiB

no label, UUID=66e11e3c-7a19-469c-b53b-8d0ae1ba2f25

[root@centos-6-1 ~]# ll /dev/sdb*

brw-rw----. 1 root disk 8, 16 6月  26 23:27 /dev/sdb

brw-rw----. 1 root disk 8, 17 6月  26 23:27 /dev/sdb1

brw-rw----. 1 root disk 8, 18 6月  26 23:27 /dev/sdb2

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# 

[root@centos-6-1 ~]# free -m

             total       used       free     shared    buffers     cached

Mem:           996        262        733          0          8        156

-/+ buffers/cache:         97        898

Swap:         2047          0       2047

[root@centos-6-1 ~]# swapon /dev/sdb1

[root@centos-6-1 ~]# free -m 

             total       used       free     shared    buffers     cached

Mem:           996        262        733          0          8        156

-/+ buffers/cache:         97        898

Swap:         2157          0       2157

[root@centos-6-1 ~]# swapon  /dev/sdb2

[root@centos-6-1 ~]# free -m 

             total       used       free     shared    buffers     cached

Mem:           996        262        733          0          8        156

-/+ buffers/cache:         97        898

Swap:         2267          0       2267



[root@centos-6-1 ~]# swapoff  /dev/sdb1;swapoff /dev/sdb2

[root@centos-6-1 ~]# free -m 

             total       used       free     shared    buffers     cached

Mem:           996        262        733          0          8        156

-/+ buffers/cache:         97        898

Swap:         2047          0       2047

[root@centos-6-1 ~]# 



开机自动挂载


/dev/sdb1 swap  swap   defaults  0  0 



通过文件增加SWAP空间


1.新建1个100MB的文件名字为swap_file,并添加到swap交换分区上 



[root@centos-6-1 ~]# dd if=/dev/zero   of=swap_file bs=1M   count=100

记录了100+0 的读入

记录了100+0 的写出

104857600字节(105 MB)已复制,0.0943323 秒,1.1 GB/秒

[root@centos-6-1 ~]# du -sh swap_file 

100M swap_file

[root@centos-6-1 ~]# mkswap -f swap_file 

Setting up swapspace version 1, size = 102396 KiB

no label, UUID=6ebd6614-15fd-4390-89df-94715a7d74a6

[root@centos-6-1 ~]# free -m 

             total       used       free     shared    buffers     cached

Mem:           996        365        630          0          8        256

-/+ buffers/cache:        100        895

Swap:         2047          0       2047

[root@centos-6-1 ~]# chmod 600 swap_file 

[root@centos-6-1 ~]# swapon swap_file 

[root@centos-6-1 ~]# free -m 

             total       used       free     shared    buffers     cached

Mem:           996        366        629          0          8        256

-/+ buffers/cache:        100        895

Swap:         2147          0       2147

[root@centos-6-1 ~]#