无来

不管你来还是不来
我都在这里,夜夜点亮
不是为了守候
只是为了做好我自己

0%

为了搞学习内核编译,需要安装一些开发工具包,索性把光盘镜像设置成本地yum源,这样更快些!以下是一些基本步骤:

  • 1、 首先挂载光盘到/mnt/cd下,mount /dev/cdrom /mnt/cd,这个就不多说了;

  • 2、 进入``/etc/yum.repos.d/目录下,把原来的yum源备份,我给它重新命名了。

    mv CentOS-Vault.repo CentOS-Vault.repo.bak
    mv CentOS-Debuginfo.repo CentOS-Debuginfo.repo.bak
    mv CentOS-Base.repo CentOS-Base.repo.bak
  • 3、新建一个光盘源

vim CentOS-Media.repo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# CentOS-Media.repo
#
# This repo can be used with mounted DVD media, verify the mount point for
# CentOS-6. You can use this repo and yum to install items directly off the
# DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
# yum --enablerepo=c6-media [command]
#
# or for ONLY the media repo, do this:
#
# yum --disablerepo=\* --enablerepo=c6-media [command]

[c6-media]
name=CentOS-$releasever - Media
baseurl=file:///mnt/cd
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

保存退出

  • 4、yum clean all (清除缓存)

  • 5、yum makecache (建立新缓存)

带你去探险

刘笑嫣(可乐)

过来吧,我要带你去个美丽的地方!
美丽的地方是树吗?
对,咱们去树里逛一逛!
不用怕,不用怕!
我会陪在你身边,一直等你长大!

过来吧,我要带你去个美丽的地方!
美丽的地方是花吗?
对,我要带你去欣赏花的美丽。
花蜜多么美味呀,甜甜的!
不用怕,不用怕!
我会陪在你身边,一直等你长大!

过来吧,我要带你去个美丽的地方!
美丽的地方是外面吗?
对,我要带你去外面的世界逛一逛!
那是一个多么美丽的地方呀,你也一定会很喜欢哒!
不用怕,不用怕!
我会陪在你身边,一直等你长大!

一个常见的问题,mysql 导出csv格式的语法,已经乱码问题:
由于数据库一般默认的是UTF-8 格式的字符集,而execl默认的是gbk格式的字符集,这里有两种方法解决乱码:

方法一: 先转出.txt格式的文件,然后选择用excel打开时,提示选择哪种编码打开,选择gbk即可

1
select * from mobile_order_region where school_id=6921 into outfile '/tmp/6921.txt'

方法二:导出时直接设置字符集格式:

1
2
3
4
5
6
7
8
9
mysql> select * from mobile_order_region where school_id=6921 into outfile '/tmp/6921.csv'
CHARACTER SET gbk
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Query OK, 6888 rows affected (0.11 sec)


mysql> \q

在Mysql 数据库中存在两种字符串连接操作.具体操作如下

一. 语法:

  1. CONCAT(string1,string2,…) 说明 : string1,string2代表字符串,concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL

    例1:

例2:

  1. CONCAT_WS(separator,str1,str2,…)

    说明 : string1,string2代表字符串,concat_ws 代表 concat with separator,第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。

    举例1:

    1
    2
    3
    4
    5
    6
    7
       mysql> select concat_ws('#','dbdh=','NorthEastTrcoon',null) AS dbdh_name_three;
    +-----------------------+
    | dbdh_name_three |
    +-----------------------+
    | dbdh=#NorthEastTrcoon |
    +-----------------------+
    1 row in set (0.00 sec)

    例2:

    1
    2
    3
    4
    5
    6
    7
    8
    mysql> select concat_ws(null,'dbdh=','NorthEastTrcoon',null) AS dbdh_name_fourth
    ;
    +------------------+
    | dbdh_name_fourth |
    +------------------+
    | NULL |
    +------------------+
    1 row in set (0.00 sec)

    例3:

    1
    2
    3
    4
    5
    6
    7
         mysql> select concat_ws('*','dbdh=','NorthEastTrcoon',null) AS dbdh_name_fifth;
    +-----------------------+
    | dbdh_name_fifth |
    +-----------------------+
    | dbdh=*NorthEastTrcoon |
    +-----------------------+
    1 row in set (0.00 sec)
  1. MySQL中group_concat函数
    完整的语法如下:
    group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])

