Skip to content

Commit

Permalink
add basic ssh support
Browse files Browse the repository at this point in the history
  • Loading branch information
jesa7955 committed Jun 26, 2016
1 parent 4fd4967 commit 280e1e1
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 110 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -12,3 +12,5 @@
/example_box/*img
/example_box/*raw
/bin/
/Vagrantfile
/.vagrant
21 changes: 6 additions & 15 deletions README.md
Expand Up @@ -6,21 +6,12 @@ TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'vagrant-bhyve'
```

And then execute:

$ bundle

Or install it yourself as:
## Usage(Test)

$ gem install vagrant-bhyve
### Setup environment

## Usage
$ bundle install (you can add --path vendor/bundle)

### Creating a box

Expand Down Expand Up @@ -65,8 +56,8 @@ Here is steps needed to create a test box.
}
```

3. Download [FreeBSD-10.3-RELEASE-amd64.raw.xz](http://ftp.freebsd.org/pub/FreeBSD/releases/VM-IMAGES/10.3-RELEASE/amd64/Latest/FreeBSD-10.3-RELEASE-amd64.raw.xz) and extract it to the same directory with Vagrantfile and metadata.json. Rename the img file into `disk.img`
4. Run `tar cvzf test.box *` to create a box.
3. Follow the instructions on [FreeBSD HandBook](https://www.freebsd.org/doc/handbook/virtualization-host-bhyve.html) to create FreeBSD VM image. Note to name the image to `disk.img`
4. Run `tar cvzf test.box ./Vagrantfil ./meta.json ./disk.img` to create a box.
5. `bundle exec vagrant box add test.box`

### Running the box
Expand All @@ -93,4 +84,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/jesa79

## License

BSD
MIT
35 changes: 32 additions & 3 deletions lib/vagrant-bhyve/action.rb
Expand Up @@ -11,14 +11,14 @@ module Action
autoload :Import, action_root.join('import')
autoload :CreateSwitch, action_root.join('create_switch')
autoload :CreateTap, action_root.join('create_tap')
autoload :Cleanup, action_root.join('cleanup')
autoload :Load, action_root.join('load')
autoload :Boot, action_root.join('boot')
autoload :ForwardPorts, action_root.join('forward_ports')
autoload :Shutdown, action_root.join('shudown')

def self.action_boot
Vagrant::Action::Builder.new.tap do |b|
b.use Setup
b.use CreateSwitch
b.use CreateTap
b.use Load
Expand All @@ -32,7 +32,6 @@ def self.action_halt
b.use ConfigValidate
b.use Call, IsState, :running do |env, b1|
if !env[:result]
######
next
end

Expand All @@ -41,7 +40,35 @@ def self.action_halt
b2.use Shutdown
end
end
#b1.user Cleanup
b1.use Cleanup
end
end
end

def self.action_ssh
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsState, :running do |env, b1|
if !env[:result]
b1.use Message, I18n.t('vagrant_bhyve.commands.common.vm_not_running')
next
end

b1.use SSHExec
end
end
end

def self.action_start
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsState, :runnig do |env, b1|
if env[:result]
b1.use Message, I18n.t('vagrant_bhyve.commands.common.vm_already_running')
next
end

b1.use action_boot
end
end
end
Expand All @@ -57,9 +84,11 @@ def self.action_up
b.use ConfigValidate
b.use Call, IsState, Vagrant::MachineState::NOT_CREATED_ID do |env,b1|
if env[:result]
b1.use Setup
b1.use Import
end
end
b.use action_start
end
end

Expand Down
6 changes: 2 additions & 4 deletions lib/vagrant-bhyve/action/cleanup.rb
Expand Up @@ -11,10 +11,8 @@ def initialize(app, env)
end

def call(env)
@machine = env[:machine]
@driver = @machine.provider.driver

@driver.cleanup
env[:ui].info('vagrant_bhyve.cleanup')
env[:machine].provider.driver.cleanup

@app.call(env)
end
Expand Down
1 change: 1 addition & 0 deletions lib/vagrant-bhyve/action/create_switch.rb
Expand Up @@ -15,6 +15,7 @@ def call(env)
@ui = env[:ui]
@driver = @machine.provider.driver

@ui.info('vagrant_bhyve.setup_nat_environment')
switch_list = %w(vagrant_bhyve_default_switch)
# The switch name is used as created bridge device's description
switch_list.each do |switch|
Expand Down
3 changes: 2 additions & 1 deletion lib/vagrant-bhyve/action/create_tap.rb
Expand Up @@ -14,7 +14,8 @@ def call(env)
@machine = env[:machine]
@driver = @machine.provider.driver

vm_name = @driver.get_vm_name
env[:ui].info('vagrant_bhyve.create_tap_device')
vm_name = @driver.get_attr('vm_name')
tap_name = "vagrant_bhyve_#{vm_name}"
tap_list = [tap_name]
# The switch name is used as created bridge device's description
Expand Down
1 change: 1 addition & 0 deletions lib/vagrant-bhyve/action/forward_ports.rb
Expand Up @@ -14,6 +14,7 @@ def call(env)
@machine = env[:machine]
@driver = @machine.provider.driver

env[:ui].info('vagrant_bhyve.forward_ports')
pf_conf = @machine.box.directory.join('pf.conf')
tap_device = @machine.env[:tap]
@env[:forwarded_ports].each do |item|
Expand Down
5 changes: 2 additions & 3 deletions lib/vagrant-bhyve/action/import.rb
Expand Up @@ -14,11 +14,10 @@ def initialize(app, env)
def call(env)
@machine = env[:machine]
@driver = @machine.provider.driver
@ui = env[:ui]

@ui.info I18n.t("vagrant.action.vm.clone.creating")
env[:ui].info I18n.t("vagrant_bhyve.create_vm")
@machine.id = SecureRandom.uuid
@driver.import(@machine.id)
@driver.import(@machine)
@app.call(env)
end

Expand Down
15 changes: 8 additions & 7 deletions lib/vagrant-bhyve/action/load.rb
Expand Up @@ -12,15 +12,16 @@ def initialize(app, env)

def call(env)
@machine = env[:machine]
@driver = machine.provider.driver
firmware = machine.box.metadata[:firmware]
loader = machine.box.metadata[:loader]
@ui.info I18n.t('vagrant.actions.vm.boot.booting')
@ui.detail I18n.t('vagrant_bhyve.actions.vm.load.loading')
@driver.load(loader, machine) if firmware == 'bios'
@driver = @machine.provider.driver
firmware = @machine.box.metadata['firmware']
loader = @machine.box.metadata['loader']

env[:ui].info I18n.t('vagrant_bhyve.load_os_kernel')
@driver.load(loader, @machine, @ui) if firmware == 'bios'

@app.call(env)
end

end
end
end
Expand Down
9 changes: 3 additions & 6 deletions lib/vagrant-bhyve/action/setup.rb
Expand Up @@ -11,12 +11,9 @@ def initialize(app, env)
end

def call(env)
@machine = env[:machine]
@driver = @machine.provider.driver
# Add vm_name into data_dir
@machine.data_dir.join('vm_name').open('w') do |name_file|
name_file.write @machine.box.name.gsub('/', '_')
end
@driver = env[:machine].provider.driver

env[:ui].info I18n.t('vagrant_bhyve.setup_environment')
@driver.check_bhyve_support
module_list = %w(vmm nmdm if_bridge if_tap)
for kernel_module in module_list
Expand Down
1 change: 1 addition & 0 deletions lib/vagrant-bhyve/action/shutdown.rb
Expand Up @@ -15,6 +15,7 @@ def call(env)
@ui = env[:ui]
@driver = machine.provider.driver

@ui.info('vagrant_bhyve.shutdown_vm')
@driver.shutdown(@ui)
@app.call(env)
end
Expand Down

0 comments on commit 280e1e1

Please sign in to comment.