Archives

使用 Vagrant 在 CentOS 7 平台上使用命令行模式访问VirtualBox虚拟机镜像

前面介绍过在 Windows 平台安装虚拟机解决一些生物信息问题,现在要讲的是在 Linux 平台安装和使用虚拟机, 当然虚拟机平台还是 选用 VirtualBox,测试场景还是 CentOS 7 操作系统。

在安装VirtualBox和vagrant之前,需要准备好sudo用户或者root用户权限

这里不讨论为什么不使用 Docker 而使用 VirtualBox。

1 安装 VirtualBox

直接rpm安装,也可以下载下来安装:

sudo rpm -ivh  http://download.virtualbox.org/virtualbox/5.1.26/VirtualBox-5.1-5.1.26_117224_el7-1.x86_64.rpm

这篇文档 VirtualBox 不是主角,所以写的不多。

2 安装 vagrant

直接rpm安装,也可以下载下来安装:

sudo rpm -ivh  https://releases.hashicorp.com/vagrant/2.0.0/vagrant_2.0.0_x86_64.rpm

vagrant的镜像叫 box, 这里有一个 vagrant box 仓库 vagrantbox 可以使用, 包含了各种Linux的发行版本, 提及有大有小, 看阉割的程度了。

vagrant 有丰度的命令行接口:

Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     connect         connect to a remotely shared Vagrant environment
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to HashiCorp's Vagrant Cloud
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     share           share your Vagrant environment with anyone in the world
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

下面几个命令,第一次使用会用到:

vagrant init 
vagrant up
vagrant ssh
vagrant global-status

3 安装 测试

现在 CentOS 5 以及不再维护 (维护截至日期 2017-03-31), 所以这里测试用例不再使用CentoS 5, 从vagrantbox可以选一个 CentOS 7, 最好包含 VirtualBox Guest Additions, 这样文件共享也比较方便, 不过这里演示 ssh 登录

mkdir ~/centos-7.0 && cd ~/centos-7.0
wget https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box
vagrant box add centos-7.0  centos-7.0-x86_64.box

通过 vagrant box list, 可以查看到已经加入box列表;

[biostack@localhost centos-7.0]$ vagrant  box list
centos-7.0 (virtualbox, 0)

下面可以开始挂载:

vagrant init  centos-7.0
vagrant up

初始化完后会生成一个 Vagrantfile 配置文件, 经过一系列的初始化工作,通过 global-status 可以查看已挂在的box

[biostack@localhost centos-7.0]$ vagrant global-status
id       name    provider   state   directory
------------------------------------------------------------------------
9fe8b66  default virtualbox running /home/biostack/centos-7.0

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

登录box

[biostack@localhost centos-7.0]$ vagrant ssh
Last login: Thu Jul 16 08:48:31 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$

用户名和密码都是vagrant, 并且有sudo用户权限,然后通过一些工具可以和宿主进行交互文件, 比如 rsync, 如果没安装:

sudo  yum  install rsync

轻松搞定:

从宿主机器拷贝文件:

[vagrant@localhost ~]$ rsync  -avP  biostack@192.168.0.101:/biostack/distribution/binaries/zstd-1.3.1-x86_64.tar.gz  .
The authenticity of host '192.168.0.101 (192.168.0.101)' can't be established.
ECDSA key fingerprint is 03:23:c0:ea:5e:54:81:90:3b:f7:aa:a9:5e:08:c1:87.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.101' (ECDSA) to the list of known hosts.
biostack@192.168.0.101's password:
receiving incremental file list
zstd-1.3.1-x86_64.tar.gz
     2936323 100%   56.01MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 2936797 bytes  345509.06 bytes/sec
total size is 2936323  speedup is 1.00

然后你可以做你想做的事情了。

虚拟机文件存放在:

/home/biostack/VirtualBox VMs/centos-70_default_1505385849643_38205

虚拟机可以通过 VBoxManage list vms 查看到名字和UUID

不用了清除掉虚拟机

vagrant destroy  9fe8b66

记得 下次想再使用从 vagrant up 开始,但是先前的/home/biostack/VirtualBox VMs/centos-70_default_1505385849643_38205 执行完 vagrant destroy 就没有了, 下次加载是全新的虚拟镜像。

4 结语

了解到 Vargant和 VirtualBox是通过Liheng的 Github 仓库 centos5-vm, 使用centos5-vm初衷是编译Linux平台兼容的生物信息程序,上面介绍的也比较详细了, 这里只留下 sourceforge devtools 的地址。

Vargant 还有很多丰度的功能,后面慢慢介绍。

Last update: 2017-09-14 7:04 PM

Comments are closed.