基本查询

1
2
3
4
5
6
7
8
9
10
11
12
mysql> select * from stu1;
+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,逗号分隔(默认)

1
2
3
4
5
6
7
8
9
mysql> select id,group_concat(name) from aa group by id;
+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,分号分隔

1
2
3
4
5
6
7
8
9
mysql> select id,group_concat(name separator ';') from aa group by id;
+------+----------------------------------+
| id| group_concat(name separator ';') |
+------+----------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500 |
+------+----------------------------------+
3 rows in set (0.00 sec)

以id分组,把去冗余的name字段的值打印在一行,

逗号分隔

1
2
3
4
5
6
7
8
9
mysql> select id,group_concat(distinct name) from aa group by id;
+------+-----------------------------+
| id| group_concat(distinct name) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序

1
2
3
4
5
6
7
8
9
mysql> select id,group_concat(name order by name desc) from aa group by id;
+------+---------------------------------------+
| id| group_concat(name order by name desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)

还有一个简单的连接方式为: ||

1
2
3
4
5
6
7
8
9
mysql> select *  from student;
+----+------+-------+----------+------------+
| id | age | score | name | birth |
+----+------+-------+----------+------------+
| 1 | 23 | 78 | 李四 | 2017-10-10 |
| 2 | 24 | 78 | zhangsan | 2017-10-10 |
| 3 | 25 | 99 | 王五 | 2016-05-17 |
+----+------+-------+----------+------------+
3 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
mysql> select id+999,name,name+99,name+'999' from student;
+--------+----------+---------+------------+
| id+999 | name | name+99 | name+'999' |
+--------+----------+---------+------------+
| 1000 | 李四 | 99 | 999 |
| 1001 | zhangsan | 99 | 999 |
| 1002 | 王五 | 99 | 999 |
+--------+----------+---------+------------+
3 rows in set, 6 warnings (0.00 sec)

0. 写作背景

好久没有写博客了,前端时间来了一场说走就走的旅行,和爱人一起辞职,去云南玩了半个月,才刚回来,最近一段时间在找工作,没事就上网看看技术相关的文章,充充电,昨天偶尔发现一遍讲解LNMP的并发与资源分配的文章,感觉不错,和大家分享下。
很多时候面试官都会问你,你们的程序性能如何?程序的并发可以达到多少?程序的瓶颈在哪儿?为了满足业务需求应该购买多少台服务器?负载均衡中php应用服务器需要多少台?
可能这些问题在面试中会设置一个应用的场景及一些前提条件,让面试的人去设计,并提出看法建议,能够回答得很好的人还是比较少的。

1. 概念

1.1 LNMP中的N是nginx充当Web Server

内容的分发者,会在文件系统找到相应的文件,就返回给浏览器,如:nginx。如果是静态的文件,就可以直接返回,但是如果是index.php需要解析并执行的脚本文件时,Web Server就无力了,需要将请求转发给相应的脚本语言的解析器来解释并执行,最终将程序的执行结果,返回给Web Server,再返回给浏览器。

1.2 LNMP中的P是php充当后端的逻辑处理程序

那么php与nginx的常规协作方式是如何的呢?需要我们明确几个概念

1.3 cgi

通用网关接口,是HTTP协议中描述的,Web Server与后端处理程序进程间通信的协议

1.4 php-cgi

php实现了cgi协议,使得web server与php共同完成一个动态网页的请求响应

1.5 fastcgi

