blob: ca867006de1e704a022602058e60304d67acac73 [file] [log] [blame]
Ot ten Thijeae835ac2010-10-18 13:37:37 +01001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Limited driver for the Qcow2 filesystem, capable of extracting snapshot
18 * metadata from Qcow2 formatted image file.
19 *
20 * Similar functionality is implemented in block/qcow2.c, block/qcow2-snapshot.c
21 * and block.c. This separate implementation was made to further decouple the UI
22 * of the Android emulator from the underlying Qemu system. It allows the UI to
23 * show snapshot listings without having to fall back on Qemu's block driver
24 * system, which would pull in a lot of code irrelevant for the UI.
25 */
26
27#include <errno.h>
28#include <fcntl.h>
29#include <stdint.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <time.h>
34#include <unistd.h>
35
36#include "android/utils/debug.h"
37#include "android/utils/system.h"
38#include "bswap.h"
39#include "snapshot.h"
40
41#if CONFIG_ANDROID_SNAPSHOTS
42
43/* "Magic" sequence of four bytes required by spec to be the first four bytes
44 * of any Qcow file.
45 */
46#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
47#define QCOW_VERSION 2
48
49/* Reads 'nbyte' bytes from 'fd' into 'buf', retrying on interrupts.
50 * Exit()s if the read fails for any other reason.
51 */
52static int
53read_or_die(int fd, void *buf, size_t nbyte)
54{
55 int ret = 0;
56 do {
57 ret = read(fd, buf, nbyte);
58 }
59 while(ret < 0 && errno == EINTR);
60
61 if (ret < 0) {
62 derror("read failed: %s", strerror(errno));
63 exit(1);
64 }
65
66 return ret;
67}
68
69/* Wrapper around lseek(), exit()s on error.
70 */
71static off_t
72seek_or_die(int fd, off_t offset, int whence)
73{
74 off_t ret = lseek(fd, offset, whence);
75 if (ret < 0) {
76 derror("seek failed: %s", strerror(errno));
77 exit(1);
78 }
79 return ret;
80}
81
82
83typedef struct SnapshotInfo {
84 char *id_str;
85 char *name;
86
87 uint32_t date_sec;
88 uint32_t date_nsec;
89
90 uint64_t vm_clock_nsec;
91 uint32_t vm_state_size;
92} SnapshotInfo;
93
94static SnapshotInfo*
95snapshot_info_alloc()
96{
97 return android_alloc(sizeof(SnapshotInfo));
98}
99
100static void
101snapshot_info_free( SnapshotInfo* info )
102{
103 AFREE(info->id_str);
104 AFREE(info->name);
105 AFREE(info);
106}
107
108
109/* Reads a snapshot record from a qcow2-formatted file.
110 *
111 * The function assumes the file position of 'fd' points to the beginning of a
112 * QcowSnapshotHeader record. When the call returns, the file position of fd is
113 * at the place where the next QcowSnapshotHeader should start, if there is one.
114 *
115 * C.f. QCowSnapshotHeader in block/qcow2-snapshot.c for the complete layout of
116 * the header.
117 */
118static void
119snapshot_info_read( int fd, SnapshotInfo* info )
120{
121 uint64_t start_offset = seek_or_die(fd, 0, SEEK_CUR);
122
123 uint32_t extra_data_size;
124 uint16_t id_str_size, name_size;
125
126 /* read fixed-length fields */
127 seek_or_die(fd, 12, SEEK_CUR); /* skip l1 info */
128 read_or_die(fd, &id_str_size, sizeof(id_str_size));
129 read_or_die(fd, &name_size, sizeof(name_size));
130 read_or_die(fd, &info->date_sec, sizeof(info->date_sec));
131 read_or_die(fd, &info->date_nsec, sizeof(info->date_nsec));
132 read_or_die(fd, &info->vm_clock_nsec, sizeof(info->vm_clock_nsec));
133 read_or_die(fd, &info->vm_state_size, sizeof(info->vm_state_size));
134 read_or_die(fd, &extra_data_size, sizeof(extra_data_size));
135
136 /* convert to host endianness */
137 be16_to_cpus(&id_str_size);
138 be16_to_cpus(&name_size);
139 be32_to_cpus(&info->date_sec);
140 be32_to_cpus(&info->date_nsec);
141 be64_to_cpus(&info->vm_clock_nsec);
142 be32_to_cpus(&info->vm_state_size);
143 be32_to_cpus(&extra_data_size);
144 be32_to_cpus(&extra_data_size);
145
146 /* read variable-length buffers*/
147 info->id_str = android_alloc(id_str_size + 1); // +1: manual null-termination
148 info->name = android_alloc(name_size + 1);
149 seek_or_die(fd, extra_data_size, SEEK_CUR); /* skip extra data */
150 read_or_die(fd, info->id_str, id_str_size);
151 read_or_die(fd, info->name, name_size);
152
153 info->id_str[id_str_size] = '\0';
154 info->name[name_size] = '\0';
155
156 /* headers are 8 byte aligned, ceil to nearest multiple of 8 */
157 uint64_t end_offset = seek_or_die(fd, 0, SEEK_CUR);
158 uint32_t total_size = end_offset - start_offset;
159 uint32_t aligned_size = ((total_size - 1) / 8 + 1) * 8;
160
161 /* skip to start of next record */
162 seek_or_die(fd, start_offset + aligned_size, SEEK_SET);
163}
164
165
166#define NB_SUFFIXES 4
167
168/* Returns the size of a snapshot in a human-readable format.
169 *
170 * This function copyright (c) 2003 Fabrice Bellard
171 */
172static char*
173snapshot_format_size( char *buf, int buf_size, int64_t size )
174{
175 static const char suffixes[NB_SUFFIXES] = "KMGT";
176 int64_t base;
177 int i;
178
179 if (size <= 999) {
180 snprintf(buf, buf_size, "%" PRId64, size);
181 } else {
182 base = 1024;
183 for(i = 0; i < NB_SUFFIXES; i++) {
184 if (size < (10 * base)) {
185 snprintf(buf, buf_size, "%0.1f%c",
186 (double)size / base,
187 suffixes[i]);
188 break;
189 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
190 snprintf(buf, buf_size, "%" PRId64 "%c",
191 ((size + (base >> 1)) / base),
192 suffixes[i]);
193 break;
194 }
195 base = base * 1024;
196 }
197 }
198 return buf;
199}
200
201static char*
202snapshot_format_create_date( char *buf, size_t buf_size, time_t *time )
203{
204 struct tm *tm;
205 tm = localtime(time);
206 strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", tm);
207 return buf;
208}
209
210static char*
211snapshot_format_vm_clock( char *buf, size_t buf_size, uint64_t vm_clock_nsec )
212{
213 uint64_t secs = vm_clock_nsec / 1000000000;
214 snprintf(buf, buf_size, "%02d:%02d:%02d.%03d",
215 (int)(secs / 3600),
216 (int)((secs / 60) % 60),
217 (int)(secs % 60),
218 (int)((vm_clock_nsec / 1000000) % 1000));
219 return buf;
220}
221
222/* Prints a row of the snapshot table to stdout. */
223static void
224snapshot_info_print( SnapshotInfo *info )
225{
226 char size_buf[8];
227 char date_buf[21];
228 char clock_buf[21];
229
230 snapshot_format_size(size_buf, sizeof(size_buf), info->vm_state_size);
231 snapshot_format_create_date(date_buf, sizeof(date_buf),
232 (time_t*) &info->date_sec);
233 snapshot_format_vm_clock(clock_buf, sizeof(clock_buf), info->vm_clock_nsec);
234
235 printf(" %-10s%-20s%7s%20s%15s\n",
236 info->id_str, info->name, size_buf, date_buf, clock_buf);
237}
238
239/* Prints table of all snapshots recorded in the file 'fd'.
240 */
241static void
242snapshot_print_table( int fd, uint32_t nb_snapshots, uint64_t snapshots_offset )
243{
244 printf(" %-10s%-20s%7s%20s%15s\n",
245 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
246
247 /* skip ahead to snapshot data */
248 seek_or_die(fd, snapshots_offset, SEEK_SET);
249
250 /* iterate over snapshot records */
251 int i;
252 for (i = 0; i < nb_snapshots; i++) {
253 SnapshotInfo *info = snapshot_info_alloc();
254 snapshot_info_read(fd, info);
255 snapshot_info_print(info);
256
257 snapshot_info_free(info);
258 }
259}
260
261/* Validates that 'fd' starts with a correct Qcow 2 header. Prints an error and
262 * exit()s if validation fails.
263 */
264static void
265snapshot_validate_qcow_file( int fd )
266{
267 /* read magic number and qcow version (2x4 bytes) */
268 uint32_t magic, version;
269 read_or_die(fd, &magic, sizeof(magic));
270 read_or_die(fd, &version, sizeof(version));
271 be32_to_cpus(&magic);
272 be32_to_cpus(&version);
273
274 if (magic != QCOW_MAGIC) {
275 derror("Not a valid Qcow snapshot file (expected magic value '%08x', got '%08x').",
276 QCOW_MAGIC, magic);
277 exit(1);
278 }
279 if (version != QCOW_VERSION) {
280 derror("Unsupported Qcow version (need %d, got %d).",
281 QCOW_VERSION, version);
282 exit(1);
283 }
284}
285
286/* Reads snapshot information from a Qcow2 file header.
287 *
288 * C.f. QCowHeader in block/qcow2.h for an exact listing of the header
289 * contents.
290 */
291static void
292snapshot_read_qcow_header( int fd, uint32_t *nb_snapshots, uint64_t *snapshots_offset )
293{
294 snapshot_validate_qcow_file(fd);
295
296 /* skip non-snapshot related metadata (4x8 + 5x4 = 52 bytes)*/
297 seek_or_die(fd, 52, SEEK_CUR);
298
299 read_or_die(fd, nb_snapshots, sizeof(*nb_snapshots));
300 read_or_die(fd, snapshots_offset, sizeof(*snapshots_offset));
301
302 /* convert to host endianness */
303 be32_to_cpus(nb_snapshots);
304 be64_to_cpus(snapshots_offset);
305}
306
307/* Prints a table with information on the snapshots in the qcow2-formatted file
308 * 'snapstorage', then exit()s.
309 */
310void
311snapshot_print_and_exit( const char *snapstorage )
312{
313 /* open snapshot file */
314 int fd = open(snapstorage, O_RDONLY);
315 if (!fd) {
316 derror("Could not open snapshot file '%s': %s", snapstorage, strerror(errno));
317 exit(1);
318 }
319
320 /* read snapshot info from file header */
321 uint32_t nb_snapshots;
322 uint64_t snapshots_offset;
323 snapshot_read_qcow_header(fd, &nb_snapshots, &snapshots_offset);
324
325 if (nb_snapshots > 0) {
326 printf("Snapshots in file '%s':\n", snapstorage);
327 snapshot_print_table(fd, nb_snapshots, snapshots_offset);
328 }
329 else {
330 printf("File '%s' contains no snapshots yet.\n", snapstorage);
331 }
332
333 close(fd);
334 exit(0);
335}
336#endif // CONFIG_ANDROID_SNAPSHOTS