fs_related.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python -tt
  2. #
  3. # Copyright (c) 2007, Red Hat, Inc.
  4. # Copyright (c) 2009, 2010, 2011 Intel, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; version 2 of the License
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc., 59
  17. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. from __future__ import with_statement
  19. import os
  20. import sys
  21. import errno
  22. import stat
  23. import random
  24. import string
  25. import time
  26. import uuid
  27. from wic import msger
  28. from wic.utils import runner
  29. from wic.utils.errors import *
  30. from wic.utils.oe.misc import *
  31. def find_binary_path(binary):
  32. if os.environ.has_key("PATH"):
  33. paths = os.environ["PATH"].split(":")
  34. else:
  35. paths = []
  36. if os.environ.has_key("HOME"):
  37. paths += [os.environ["HOME"] + "/bin"]
  38. paths += ["/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin"]
  39. for path in paths:
  40. bin_path = "%s/%s" % (path, binary)
  41. if os.path.exists(bin_path):
  42. return bin_path
  43. print "External command '%s' not found, exiting." % binary
  44. print " (Please install '%s' on your host system)" % binary
  45. sys.exit(1)
  46. def makedirs(dirname):
  47. """A version of os.makedirs() that doesn't throw an
  48. exception if the leaf directory already exists.
  49. """
  50. try:
  51. os.makedirs(dirname)
  52. except OSError, err:
  53. if err.errno != errno.EEXIST:
  54. raise
  55. class Disk:
  56. """
  57. Generic base object for a disk.
  58. """
  59. def __init__(self, size, device = None):
  60. self._device = device
  61. self._size = size
  62. def create(self):
  63. pass
  64. def cleanup(self):
  65. pass
  66. def get_device(self):
  67. return self._device
  68. def set_device(self, path):
  69. self._device = path
  70. device = property(get_device, set_device)
  71. def get_size(self):
  72. return self._size
  73. size = property(get_size)
  74. class DiskImage(Disk):
  75. """
  76. A Disk backed by a file.
  77. """
  78. def __init__(self, image_file, size):
  79. Disk.__init__(self, size)
  80. self.image_file = image_file
  81. def exists(self):
  82. return os.path.exists(self.image_file)
  83. def create(self):
  84. if self.device is not None:
  85. return
  86. blocks = self.size / 1024
  87. if self.size - blocks * 1024:
  88. blocks += 1
  89. # create disk image
  90. dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=1" % \
  91. (self.image_file, blocks)
  92. exec_cmd(dd_cmd)
  93. self.device = self.image_file