是为了解决cgi性能问题,而规范的另外一种协议,为什么说解决cgi性能问题,因为在面对各大中型网站的业务需求中,cgi程序表现得越来越无力,因为cgi程序在每次接收到请求时都需要启动新的进程,并初始化环境,然后执行程序,具体的协议内容,在此不引述。

1.6 php-fpm

实现了fastcgi协议,是php-cgi的进程管理器,解决高并发网站的性能问题。
在最终回答LNMP的并发考虑与资源分配还需要明确的几个概念

1.7 并发

一般由单位内完成的请求数来衡量,如,每秒事务数(TPS),每秒HTTP请求数(HPS),每秒查询数(QPS)。通常情况下,我们说PHP的并发,都是指一秒内PHP完成的动态请求的次数。如某网站高峰期的动态请求并发为5000每秒,这个数字不算太高,但也不低。一般日活跃用户数在1000万-5000万的网站应用才能达到这个级别。

1.8 性能

一般是指应用程序的处理速度,如果php的应用程序,打开一个页面(执行一个脚本程序)通常需要在50-100ms完成,这对程序的性能要求还是比较高的。但是这还仅仅只是程序处理,php处理完成之后,还要交给web server,web server再将数据返回浏览器,这中间会有一个网络延迟,通常网络正常的情况下,需要大约100ms,最终一个动态网页的请求大约200ms(理想的情况下)可以到达用户浏览器端(仅仅是一个html结构)。

#2.资源分配

2.1 php-fpm进程数

按照上面的描述,并发为5000每秒,每个请求完成大约200ms(具体页面要具体分析,这里只是一个理想值),如果只有5台PHP应用程序服务器,那么每台机器平均为并发1000每秒,如果是使用nginx+php-fpm的架构,php-fpm的php-cgi进程管理器的配置应该如何呢?我计算的结果为(具体的配置项说明在后文):

  • pm=static
  • pm.max_children=100
    上面的100是如何得来的,由于机器平均并发为1000每秒,每个动态请求的处理时间为100ms,也就是说1个php-fpm的worker处理进程在1秒内可以处理10个请求,100个php-fpm的worker处理进程,就可以处理1000个请求。
    当然需要结合服务器硬件资源来进行配置,如果配置不当,很容易在请求高峰期或者流量猛增导致服务器宕机。

2.2 网络带宽

网络带宽也会是一个重要的因素,如果你的服务处理很强,但是用户的请求和响应不能及时到达也是白忙活,这个参数如何计算呢?
并发5000每秒,每个请求的输出为20K,则5000x20K=100000K=100M
这就要求你的公网负载均衡器外网出口带宽至少要达到100M

2.3 内存

上述中100个php-fpm的worker处理进程,理论上如果服务器只运行php-fpm,那么我们可以将服务器内存的一半分配给php-fpm,通常情况下,我们可以认为一个php-fpm的worker处理进程占用内存20M,那么100x20M=2G,也就是说明服务器的内存大约为4G

2.4 CPU

由于php-fpm是一个多进程的模型应用,CPU进程调度消耗也是很大的,并且PHP应用程序有问题也会导致CPU占用率高,这就没有量化的指标,需要具体情况具体分析了。但是有一个小建议,可以部署一个crontab每隔一分钟检测cpu占用率超过多少就kill掉相应的php-fpm的worker处理进程。

2.5 php-fpm Unix Socket

如果nginx与php在同一台机器,nginx与php-fpm使用unix域套接字代替tcp socke进行通信,这个配置挺关键的,纯echo的ab测试,采用unix域套接字每秒请求数提升10%-20%

即nginx中配置:

1
fastcgi_pass unix:/data/server/var/php/php-fpm.sock;

php-fpm.conf中配置:

1
listen = /data/server/var/php/php-fpm.sock

最后遇到很多同学对php-fpm的进程管理器的核心配置不太了解,下面是我翻译的配置说明:

3. php-fpm配置项

