vagrant 调用 VirtualBox - 无法控制串行端口原始文件值

vagrant 调用 VirtualBox - 无法控制串行端口原始文件值

好的,我先简单解释一下这个问题是什么,然后再解释为什么这是一个问题。

我使用 Vagrant 启动bionic64虚拟机。启动后,会在根目录中创建一个名为ubuntu-bionic-18.04-cloudimg-console.log

但在 VirtualBox 中,串行端口的设置包含绝对路径。我可以vagrant halt将其更改为

./ubuntu-bionic-18.04-cloudimg-console-ci4.log

但当我 时vagrant up,设置已恢复,文件名仍然相同。我该如何修复此问题,或者什么会覆盖此问题?

默认设置包括绝对路径

我希望它是什么(但它被覆盖了)

为什么这是一个问题

打包盒子并尝试分发它,该绝对路径仍然存在,但在目标计算机中不存在。这使得无法共享盒子。

哦,还有 Vagrantfile。

Vagrant.configure("2") do |config|
  # VM Box
  config.vm.box = "ubuntu/bionic64"
  # Automatic box update checking
  config.vm.box_check_update = true

  # CodeIgniter virtual host
  config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  # Code Coverage virtual host
  config.vm.network "forwarded_port", guest: 81, host: 8081, host_ip: "127.0.0.1"
  # User Guide virtual host
  config.vm.network "forwarded_port", guest: 82, host: 8082, host_ip: "127.0.0.1"
  # MySQL server
  #config.vm.network "forwarded_port", guest: 3306, host: 3307, host_ip: "127.0.0.1"
  # PostgreSQL server
  #config.vm.network "forwarded_port", guest: 5432, host: 5433, host_ip: "127.0.0.1"
  # Memcached server
  #config.vm.network "forwarded_port", guest: 11211, host: 11212, host_ip: "127.0.0.1"
  # Redis server
  #config.vm.network "forwarded_port", guest: 6379, host: 6380, host_ip: "127.0.0.1"

  # Add "192.168.10.10 ${VIRTUALHOST}" in your host file to access by domain
  config.vm.network "private_network", ip: "192.168.33.101"

  # Same path set in the $CODEIGNITER_PATH Provision
  # "virtualbox" type allow auto-sync host to guest and guest to host
  # but chmod does not work... tests will fail.
  # Default rsync__args except "--copy-links", to allow phpunit correctly works by symlink
  # Wait, this might be a problem using rsync - I saw a folder that wasn't there till I rebooted
  # config.vm.synced_folder ".", "/var/www", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"]


  config.vm.synced_folder ".", "/var/www", :mount_options => ["dmode=777", "fmode=757"]


  # Provider-specific configuration
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false
    # Customize the amount of memory on the VM:
    vb.memory = "1024"
  end

  # Provision
  config.vm.provision "shell", inline: <<-SHELL
    MYSQL_ROOT_PASS="password"
    PGSQL_ROOT_PASS="password"
    VIRTUALHOST="localhost"
    CODEIGNITER_PATH="/var/www/codeigniter"
    PHP_VERSION=7.2
    PGSQL_VERSION=10
    #APT_PROXY="192.168.10.1:3142"

    grep -q "127.0.0.1 ${VIRTUALHOST}" /etc/hosts || echo "127.0.0.1 ${VIRTUALHOST}" >> /etc/hosts

    # Creates a swap file if necessary
    RAM=`awk '/MemTotal/ {print $2}' /proc/meminfo`
    if [ $RAM -lt 1000000 ] && [ ! -f /swap/swapfile ]; then
        echo "================================================================================"
        echo "Adding swap"
        echo "================================================================================"
        echo "This process may take a few minutes. Please wait..."
        mkdir /swap
        dd if=/dev/zero of=/swap/swapfile bs=1024 count=1000000
        chmod 600 /swap/swapfile
        mkswap /swap/swapfile
        swapon /swap/swapfile
        echo "/swap/swapfile swap swap defaults 0 0" >> /etc/fstab
        echo "Done."
    fi

    # Prepare to use APT Proxy
    if [ ! -z $APT_PROXY ]; then
        if [ ! -f /etc/apt/sources.list-origin ]; then
            cp /etc/apt/sources.list /etc/apt/sources.list-origin
        fi
        sed -i "s/archive.ubuntu.com/${APT_PROXY}/" /etc/apt/sources.list
        sed -i "s/security.ubuntu.com/${APT_PROXY}/" /etc/apt/sources.list
    fi

    export DEBIAN_FRONTEND=noninteractive

    echo "================================================================================"
    echo "Updating and Installing Required Packages"
    echo "================================================================================"

    apt-get update

    debconf-set-selections <<< "mysql-server mysql-server/root_password password ${MYSQL_ROOT_PASS}"
    debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ${MYSQL_ROOT_PASS}"

    apt-get install -y \
    php$PHP_VERSION apache2 composer \
    php-intl php-mbstring php-xml php-zip php-xdebug \
    php-mysql mysql-server mysql-client \
    php-pgsql postgresql-$PGSQL_VERSION \
    php-sqlite3 sqlite3 \
    php-memcached memcached \
    php-redis redis-server \
    php-curl curl \
    php-gd php-imagick \
    python-pip

    pip install sphinx sphinxcontrib-phpdomain

    apt-get autoclean

    echo "================================================================================"
    echo "Preparing User Guide"
    echo "================================================================================"

    cd "${CODEIGNITER_PATH}/user_guide_src/cilexer"
    python setup.py install
    cd ..
    make html

    echo "================================================================================"
    echo "Configuring Databases"
    echo "================================================================================"

    sed -i "s/^bind-address/#bind-address/" /etc/mysql/mysql.conf.d/mysqld.cnf
    mysql -e "CREATE DATABASE IF NOT EXISTS codeigniter COLLATE 'utf8_general_ci';
    UPDATE mysql.user SET Host='%' WHERE user='root';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    FLUSH PRIVILEGES;" -uroot -p$MYSQL_ROOT_PASS
    systemctl restart mysql

    sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" /etc/postgresql/$PGSQL_VERSION/main/postgresql.conf
    grep -q "host    all             root            all                     md5" /etc/postgresql/$PGSQL_VERSION/main/pg_hba.conf || echo "host    all             root            all                     md5" >> /etc/postgresql/$PGSQL_VERSION/main/pg_hba.conf
    sudo -u postgres psql -tc "SELECT 1 FROM pg_roles WHERE rolname='root'" | grep -q 1 || sudo -u postgres psql -c "CREATE ROLE root WITH SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN"
    sudo -u postgres psql -c "ALTER ROLE root WITH PASSWORD '${PGSQL_ROOT_PASS}'"
    sudo -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname='codeigniter'" | grep -q 1 ||sudo -u postgres psql -c "CREATE DATABASE codeigniter"
    sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE codeigniter TO root"
    systemctl restart postgresql

    echo "================================================================================"
    echo "Configuring Memcached and Redis"
    echo "================================================================================"

    sed -i "s/^bind 127.0.0.1/#bind 127.0.0.1/" /etc/redis/redis.conf
    sed -i "s/^protected-mode yes/protected-mode no/" /etc/redis/redis.conf
    sed -i "s/^-l 127.0.0.1/#-l 127.0.0.1/" /etc/memcached.conf
    systemctl restart redis
    systemctl restart memcached

    echo "================================================================================"
    echo "Configuring Virtual Hosts"
    echo "================================================================================"

    mkdir -p "${CODEIGNITER_PATH}/build/coverage-html"
    mkdir -p "${CODEIGNITER_PATH}/public"
    mkdir -p "${CODEIGNITER_PATH}/user_guide_src/build/html"
    mkdir -p "${CODEIGNITER_PATH}/writable/apache"
    chown -R vagrant:vagrant $CODEIGNITER_PATH

    # Creates a symlink in the user home
    if [ ! -d /home/vagrant/codeigniter ]; then
        ln -s $CODEIGNITER_PATH /home/vagrant/codeigniter
    fi

    sed -i "s/APACHE_RUN_USER=www-data/APACHE_RUN_USER=vagrant/" /etc/apache2/envvars
    sed -i "s/APACHE_RUN_GROUP=www-data/APACHE_RUN_GROUP=vagrant/" /etc/apache2/envvars
    grep -q "Listen 81" /etc/apache2/ports.conf || sed -i "s/^Listen 80/Listen 80\\nListen 81\\nListen 82/" /etc/apache2/ports.conf
    sed -i "s/^display_errors = Off/display_errors = On/" /etc/php/7.2/apache2/php.ini
    sed -i "s/^display_startup_errors = Off/display_startup_errors = On/" /etc/php/7.2/apache2/php.ini

    echo "ServerName ${VIRTUALHOST}
<Directory ${CODEIGNITER_PATH}>
    DirectoryIndex index.html index.php
    Options All
    AllowOverride All
</Directory>
<VirtualHost *:80>
    ServerAdmin vagrant@localhost
    DocumentRoot ${CODEIGNITER_PATH}/public
    ErrorLog  ${CODEIGNITER_PATH}/writable/apache/error.log
    CustomLog ${CODEIGNITER_PATH}/writable/apache/custom.log combined
</VirtualHost>
<VirtualHost *:81>
    DocumentRoot ${CODEIGNITER_PATH}/build/coverage-html
</VirtualHost>
<VirtualHost *:82>
    DocumentRoot ${CODEIGNITER_PATH}/user_guide_src/build/html
</VirtualHost>
" > /etc/apache2/sites-available/codeigniter.conf

    a2enmod rewrite
    a2dissite 000-default.conf
    a2ensite codeigniter.conf
    systemctl restart apache2

    echo "================================================================================"
    echo "Services Status"
    echo "================================================================================"
    service --status-all

  SHELL
end

答案1

我找到了解决该问题的答案和顺序。

  • 我无法更改串行端口 RawFile #0 的绝对路径,我无法在虚拟机运行时更改它,因为 VirtualBox 不允许这样做
  • 我可以在不在 VirtualBox 中运行时更改它,但如果流浪汉启动虚拟机时,它会自动将该值覆盖为Vagrantfile根目录的绝对路径
  • 这对于清晰起见是有意义的,但当我在运行的盒子上执行此操作时它也会留在盒子里vagrant package(我相信它必须正在运行)。

解决方案如下:

  • 在虚拟机未运行时,打开 VirtualBox,并将文件路径和名称更改为./your-file-name
  • 然后在 VirtualBox 中启动虚拟机,而不是使用vagrant up
  • (可选)vagrant ssh如果您需要从命令行设置任何所需的运行状态。
  • 注销并运行vagrant package。文件路径和名称将按照您命名的方式导出!

这个故事并没有完美地结束,因为我想知道我的文件./my-custom-file会放在哪里。结果发现,对于我的 Vagrant 安装,在 Vagrant 的大脑中,它位于/Applications文件夹中。我本以为路径会在 Vagrant 程序的根目录或子文件夹中,或者在特定框的文件夹中。可能有一个$PATH可以使用的设置,但目前这有效(在 Mac 之间,可能在 Windows 中也有效)。

相关内容