/nova/virt/images.py

https://github.com/blackantcloud/nova · Python · 112 lines · 62 code · 24 blank · 26 comment · 12 complexity · 9453af9d06ba679423261e8074c2334c MD5 · raw file

  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2010 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. # Copyright (c) 2010 Citrix Systems, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. # not use this file except in compliance with the License. You may obtain
  9. # a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. # License for the specific language governing permissions and limitations
  17. # under the License.
  18. """
  19. Handling of VM disk images.
  20. """
  21. import errno
  22. import os
  23. from nova import exception
  24. from nova import flags
  25. import nova.image
  26. from nova import log as logging
  27. from nova.openstack.common import cfg
  28. from nova import utils
  29. LOG = logging.getLogger(__name__)
  30. image_opts = [
  31. cfg.BoolOpt('force_raw_images',
  32. default=True,
  33. help='Force backing images to raw format'),
  34. ]
  35. FLAGS = flags.FLAGS
  36. FLAGS.register_opts(image_opts)
  37. def fetch(context, image_href, path, _user_id, _project_id):
  38. # TODO(vish): Improve context handling and add owner and auth data
  39. # when it is added to glance. Right now there is no
  40. # auth checking in glance, so we assume that access was
  41. # checked before we got here.
  42. (image_service, image_id) = nova.image.get_image_service(context,
  43. image_href)
  44. with utils.remove_path_on_error(path):
  45. with open(path, "wb") as image_file:
  46. metadata = image_service.get(context, image_id, image_file)
  47. return metadata
  48. def fetch_to_raw(context, image_href, path, user_id, project_id):
  49. path_tmp = "%s.part" % path
  50. metadata = fetch(context, image_href, path_tmp, user_id, project_id)
  51. def _qemu_img_info(path):
  52. out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
  53. 'qemu-img', 'info', path)
  54. # output of qemu-img is 'field: value'
  55. # the fields of interest are 'file format' and 'backing file'
  56. data = {}
  57. for line in out.splitlines():
  58. (field, val) = line.split(':', 1)
  59. if val[0] == " ":
  60. val = val[1:]
  61. data[field] = val
  62. return(data)
  63. with utils.remove_path_on_error(path_tmp):
  64. data = _qemu_img_info(path_tmp)
  65. fmt = data.get("file format")
  66. if fmt is None:
  67. raise exception.ImageUnacceptable(
  68. reason=_("'qemu-img info' parsing failed."),
  69. image_id=image_href)
  70. if "backing file" in data:
  71. backing_file = data['backing file']
  72. raise exception.ImageUnacceptable(image_id=image_href,
  73. reason=_("fmt=%(fmt)s backed by: %(backing_file)s") % locals())
  74. if fmt != "raw" and FLAGS.force_raw_images:
  75. staged = "%s.converted" % path
  76. LOG.debug("%s was %s, converting to raw" % (image_href, fmt))
  77. with utils.remove_path_on_error(staged):
  78. out, err = utils.execute('qemu-img', 'convert', '-O', 'raw',
  79. path_tmp, staged)
  80. data = _qemu_img_info(staged)
  81. if data.get('file format', None) != "raw":
  82. raise exception.ImageUnacceptable(image_id=image_href,
  83. reason=_("Converted to raw, but format is now %s") %
  84. data.get('file format', None))
  85. os.rename(staged, path)
  86. else:
  87. os.rename(path_tmp, path)
  88. return metadata