3.1 pm

  • static 一个固定的值,由pm.max_children指定
  • dynamic 动态的(工作方式和Apache的prefork模式一致),但是保持至少一个,由
    • pm.max_children 在同一时间最大的进程数
    • pm.start_servers php-fpm启动时开启的等待请求到来的进程数
    • pm.min_spare_servers 在空闲状态下,运行的最小进程数,如果小于此值,会创建新的进程
    • pm.max_spare_servers 在空闲状态下,运行的最大进程数,如果大于此值,会kill部分进程
    • ondemand 启动时不会创建进程,当请求达到时创建子进程处理请求
    • pm.max_children 在同一时间最大的进程数
    • pm.process_idle_timeout 空闲多少秒之后进程会被kill
  • pm = static

3.2 pm.max_children

在同一时间最大的进程数

  • pm.max_children = 120

3.3 pm.start_servers

php-fpm启动时开启的等待请求到来的进程数,默认值为:min_spare_servers + (max_spare_servers – min_spare_servers) / 2

  • pm.start_servers = 80

3.4 pm.min_spare_servers

在空闲状态下,运行的最小进程数,如果小于此值,会创建新的进程

  • pm.min_spare_servers = 60

3.5 pm.max_spare_servers

在空闲状态下,运行的最大进程数,如果大于此值,会kill部分进程

  • pm.max_spare_servers = 120

3.6 pm.process_idle_timeout

空闲多少秒之后进程会被kill,默认为10s

  • pm.process_idle_timeout = 10s

3.7 pm.max_requests

每个进程处理多少个请求之后自动终止,可以有效防止内存溢出,如果为0则不会自动终止,默认为0

  • pm.max_requests = 5000

3.8 pm.status_path

注册的URI,以展示php-fpm状态的统计信息

  • pm.status_path = /status
    其中统计页面信息有:
    • pool 进程池名称
    • process manager 进程管理器名称(static, dynamic or ondemand)
    • start time php-fpm启动时间
    • start since php-fpm启动的总秒数
    • accepted conn 当前进程池接收的请求数
    • listen queue 等待队列的请求数
    • max listen queue 自启动以来等待队列中最大的请求数
    • listen queue len 等待连接socket队列大小
    • idle processes 当前空闲的进程数
    • active processes 活动的进程数
    • total processes 总共的进程数(idle+active)
    • max active processes 自启动以来活动的进程数最大值
    • max children reached 达到最大进程数的次数

3.9 ping.path

ping url,可以用来测试php-fpm是否存活并可以响应

  • ping.path = /ping

ping.response

ping url的响应正文

  • ping.response = pong

zeroconf (Zero configuration networking), is a techniques that automatically creates a usable Internet Protocol (IP) network without manual operator intervention or special configuration servers. 169.254.0.0/255.255.0.0 route is part of zeroconf under RHEL 6 / CentOS 6 or older versions. To see current routing table, enter:

1
# route -n

Sample outputs:

1
2
3
4
5
6
7
8
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
74.8x.4y.zz 0.0.0.0 255.255.255.248 U 0 0 0 eth1
10.10.29.64 0.0.0.0 255.255.255.192 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
10.0.0.0 10.10.29.65 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 74.8x.yy.zz 0.0.0.0 UG 0 0 0 eth1

Every time the server or Linux desktop boots, the zeroconf route 169.254.0.0 is enabled and added to the kernel routing table. To disable zeroconf route under RHEL / CentOS / Fedora Linux, enter:

1
# vi /etc/sysconfig/network

Append the following directive:

NOZEROCONF=yes
Save and close the file. Reboot the system / server or restart the networking service:

/etc/init.d/network restart

Verify routing table, enter:

route -n

OR

ip route

Oracle VirtualBox is a cross-platform virtualization application. It installs on your existing Intel or AMD-based computers, whether they are running Windows, Mac, Linux or Solaris operating systems. It extends the capabilities of your existing computer so that it can run multiple operating systems at the same time. Click here to read more about VirtualBox

Oracle has released VirtualBox 5.1.14 maintenance release of VirtualBox 5.1 on January 17th, 2017. In this release VirtualBox has improves stability and fixes regressions. To read more about this release read changelog.

