标签归档:vagrant

Vagrant CentOS 共享目录挂载问题解决

一直以来都是使用Vagrant与VirtualBox运行CentOS系统来搭建环境,然而有一天突然出现Windows下面的目录无法映射进去了,报错:

D:\project\vagrant\centos64php56>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
  default: Adapter 1: nat
  default: Adapter 2: hostonly
==> default: Forwarding ports...
  default: 80 (guest) => 8080 (host) (adapter 1)
  default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
  default: SSH address: 127.0.0.1:2222
  default: SSH username: vagrant
  default: SSH auth method: private key
  default: Warning: Connection reset. Retrying...
  default: Warning: Connection aborted. Retrying...
  default: Warning: Connection reset. Retrying...
  default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions versions on your host (5.1.26) and guest (4.3.6) do not matc
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * epel: repo.fedoralinux.ir
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
No package kernel-devel-2.6.32-358.23.2.el6.x86_64 available.
Package gcc-4.4.7-18.el6.x86_64 already installed and latest version
Package binutils-2.20.51.0.2-5.47.el6_9.1.x86_64 already installed and latest version
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package 4:perl-5.10.1-144.el6.x86_64 already installed and latest version
Package bzip2-1.0.5-7.el6_0.x86_64 already installed and latest version
Nothing to do
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box
Installing Virtualbox Guest Additions 5.1.26 - guest version is 4.3.6
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.26 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.1.26 of VirtualBox Guest Additions...
vboxadd.sh: Stopping VirtualBox Additions.
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.
Failed to set up service vboxadd, please check the log file
/var/log/VBoxGuestAdditions.log for details.
An error occurred during installation of VirtualBox Guest Additions 5.1.26. Some func
In most cases it is OK that the "Window System drivers" installation failed.
vboxadd.sh: Starting the VirtualBox Guest Additions.
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: modprobe vboxguest failed.
==> default: Checking for guest additions in VM...
  default: The guest additions on this VM do not match the installed version of
  default: VirtualBox! In most cases this is fine, but in rare cases it can
  default: prevent things such as shared folders from working properly. If you see
  default: shared folder errors, please make sure the guest additions within the
  default: virtual machine match the version of VirtualBox you have installed on
  default: your host and reload your VM.
  default:
  default: Guest Additions Version: 4.3.6
  default: VirtualBox Version: 5.1
==> default: Configuring and enabling network interfaces...
  default: SSH address: 127.0.0.1:2222
  default: SSH username: vagrant
  default: SSH auth method: private key
==> default: Mounting shared folders...
  default: /home/rc => D:/project/vagrant/centos64php56
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=500,gid=500 home_rc_ /home/rc

The error output from the command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

这里提示虚拟机里CentOS的VBoxGuestAdditions与VirtualBox的版本匹配,需要升级:

[default] GuestAdditions versions on your host (5.1.26) and guest (4.3.6) do not matc

应该是升级了VirtualBox导致的。然而自动安装新插件失败:

vboxadd.sh: Starting the VirtualBox Guest Additions.
Failed to set up service vboxadd, please check the log file
/var/log/VBoxGuestAdditions.log for details.
An error occurred during installation of VirtualBox Guest Additions 5.1.26. Some func
In most cases it is OK that the "Window System drivers" installation failed.
vboxadd.sh: Starting the VirtualBox Guest Additions.
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: modprobe vboxguest failed.

导致共享目录无法映射。但是虚拟机仍然是启动成功的,可以ssh进去,或者使用sftp挂载。
Google了下有说是VirtualBox bug的,也有说是Windows问题的,各种折腾不能解决。升级插件也无效:vagrant plugin install vagrant-vbguest。重新安装VirtualBox和Vagrant,并不会影响现有虚拟机及网络配置,但不能解决问题。
直到看到这篇文章,决定从CentOS入手解决。
启动虚拟机后,使用sftp上传VBoxGuestAdditions.iso,ssh进入手动安装:

$ sudo
$ mount VBoxGuestAdditions.iso -o loop /mnt
$ cd /mnt
$ sh VBoxLinuxAdditions.run

安装失败,查看日志:

Building the main Guest Additions module                   [FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)

vboxadd-install.log日志:

/tmp/vbox.0/Makefile.include.header:97: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again.  Stop.

参照这篇文章,查看kernel版本

$ rpm -qa kernel\* | sort
kernel-2.6.32-358.23.2.el6.x86_64
kernel-devel-2.6.32-696.10.2.el6.x86_64
kernel-firmware-2.6.32-358.10.2.el6.noarch
kernel-headers-2.6.32-696.10.2.el6.x86_64

