【问题标题】:Vagrant filesystem issues with npmnpm 的 Vagrant 文件系统问题
【发布时间】:2017-05-26 20:47:15
【问题描述】:

我在使用 Vagrant 机器和 npm 时经常遇到错误,文件系统突然变为只读。在所有情况下,都涉及到一个包含 git repo 的同步目录。

这是我遇到的问题的配置设置。这两个文件都位于面向节点的 git 存储库的根目录中,例如 this one。

流浪文件

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Use host's SSH keys
  config.ssh.forward_agent = true

  # Get current directory name (presumably, repo dirname)
  repo_dirname = File.basename(Dir.getwd)

  # Set US locale
  ENV['LC_ALL']="en_US.UTF-8"

  # Ensures virtualbox can create symlinks in shared folders
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["setextradata", :id,
                  "VBoxInternal2/SharedFoldersEnableSymlinksCreate/vagrant", "1"]
  end

  # Forward usual dev port through to host machine
  config.vm.network :forwarded_port, guest: 3000, host: 3000
  # Also forward production port just in case
  config.vm.network :forwarded_port, guest: 80, host: 8080
  # Forward a folder for the repo so that code can be worked on from outside the
  # VM as usual
  config.vm.synced_folder ".", "/home/ubuntu/#{repo_dirname}", create: true

  config.vm.define "#{repo_dirname}-vm" do |repo_vm|
    repo_vm.vm.box = "ubuntu/xenial64"
    repo_vm.vm.host_name = "#{repo_dirname}-vm"
    repo_vm.vm.provision :shell do |sh|
      sh.path = "vagrant_provision.sh"
      sh.privileged = false
      sh.env = {
        REPO_DIR: repo_dirname
      }
    end
  end

end

vagrant_provision.sh

#!/usr/bin/env bash

function set_locale() {
    sudo locale-gen en.US
}

function get_builds() {
    sudo apt-get update &&
        sudo apt-get upgrade -y &&
        sudo apt-get install -y curl build-essential
}

function get_n_installer() {
    if [ -f n-install ]
    then
        echo "n installer already exists. Not downloading."
        return 0
    else
        echo "n installer not found. Downloading..."
        wget -L https://git.io/n-install
    fi
}

function install_n() {
    if [ -d "n" ]
    then
        echo "n install directory already exists. Not installing."
        return 0
    else
        echo "n install directory not found. Installing..."
        yes | bash n-install
    fi
}

function add_github_keys() {
    # Adds the github server key to known hosts
    ssh-keyscan github.com >> ~/.ssh/known_hosts &&
        return 0 || return 1
}

function add_n_to_path() {
    # Added by n-install, but ignored by vagrant
    export N_PREFIX="$HOME/n";
    [[ :$PATH: == *":$N_PREFIX/bin:"* ]] || PATH+=":$N_PREFIX/bin"
}

function get_repo() {
    if [ -d "$2" ]
    then
        echo "Destination directory $2 already exists. Deleting contents..."
        rm -rfv ./$2
    fi
    echo "Cloning $1 into $2..."
    git clone $1 $2
}

function install_npm_deps() {
    echo "Installing npm dependencies..."
    npm install
}

function rebuild_node_sass() {
    # Needed because of https://github.com/sass/node-sass/issues/1579
    npm rebuild node-sass
}

# . ~/.bashrc &&
set_locale &&
    add_github_keys &&
    get_builds &&
    get_n_installer &&
    install_n &&
    add_n_to_path &&
    cd $REPO_DIR &&
    install_npm_deps &&
    rebuild_node_sass &&
    echo "Provisioned user is: $USER" &&
    echo "Frontend provisioned. To run, type 'vagrant ssh'."

似乎导致问题的主要步骤是由非特权供应商执行npm install。有时会出现an archive unpacking error,但有时,npm 会因 ENOENT 错误而失败。

在使用vagrant ssh 登录完成作业后,会出现只读文件系统错误。尽管如此,系统上仍有足够的空间(例如,8GB 中剩余 7 个)。

当 webpack 服务器在相同情况下运行时,也会出现只读文件系统错误。

有一些问题和票围绕着引用类似的错误,但我还没有看到任何关于底层机制的探索。有什么关系?

【问题讨论】:

    标签: node.js npm vagrant virtualbox webpack-dev-server


    【解决方案1】:

    我设法通过在 /vagrant 共享文件夹(即 /home/vagrant/node_modules)外部对 node_modules 进行符号链接来解决此问题:

    mkdir /home/vagrant/node_modules
    ln -sf /home/vagrant/node_modules /vagrant/
    

    这样做的缺点是您不能再在 vagrant 之外进行 npm install,只能从内部(除非您在主机上使用 mkdir /vagrant)。

    【讨论】:

    • 我使用过这种方法,到目前为止它对我很有效。谢谢!
    • 谢谢!有点遗憾的是,在 2021 年 vagrant 2.2.6 似乎仍然需要这样做。很高兴知道问题实际上是什么……可能与流浪者 fs 有关? (另外,奇怪的是我在 2014 年似乎没有这个问题……)
    • 这不是 Vagrant 的错误,而是 Oracle Virtualbox 和 sendfile -kernel 调用的问题。切换到其他虚拟化(VMWare,但它需要许可证)可能会有所帮助,或者对共享文件夹使用不同的同步机制(NFS?)可能会有所帮助,但我没有尝试过。
    【解决方案2】:

    我在使用 Vagrant 1.8.7 和 Virtualbox 5.1.10 时遇到了类似的问题。通过将 cpu 核心数减少到 1,我能够修复文件系统只读问题:

    config.vm.provider "virtualbox" do |v|
      v.cpus = 1
    end
    

    对我来说,构建问题仍然存在(未找到 npm 模块、ENOENT、ELIFECYCLE 等),但速度较慢。不幸的是,我无法找到这些错误的根本原因。

    在我的设置中,npm 没有写入共享目录。如果写入位置是共享文件夹,您可以尝试a different syncing mechanism,它绕过了 virtualbox 共享文件夹实现的一些限制。

    【讨论】:

    • 切换到rsync 更加可靠。好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2021-04-03
    • 2021-08-06
    • 2011-10-29
    • 1970-01-01
    相关资源
    最近更新 更多