Total Pageviews

Showing posts with label VirtualBox. Show all posts
Showing posts with label VirtualBox. Show all posts

Sunday, 22 September 2019

在 mac上的VirtualBox 里面安装最小化的 Linux 开发环境

准备工具

VirtualBox下载地址:https://www.virtualbox.org/wiki/Downloads Fedora Server 下载地址:https://getfedora.org/zh_CN/server/download/
在 OSX 下安装一个无桌面的 Linux 开发环境。主要完成三个目标:
  1. 剪贴板共享
  2. 文件共享
  3. 使用 linux 的命令行和开发环境(使用 ssh 配合 zsh+tmux+vim)

虚拟机设置

Virtual Box 关键配置如下:
  1. 常规 - 高级 - 共享剪贴板:双向
  2. 系统 - 主板 - 内存大小:足够大
  3. 系统 - 主板 - 启动顺序:硬盘 > 光驱
  4. 系统 - 处理器 - 处理器数量:最大
  5. 存储 - 存储介质 - 光驱:Fedora ISO
  6. 网络 - 网卡 1 - 网络地址转换(NAT)
  7. 网络 - 网卡 2 - 仅主机 (Host-Only) 网络:选择一个接口
    • 这个需要先在全局工具 - 主机网路管理器里创建
    • 设置 IPv4 地址和掩码,不需要启用 DHCP 服务器
  8. 共享文件夹:共享代码文件夹
备注:Virtual Box 可以使用 shift + 启动 启动虚拟机但不打开虚拟机 UI 界面

安装 Fedora Server 和 VBox 组件

安装 Fedora,过程中需要配置网卡,第一张网卡无需配置,主要是第二张网卡的 IP 地址和掩码需要设置一下。
安装完成后,重启,弹出光驱。然后在虚拟机界面的菜单中插入 VBox 的组件:Devices -> Insert Guest Additions CD Image…
更新系统:
dnf update kernel*
dnf install gcc kernel-devel kernel-headers dkms make bzip2 perl
dnf install xorg-x11-server-Xorg libXrandr
dnf install xclip
reboot

mkdir /media/cdrom
mount -r /dev/cdrom /media/cdrom
cd /media/cdrom
export KERN_DIR=/usr/src/kernels/`uname -r`/build
./VBoxLinuxAdditions.run
reboot
基础组件安装完成后,开机需要启动两个服务:
  1. X :0:用于提供剪切板服务,指定显示编号为 :0
  2. DISPLAY=:0:环境变量,用于给各种需要剪贴板的服务指定显示编号
  3. VBoxClient –clipboard:用于同步主机和虚拟机的剪贴板
开机自启动服务:
/etc/systemd/system/x.service
[Unit]
Description=X Server
Requires=network.target

[Service]
Type=simple
ExecStart=/usr/bin/X :0

[Install]
WantedBy=multi-user.target
/etc/systemd/system/vboxclient.service
[Unit]
Description=VBoxClient
After=x.service

[Service]
Type=forking
ExecStart=/usr/bin/bash /usr/bin/StartVBoxClient.sh

[Install]
WantedBy=multi-user.target
/usr/bin/StartVBoxClient.sh
#/bin/bash

set -e;

# waiting for x server running
sleep 3;

DISPLAY=:0 /usr/bin/VBoxClient --clipboard;
可以通过以下命令检查剪贴板同步是否正常(需要 DISPLAY 环境变量):
写入剪贴板:
echo test | xclip -sel clipboard -i
读取剪贴板:
xclip -sel clipboard -o
挂载共享文件夹:
mount -t vboxsf dev /root/dev

Saturday, 28 January 2017

创建虚拟机/容器镜像的工具-packer

Packer is a tool for creating machine and container images for multiple platforms from a single source configuration. 

Works Great With

Out of the box Packer comes with support to build images for Amazon EC2, CloudStack, DigitalOcean, Docker, Google Compute Engine, Microsoft Azure, QEMU, VirtualBox, VMware, and more. Support for more platforms is on the way, and anyone can add new platforms via plugins.

from https://www.packer.io/
----------------

The images that Packer creates can easily be turned into Vagrant boxes.

Quick Start

Download and install packages and dependencies
go get github.com/mitchellh/packer
Note: There is a great introduction and getting started guide for those with a bit more patience. Otherwise, the quick start below will get you up and running quickly, at the sacrifice of not explaining some key points.
First, download a pre-built Packer binary for your operating system or compile Packer yourself.
After Packer is installed, create your first template, which tells Packer what platforms to build images for and how you want to build them. In our case, we'll create a simple AMI that has Redis pre-installed. Save this file as quick-start.json. Export your AWS credentials as the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.
{
  "variables": {
    "access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
    "secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}"
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `access_key`}}",
    "secret_key": "{{user `secret_key`}}",
    "region": "us-east-1",
    "source_ami": "ami-de0d9eb7",
    "instance_type": "t1.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-example {{timestamp}}"
  }]
}
Next, tell Packer to build the image:
$ packer build quick-start.json
...
Packer will build an AMI according to the "quick-start" template. The AMI will be available in your AWS account. To delete the AMI, you must manually delete it using the AWS console. Packer builds your images, it does not manage their lifecycle. Where they go, how they're run, etc. is up to you.

Documentation

Comprehensive documentation is viewable on the Packer website:
http://www.packer.io/docs

from https://github.com/mitchellh/packer

 

Thursday, 9 June 2016

docker-phpvirtualbox


