设为首页 收藏本站
查看: 991|回复: 0

[经验分享] image Modify for kvm , openstack

[复制链接]

尚未签到

发表于 2018-6-2 11:04:14 | 显示全部楼层 |阅读模式
  4. Modifying images
  guestfish.......................................................................................................................11
  guestmount...................................................................................................................12
  virt-*tools.....................................................................................................................13
  Loopdevices, kpartx, network block devices.................................................................. 14
  Onceyou have obtained a virtual machine image, you may want to make some changes
  toit before uploading it to the OpenStack Image service. Here we describe severaltools
  availablethat allow you to modify images.
  Warning
  Donot attempt to use these tools to modify an image that is attached to a
  runningvirtual machine. These tools are designed to only modify images that
  arenot currently running.
  guestfish
  Theguestfish program is a tool from the libguestfs project that allows you to modify the
  filesinside of a virtual machine image.
  Notethat guestfish doesn't mount the image directly into the local filesystem.Instead, it
  providesyou with a shell interface that allows you to view, edit, and delete files.Many of
  theguestfish commands (e.g., touch, chmod, rm) are similar totraditional bash commands.
  Example guestfish session
  Weoften need to modify a virtual machine image to remove any traces of the MAC
  addressthat was assigned to the virtual network interface card when the image was
  firstcreated, since the MAC address will be different when it boots the next time.In this
  example,we show how we can use guestfish to remove references to the old MAC address
  bydeleting the /etc/udev/rules.d/70-persistent-net.rules file and removing
  theHWADDR line from the /etc/sysconfig/network-scripts/ifcfg-eth0 file.
  Assumewe have a CentOS qcow2 image called centos63_desktop.img. We would
  mountthe image in read-write mode by doing, as root:
  #guestfish --rw -a centos63_desktop.img
  Welcometo guestfish, the libguestfs filesystem interactive shell for
  editingvirtual machine filesystems.
  Type:'help' for help on commands
  'man'to read the manual
  'quit'to quit the shell
  ><fs>
  Thisstarts a guestfish session. Note that the guestfish prompt looks like a fish: ><fs>.
  ComputeVM Image Guide Sep 6, 2013 current
  12
  Wemust first use the run command at the guestfish prompt before we can doanything
  else.This will launch a virtual machine, which will be used to perform all of thefile
  manipulations.
  ><fs>run
  Wecan now view the filesystems in the image using the list-filesystems command:
  ><fs>list-filesystems
  /dev/vda1:ext4
  /dev/vg_centosbase/lv_root:ext4
  /dev/vg_centosbase/lv_swap:swap
  Weneed to mount the logical volume that contains the root partition:
  ><fs>mount /dev/vg_centosbase/lv_root /
  Next,we want to delete a file. We can use the rm guestfish command, whichworks the
  sameway it does in a traditional shell.
  ><fs>rm /etc/udev/rules.d/70-persistent-net.rules
  Wewant to edit the ifcfg-eth0 file to remove the HWADDR line. The edit commandwill
  copythe file to the host, invoke your editor, and then copy the file back.
  ><fs>edit /etc/sysconfig/network-scripts/ifcfg-eth0
  Le'ssay we want to modify this image to load the 8021q kernel at boot time. We'llneed to
  createan executable script in the /etc/sysconfig/modules/ directory. We can use the
  touchguestfish command to create an empty file,use the edit command to edit it, and use
  thechmod command to make it executable.
  ><fs>touch /etc/sysconfig/modules/8021q.modules
  ><fs>edit /etc/sysconfig/modules/8021q.modules
  Weadd the following line to the file and save it
  modprobe8021q
  Thenwe set to executable:
  ><fs>chmod 0755 /etc/sysconfig/modules/8021q.modules
  We'redone, so we can exit using the exit command:
  ><fs>exit
  Going further with guestfish
  Thereis an enormous amount of functionality in guestfish and a full treatment isbeyond
  thescope of this document. Instead, we recommend that you read the guestfs-recipes
  documentationpage for a sense of what is possible with these tools.
  guestmount
  Forsome types of changes, you may find it easier to mount the image's filesystemdirectly
  inthe guest. The guestmount program, also from the libguestfs project,allows you to do
  so.
  ComputeVM Image Guide Sep 6, 2013 current
  13
  Forexample, to mount the root partition from our centos63_desktop.qcow2 image to
  /mnt,we can do:
  #guestmount -a centos63_desktop.qcow2 -m /dev/vg_centosbase/lv_root --rw /mnt
  Ifwe didn't know in advance what the mountpoint is in the guest, we could use the-
  i(inspect)flag to tell guestmount to automatically determine what mount point to use:
  #guestmount -a centos63_desktop.qcow2 -i --rw /mnt
  Oncemounted, we could do things like list the installed packages using rpm:
  #rpm -qa --dbpath /mnt/var/lib/rpm
  Oncedone, we unmount:
  #umount /mnt
  virt-* tools
  Thelibguestfs project has a number of other useful tools,including:
   virt-df for displaying free space inside of an image.
   virt-resize for resizing an image.
   virt-sysprep for preparing an image for distribution(e.g., delete SSH host keys, remove
  MACaddress info, remove user accounts).
   virt-sparsify for making an image sparse
   virt-p2v for converting a physical machine to an imagethat runs on KVM
   virt-v2v for converting Xen and VMWare images to KVMimages
  Resize an image
  Here'sa simple of example of how to use virt-resize to resize an image. Assumewe have a
  16GBWindows image in qcow2 format that we want to resize to 50GB. First, we use virtfilesystems
  toidentify the partitions:
  #virt-filesystems --long --parts --blkdevs -h -a /data/images/win2012.qcow2
  NameType MBR Size Parent
  /dev/sda1partition 07 350M /dev/sda
  /dev/sda2partition 07 16G /dev/sda
  /dev/sdadevice - 16G -
  Inthis case, it's the /dev/sda2 partition that we want to resize. We create a newqcow2
  imageand use the virt-resize command to write a resized copy of the originalinto the new
  image
  #qemu-img create -f qcow2 /data/images/win2012-50gb.qcw2 50G
  #virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 /data/images/
  win2012-50gb.qcow2
  Examining/data/images/win2012.qcow2 ...
  ComputeVM Image Guide Sep 6, 2013 current
  14
  **********
  Summaryof changes:
  /dev/sda1:This partition will be left alone.
  /dev/sda2:This partition will be resized from 15.7G to 49.7G. The
  filesystemntfs on /dev/sda2 will be expanded using the
  'ntfsresize'method.
  **********
  Settingup initial partition table on /data/images/win2012-50gb.qcow2 ...
  Copying/dev/sda1 ...
  100%###################################################################
  00:00
  Copying/dev/sda2 ...
  100%###################################################################
  00:00
  Expanding/dev/sda2 using the 'ntfsresize' method ...
  Resizeoperation completed with no errors. Before deleting the old
  disk,carefully check that the resized disk boots and works correctly.
  Loop devices, kpartx, network block devices
  Ifyou don't have access to libguestfs, you can mount image file systems directlyin the host
  usingloop devices, kpartx, and network block devices.
  Warning
  Mountinguntrusted guest images using the tools described in this section is a
  securityrisk, always use libguestfs tools such as guestfish and guestmount if you
  haveaccess to them. See Areminder why you should never mount guest disk
  imageson the host OS byDaniel Berrangé for more details.
  Mounting a raw image (without LVM)
  Ifyou have a raw virtual machine image that is not using LVM to manage itspartitions.
  First,use the losetup command to find an unused loop device.
  #losetup -f
  /dev/loop0
  Inthis example, /dev/loop0 is free. Associate a loop device with the raw image:
  #losetup /dev/loop0 fedora17.img
  Ifthe image only has a single partition, you can mount the loop device directly:
  #mount /dev/loop0 /mnt
  Ifthe image has multiple partitions, use kpartx to expose the partitionsas separate devices
  (e.g.,/dev/mapper/loop0p1), then mount the partition that corresponds to the rootfile
  system:
  #kpartx -av /dev/loop0
  ComputeVM Image Guide Sep 6, 2013 current
  15
  Ifthe image has, say three partitions (/boot, /, /swap), there should be one newdevice
  createdper partition:
  $ls -l /dev/mapper/loop0p*
  brw-rw----1 root disk 43, 49 2012-03-05 15:32 /dev/mapper/loop0p1
  brw-rw----1 root disk 43, 50 2012-03-05 15:32 /dev/mapper/loop0p2
  brw-rw----1 root disk 43, 51 2012-03-05 15:32 /dev/mapper/loop0p3
  Tomount the second partition, as root:
  #mkdir /mnt/image
  #mount /dev/mapper/loop0p2 /mnt
  Onceyou're done, to clean up:
  #umount /mnt
  #kpartx -d /dev/loop0
  #losetup -d /dev/loop0
  Mounting a raw image (with LVM)
  Ifyour partitions are managed with LVM, use losetup and kpartx as in the previous
  exampleto expose the partitions to the host:
  #losetup -f
  /dev/loop0
  #losetup /dev/loop0 rhel62.img
  #kpartx -av /dev/loop0
  Next,you need to use the vgscan command to identify the LVM volume groups andthen
  vgchangeto expose the volumes as devices:
  #vgscan
  Readingall physical volumes. This may take a while...
  Foundvolume group "vg_rhel62x8664" using metadata type lvm2
  #vgchange -ay
  2logical volume(s) in volume group "vg_rhel62x8664" now active
  #mount /dev/vg_rhel62x8664/lv_root /mnt
  Cleanup when you're done:
  #umount /mnt
  #vgchange -an vg_rhel62x8664
  #kpartx -d /dev/loop0
  #losetup -d /dev/loop0
  Mounting a qcow2 image (without LVM)
  Youneed the nbd (network block device) kernel module loaded to mount qcow2 images.
  Thiswill load it with support for 16 block devices, which is fine for our purposes.As root:
  #modprobe nbd max_part=16
  Assumingthe first block device (/dev/nbd0) is not currently in use, we can expose thedisk
  partitionsusing the qemu-nbd and partprobe commands. As root:
  #qemu-nbd -c /dev/nbd0 image.qcow2
  #partprobe /dev/nbd0
  ComputeVM Image Guide Sep 6, 2013 current
  16
  Ifthe image has, say three partitions (/boot, /, /swap), there should be one newdevice
  createdper partition:
  $ls -l /dev/nbd3*
  brw-rw----1 root disk 43, 48 2012-03-05 15:32 /dev/nbd0
  brw-rw----1 root disk 43, 49 2012-03-05 15:32 /dev/nbd0p1
  brw-rw----1 root disk 43, 50 2012-03-05 15:32 /dev/nbd0p2
  brw-rw----1 root disk 43, 51 2012-03-05 15:32 /dev/nbd0p3
  Note
  Ifthe network block device you selected was already in use, the initial qemunbd
  commandwill fail silently, and the /dev/nbd3p{1,2,3} device files will
  notbe created.
  Ifthe image partitions are not managed with LVM, they can be mounted directly:
  #mkdir /mnt/image
  #mount /dev/nbd3p2 /mnt
  Whenyou're done, clean up:
  #umount /mnt
  #qemu-nbd -d /dev/g nbd0
  Mounting a qcow2 image (with LVM)
  Ifthe image partitions are managed with LVM, after you use qemu-nbd and partprobe,
  youmust use vgscan and vgchange -ay in order to expose the LVMpartitions as devices
  thatcan be mounted:
  #modprobe nbd max_part=16
  #qemu-nbd -c /dev/nbd0 image.qcow2
  #partprobe /dev/nbd0# vgscan
  Readingall physical volumes. This may take a while...
  Foundvolume group "vg_rhel62x8664" using metadata type lvm2
  #vgchange -ay
  2logical volume(s) in volume group "vg_rhel62x8664" now active
  #mount /dev/vg_rhel62x8664/lv_root /mnt
  Whenyou're done, clean up:
  #umount /mnt
  #vgchange -an vg_rhel62x8664
  #qemu-nbd -d /dev/nbd0
  

  

  转载URL: http://docs.openstack.org/grizzly/openstack-image/content/centos-image.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-506391-1-1.html 上篇帖子: puppet部署openstack(controller) 下篇帖子: Python 调用libguestfs 实现文件注入(Openstack)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表