$ uname -r
2.6.32-358.10.2.el6.x86_64

其实一开始是更多版本不匹配的,尝试更新kernel:

yum update
yum install kernel-headers kernel-devel

结果部分更新失败:

Warning: No matches found for: kernel-devel
No Matches found

参照这里解除版本锁定,设置enabled = 0:

$ vim /etc/yum/pluginconf.d/versionlock.conf

再次运行升级kernel就可以了。安装成功后,重启后,再次运行sh VBoxLinuxAdditions.run 就可以了。事实上Vagrant启动时就会自动安装VBoxGuestAdditions:

[default] GuestAdditions 5.1.26 running --- OK.

Vagrant升级到后发现vagrant up初始化下载box卡住了,那是你的vagrant版本太高与对应的powershell版本对应不上,可以下载最新的powershell安装即可。
如果box下载很慢,可以参照这里的方法自我映射取得url单独下载:

https://app.vagrantup.com/box-cutter/boxes/centos73
==> https://atlas.hashicorp.com/box-cutter/boxes/centos73/versions/2.0.21/providers/virtualbox.box

参考链接:
Guest Additions Version error on VirtualBox5
VirtualBox下挂载共享文件目录问题处理
Resolving GuestAdditions version mismatch in vagrant/homestead vm (failed to mount shared folders / modprobe vboxsf failed)
Problem installing virtualBox guest additions
Warning: No matches found for: kernel-devel
Vagrant up hangs forever on Windows 7, Vagrant 1.9.7, VirtualBox 5.1.22.r11512

Ubuntu 16.04 上安装Docker

Docker是一个轻量级的虚拟化容器技术,既可以为不同应用程序提供了隔离空间(内存,资源,网络),又可以将应用程序与环境一起打包,发布到其他机器上,避免环境的差异,这点像Vagrant。 但是,它又与传统的虚拟机不容(如VirtualBox),它的应用程序仍然是跑在宿主机上面的,并且镜像里面通常仅包含所必须的组件而非完整的系统,因此拥有极高的性能。这又有点像Python的Virtual Environment,不过Docker更加强大,本质是是个Linux,可以打包的不同语言的应用程序。
Docker要求linux内核版本大于3.10,可以在通过以下命令查看是否支持

$ uname -r
4.4.0-38-generic

需要先更新以下apt仓库

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
[/sell]
检查以下是否已经创建Docker软件仓库文件,如果没有需要手动创建,并添加以下内容

root@thinkpad:~# cat /etc/apt/sources.list.d/docker.list
deb https://apt.dockerproject.org/repo ubuntu-xenial main
[/sell]
确认是否配置成功

root@thinkpad:~# sudo apt-get update
root@thinkpad:~# apt-cache policy docker-engine
docker-engine:
  Installed: 1.12.1-0~xenial
  Candidate: 1.12.1-0~xenial
  Version table:
 *** 1.12.1-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
        100 /var/lib/dpkg/status
     1.12.0-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.2-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.1-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
     1.11.0-0~xenial 500
        500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages

Docker容器建立在Auf文件系统之上,以便支持将不同的目录挂载到同一虚拟文件系统下面,并设置不同的读写权限。需要安装linux-image-extra:

$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
[/sell]
现在可以开始安装Docker了:

$ sudo apt-get install docker-engine
$ sudo service docker start

试运行一下:

root@thinkpad:~# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

如果你本地没有hello-world这个镜像,它是会自动去Docker官网上下载的。
查看最近运行过的容器,不加参数-l,是查看正在运行的容器

root@thinkpad:~# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
1931765938dc        hello-world         "/hello"            4 minutes ago       Exited (0) 4 minutes ago                       suspicious_lichterman

运行镜像的时候也可以指定名称(而不是suspicious_lichterman)

root@thinkpad:~# docker run --name hello hello-world

Hello from Docker!
...

root@thinkpad:~# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
e4773606e6cf        hello-world         "/hello"            7 seconds ago       Exited (0) 6 seconds ago                       hello

但是再次运行同样名称为hello的容器时会报错

root@thinkpad:~# docker run --name hello hello-world
docker: Error response from daemon: Conflict. The name "/hello" is already in use by container e4773606e6cf4222b786d5301ab38e4dddb90e11a0358f64d33109bf829f0b5a. You have to remove (or rename) that container to be able to reuse that name..
See 'docker run --help'.

这时候需要先把旧的给删掉,再运行就不会了

