cwang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: Vagrant 參考 tags: Vagrant, VM, 簡報 description: 本簡報的內容是 cwang 在做新生訓練時,所有做的相關操作。 langs: zh slideOptions: theme: moon transition: slide viewDistance: 5 allottedMinutes: 10 help: true preloadIframes: true showNotes: true # spotlight: # enabled: true --- # Vagrant 參考 slide: https://hackmd.io/@cwang/SkyKdDHeF --- 這是個讓開發環境變簡單的玩意。 ![](https://i.imgur.com/ltJRPb7.png =600x) --- ## 學習誘因 ---- 1. 如果在虛擬主機上設定錯誤的話,可能造成原本寫對的作業評測又過不了了。 - 這時如果有方法能明確定義每個打開的虛擬機都是固定的設定,是否會比較好呢? 2. 如果虛擬主機因為沒注意到而做了某些操作,可能導致原本寫對的作業評測又要重弄。 - 這時如果有方法可以讓每一題都可以快速且自動化,是否就能重現環境以及減少手動重建得時間呢? ---- 3. 單⼀虛擬機的簡單界⾯,學習曲線⼩。 4. 其實對系統管理員,就某種層面來說自動化可算是顯學了(?) --- ## 學成效益 ---- 1. 讓大家更好地表示自己的開發環境及規格。 3. 更方便與優雅的使用虛擬主機,以減少多餘的操作。 3. 減少啟動虛擬主機前的操作,像是手動下載 iso 與一步步安裝的過程。 4. 初步地實現 Infrastructure as code(IaC),輕量容易傳遞分享,並使其可程式化、版控化與自動化。 --- ## 簡短介紹 ---- [Vagrant](https://www.vagrantup.com) 是一款由 [HashiCorp](https://www.hashicorp.com) 基於 [Ruby](https://www.ruby-lang.org) 且用於構建及配置虛擬開發環境的軟體,主要以 cli 的方式運行。 主要使用 [Oracle](https://www.oracle.com) 的開源 [VirtualBox](https://www.virtualbox.org) 虛擬化系統,與 [Ansible](https://www.ansible.com), [Chef](https://www.chef.io), [Salt](https://saltproject.io), [Puppet](https://puppet.com) 等環境配置管理軟體搭配使用,可以實行快速虛擬開發環境的構建。 --- ## 如何安裝 ---- 1. 安裝 [Vagrant](https://www.vagrantup.com/downloads)。 2. 安裝 [VirtualBox](https://www.virtualbox.org/)。 --- ## 如何使用 ---- 1. 先到 [Vagrant Cloud](https://app.vagrantup.com/boxes/search) 找一個想要用的 Image。 2. 假設我想要使用這個 [generic/freebsd13](https://app.vagrantup.com/generic/boxes/freebsd13),只需要使用以下指令即可在當前目錄獲得 [Vagrantfile 模板](https://www.vagrantup.com/docs/vagrantfile)。 ```bash= vagrant init generic/freebsd13 ``` 3. 然後再使用以下指令,一個裝有 13 版的 [FreeBSD](https://www.freebsd.org) 的虛擬主機就起起來給你用了。 ```bash=+ vagrant up ``` --- ## 其餘常用指令 ---- ### ssh 到虛擬機 ```bash=+ vagrant ssh ``` ### 重載 ```bash=+ vagrant reload ``` ---- ### 關機 ```bash=+ vagrant halt ``` ### 刪除 ```bash=+ vagrant destroy ``` [Vagrant Command-Line Interface](https://www.vagrantup.com/docs/cli) --- ## 定義虛擬機規格 ---- 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.provider :virtualbox do |v| v.gui = false v.customize ["modifyvm", :id, "--cpus", 1] v.customize ["modifyvm", :id, "--memory", 1024] end end ``` [Vagrant VirtualBox Configuration](https://www.vagrantup.com/docs/providers/virtualbox/configuration) [VBoxManage](https://www.virtualbox.org/manual/ch08.html) --- ## 設定虛擬機網路 ---- ### 設定私有靜態 IP 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.network "private_network", ip: "192.168.50.4" end ``` ---- ### Forwarded Ports 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.network "forwarded_port", guest: 80, host: 8080 end ``` [Vagrant Networking](https://www.vagrantup.com/docs/networking) --- ## 添加初始化指令 ---- 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.provision "shell", inline: <<-SHELL pkg install -y git vim ln -s /usr/share/zoneinfo/Asia/Taipei /etc/localtime SHELL end ``` [Vagrant Shell Provisioner](https://www.vagrantup.com/docs/provisioning/shell) --- ## 同步資料夾 ---- 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.synced_folder ".", "/vagrant" end ``` [Vagrant Synced Folders](https://www.vagrantup.com/docs/synced-folders) --- ## 添加額外的硬碟 ---- 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.disk :disk, name: "a1", size: "1GB" config.vm.disk :disk, name: "a2", size: "1GB" end ``` [Vagrant Disks Usage](https://www.vagrantup.com/docs/disks/usage) ---- ### vagrant up 前輸入指令 #### powershell ```= set VAGRANT_EXPERIMENTAL 1 ``` #### cmd ```= set VAGRANT_EXPERIMENTAL=1 ``` #### sh ```bash= export VAGRANT_EXPERIMENTAL=1 ``` --- ## 一次建立多台 VM ---- 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.box_version = "3.3.4" config.vm.define "nfs" do |nfs| nfs.vm.hostname = "nfs.1.nctu.cs" nfs.vm.network :private_network, ip: "10.113.0.1" end config.vm.define "www" do |www| www.vm.hostname = "www.1.nctu.cs" www.vm.network :private_network, ip: "10.113.0.2" end end ``` [Vagrant Multi-Machine](https://www.vagrantup.com/docs/multi-machine) ---- ### 只對 nfs 操作 ```bash= vagrant up nfs ``` ### 同時對 nfs 與 www 操作 ```bash= vagrant up ``` --- ## 使用 Vagrant 插件 以 vagrant-hosts 為例 目的為成功同步所有 VM 的 hosts ---- ### 為所有 VM 設定 hosts 安裝 vagrant-hosts 插件 ```bash= vagrant plugin install vagrant-hosts ``` 編輯 `Vagrantfile` ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.provision :hosts do |provisioner| provisioner.autoconfigure = true provisioner.sync_hosts = true end end ``` [vagrant-hosts](https://github.com/oscar-stack/vagrant-hosts) --- ## 將上述 Vagrantfile 組合 ---- ```ruby= Vagrant.configure("2") do |config| config.vm.box = "generic/freebsd13" config.vm.box_version = "3.3.4" config.vm.provision :hosts do |provisioner| provisioner.autoconfigure = true provisioner.sync_hosts = true end config.vm.provider :virtualbox do |v| v.customize ["modifyvm", :id, "--cpus", 1] v.customize ["modifyvm", :id, "--memory", 1024] end config.vm.provision "shell", inline: <<-SHELL pkg install -y git vim ln -s /usr/share/zoneinfo/Asia/Taipei /etc/localtime SHELL ``` ---- ```ruby=+ config.vm.define "nfs" do |nfs| nfs.vm.hostname = "nfs.1.nctu.cs" nfs.vm.network :private_network, ip: "10.113.0.1" nfs.vm.network :forwarded_port, guest: 21, host: 21, id: 'ftp' nfs.vm.disk :disk, name: "a1", size: "1GB" nfs.vm.disk :disk, name: "a2", size: "1GB" nfs.vm.synced_folder ".", "/mnt" end ``` ---- ```ruby=+ config.vm.define "www" do |www| www.vm.hostname = "www.1.nctu.cs" www.vm.network :private_network, ip: "10.113.0.2" www.vm.network :forwarded_port, guest: 443, host: 8443 www.vm.provider :virtualbox do |v| v.customize ["modifyvm", :id, "--cpus", 2] v.customize ["modifyvm", :id, "--memory", 2048] end end end ``` --- ## 補充 ---- - 可以選擇不使用 [VirtualBox](https://www.virtualbox.org),而改使用 [VMware Workstation](https://www.vmware.com/products/workstation-player/workstation-player-evaluation.html) 或是 [Parallels Desktop](https://www.parallels.com/)。那你得去看 [Vagrant VMware Provider](https://www.vagrantup.com/docs/providers/vmware) 或是 [Vagrant Parallels Provider](https://parallels.github.io/vagrant-parallels)。 - 若欲使用的系統沒有提供給當前使用之虛擬化平台,則可以在 [Vagrant Cloud generic](https://app.vagrantup.com/generic) 中尋找。另外,我們能發現他的專案 [lavabit/robox](https://github.com/lavabit/robox) 是使用 [Packer](https://www.packer.io) 來自動化 Build 多種平台的 Image。 ---- - 如果是在 PRDO 環境下,則可以根據情境將 [Vagrant](https://www.vagrantup.com) 所做的事情分別拆給 [Terraform](https://www.terraform.io) 和 [Ansible](https://www.ansible.com) 以及 [Packer](https://www.packer.io) 去做,以達到更精細的控制。 --- ## Thank you! ![](https://emoji.slack-edge.com/T04SF4XEX/sheepy/e1f80d45bfdfb1ad.gif)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully