微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 qemu-nbd 在 KVM 上的云映像中烘焙一个新用户和一个密码,以访问生成的 KVM-VM

本练习的目标是下载云映像并生成 KVM - 虚拟机。云映像预装了 cloud-init,可用于将用户密钥、主机名和其他元数据注入正在生成的虚拟机中。我们不会使用 cloud-init 注入,我们将采用的方法是在云映像中烘焙一个新用户一个密码,以访问生成的 KVM-VM。这种方法是一种 hack,它也可用于访问 VM,其中用户被锁定或用于在 Openstack 云上可用的云映像中设置后门。请注意,我在本练习中使用的是 Ubuntu 云 (Trusty - 14.04) 映像。

wget -c https://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img
mv trusty-server-cloudimg-amd64-disk1.img ubuntu-server-1404.img
  • 安装 qemu 工具
apt-get install qemu-utils
  • 加载 nbd 模块,这将允许使用 qemu-nbd 附加云图像
modprobe nbd
  • 验证是否加载了 nbd 模块
root@hp-envy:~# lsmod | grep nbd
nbd                    32768  0
  • 加载 nbd 模块后,您应该在 /dev 中找到以 nbd 开头的对象数量
root@hp-envy:~# ls /dev/nbd*
/dev/nbd0  /dev/nbd10  /dev/nbd12  /dev/nbd14  /dev/nbd2  /dev/nbd4  /dev/nbd6  /dev/nbd8
/dev/nbd1  /dev/nbd11  /dev/nbd13  /dev/nbd15  /dev/nbd3  /dev/nbd5  /dev/nbd7  /dev/nbd9
  • 将云映像附加到 nbd 设备之一

请注意:我已将下载的图像复制到 /var/lib/libvirt/images/

root@hp-envy:~# qemu-nbd --connect=/dev/nbd0 /var/lib/libvirt/images/ubuntu-server-1404.img 
  • 创建挂载点目录
root@hp-envy:~# mkdir /mnt/srcVM
  • 使用 fdisk 检查挂载的云镜像上的分区
root@hp-envy:~# fdisk -l /dev/nbd0
disk /dev/nbd0: 2.2 GiB,2361393152 bytes,4612096 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
disklabel type: dos
disk identifier: 0x000c5984

Device      Boot Start     End Sectors  Size Id Type
/dev/nbd0p1 *     2048 4612095 4610048  2.2G 83 Linux
root@hp-envy:~# mount /dev/nbd0p1 /mnt/srcVM
  • 检查映像上的根文件系统
root@hp-envy:~# cd /mnt/srcVM/
root@hp-envy:/mnt/srcVM# ls
bin  boot  dev  etc  home  initrd.img  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  vmlinuz
  • 使用 chroot jail 在挂载的根文件系统中执行命令
root@hp-envy:~# chroot /mnt/srcVM/

root@hp-envy:/# ls
bin   dev  home        lib    lost+found  mnt  proc  run   srv  tmp  var
boot  etc  initrd.img  lib64  media       opt  root  sbin  sys  usr  vmlinuz
root@hp-envy:/# useradd amol
root@hp-envy:/# passwd amol
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@hp-envy:/# sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
vi /etc/sudoers
文件最后增加如下内容

amol ALL=(ALL) nopASSWD: ALL
root@hp-envy:~# umount /mnt/srcVM/
root@hp-envy:~# qemu-nbd --disconnect /dev/nbd0 
/dev/nbd0 disconnected
root@hp-envy:~# virt-install --name ubuntu14 --vcpus 1 --memory 1024 --disk path=/var/lib/libvirt/images/ubuntu-server-1404.img,bus=virtio,cache=writeback 
--graphics vnc,listen=0.0.0.0 
--network bridge:virbr0,model=virtio --noautoconsole --os-type=linux --import 
WARNING  No operating system detected,VM performance may suffer. Specify an OS with --os-variant for optimal results.

Starting install...
Creating domain...                                                                                                             |    0 B  00:00:00     
Domain creation completed.
  • 列出虚拟机
root@hp-envy:~# virsh list --all
 Id    Name                           State
----------------------------------------------------
 12    ubuntu14                       running
  • 使用 virt-manager 查看新启动的 VM 的控制台

您可能会看到 Cloud-init 尝试到达网桥 newtwork 网关以获取元数据的警告消息很少,我们将在另一篇文章中讨论这一点。

2017-11-16 14:22:35,359 - url_helper.py[WARNING]: Calling 'http://192.168.122.1//latest/Meta-data/instance-id' Failed [112/120s]: request error [httpconnectionPool(host='192.168.122.1',port=80): Max retries exceeded with url: //latest/Meta-data/instance-id (Caused by <class 'socket.error'>: [Errno 111] Connection refused)]
2017-11-16 14:22:42,367 - url_helper.py[WARNING]: Calling 'http://192.168.122.1//latest/Meta-data/instance-id' Failed [119/120s]: request error [httpconnectionPool(host='192.168.122.1',port=80): Max retries exceeded with url: //latest/Meta-data/instance-id (Caused by <class 'socket.error'>: [Errno 115] Operation Now in progress)]
2017-11-16 14:22:49,374 - DataSourceCloudStack.py[CRITICAL]: Giving up on waiting for the Metadata from ['http://192.168.122.1//latest/Meta-data/instance-id'] after 126 seconds
root@hp-envy:~# virsh console ubuntu14
Connected to domain ubuntu14
Escape character is ^]

Ubuntu 14.04.5 LTS ubuntu ttyS0

ubuntu login: amol
Password: 
Last login: Thu Nov 16 17:52:53 UTC 2017 on tty1
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-135-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System @R_296_4045@ion as of Thu Nov 16 17:52:53 UTC 2017

恭喜!!您已成功在 KVM 上启动云映像。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