root@thinkpad:~# docker rm hello
hello

通过Docker stop停止运行指定容器。
查看本地的Docker镜像

root@thinkpad:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-php-fpm       phalcon             1c97ee169a55        3 hours ago         364.5 MB
nginx-php-fpm       latest              4fc9ac9f2945        7 hours ago         228.5 MB
ubuntu              16.04               c73a085dc378        47 hours ago        127.1 MB
ubuntu              latest              c73a085dc378        47 hours ago        127.1 MB
nginx               mainline-alpine     00bc1e841a8f        5 days ago          54.21 MB
mysql               5.7                 4b3b6b994512        5 weeks ago         384.5 MB
hello-world         latest              c54a2cc56cbb        12 weeks ago        1.848 kB

这里先简单的介绍Docker的安装、运行,后续将介绍Docker命令行交互,网络,文件存储等。

参考链接:
Docker Overview
Docker:利用Linux容器实现可移植的应用部署
Installation
on Ubuntu

Docker:具备一致性的自动化软件部署
Docker — 从入门到实践
Docker简介与入门
剖析Docker文件系统:Aufs与Devicemapper
What is the runtime performance cost of a Docker container
Docker – The Linux Container
当流浪者(Vagrant)遇见码头工人(Docker): 实战

Windows 8 安装Vagrant和VirtualBox

由于php的一些扩展只有在linux下面才能用,就算是Cygwin下面也不行,例如pcntl。所以就决定安装一个linux虚拟机来作为开发环境,顺带找到了另外一个东西:Vagrant。Vagrant作为一款虚拟机环境统一配置管理工具,后端可以是VirtualBox,VMWare,AWS。这样当运行环境配置完了可以方便的部署在其他机器上或给予其他开发人员,实现配置一次,到处运行的功能。当使用Vagrant管理VirtualBox是运行在命令行下面的,而不需要打开界面。

首先,安装VirtualBox,地址

然后,安装Vagrant,地址。安装完成后,将Vagrant的bin目录添加到系统的环境变量Path里面。

再然后,下载做好的虚拟机镜像,地址。我这里下载的是Ubuntu precise 64 VirtualBox,对应链接:http://files.vagrantup.com/precise64.box。这个镜像的下载速度还不错,centos的就不怎么样了,但还是建议先下载到本地硬盘在加载进来。

这时候应该先为这个虚拟机(Ubuntu precise 64)建一个目录,以便初始化和管理这个虚拟机主机。如果有多个虚拟机的话,就分别建立不同的目录来初始化。比如:E:\project\vagrant\dev。然后就在这个目录下面初始化这个镜像。

E:\project\vagrant\dev>vagrant box add Ubuntu12.04x64 "file:///f:\box\precise64.box"

注意,file后面应该是三个/而不是两个。add后面跟的是这个虚拟机的名称,也可以用base自动识别。
打开对应目录下面的配置文件Vagrantfile,配置虚拟机的ip和共享目录。

 config.vm.network "private_network", ip: "192.168.33.10"
 config.vm.synced_folder "E:/wamp/www", "/home/vagrant/www" #将前者映射到后者

初始化并启动:

E:\project\vagrant\dev>vagrant init Ubuntu12.04x64
E:\project\vagrant\dev>vagrant up

vagrant up
然后可以通过ssh连接上去管理了,这里使用Cygwin下面的ssh客户端,默认用户和密码都是vagrant。
vagrant ssh

sudo apt-get update #先更新下软件包,要不然有的可能会装不上

注意启动和连接虚拟机都需要切换到该虚拟机所在开发目录。还有一些其他的命令。

vagrant init  # 初始化
vagrant up  # 启动虚拟机
vagrant halt  # 关闭虚拟机
vagrant reload  # 重启虚拟机
vagrant ssh  # SSH 至虚拟机
vagrant status  # 查看虚拟机运行状态
vagrant destroy  # 销毁当前虚拟机
vagrant package  # 导出当前虚拟机

这里有别人推荐的一个小软件win-sshfs,可以将Linux的目录直接mount到Windows系统上作为一个根盘符。注意:这是个32位软件,可能会安装不成功,需要采用兼容模式。
sshfs
然后就可以在我的电脑里面像本地文件一样管理。

参考链接:
使用 Vagrant 打造跨平台开发环境
使用vagrant和win Sshfs支持openstack开发
Vagrant安装配置
Windows 7使用Vagrant构建虚拟开发环境
Vagrant+VirtualBox搭建统一开发环境