~tr3buchet/nova/lock

« back to all changes in this revision

Viewing changes to nova/compute/disk.py

  • Committer: Vishvananda Ishaya
  • Date: 2010-12-22 20:59:53 UTC
  • mto: This revision was merged to the branch mainline in revision 482.
  • Revision ID: vishvananda@gmail.com-20101222205953-j2j5t0qjwlcd0t2s
merge trunk and upgrade to cheetah templating

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
        execute('resize2fs %s' % infile)
68
68
        file_size = FLAGS.minimum_root_size
69
69
    elif file_size % sector_size != 0:
70
 
        logging.warn("Input partition size not evenly divisible by"
71
 
                     " sector size: %d / %d", file_size, sector_size)
 
70
        logging.warn(_("Input partition size not evenly divisible by"
 
71
                       " sector size: %d / %d"), file_size, sector_size)
72
72
    primary_sectors = file_size / sector_size
73
73
    if local_bytes % sector_size != 0:
74
 
        logging.warn("Bytes for local storage not evenly divisible"
75
 
                     " by sector size: %d / %d", local_bytes, sector_size)
 
74
        logging.warn(_("Bytes for local storage not evenly divisible"
 
75
                       " by sector size: %d / %d"), local_bytes, sector_size)
76
76
    local_sectors = local_bytes / sector_size
77
77
 
78
78
    mbr_last = 62  # a
106
106
                % (outfile, local_type, local_first, local_last))
107
107
 
108
108
 
 
109
def extend(image, size, execute):
 
110
    file_size = os.path.getsize(image)
 
111
    if file_size >= size:
 
112
        return
 
113
    return execute('truncate -s size %s' % (image,))
 
114
 
 
115
 
109
116
def inject_data(image, key=None, net=None, partition=None, execute=None):
110
117
    """Injects a ssh key and optionally net data into a disk image.
111
118
 
115
122
    If partition is not specified it mounts the image as a single partition.
116
123
 
117
124
    """
118
 
    out, err = execute('sudo losetup -f --show %s' % image)
 
125
    out, err = execute('sudo losetup --find --show %s' % image)
119
126
    if err:
120
 
        raise exception.Error('Could not attach image to loopback: %s' % err)
 
127
        raise exception.Error(_('Could not attach image to loopback: %s')
 
128
                              % err)
121
129
    device = out.strip()
122
130
    try:
123
131
        if not partition is None:
124
132
            # create partition
125
133
            out, err = execute('sudo kpartx -a %s' % device)
126
134
            if err:
127
 
                raise exception.Error('Failed to load partition: %s' % err)
 
135
                raise exception.Error(_('Failed to load partition: %s') % err)
128
136
            mapped_device = '/dev/mapper/%sp%s' % (device.split('/')[-1],
129
137
                                                   partition)
130
138
        else:
131
139
            mapped_device = device
 
140
 
 
141
        # We can only loopback mount raw images. If the device isn't there,
 
142
        # it's normally because it's a .vmdk or a .vdi etc
 
143
        if not os.path.exists(mapped_device):
 
144
            raise exception.Error('Mapped device was not found (we can'
 
145
                                  ' only inject raw disk images): %s' %
 
146
                                  mapped_device)
 
147
 
 
148
        # Configure ext2fs so that it doesn't auto-check every N boots
132
149
        out, err = execute('sudo tune2fs -c 0 -i 0 %s' % mapped_device)
133
150
 
134
151
        tmpdir = tempfile.mkdtemp()
137
154
            out, err = execute(
138
155
                    'sudo mount %s %s' % (mapped_device, tmpdir))
139
156
            if err:
140
 
                raise exception.Error('Failed to mount filesystem: %s' % err)
 
157
                raise exception.Error(_('Failed to mount filesystem: %s')
 
158
                                      % err)
141
159
 
142
160
            try:
143
161
                if key:
156
174
                execute('sudo kpartx -d %s' % device)
157
175
    finally:
158
176
        # remove loopback
159
 
        execute('sudo losetup -d %s' % device)
 
177
        execute('sudo losetup --detach %s' % device)
160
178
 
161
179
 
162
180
def _inject_key_into_fs(key, fs, execute=None):
165
183
    key is an ssh key string.
166
184
    fs is the path to the base of the filesystem into which to inject the key.
167
185
    """
168
 
    sshdir = os.path.join(os.path.join(fs, 'root'), '.ssh')
 
186
    sshdir = os.path.join(fs, 'root', '.ssh')
169
187
    execute('sudo mkdir -p %s' % sshdir)  # existing dir doesn't matter
170
188
    execute('sudo chown root %s' % sshdir)
171
189
    execute('sudo chmod 700 %s' % sshdir)