This article will help you to install Oracle VirtualBox 5.1 on CentOS, Redhat and Fedora systems using Yum.

Step 1 – Add Required Yum Repositories

Firstly you are required to add VirtualBox yum repository in your system. Download repository file from its official site and place it under at __ /etc/yum.repos.d/virtualbox.repo __ .First navigate to /etc/yum.repos.d/ directory and use one of below commands as per your operating system.

1
2
3
4
5
6
7
# cd /etc/yum.repos.d/

For CentOS/RHEL Systems:
# wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo

For Fedora Systems:
# wget http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo

__ CentOS/RHEL __ Users also need to add EPEL yum repository using one of the following commands.

1
2
3
4
5
6
CentOS/RHEL 7, 64 Bit (x86_64):
# rpm -Uvh http://epel.mirror.net.in/epel/7/x86_64/e/epel-release-7-9.noarch.rpm

CentOS/RHEL 6, 64 Bit (x86_64):
# rpm -Uvh http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm

Step 2 – Install Required Packages

Before installing VirtualBox make sure to install all required packages to run VirtualBox like kernel-headers, kernel-devels etc. Use the following command to install required packages.

1
2
# yum install gcc make patch  dkms qt libgomp
# yum install kernel-headers kernel-devel fontforge binutils glibc-headers glibc-devel

Step 3 – Setup Environment Variable

VirtualBox installation required kernel source code to install required modules, So we need to configure environment variable __ KERN_DIR __ to which VirtualBox get kernel source code. In my case latest kernel source is available in ** 2.6.32-504.3.3.el6.x86_64 ** directory under ** /usr/src/kernels/ **. Make sure you are using correct source path.

1
# export KERN_DIR=/usr/src/kernels/2.6.32-504.3.3.el6.x86_64

Step 4 – Install Oracle VirtualBox and Setup

Use the following command to install VirtualBox 5.1 using yum command line tool. It will install the latest version of VirtualBox 5.1.x on your system.

1
# yum install VirtualBox-5.1

After installation, we need to rebuild kernel modules using the following command.

1
sudo /usr/lib/virtualbox/vboxdrv.sh setup

Step 5 – Start VirtualBox

Use following command to start VirtualBox from X windows. You can switch to GUI mode using ** init 5 ** or ** startx ** commands from terminal.

1
# virtualbox &

一、 Mac下将ISO写入U盘可使用命令行工具dd,操作如下:

1、找出U盘挂载的路径,使用如下命令:diskutil list

2、将U盘unmount(将N替换为挂载路径):diskutil unmountDisk /dev/disk[N]

3、写入U盘:sudo dd if=iso路径 of=/dev/rdisk[N] bs=1m rdisk 中加入r可以让写入速度加快

二、具体操作示例:

  1. 将iso转换成dmg 转iso 用

UDRW 替换为 UDTO

