使用 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
  • nbd0p1 显示为第一个分区,挂载相同
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
  • 修改 sshd 配置以允许密码认证
root@hp-envy:/# sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
  • 设置新增用户免密码sudo,
vi /etc/sudoers
文件最后增加如下内容

amol ALL=(ALL) NOPASSWD: ALL
  • 退出 chroot 并卸载镜像文件
root@hp-envy:~# umount /mnt/srcVM/
root@hp-envy:~# qemu-nbd --disconnect /dev/nbd0 
/dev/nbd0 disconnected
  • 使用云映像文件生成 VM
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 information as of Thu Nov 16 17:52:53 UTC 2017

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值