我正在玩QEMU。在这里,我找到了一些预构建的OS映像:
http://docs.openstack.org/trunk/openstack-compute/admin/content/starting-images.html
但是它们都适用于64位系统,而我的系统是32位。有人知道在线是否有32位预建映像?
因此,我可以直接使用它们,而无需理会安装。
谢谢。
我正在玩QEMU。在这里,我找到了一些预构建的OS映像:
http://docs.openstack.org/trunk/openstack-compute/admin/content/starting-images.html
但是它们都适用于64位系统,而我的系统是32位。有人知道在线是否有32位预建映像?
因此,我可以直接使用它们,而无需理会安装。
谢谢。
Answers:
快速的Google搜索显示了以下内容(我没有尝试过):
另外,您可以使用vmbuilder(此处称为ubuntu-vmbuilder
)快速将Ubuntu映像创建到KVM,VirtualBox等。
作为最后的选择,您可以使用qemu-img
命令将磁盘映像从VirtualBox / VMware转换为更适合QEMU / KVM的格式(可能不需要:我认为QEMU / KVM可以与其他映像类型一起使用,例如vdi或vmdk)。
$ qemu-img convert -f [vdi|vmdk|...] -O qcow2 OriginalImage NewImage
注意:如果使用的是32位操作系统,则无法在KVM上运行64位虚拟机。但是QEMU是一个仿真器,因此它应该允许您在32位操作系统上运行64位vm。但是性能开销可能会很大!
该答案包含以下设置的详细步骤:
所有这些都在针对18.04来宾的Ubuntu 18.04主机上进行了测试。
Ubuntu云映像是预安装的映像,可让您直接引导而无需执行通常的桌面系统安装。另请参阅:https : //serverfault.com/questions/438611/what-are-ubuntu-cloud-images
#!/usr/bin/env bash
sudo apt-get install cloud-image-utils qemu
# This is already in qcow2 format.
img=ubuntu-18.04-server-cloudimg-amd64.img
if [ ! -f "$img" ]; then
wget "https://cloud-images.ubuntu.com/releases/18.04/release/${img}"
# sparse resize: does not use any extra space, just allows the resize to happen later on.
# /superuser/1022019/how-to-increase-size-of-an-ubuntu-cloud-image
qemu-img resize "$img" +128G
fi
user_data=user-data.img
if [ ! -f "$user_data" ]; then
# For the password.
# /programming/29137679/login-credentials-of-ubuntu-cloud-server-image/53373376#53373376
# /server/920117/how-do-i-set-a-password-on-an-ubuntu-cloud-image/940686#940686
# /ubuntu/507345/how-to-set-a-password-for-ubuntu-cloud-images-ie-not-use-ssh/1094189#1094189
cat >user-data <<EOF
#cloud-config
password: asdfqwer
chpasswd: { expire: False }
ssh_pwauth: True
EOF
cloud-localds "$user_data" user-data
fi
qemu-system-x86_64 \
-drive "file=${img},format=qcow2" \
-drive "file=${user_data},format=raw" \
-device rtl8139,netdev=net0 \
-enable-kvm \
-m 2G \
-netdev user,id=net0 \
-serial mon:stdio \
-smp 2 \
-vga virtio \
;
QEMU启动后,您可能必须按Enter键才能显示启动菜单。Ubuntu
从那里选择。
然后,启动的开始说:
error: no such device: root.
Press any key to continue...
但是即使您没有按任何键,启动也会在短暂的超时后继续。升级此错误报告: https //bugs.launchpad.net/cloud-images/+bug/1726476
启动完成后,使用以下命令登录:
ubuntu
asdfqwer
互联网正常工作。
待办事项:我注意到使用此工具时有时会发生错误:https : //bugs.launchpad.net/cloud-images/+bug/1818197
与amd64非常相似,但是我们需要一些UEFI黑魔法来引导它。
sudo apt-get install cloud-image-utils qemu-system-arm qemu-efi
# Get the image.
img=ubuntu-18.04-server-cloudimg-arm64.img
if [ ! -f "$img" ]; then
wget "https://cloud-images.ubuntu.com/releases/18.04/release/${img}"
qemu-img resize "$img" +128G
fi
# For the password.
user_data=user-data.img
if [ ! -f "$user_data" ]; then
cat >user-data <<EOF
#cloud-config
password: asdfqwer
chpasswd: { expire: False }
ssh_pwauth: True
EOF
cloud-localds "$user_data" user-data
# Use the EFI magic. Picked up from:
# https://wiki.ubuntu.com/ARM64/QEMU
dd if=/dev/zero of=flash0.img bs=1M count=64
dd if=/usr/share/qemu-efi/QEMU_EFI.fd of=flash0.img conv=notrunc
dd if=/dev/zero of=flash1.img bs=1M count=64
fi
qemu-system-aarch64 \
-M virt \
-cpu cortex-a57 \
-device rtl8139,netdev=net0 \
-m 4096 \
-netdev user,id=net0 \
-nographic \
-smp 4 \
-drive "if=none,file=${img},id=hd0" \
-device virtio-blk-device,drive=hd0 \
-drive "file=${user_data},format=raw" \
-pflash flash0.img \
-pflash flash1.img \
;
debootstrap
amd64它不是预制映像,而是下载所有预制软件包,因此它速度很快,但也更具可配置性和实用性。
#!/usr/bin/env bash
set -eux
debootstrap_dir=debootstrap
root_filesystem=debootstrap.ext2.qcow2
sudo apt-get install \
debootstrap \
libguestfs-tools \
qemu-system-x86 \
;
if [ ! -d "$debootstrap_dir" ]; then
# Create debootstrap directory.
# - linux-image-generic: downloads the kernel image we will use under /boot
# - network-manager: automatically starts the network at boot for us
sudo debootstrap \
--include linux-image-generic \
bionic \
"$debootstrap_dir" \
http://archive.ubuntu.com/ubuntu \
;
sudo rm -f "$root_filesystem"
fi
linux_image="$(printf "${debootstrap_dir}/boot/vmlinuz-"*)"
if [ ! -f "$root_filesystem" ]; then
# Set root password.
echo 'root:root' | sudo chroot "$debootstrap_dir" chpasswd
# Remount root filesystem as rw.
cat << EOF | sudo tee "${debootstrap_dir}/etc/fstab"
/dev/sda / ext4 errors=remount-ro,acl 0 1
EOF
# Automaticaly start networking.
# Otherwise network commands fail with:
# Temporary failure in name resolution
# /ubuntu/1045278/ubuntu-server-18-04-temporary-failure-in-name-resolution/1080902#1080902
cat << EOF | sudo tee "$debootstrap_dir/etc/systemd/system/dhclient.service"
[Unit]
Description=DHCP Client
Documentation=man:dhclient(8)
Wants=network.target
Before=network.target
[Service]
Type=forking
PIDFile=/var/run/dhclient.pid
ExecStart=/sbin/dhclient -4 -q
[Install]
WantedBy=multi-user.target
EOF
sudo ln -sf "$debootstrap_dir/etc/systemd/system/dhclient.service" \
"${debootstrap_dir}/etc/systemd/system/multi-user.target.wants/dhclient.service"
# Why Ubuntu, why.
# https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
sudo chmod +r "${linux_image}"
# Generate image file from debootstrap directory.
# Leave 1Gb extra empty space in the image.
sudo virt-make-fs \
--format qcow2 \
--size +1G \
--type ext2 \
"$debootstrap_dir" \
"$root_filesystem" \
;
sudo chmod 666 "$root_filesystem"
fi
qemu-system-x86_64 \
-append 'console=ttyS0 root=/dev/sda' \
-drive "file=${root_filesystem},format=qcow2" \
-enable-kvm \
-serial mon:stdio \
-m 2G \
-kernel "${linux_image}" \
-device rtl8139,netdev=net0 \
-netdev user,id=net0 \
;
引导时不会出现任何systemd错误或警告。
现在从终端上,使用root
/ 登录root
,然后检查Internet是否可以使用以下命令:
printf 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | nc example.com 80
apt-get update
apt-get install hello
hello
我们nc
按照/programming/32341518/how-to-make-an-http-get-request-manually-with-netcat/52662497#52662497的说明进行使用,因为:
wget
并且curl
默认情况下未安装ping
默认情况下无法在QEMU上运行:https : //unix.stackexchange.com/questions/473448/how-to-ping-from-the-qemu-guest-to-an-external-url类似的Debian版本:https : //unix.stackexchange.com/questions/275429/creating-bootable-debian-image-with-debootstrap/473256#473256
由于我们在这里:
git clone git://kernel.ubuntu.com/ubuntu/ubuntu-bionic.git
cd ubuntu-bionic
# Tag matches the working kernel that debootstrap downloaded for us.
git checkout Ubuntu-4.15.0-20.21
fakeroot debian/rules clean
debian/rules updateconfigs
fakeroot debian/rules build-generic
linux_image="$(pwd)/debian/build/build-generic/arch/x86_64/boot/bzImage"
这产生了完全相同的配置,并且我相信使用了与debootstrap
下载的打包Ubuntu完全相同的源代码,如以下所述:在哪里可以获得11.04内核.config文件?
然后我用:
diff --git a/init/main.c b/init/main.c
index b8b121c17ff1..542229349efc 100644
--- a/init/main.c
+++ b/init/main.c
@@ -516,6 +516,8 @@ asmlinkage __visible void __init start_kernel(void)
char *command_line;
char *after_dashes;
+ pr_info("I'VE HACKED THE LINUX KERNEL!!!");
+
set_task_stack_end_magic(&init_task);
smp_setup_processor_id();
debug_objects_early_init();
并重建:
fakeroot debian/rules build-generic
它确实在引导过程中打印了我的消息:
I'VE HACKED THE LINUX KERNEL!!!
重建不是很快,所以也许有更好的命令?我只是在等待它说:
Kernel: arch/x86/boot/bzImage is ready (#3)
然后继续前进
该过程与amd64相似,但有以下区别:
1)
我们必须分两个阶段debootstrap
:
--foreign
要下载软件包chroot
--second-stage
使用QEMU用户模式仿真+binfmt_misc
另请参阅:什么是debootstrap --second-stage
2)默认内核启动最终失败,并显示:
[ 0.773665] Please append a correct "root=" boot option; here are the available partitions:
[ 0.774033] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
空的分区列表表示在尝试缺少的选项后,磁盘驱动程序存在严重错误:
CONFIG_VIRTIO_BLK=y
我认为在使用ISO时它可以工作,因为必须从initrd加载模块。
我尝试使用其他磁盘类型,但是virtio是-drive if=
when 的唯一有效值-M virt
,这是当今较精明的计算机类型。
因此,我们必须在启用该选项的情况下重新编译我们自己的内核,如下所述:当交叉编译内核时,当我只想修改一个文件时,如何才能避免每次清理干净?
Ubuntu开发人员应y
默认打开此CONFIG !这非常有用!
TODO:网络无法正常工作,错误消息是:
root@ciro-p51:~# systemctl status dhclient.service
root@ciro-p51:~# cat f
● dhclient.service - DHCP Client
Loaded: loaded (/etc/systemd/system/dhclient.service; enabled; vendor preset: enabled)
Active: failed (Result: protocol) since Sun 2018-01-28 15:58:42 UTC; 2min 2s ago
Docs: man:dhclient(8)
Process: 171 ExecStart=/sbin/dhclient -4 -q (code=exited, status=0/SUCCESS)
Jan 28 15:58:40 ciro-p51 systemd[1]: Starting DHCP Client...
Jan 28 15:58:42 ciro-p51 dhclient[171]: No broadcast interfaces found - exiting.
Jan 28 15:58:42 ciro-p51 systemd[1]: dhclient.service: Can't open PID file /var/run/dhclient.pid (yet?) after start: No such file or directory
Jan 28 15:58:42 ciro-p51 systemd[1]: dhclient.service: Failed with result 'protocol'.
Jan 28 15:58:42 ciro-p51 systemd[1]: Failed to start DHCP Client.
这是全自动脚本:
#!/usr/bin/env bash
# /ubuntu/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171
set -eux
debootstrap_dir=debootstrap
root_filesystem=debootstrap.ext2.qcow2
sudo apt-get install \
gcc-aarch64-linux-gnu \
debootstrap \
libguestfs-tools \
qemu-system-aarch64 \
qemu-user-static \
;
if [ ! -d "$debootstrap_dir" ]; then
sudo debootstrap \
--arch arm64 \
--foreign \
bionic \
"$debootstrap_dir" \
http://ports.ubuntu.com/ubuntu-ports \
;
sudo mkdir -p "${debootstrap_dir}/usr/bin"
sudo cp "$(which qemu-aarch64-static)" "${debootstrap_dir}/usr/bin"
sudo chroot "$debootstrap_dir" /debootstrap/debootstrap --second-stage
sudo rm -f "$root_filesystem"
fi
linux_image="$(printf "${debootstrap_dir}/boot/vmlinuz-"*)"
if [ ! -f "$root_filesystem" ]; then
# Set root password.
echo 'root:root' | sudo chroot "$debootstrap_dir" chpasswd
# Remount root filesystem as rw.
cat << EOF | sudo tee "${debootstrap_dir}/etc/fstab"
/dev/sda / ext4 errors=remount-ro,acl 0 1
EOF
# Automaticaly start networking.
# Otherwise network commands fail with:
# Temporary failure in name resolution
# /ubuntu/1045278/ubuntu-server-18-04-temporary-failure-in-name-resolution/1080902#1080902
cat << EOF | sudo tee "${debootstrap_dir}/etc/systemd/system/dhclient.service"
[Unit]
Description=DHCP Client
Documentation=man:dhclient(8)
Wants=network.target
Before=network.target
[Service]
Type=forking
PIDFile=/var/run/dhclient.pid
ExecStart=/sbin/dhclient -4 -q
[Install]
WantedBy=multi-user.target
EOF
sudo ln -sf "${debootstrap_dir}/etc/systemd/system/dhclient.service" \
"${debootstrap_dir}/etc/systemd/system/multi-user.target.wants/dhclient.service"
# Why Ubuntu, why.
# https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
sudo chmod +r "${linux_image}"
# Generate image file from debootstrap directory.
# Leave 1Gb extra empty space in the image.
sudo virt-make-fs \
--format qcow2 \
--size +1G \
--type ext2 \
"$debootstrap_dir" \
"$root_filesystem" \
;
sudo chmod 666 "$root_filesystem"
fi
# Build the Linux kernel.
linux_image="$(pwd)/linux/debian/build/build-generic/arch/arm64/boot/Image"
if [ ! -f "$linux_image" ]; then
git clone --branch Ubuntu-4.15.0-20.21 --depth 1 git://kernel.ubuntu.com/ubuntu/ubuntu-bionic.git linux
cd linux
patch -p1 << EOF
diff --git a/debian.master/config/config.common.ubuntu b/debian.master/config/config.common.ubuntu
index 5ff32cb997e9..8a190d3a0299 100644
--- a/debian.master/config/config.common.ubuntu
+++ b/debian.master/config/config.common.ubuntu
@@ -10153,7 +10153,7 @@ CONFIG_VIDEO_ZORAN_ZR36060=m
CONFIG_VIPERBOARD_ADC=m
CONFIG_VIRTIO=y
CONFIG_VIRTIO_BALLOON=y
-CONFIG_VIRTIO_BLK=m
+CONFIG_VIRTIO_BLK=y
CONFIG_VIRTIO_BLK_SCSI=y
CONFIG_VIRTIO_CONSOLE=y
CONFIG_VIRTIO_INPUT=m
EOF
export ARCH=arm64
export $(dpkg-architecture -aarm64)
export CROSS_COMPILE=aarch64-linux-gnu-
fakeroot debian/rules clean
debian/rules updateconfigs
fakeroot debian/rules DEB_BUILD_OPTIONS=parallel=`nproc` build-generic
cd -
fi
qemu-system-aarch64 \
-append 'console=ttyAMA0 root=/dev/vda rootfstype=ext2' \
-device rtl8139,netdev=net0 \
-drive "file=${root_filesystem},format=qcow2" \
-kernel "${linux_image}" \
-m 2G \
-netdev user,id=net0 \
-serial mon:stdio \
-M virt,highmem=off \
-cpu cortex-a57 \
-nographic \
;
它确实需要手动检查安装程序,但这是您可能要做的最稳定的事情,如果您只是想让虚拟机不时地运行以进行交互使用,那完全可以。
对于aarch64,我还没有使台式机正常工作,也许还需要注意:如何在QEMU中运行Ubuntu 16.04 ARM?
dhclient.service: Can't open PID file /var/run/dhclient.pid (yet?) after start: No such file or directory
,有任何线索吗?启动后,我能够touch /var/run/a
。
请参阅http://cloud-images.ubuntu.com/,其中包含可与qemu / kvm一起使用的云映像。
https://www.turnkeylinux.org/已经存在了很长时间。它们具有大量可下载的预制“设备”目录,例如多种格式的图像(ova,iso,vdmk,openstack,xen)。他们甚至可以直接在AWS中为您启动映像。
当我想开始探索特定的堆栈或需要解决一个问题时,我经常会下载其图像,将其转换为cow2并使用它。
您也可以从https://app.vagrantup.com/boxes/search或https://virtualboxes.org/images/抓取图像并进行转换。