1
2
3
4
5
6
7
8
9
10
11
12
lapommedeMacBook-Pro:~ lapomme$ sudo hdiutil convert -format UDRW -o /linux.dmg kali.iso
Password:
正在读取Master Boot Record(MBR:0)…
正在读取Kali Live (Apple_ISO:1)…
正在读取(Windows_NTFS_Hidden:2)…
............................................................................
正在读取(DOS_FAT_12:3)…
..............................................................................
已耗时:10.178s
速度:288.3M 字节/秒
节省:0.0%
created: /linux.dmg
  1. 查看u盘盘符

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    lapommedeMacBook-Pro:~ lapomme$ diskutil list
    /dev/disk0 (internal, physical):
    #: TYPE NAME SIZE IDENTIFIER
    0: GUID_partition_scheme *251.0 GB disk0
    1: EFI EFI 209.7 MB disk0s1
    2: Apple_CoreStorage Macintosh HD 250.1 GB disk0s2
    3: Apple_Boot Recovery HD 650.0 MB disk0s3
    /dev/disk1 (internal, virtual):
    #: TYPE NAME SIZE IDENTIFIER
    0: Apple_HFS Macintosh HD +249.8 GB disk1
    Logical Volume on disk0s2
    E2BD4617-5A22-46A9-A6F4-D54E3EE92BBC
    Unencrypted
    /dev/disk2 (external, physical):
    #: TYPE NAME SIZE IDENTIFIER
    0: FDisk_partition_scheme *62.0 GB disk2
    1: DOS_FAT_32 UNTITLED 1 62.0 GB disk2s1
    /dev/disk3 (disk image):
    #: TYPE NAME SIZE IDENTIFIER
    0: FDisk_partition_scheme +3.1 GB disk3
    1: 0x17 3.0 GB disk3s1
    2: DOS_FAT_12 NO NAME 110.1 MB disk3s2
  2. 取消挂载U盘

    1
    2
    lapommedeMacBook-Pro:~ lapomme$ diskutil umountDisk /dev/disk2
    Unmount of all volumes on disk2 was successful
  3. 用dd命令写入U盘

说明:

(1)sudo dd if=源路径 of=/dev/r卷标 bs=1m [‘r’ 会让命令执行加快一点] [‘bs’为一次填充的容量]

(2)获取映像名称和完整路径可以直接将文件拖入终端,即在终端中显示

1
2
3
4
lapommedeMacBook-Pro:~ lapomme$ sudo dd if=/linux.dmg of=/dev/rdisk2 bs=1m
2934+1 records in
2934+1 records out
3076767744 bytes transferred in 149.567568 secs (20571089 bytes/sec)
  1. 查看磁盘进度,可以用iostat命令查看磁盘写入状态

    1
    2
    3
    4
    5
    6
    7
    lapommedeMacBook-Pro:~ lapomme$ iostat -w 2
    disk0 disk2 disk3 cpu load average
    KB/t tps MB/s KB/t tps MB/s KB/t tps MB/s us sy id 1m 5m 15m
    102.71 25 2.49 598.70 0 0.15 24.76 1 0.02 5 4 90 2.04 1.71 1.69
    512.00 48 23.93 1024.00 24 23.93 0.00 0 0.00 1 3 96 2.11 1.74 1.69
    473.00 26 11.99 1024.00 12 11.98 0.00 0 0.00 3 3 93 2.11 1.74 1.69
    491.68 50 23.99 1024.00 24 23.99 0.00 0 0.00 24 8 68 2.11 1.74 1.69
  2. 操作完毕后将U盘弹出

    1
    2
    lapommedeMacBook-Pro:~ lapomme$ diskutil eject /dev/disk2
    Disk /dev/disk2 ejected

1.导出OVA -> test.ova

2.copy到服务器&导入

1
[root@localhost vm] BoxManage import test.ovf

