第1章 知识回顾:

1.磁盘组成

2.RAID级别

3.如何让系统更安全

[root@oldboyedu50-lnb ~]# md5sum oldboy.txt

890b185727556f1be31d7fe5ee5ce4dc  oldboy.txt

[root@oldboyedu50-lnb ~]# sha512sum oldboy.txt

c9a326ffb217c4dc7f72ccf02aba9abf9ec94ca40aa47d848f57741e313a7df52b80f8cca9130acc5930815a1728d93bd781b29d4598eb5cbcaf55ef6e2a7d98  oldboy.txt

4.如何防止系统中×××

5磁盘接口 磁盘组成

6如何进行计算

实例1-1              如何查看内存使用情况

[root@oldboyedu50-lnb ~]# free -h

             total       used       free     shared    buffers     cached

Mem:          1.8G       1.6G       195M       236K       106M       1.3G

-/+ buffers/cache:       189M       1.6G   

Swap:         767M         0B       767M

实例1-2              磁盘分区

1.2 Linux启动流程:

1. 开机自检BIOS

2. MBR引导

3. GRUB菜单

4. 加载内核

5. 运行init进程 1个进程

6. 读取运行级别 /etc/inittab

7. /etc/rc.sysinit 系统初始化

8. /etc/rc3.d  根据运行级别 启动对应服务(开机自启动)

9. mingetty    登录页面

1.3 磁盘分区

1.磁盘的引导扇区  0磁头 0磁道 1扇区

MBR引导         001扇区前446字节

MBR Master Boot Record    主引导记录 引导系统启动

DPT  (Disk   Partition Table) 磁盘分区表 记录着磁盘分区从哪里开始到哪里结束

1.      主分区(primary) 每个分区占用16个字节的分区表

一般一定要有

存放数据

2.      扩展分区(extended) 无法直接使用的

最多1

无法直接使用

3.      逻辑分区(logical

存放数据

1.4 磁盘分区

[root@oldboyedu50-lnb ~]# fdisk -l |grep sd[a-c]:

Disk /dev/sda: 21.5 GB, 21474836480 bytes

Disk /dev/sdc: 213 MB, 213909504 bytes

Disk /dev/sdb: 213 MB, 213909504 bytes

fdisk  创建分区

-u    磁盘分区的时候以扇区为单位默认是按照柱面

-c     关闭dos兼容模式

fdisk -cu  /dev/sdb

fdisk内部命令   

显示帮助

n  new 创建分区

显示所有分区信息

删除分区

保存并退出

退出不保存

实例1-3              创建一个10MB主分区

fdisk -cu  /dev/sdb

Command (m for help): n

Command action  #分区类型

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4):

分区号码

First sector (2048-417791, default 2048):

从哪里开始(回车 使用默认)

Last sector, +sectors or +size{K,M,G} (2048-417791, default 417791): +10M

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders, total 417792 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 identifier: 0x8dde1fa5

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       22527       10240   83  Linux

实例1-4              创建10MB的主分区 创建一个使用所有容量的扩展分区  创建两个50MB逻辑分区

[root@oldboyedu50-lnb ~]# fdisk -cu  /dev/sdb

Command (m for help): p

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders, total 417792 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 identifier: 0x8dde1fa5

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       22527       10240   83  Linux

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

e                               #创建扩展分区

Partition number (1-4): 2

First sector (22528-417791, default 22528):

Using default value 22528

Last sector, +sectors or +size{K,M,G} (22528-417791, default 417791):

Using default value 417791

Command (m for help): p

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders, total 417792 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 identifier: 0x8dde1fa5

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       22527       10240   83  Linux

/dev/sdb2           22528      417791      197632    5  Extended

Command (m for help): n    

Command action

   l   logical (5 or over)

   p   primary partition (1-4)

l

First sector (24576-417791, default 24576):

Using default value 24576

Last sector, +sectors or +size{K,M,G} (24576-417791, default 417791): +50M  #大小50MB

Command (m for help): p

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders, total 417792 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 identifier: 0x8dde1fa5

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       22527       10240   83  Linux

/dev/sdb2           22528      417791      197632    5  Extended

/dev/sdb5           24576      126975       51200   83  Linux

Command (m for help): n

Command action

   l   logical (5 or over)

   p   primary partition (1-4)

l

First sector (129024-417791, default 129024):

Using default value 129024

Last sector, +sectors or +size{K,M,G} (129024-417791, default 417791): +50M  #大小50MB

Command (m for help): p

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders, total 417792 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 identifier: 0x8dde1fa5

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       22527       10240   83  Linux

/dev/sdb2           22528      417791      197632    5  Extended

/dev/sdb5           24576      126975       51200   83  Linux

/dev/sdb6          129024      231423       51200   83  Linux

实例1-5              增加硬盘200MB,硬盘创建一个分区挂载到/data目录

1个里程碑-创建分区

Command (m for help): p

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders

Units = cylinders of 2048 * 512 = 1048576 bytes

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

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x8dde1fa5

 

   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-204, default 1):

Using default value 1

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

Using default value 204

 

Command (m for help): p

 

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders

Units = cylinders of 2048 * 512 = 1048576 bytes

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

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x8dde1fa5

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         204      208880   83  Linux

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

 

[root@oldboyedu50-lnb ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 213 MB, 213909504 bytes

64 heads, 32 sectors/track, 204 cylinders

Units = cylinders of 2048 * 512 = 1048576 bytes

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

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x8dde1fa5

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         204      208880   83  Linux

2个里程碑-通知系统sdb磁盘分区表变化

partprobe /dev/sdb

3个里程碑-创建文件系统(格式化)

            make filesystem

            mkfs

            #对每个房间装修(磁盘分区)

mkfs.ext4  /dev/sdb1

4个里程碑-关闭磁盘自动检查

This filesystem will be automatically checked every 38 mounts or

这个磁盘分区会被自动检查(没挂载38次或每隔180天)

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

自己创建的磁盘分区关闭磁盘检查.

[root@oldboyedu50-lnb ~]# tune2fs -c 0 -i 0 /dev/sdb1

tune2fs 1.41.12 (17-May-2010)

Setting maximal mount count to -1

Setting interval between checks to 0 seconds

-c  0  关闭 每挂载多少次进行一次磁盘检查

-i  0  关闭 每隔180天进行一次磁盘检查

5个里程碑-挂载

[root@oldboyedu50-lnb ~]# mkdir -p /data

[root@oldboyedu50-lnb ~]# mount  /dev/sdb1  /data/

[root@oldboyedu50-lnb ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda3        19G  1.5G   17G   9% /

tmpfs           931M     0  931M   0% /dev/shm

/dev/sda1       190M   40M  141M  22% /boot

/dev/sdb1       194M  1.8M  182M   1% /data

6个里程碑-永久挂载

#方法1

/etc/rc.local

#方法2

/etc/fstab  开机自动挂载

UUID=cf634253-6c41-4771-87b7-e86afb9284a7   /       ext4      defaults     1     1

UUID=8519938a-dccb-4eb5-bbbc-4fd22f9f99fe   /boot   ext4      defaults     1     2

UUID=f70549a5-ec74-4cd1-99ba-49eb175e712d   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

设备名称(分区)      载点(目录)    文件系统类型  挂载参数  是否进行备份   是否开机磁盘检查

#/dev/cdrom           /mnt              iso9660       defaults        0                 0

/dev/sdb1               /data             ext4          defaults        0                 0 

实例1-6              故障案例: java程序占用大量内存,开始使用swapswap不足

创建一个文件成为swap

[root@oldboyedu50-lnb ~]# free -h

             total       used       free     shared    buffers     cached

Mem:          1.8G       158M       1.7G       240K        11M        51M

-/+ buffers/cache:        95M       1.7G

Swap:         767M         0B       767M

1.创建一个100M的文件

[root@oldboyedu50-lnb ~]# dd   if=/dev/zero           of=/tmp/100m    bs=1M              count=100

                               input                  output          block

                               file                   file            size

[root@oldboyedu50-lnb ~]# file /tmp/100m

/tmp/100m: data

2.创建swap 让这个文件成为swap(格式化)

[root@oldboyedu50-lnb ~]# mkswap /tmp/100m

mkswap: /tmp/100m: warning: don't erase bootbits sectors

        on whole disk. Use -f to force.

Setting up swapspace version 1, size = 102396 KiB

no label, UUID=531f48e2-be07-4f24-86e5-dfe9ed549b16

 

[root@oldboyedu50-lnb ~]# file /tmp/100m

/tmp/100m: Linux/i386 swap file (new style) 1 (4K pages) size 25599 pages

 

3.激活swap分区

swapon /tmp/100m

[root@oldboyedu50-lnb ~]# swapon /tmp/100m

[root@oldboyedu50-lnb ~]# free  -h

             total       used       free     shared    buffers     cached

Mem:          1.8G       264M       1.6G       240K        11M       153M

-/+ buffers/cache:        98M       1.7G

Swap:         867M         0B       867M

 

4.永久增加

/etc/rc.local

/etc/fstab

#/tmp/100m               swap                    swap    defaults        0 0

print    显示分区信息

parted /dev/sdc

mktable mklabel  创建磁盘分区表 

                 gpt

                 msdos(mbr)

mkpart           创建分区

rm               删除分区

q                退出不保存

 

实例1-7              企业创建分区

1.创建分区表

(parted) mktable  gpt

(parted) p                                                               

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 214MB

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

Partition Table: gpt

 

Number  Start  End  Size  File system  Name  Flags

 

2.创建分区

(parted) mkpart  primary  0  10

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

Ignore/Cancel? i                                                         

(parted) p                                                               

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 214MB

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

Partition Table: gpt

 

Number  Start   End     Size    File system  Name     Flags

 1      17.4kB  10.0MB  9983kB               primary

非交互式创建分区

 parted  /dev/sdc  p

 parted  /dev/sdc  mktable gpt

 parted  /dev/sdc  mkpart primary 0 10 ignore

 parted  /dev/sdc  mkpart primary 10 20

 parted  /dev/sdc  p