如何使用命令行在 ubuntu 中创建虚拟机
How to create a virtual machine in ubuntu using command line
我正在尝试在 virtualbox 中模拟一个英特尔 NUC 网关。我是 运行 这个亚马逊 EC2 实例中的虚拟盒子。由于连接不良,我无法查看桌面的 GUI。因此开始使用命令行创建虚拟机。以下是我的步骤:
在 resin.io 中创建了一个应用程序并选择了 intel nuc 板作为应用程序并下载了图像
将 .img 转换为 .vmdk 图像并将该图像保存在我的 ec2 实例中
现在我使用命令行参数在 EC2 中创建了我的虚拟机,当我尝试导入此映像时..我很震惊..我没有收到相关命令
(见下方编辑!)
目前看来这在 AWS EC2 中是不可能的。他们有很好的 basic info and detailed step-by-step guide to import virtual machine images, but the resin.io image does not fit their operating systems prerequisits:基本上,在 EC2 之上的 OS 图像 运行ning 需要是列出的 OS 类型之一(Ubuntu,红色Hat、SUSE 等),但 resin.io 映像是自定义 Linux 系统,EC2 平台不接受它。我已经尝试 运行 他们的导入程序,但各种尝试都被拒绝了。
建议尝试 运行虚拟机的不同方式。如果您只是尝试虚拟设备(我猜基于现在 resin.io 上可用的基于 this blogpost), and you don't need a NUC image, just any virtual device would do, then there are also QEMU 的图像,那应该也适用于 运行 在您的本地计算机上(那些不起作用在 EC2 上,出于同样的原因)。
编辑:
重新阅读您的问题,我很抱歉,它与 EC2 本身的关系要少得多,而与 VirtualBox 的关系要多得多。 VBoxManage 有大量文档。在这种情况下,这里有一个脚本可以用于在命令行上的 VirtualBox 上设置和启动 resin.io NUC 映像。
需要什么:从 resin.io 仪表板下载 NUC 映像,并转换为 VMDK 映像。在主机上安装 VirtualBox,将 VMDK 复制到那里,然后修改下面文件中的设置(根据需要调整可用内存、磁盘存储和文件名)。
脚本将:
- 创建虚拟机并注册到 VirtualBox
- 为 resin.io NUC 映像设置正确的硬件设置
- 创建 SATA 存储驱动程序
- 创建主硬盘并将其附加到虚拟机
- 将 resin.io 安装媒体附加到计算机
- 运行 headless 模式下的虚拟机为 resin 进行首次配置。此过程将在完成后关闭虚拟机
- 分离安装介质,因为之后不需要它
然后您的机器就可以 运行。
#!/bin/bash
## Fill in these Variables
# the virtualmachine's name
MACHINE=MyMachine2
# memory in MB
MEMORY=2048
# storage in MB
STORAGE=8096
# resin installation media path & filename
RESIN_DISK="resin-MyApplication-1.8.0-1.13.0-eb7236d1bd7e.vmdk"
# Storage disk, by defalt created in the current working directory!
DISKFILE="./${MACHINE}.vdi"
###
## Convert the original image to a Virtualbox image as:
# VBoxManage convertdd resin.img resin.vmdk --format vmdk
# and then use RESIN_DISK="resin.vmdk" above
echo "Createing Machine" && \
VBoxManage createvm --name "$MACHINE" \
--ostype Linux_64 \
--register && \
\
echo "Setting up Machine" && \
VBoxManage modifyvm "$MACHINE" \
--memory $MEMORY \
--ioapic off \
--firmware efi64 \
--rtcuseutc on && \
\
echo "Createing Storage Controller" && \
VBoxManage storagectl "$MACHINE" \
--name SATA \
--add sata && \
\
echo "Creating Main Disk" && \
VBoxManage createmedium disk \
--filename "$DISKFILE" \
--size $STORAGE && \
\
echo "Attaching Main Disk" && \
VBoxManage storageattach "$MACHINE" \
--storagectl SATA \
--port 0 --device 0 --type hdd --medium "$DISKFILE" && \
\
echo "Attaching Resin Install Media" && \
VBoxManage storageattach "$MACHINE" \
--storagectl SATA \
--port 1 --device 0 --type hdd --medium "$RESIN_DISK" && \
\
echo "Starting machine for first time setup" && \
VBoxHeadless --startvm "$MACHINE" && \
\
echo "Removing install media" && \
VBoxManage storageattach "$MACHINE" \
--storagectl SATA \
--port 1 --device 0 --type hdd --medium none && \
\
echo -e "You now can start machine for future use as: \nVBoxHeadless --startvm \"$MACHINE\""
额外:
附带说明一下,如果您在命令行上工作,您也可以通过命令行获取所需的 resin.io 图像!
将 resin-cli 安装到您的主机上,并使用 resin login
登录(例如使用 resin.io 的仪表板/首选项部分中的 API 密钥) ,
为 NUC 下载裸 OS 图像,例如:
resin os download intel-nuc -o intel-nuc.img
为您的应用程序创建一个配置,假设您的应用程序名称是 MyApp:
resin config generate --app MyApp -o config-MyApp.json
然后将此配置添加到您的映像中:
sudo resin config inject config-MyApp.json --type intel-nuc --drive intel-nuc.img
(为此,您可能必须 运行 sudo resin login
以便您能够正确地将 sudo 与 resin 命令一起使用。)
在此之后,您可以将 intel-nuc.img
转换为 VMDK 格式,并如上所述设置您的虚拟机。
我正在尝试在 virtualbox 中模拟一个英特尔 NUC 网关。我是 运行 这个亚马逊 EC2 实例中的虚拟盒子。由于连接不良,我无法查看桌面的 GUI。因此开始使用命令行创建虚拟机。以下是我的步骤:
在 resin.io 中创建了一个应用程序并选择了 intel nuc 板作为应用程序并下载了图像
将 .img 转换为 .vmdk 图像并将该图像保存在我的 ec2 实例中
现在我使用命令行参数在 EC2 中创建了我的虚拟机,当我尝试导入此映像时..我很震惊..我没有收到相关命令
(见下方编辑!)
目前看来这在 AWS EC2 中是不可能的。他们有很好的 basic info and detailed step-by-step guide to import virtual machine images, but the resin.io image does not fit their operating systems prerequisits:基本上,在 EC2 之上的 OS 图像 运行ning 需要是列出的 OS 类型之一(Ubuntu,红色Hat、SUSE 等),但 resin.io 映像是自定义 Linux 系统,EC2 平台不接受它。我已经尝试 运行 他们的导入程序,但各种尝试都被拒绝了。
建议尝试 运行虚拟机的不同方式。如果您只是尝试虚拟设备(我猜基于现在 resin.io 上可用的基于 this blogpost), and you don't need a NUC image, just any virtual device would do, then there are also QEMU 的图像,那应该也适用于 运行 在您的本地计算机上(那些不起作用在 EC2 上,出于同样的原因)。
编辑:
重新阅读您的问题,我很抱歉,它与 EC2 本身的关系要少得多,而与 VirtualBox 的关系要多得多。 VBoxManage 有大量文档。在这种情况下,这里有一个脚本可以用于在命令行上的 VirtualBox 上设置和启动 resin.io NUC 映像。
需要什么:从 resin.io 仪表板下载 NUC 映像,并转换为 VMDK 映像。在主机上安装 VirtualBox,将 VMDK 复制到那里,然后修改下面文件中的设置(根据需要调整可用内存、磁盘存储和文件名)。
脚本将:
- 创建虚拟机并注册到 VirtualBox
- 为 resin.io NUC 映像设置正确的硬件设置
- 创建 SATA 存储驱动程序
- 创建主硬盘并将其附加到虚拟机
- 将 resin.io 安装媒体附加到计算机
- 运行 headless 模式下的虚拟机为 resin 进行首次配置。此过程将在完成后关闭虚拟机
- 分离安装介质,因为之后不需要它
然后您的机器就可以 运行。
#!/bin/bash
## Fill in these Variables
# the virtualmachine's name
MACHINE=MyMachine2
# memory in MB
MEMORY=2048
# storage in MB
STORAGE=8096
# resin installation media path & filename
RESIN_DISK="resin-MyApplication-1.8.0-1.13.0-eb7236d1bd7e.vmdk"
# Storage disk, by defalt created in the current working directory!
DISKFILE="./${MACHINE}.vdi"
###
## Convert the original image to a Virtualbox image as:
# VBoxManage convertdd resin.img resin.vmdk --format vmdk
# and then use RESIN_DISK="resin.vmdk" above
echo "Createing Machine" && \
VBoxManage createvm --name "$MACHINE" \
--ostype Linux_64 \
--register && \
\
echo "Setting up Machine" && \
VBoxManage modifyvm "$MACHINE" \
--memory $MEMORY \
--ioapic off \
--firmware efi64 \
--rtcuseutc on && \
\
echo "Createing Storage Controller" && \
VBoxManage storagectl "$MACHINE" \
--name SATA \
--add sata && \
\
echo "Creating Main Disk" && \
VBoxManage createmedium disk \
--filename "$DISKFILE" \
--size $STORAGE && \
\
echo "Attaching Main Disk" && \
VBoxManage storageattach "$MACHINE" \
--storagectl SATA \
--port 0 --device 0 --type hdd --medium "$DISKFILE" && \
\
echo "Attaching Resin Install Media" && \
VBoxManage storageattach "$MACHINE" \
--storagectl SATA \
--port 1 --device 0 --type hdd --medium "$RESIN_DISK" && \
\
echo "Starting machine for first time setup" && \
VBoxHeadless --startvm "$MACHINE" && \
\
echo "Removing install media" && \
VBoxManage storageattach "$MACHINE" \
--storagectl SATA \
--port 1 --device 0 --type hdd --medium none && \
\
echo -e "You now can start machine for future use as: \nVBoxHeadless --startvm \"$MACHINE\""
额外:
附带说明一下,如果您在命令行上工作,您也可以通过命令行获取所需的 resin.io 图像!
将 resin-cli 安装到您的主机上,并使用 resin login
登录(例如使用 resin.io 的仪表板/首选项部分中的 API 密钥) ,
为 NUC 下载裸 OS 图像,例如:
resin os download intel-nuc -o intel-nuc.img
为您的应用程序创建一个配置,假设您的应用程序名称是 MyApp:
resin config generate --app MyApp -o config-MyApp.json
然后将此配置添加到您的映像中:
sudo resin config inject config-MyApp.json --type intel-nuc --drive intel-nuc.img
(为此,您可能必须 运行 sudo resin login
以便您能够正确地将 sudo 与 resin 命令一起使用。)
在此之后,您可以将 intel-nuc.img
转换为 VMDK 格式,并如上所述设置您的虚拟机。