1. 程式人生 > 實用技巧 >vagrant - 問題: 修改 共享目錄, 導致 vagrant ssh 失敗

vagrant - 問題: 修改 共享目錄, 導致 vagrant ssh 失敗

  1. 概述

    1. vagrant 修改時候, 遇到的坑
  2. 背景

    1. 初學 vagrant
    2. 剛開始, 該配置需要 先 destroy 再 up 生效
      1. 我感覺很麻煩
    3. 後來找到了一個叫 reload 的命令
      1. 據說可以不用 destroy 就直接 過載配置
      2. 而且還要比普通的快一些
    4. 結果一試, 就試出了問題
  3. 型別

    1. 這是一篇 教程向 的部落格
      1. 如果比較著急, 可以直接到後面找結論
  4. 環境

    1. OS
      1. win10 - 1909
    2. virtualBox
      1. 6.1.14
    3. vagrant
      1. 2.2.13
    4. precise64
      1. 1.1.0

1. 場景

  1. 概述

    1. 問題的場景
  2. 思路

    1. 啟動 vagrant
    2. 修改配置
    3. 過載 vagrant

1. 啟動 vagrant

  1. 配置

    # vagrant init 已經執行
    Vagrant.configure("2") do |config|
      config.vm.box = "hashicorp/precise64"
      # 這是我新增的共享目錄
      config.vm.synced_folder "vagrant-data", "/vagrant_data",
        create: true,
        id: "data"
    end
    
  2. 命令

    > vagrant up
    
  3. 結果

    1. 成功啟動
    2. 共享目錄有兩個
      1. 預設的目錄 vagrant
      2. 我建立的 data 目錄

2. 修改配置

  1. 配置

    Vagrant.configure("2") do |config|
      config.vm.box = "hashicorp/precise64"
      # 這是我新增的共享目錄
      config.vm.synced_folder "vagrant-data", "/vagrant_data",
        create: true,
        id: "data"
      # 關閉預設的共享目錄
      config.vm.synced_folder ".", "/vagrant",
        disabled: true
    end
    
  2. 命令

    > vagrant reload
    
  3. 結果

    1. vagrant 無法連線
      1. 日誌再 ref 裡
      2. 失敗: ssh 超時
      3. vagrant status 檢視,
        1. 發現 vagrant 的狀態, 既然是 running

3. 疑問: 為什麼超時了

  1. 概述

    1. 問題
  2. 問題

    1. 為什麼超時了
      1. 我只改了 共享目錄 的配置
      2. 我沒有動 網路 的東西
      3. 為什麼 提示的是 ssh 連不上?
      4. 明明 running 了, 我還是連不上

2. 嘗試

  1. 概述
    1. 嘗試解決問題

1. ssh

  1. 概述

    1. ssh 的思路
  2. 問題

    1. 我明明沒改網路配置
  3. 嘗試1: destroy 後再 up

    1. 操作

      > vagrant destroy
      > vagrant up
      > vagrant ssh
      
    2. 結果

      1. 沒有一點問題
    3. 結論

      1. 就是我改 共享目錄 改壞的
  4. 嘗試2: 百度

    1. 操作

      1. 搜尋了一堆 vagrant 連不上的問題
        1. 附帶了一些日誌的片段
    2. 結果

      1. 各路結果都是要檢查 ssh

        1. 有檢查 key 的
        2. 有刪配置的
        3. 有直接 destroy 再 up 的
      2. 但是 ssh 我碰都沒碰過

2. 虛機

  1. 概述

    1. 換個角度找問題
  2. 嘗試3: 既然虛擬機器啟動了, 為什麼不 ssh 去看看

    1. 操作

      1. 確認 ssh 埠
        1. 正常啟動的日誌, 告訴我 22 埠被對映到了 2222
      2. 賬戶
        1. 也不知道在哪看到的, 預設賬戶密碼, 都是 vagrant
      3. 開啟虛機
        1. 結果我驚了
        2. 虛機啟動中, 被卡住了
    2. 結果

      1. 虛機卡在一個介面
        1. 日誌在 ref 裡
        2. 大概提示 fsck 檢查
          1. /vagrant 掛載失敗
          2. 需要手動確認, 是 跳過 還是 手動處理
          3. 一直卡在這, 不會自己走

3. 結果

  1. 概述

    1. 初步的結論
  2. 初步結論

    1. 修改 共享目錄, 導致 vagrant 無法啟動
  3. 下一步

    1. 修改 共享目錄 的配置, 就能搞成這樣, 怎麼感覺不科學...
      1. 自己試試

3. 實驗

  1. 概述
    1. 自己的一些嘗試
    2. 不想寫太詳細了

1. 嘗試1: 沒有任何共享目錄

  1. 概述

    1. 嘗試1
  2. 操作

    1. vagrant up
    2. vagrant reload
  3. 結果

    1. 沒有問題

2. 嘗試2: 新增共享目錄

  1. 概述

    1. 嘗試2
  2. 操作

    1. vagrant up
    2. 添加了一條共享目錄
    3. vagrant reload
  3. 結果

    1. 沒有問題

3. 嘗試3: 修改共享目錄

  1. 概述

    1. 嘗試3
  2. 操作

    1. vagrant up
    2. 修改了一條共享目錄
    3. vagrant reload
  3. 結果

    1. 虛機 running
    2. ssh 失敗
    3. 卡在了 fsck