查看虚拟机镜像文件配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
[root@localhost vm]# VBoxManage showvminfo HKAirline
Name: HKAirline
Groups: /
Guest OS: Red Hat (64-bit)
UUID: 041f669d-2974-4322-a046-a55903285424
Config file: /root/VirtualBox VMs/HKAirline/HKAirline.vbox
Snapshot folder: /root/VirtualBox VMs/HKAirline/Snapshots
Log folder: /root/VirtualBox VMs/HKAirline/Logs
Hardware UUID: 041f669d-2974-4322-a046-a55903285424
Memory size: 2048MB
Page Fusion: off
VRAM size: 16MB
CPU exec cap: 100%
HPET: off
Chipset: piix3
Firmware: BIOS
Number of CPUs: 1
PAE: on
Long Mode: on
Triple Fault Reset: off
APIC: on
X2APIC: on
CPUID Portability Level: 0
CPUID overrides: None
Boot menu mode: message and menu
Boot Device (1): Floppy
Boot Device (2): DVD
Boot Device (3): HardDisk
Boot Device (4): Not Assigned
ACPI: on
IOAPIC: on
BIOS APIC mode: APIC
Time offset: 0ms
RTC: UTC
Hardw. virt.ext: on
Nested Paging: on
Large Pages: on
VT-x VPID: on
VT-x unr. exec.: on
Paravirt. Provider: Default
Effective Paravirt. Provider: KVM
State: powered off (since 2017-08-03T11:27:45.000000000)
Monitor count: 1
3D Acceleration: off
2D Video Acceleration: off
Teleporter Enabled: off
Teleporter Port: 0
Teleporter Address:
Teleporter Password:
Tracing Enabled: off
Allow Tracing to Access VM: off
Tracing Configuration:
Autostart Enabled: off
Autostart Delay: 0
Default Frontend:
Storage Controller Name (0): IDE
Storage Controller Type (0): PIIX4
Storage Controller Instance Number (0): 0
Storage Controller Max Port Count (0): 2
Storage Controller Port Count (0): 2
Storage Controller Bootable (0): on
Storage Controller Name (1): SATA
Storage Controller Type (1): IntelAhci
Storage Controller Instance Number (1): 0
Storage Controller Max Port Count (1): 30
Storage Controller Port Count (1): 2
Storage Controller Bootable (1): on
IDE (1, 0): Empty
SATA (0, 0): /root/VirtualBox VMs/HKAirline/HKAirline-disk001.vmdk (UUID: 6afa88f3-3035-4aa5-a940-f6ec43bc56bb)
NIC 1: MAC: 0800274D5932, Attachment: Bridged Interface 'eth0', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 2: MAC: 0800279B57E7, Attachment: Bridged Interface 'eth0', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 3: MAC: 080027716E47, Attachment: Bridged Interface 'eth0', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 4: MAC: 080027C363BB, Attachment: Bridged Interface 'eth0', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 5: disabled
NIC 6: disabled
NIC 7: disabled
NIC 8: disabled
Pointing Device: PS/2 Mouse
Keyboard Device: PS/2 Keyboard
UART 1: disabled
UART 2: disabled
UART 3: disabled
UART 4: disabled
LPT 1: disabled
LPT 2: disabled
Audio: enabled (Driver: ALSA, Controller: AC97, Codec: AD1980)
Clipboard Mode: disabled
Drag and drop Mode: disabled
VRDE: enabled (Address 0.0.0.0, Ports 3389, MultiConn: off, ReuseSingleConn: off, Authentication type: external)
Video redirection: disabled
VRDE property: TCP/Ports = "3389"
VRDE property: TCP/Address = <not set>
VRDE property: VideoChannel/Enabled = <not set>
VRDE property: VideoChannel/Quality = <not set>
VRDE property: VideoChannel/DownscaleProtection = <not set>
VRDE property: Client/DisableDisplay = <not set>
VRDE property: Client/DisableInput = <not set>
VRDE property: Client/DisableAudio = <not set>
VRDE property: Client/DisableUSB = <not set>
VRDE property: Client/DisableClipboard = <not set>
VRDE property: Client/DisableUpstreamAudio = <not set>
VRDE property: Client/DisableRDPDR = <not set>
VRDE property: H3DRedirect/Enabled = <not set>
VRDE property: Security/Method = <not set>
VRDE property: Security/ServerCertificate = <not set>
VRDE property: Security/ServerPrivateKey = <not set>
VRDE property: Security/CACertificate = <not set>
VRDE property: Audio/RateCorrectionMode = <not set>
VRDE property: Audio/LogPath = <not set>
USB: enabled
EHCI: disabled
XHCI: disabled

USB Device Filters:

<none>

Bandwidth groups: <none>

Shared folders: <none>

Video capturing: not active
Capture screens: 0
Capture file: /root/VirtualBox VMs/HKAirline/HKAirline.webm
Capture dimensions: 1024x768
Capture rate: 512 kbps
Capture FPS: 25

Guest:

Configured memory balloon size: 0 MB

3.检查服务器的网卡名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost var]# ifconfig
enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 11.0.0.30 netmask 255.255.255.0 broadcast 11.0.0.255
inet6 fe80::7627:eaff:fe0b:cbdd prefixlen 64 scopeid 0x20<link>
ether 74:27:ea:0b:cb:dd txqueuelen 1000 (Ethernet)
RX packets 1176518 bytes 1473593219 (1.3 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 629933 bytes 45138166 (43.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 83 bytes 7928 (7.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 83 bytes 7928 (7.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


5. 启动网卡配置

1
/usr/lib/virtualbox/vboxdrv.sh setup

5.1 安装内核

1
2
3
4
5
6
7
8
9
[root@localhost ~]# yum install kernel-headers-$(uname -r)
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.btte.net
* updates: mirror.bit.edu.cn
No package kernel-headers-3.10.0-327.el7.x86_64 available.
Error: Nothing to do

5.2 查看内核

1
2
3
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

5.3 下载匹配内核

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# wget ftp://ftp.pbone.net/mirror/ftp.scientificlinux.org/linux/scientific/7.0/x86_64/updates/security/kernel-devel-3.10.0-
327.el7.x86_64.rpm
--2017-08-03 09:00:48-- ftp://ftp.pbone.net/mirror/ftp.scientificlinux.org/linux/scientific/7.0/x86_64/updates/security/kernel-devel-3.10.0-327.el7.x86_64.rpm
=> ‘kernel-devel-3.10.0-327.el7.x86_64.rpm.1’
Resolving ftp.pbone.net (ftp.pbone.net)... 85.14.85.4
Connecting to ftp.pbone.net (ftp.pbone.net)|85.14.85.4|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done. ==> PWD ... done.
==> TYPE I ... done. ==> CWD (1) /mirror/ftp.scientificlinux.org/linux/scientific/7.0/x86_64/updates/security ... done.
==> SIZE kernel-devel-3.10.0-327.el7.x86_64.rpm ... 11470260
==> PASV ... done. ==> RETR kernel-devel-3.10.0-327.el7.x86_64.rpm ... done.
Length: 11470260 (11M) (unauthoritative)

100%[====================================================================================================>] 11,470,260 603KB/s in 28s

2017-08-03 09:01:21 (406 KB/s) - ‘kernel-devel-3.10.0-327.el7.x86_64.rpm’ saved [11470260]


安装依赖包

1
yum install make gcc libpcap libpcap-devel -y

安装

通过如下命令直接安装:

1
rpm -ivh kernel-devel-3.10.0-327.el7.x86_64.rpm

如果系统已经安装了较高版本的内核头文件,则需要通过如下命令实现降级:

1
rpm -Uvh --oldpackage kernel-devel-3.10.0-327.el7.x86_64.rpm

查看Bradge接口

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost vm]# VBoxManage list bridgedifs
Name: enp3s0
GUID: 33706e65-3073-4000-8000-7427ea0bcbdd
DHCP: Disabled
IPAddress: 11.0.0.30
NetworkMask: 255.255.255.0
IPV6Address: fe80:0000:0000:0000:7627:eaff:fe0b:cbdd
IPV6NetworkMaskPrefixLength: 64
HardwareAddress: 74:27:ea:0b:cb:dd
MediumType: Ethernet
Status: Up
VBoxNetworkName: HostInterfaceNetworking-enp3s0

修改网卡设备的名称

1
[root@localhost vm]# VBoxManage modifyvm HKAirline --bridgeadapter4 enp3s0

启动虚拟机

1
VBoxManage startvm HKAirline --type headless

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ cat /etc/tmpfiles.d/tmp.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.

# See tmpfiles.d(5) for details

# Clear tmp directories separately, to make them easier to override
D /tmp 1777 root root 0
D /var/tmp 1777 root root 10d

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-*
x /var/tmp/systemd-private-*
X /tmp/systemd-private-*/tmp
X /var/tmp/systemd-private-*/tmp