phpVirtualBox is a modern web interface that allows you to control remote VirtualBox instances - mirroring the VirtualBox GUI. This is a docker image that eases setup.

About phpVirtualBox

From the official description:
An open source, AJAX implementation of the VirtualBox user interface written in PHP. As a modern web interface, it allows you to access and control remote VirtualBox instances. phpVirtualBox is designed to allow users to administer VirtualBox in a headless environment - mirroring the VirtualBox GUI through its web interface.

Usage

This docker image is available as a trusted build on the docker index. Using this image for the first time will start a download automatically. Further runs will be immediate, as the image will be cached locally.
This image provides the phpVirtualBox web interface that communicates with any number of VirtualBox installations on your computers.

As per the above diagram the following examples assume we have two computers PC2 and PC3 in our cluster that we want to manage through a single installation of phpVirtualBox.
Applying the same scheme to any number of computers running Virtualbox should be fairly straight forward. Also, the same scheme applies if you only want to manage a single PC, which hosts both VirtualBox and this image.
Internally, the phpVirtualBox web interface communicates with each VirtualBox installation through the vboxwebsrv program that is installed as part of VirtualBox. So for every computer connected to the phpVirtualbox instance, we're going to use a minimal container that eases exposing the vboxwebsrv.
For PC2:
$ docker run -it --name=vb2 clue/vboxwebsrv vbox@10.1.1.2
And for PC3:
$ docker run -it --name=vb3 clue/vboxwebsrv myuser@10.1.1.3
This will start an interactive container that will establish a connection to the given host. To establish an encrypted SSH connection it will likely ask for your password for each user. This is the user that runs your virtual machines (VMs). See clue/vboxwebsrv for more details.
Some background: The official phpVirtualBox readme describes setting up the vboxwebsrv daemon so that it is automatically started when the machine boots up and is exposed over the network. Instead of requiring this upfront configuration, we only spawn the vboxwebsrv on demand and expose its socket only through an encrypted SSH tunnel.
In fact, if you already have your vboxwebsrv set up, you don't have to rely on the clue/vboxwebrv container. In this case, you can substitute the following link by supplying ENV variables instead. You can also pass a visual name like this:
-e VB3_PORT_18083_TCP=10.1.1.4:18083 -e VB3_NAME=MyServer
Now that all vboxwebsrv instance are up, we can link everything together and start our actual phpVirtualBox container. The recommended way to run this container looks like this:
$ docker run -d --link vb2:FirstPC --link vb3:MyLaptop -p 80:80 clue/phpvirtualbox
You can now point your webbrowser to this URL:
http://localhost
This is a rather common setup following docker's conventions:
  • -d will run a detached session running in the background
  • -p {OutsidePort}:80 will bind the webserver to the given outside port
  • --link {ContainerName}:{DisplayName} links a vboxwebsrv instance with the given {ContainerName} and exposes it under the visual {DisplayName}
  • clue/phpvirtualbox the name of this docker image
from  https://github.com/clue/docker-phpvirtualbox

Saturday, 17 October 2015

在VirtualBox中配置(刚创建好的)Ubuntu虚拟机

“在VirtualBox中创建Ubuntu虚拟机”是容易的,但“在VirtualBox中配置(刚创建好的)Ubuntu虚拟机”就不那么容易了,然后“在VirtualBox中安装(刚配置好的)Ubuntu虚拟机”则更是一件易事。

http://www.crifan.com/virtualbox_create_new_ubuntu_virtual_machine/ (在VirtualBox中创建Ubuntu的虚拟机)
http://www.crifan.com/virtualbox_configure_newly_created_ubuntu_virtual_machine/ (在VirtualBox中配置刚创建好的Ubuntu虚拟机。建议用可以抓取滚动页面的抓图程序-snagit把整个页面截图保存)
http://www.crifan.com/virtualbox_install_previously_configured_ubuntu_virtual_machine/ (在VirtualBox中安装刚配置好的Ubuntu虚拟机

Friday, 30 August 2013

通过命令获取VirtualBox虚拟机Guest的IP地址

需要在虚拟机和主机之间同步代码和文件。Guest通过Bridge方式上网。我的想法是写个脚本用rsync同步,可是由于Bridge进局域网之后,IP会随DHCP配置改变而改变,因此需要通过命令获取Guest对应的IP地址。(我知道可以用Shared Folder,但是我总是配置不好那个Guest Addition。而且那个东西效率差的一X)
查了文档,发现这个问题不是很复杂。用VBoxManage的命令可以方便的获取IP地址。下面命令会返回一个差不多我们想要的:
VBoxManage guestproperty get "abc" /VirtualBox/GuestInfo/Net/0/V4/IP
其中abc是你虚拟机的名称,你可以使用如下命令列出所有注册过的虚拟机
VBoxManage list vms
后面那串乱码是所谓的UUID,在前一个代码里的"abc"也可以用它对应的UUID代替,那样你的代码会显得,俄,更加犀利以及难懂一些~_^
也许某些同学会注意到,命令除了返回IP地址之外,还有一个Value:。这些字符如果传给ssh,rsync等程序必然会引起错误。解决方法是用 tr命令在输入之前把多余的字符去掉,注意,返回值末尾有一个\n,同样需要去掉。以下代码用于自动在主机的a文件夹和Guest的a文件夹之间同步。
guestIP=`VBoxManage guestproperty get "abc" /VirtualBox/GuestInfo/Net/0/V4/IP | tr -d "[\"Value: \"\n]"`
rsync -avz -e ssh ronald@$guestIP:/home/ronald/a a
ronald是俺在Guest中的用户名.