4. 嘗試4: 刪除共享目錄

  1. 概述

    1. 嘗試4
  2. 操作

    1. vagrant up
    2. 刪除了一條共享目錄
    3. vagrant reload
  3. 結果

    1. 虛機 running
    2. ssh 失敗
    3. 卡在了 fsck

5. 結論

  1. 一下操作, 可能會使得 vagrant 重啟時, 卡在 fsck

    1. 修改 共享目錄
    2. 刪除 共享目錄
  2. 所以, 這是為啥呢?

    1. 簡單的看了看

4. 繼續

  1. 概述

    1. 好奇這是為啥
  2. 準備

    1. mount

      1. 概述
        1. 檢視 mount 情況的命令列工具
    2. fstab

      1. 概述
        1. linux 中描述 掛載 的檔案
      2. 檢查
        1. vagrant 的共享目錄, 確實在裡面
    3. vagrant 的啟動日誌

      1. 概述
        1. 這是關鍵

1. 看日誌

  1. 概述

    1. 檢視日誌
  2. 比對: 啟動超時 和 啟動成功 的日誌

    1. 結果
      1. 超時的日誌, 沒有對 共享目錄 做任何調整
      2. 反倒是成功的日誌, 在 ssh 通過了之後, 會對 共享目錄 做一些修改...

2. mount 和 fstab

  1. 概述

    1. 進入系統檢視
  2. 結論

    1. mount 結果正確
    2. fstab 也是有專門的修改

5. 總結與後續

  1. 原因

    1. 修改 vagrantfile, 導致 虛機開機自檢 無法通過
      1. fsck
  2. 機制

    1. 新的 vagrant 虛機, 預設第一次是可以啟動的
    2. 有些配置, 是需要在 ssh 通過之後, 才能去修改
    3. 如果啟動中出現問題, 很容易出現 ssh 失敗的假象
    4. 配合 running 的狀態, 更容易誤導人
  3. 辦法

    1. 是在沒招了, 去虛機上看看, 可能會有新發現
  4. 建議

    1. 對 vagrant 共享目錄的配置
      1. 儘量一開始就配置好
      2. 如果要更改, 儘量只新增, 不做其他操作
    2. 換映象(box)
      1. 可能真的是我映象的問題, 沒準別的映象就不會有這種問題了
  5. 修改儲存配置

    1. 場景

      1. 忽然不想暴露目錄了
    2. 解決

      1. 想辦法跳過 開頭的 fsck 自檢查
      2. 通過 許可權 設定, 來對特定的使用者遮蔽
  6. 後續

    1. fsck
    2. fstab
    3. linux 許可權

ps

  1. ref

    1. 暫無
  2. 日誌

    1. vagrant

      # 失敗: ssh 超時
      $ vagrant reload
      ==> default: Attempting graceful shutdown of VM...
      ==> default: Checking if box 'hashicorp/precise64' version '1.1.0' is up to date...
      ==> 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: Forwarding ports...
          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 aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Remote connection disconnect. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
      Timed out while waiting for the machine to boot. This means that
      Vagrant was unable to communicate with the guest machine within
      the configured ("config.vm.boot_timeout" value) time period.
      
      If you look above, you should be able to see the error(s) that
      Vagrant had when attempting to connect to the machine. These errors
      are usually good hints as to what may be wrong.
      
      If you're using a custom box, make sure that networking is properly
      working and you're able to connect to the machine. It is a common
      problem that networking isn't setup properly in these boxes.
      Verify that authentication configurations are also setup properly,
      as well.
      
      If the box appears to be booting properly, you may want to increase
      the timeout ("config.vm.boot_timeout") value.
      
      # 成功: 我趕在超時之前, 登上了 vagrant 虛機, 手動跳過了 fsck
      ==> default: Attempting graceful shutdown of VM...
      ==> default: Checking if box 'hashicorp/precise64' version '1.1.0' is up to date...
      ==> 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: Forwarding ports...
          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 aborted. Retrying...
          default: Warning: Connection reset. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection aborted. Retrying...
          default: Warning: Connection reset. Retrying...
      ==> default: Machine booted and ready!
      ==> 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.2.0
          default: VirtualBox Version: 6.1
      ==> default: Mounting shared folders...
          default: /vagrant => G:/VagrantVMs/vagrant_book_example
          default: /vagrant_data => G:/VagrantVMs/vagrant_book_example/vagrant-data
      ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
      ==> default: flag to force provisioning. Provisioners marked to run always will still run.
      
    2. precise64

      # 虛機啟動時, 卡住的日誌
      fsck from util-linux 2.20.1
      fsck from util-linux 2.20.1 
      dev/mapper/precise64-root: clean, 61688/5185536 files, 597883 20711424 blocks
      /dev/sda1: clean, 230/124496 files, 40607/248832 blocks
      rpcbind: Cannot open '/run/rpcbind/rpcbind.xdr' file for reading, errno 2 (No such file or directory)
      rpcbind: Cannot open '/run/rpcbind/portmap.xdr' file for reading, errno 2 (No such file or directory) 
      /sbin/mount.vboxsf : mounting failed with the error: Protocol error 
      mountall: mount /uagrant [458] term inated with status 1
      mountall: Filesystem could not be mounted: /vagrant
      Am error occurred while mounting /uagrant.
      Press S to skip mounting or M for manual recovery