blob: 7ab135f6dda27dbbad5468b4579b54a66221f4a7 [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
David 'Digit' Turner6e9d1d42013-12-13 17:29:58 +010036#include "qemu/bswap.h"
Ot ten Thijeae835ac2010-10-18 13:37:37 +010037#include "android/utils/debug.h"
38#include "android/utils/system.h"
David 'Digit' Turnere5af8a22011-02-24 16:48:19 +010039#include "android/snapshot.h"
Ot ten Thijeae835ac2010-10-18 13:37:37 +010040
41/* "Magic" sequence of four bytes required by spec to be the first four bytes
42 * of any Qcow file.
43 */
44#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
45#define QCOW_VERSION 2
46
47/* Reads 'nbyte' bytes from 'fd' into 'buf', retrying on interrupts.
48 * Exit()s if the read fails for any other reason.
49 */
50static int
51read_or_die(int fd, void *buf, size_t nbyte)
52{
53 int ret = 0;
54 do {
55 ret = read(fd, buf, nbyte);
56 }
57 while(ret < 0 && errno == EINTR);
58
59 if (ret < 0) {
60 derror("read failed: %s", strerror(errno));
61 exit(1);
62 }
63
64 return ret;
65}
66
67/* Wrapper around lseek(), exit()s on error.
68 */
69static off_t
70seek_or_die(int fd, off_t offset, int whence)
71{
72 off_t ret = lseek(fd, offset, whence);
73 if (ret < 0) {
74 derror("seek failed: %s", strerror(errno));
75 exit(1);
76 }
77 return ret;
78}
79
80
81typedef struct SnapshotInfo {
82 char *id_str;
83 char *name;
84
85 uint32_t date_sec;
86 uint32_t date_nsec;
87
88 uint64_t vm_clock_nsec;
89 uint32_t vm_state_size;
90} SnapshotInfo;
91
92static SnapshotInfo*
93snapshot_info_alloc()
94{
95 return android_alloc(sizeof(SnapshotInfo));
96}
97
98static void
99snapshot_info_free( SnapshotInfo* info )
100{
101 AFREE(info->id_str);
102 AFREE(info->name);
103 AFREE(info);
104}
105
106
107/* Reads a snapshot record from a qcow2-formatted file.
108 *
109 * The function assumes the file position of 'fd' points to the beginning of a
110 * QcowSnapshotHeader record. When the call returns, the file position of fd is
111 * at the place where the next QcowSnapshotHeader should start, if there is one.
112 *
113 * C.f. QCowSnapshotHeader in block/qcow2-snapshot.c for the complete layout of
114 * the header.
115 */
116static void
117snapshot_info_read( int fd, SnapshotInfo* info )
118{
119 uint64_t start_offset = seek_or_die(fd, 0, SEEK_CUR);
120
121 uint32_t extra_data_size;
122 uint16_t id_str_size, name_size;
123
124 /* read fixed-length fields */
125 seek_or_die(fd, 12, SEEK_CUR); /* skip l1 info */
126 read_or_die(fd, &id_str_size, sizeof(id_str_size));
127 read_or_die(fd, &name_size, sizeof(name_size));
128 read_or_die(fd, &info->date_sec, sizeof(info->date_sec));
129 read_or_die(fd, &info->date_nsec, sizeof(info->date_nsec));
130 read_or_die(fd, &info->vm_clock_nsec, sizeof(info->vm_clock_nsec));
131 read_or_die(fd, &info->vm_state_size, sizeof(info->vm_state_size));
132 read_or_die(fd, &extra_data_size, sizeof(extra_data_size));
133
134 /* convert to host endianness */
135 be16_to_cpus(&id_str_size);
136 be16_to_cpus(&name_size);
137 be32_to_cpus(&info->date_sec);
138 be32_to_cpus(&info->date_nsec);
139 be64_to_cpus(&info->vm_clock_nsec);
140 be32_to_cpus(&info->vm_state_size);
141 be32_to_cpus(&extra_data_size);
142 be32_to_cpus(&extra_data_size);
143
144 /* read variable-length buffers*/
145 info->id_str = android_alloc(id_str_size + 1); // +1: manual null-termination
146 info->name = android_alloc(name_size + 1);
147 seek_or_die(fd, extra_data_size, SEEK_CUR); /* skip extra data */
148 read_or_die(fd, info->id_str, id_str_size);
149 read_or_die(fd, info->name, name_size);
150
151 info->id_str[id_str_size] = '\0';
152 info->name[name_size] = '\0';
153
154 /* headers are 8 byte aligned, ceil to nearest multiple of 8 */
155 uint64_t end_offset = seek_or_die(fd, 0, SEEK_CUR);
156 uint32_t total_size = end_offset - start_offset;
157 uint32_t aligned_size = ((total_size - 1) / 8 + 1) * 8;
158
159 /* skip to start of next record */
160 seek_or_die(fd, start_offset + aligned_size, SEEK_SET);
161}
162
163
164#define NB_SUFFIXES 4
165
166/* Returns the size of a snapshot in a human-readable format.
167 *
168 * This function copyright (c) 2003 Fabrice Bellard
169 */
170static char*
171snapshot_format_size( char *buf, int buf_size, int64_t size )
172{
173 static const char suffixes[NB_SUFFIXES] = "KMGT";
174 int64_t base;
175 int i;
176
177 if (size <= 999) {
178 snprintf(buf, buf_size, "%" PRId64, size);
179 } else {
180 base = 1024;
181 for(i = 0; i < NB_SUFFIXES; i++) {
182 if (size < (10 * base)) {
183 snprintf(buf, buf_size, "%0.1f%c",
184 (double)size / base,
185 suffixes[i]);
186 break;
187 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
188 snprintf(buf, buf_size, "%" PRId64 "%c",
189 ((size + (base >> 1)) / base),
190 suffixes[i]);
191 break;
192 }
193 base = base * 1024;
194 }
195 }
196 return buf;
197}
198
199static char*
200snapshot_format_create_date( char *buf, size_t buf_size, time_t *time )
201{
202 struct tm *tm;
203 tm = localtime(time);
David 'Digit' Turneref009972014-01-29 19:25:54 +0100204 if (!tm) {
205 snprintf(buf, buf_size, "<invalid-snapshot-date>");
206 } else {
207 strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S", tm);
208 }
Ot ten Thijeae835ac2010-10-18 13:37:37 +0100209 return buf;
210}
211
212static char*
213snapshot_format_vm_clock( char *buf, size_t buf_size, uint64_t vm_clock_nsec )
214{
215 uint64_t secs = vm_clock_nsec / 1000000000;
216 snprintf(buf, buf_size, "%02d:%02d:%02d.%03d",
217 (int)(secs / 3600),
218 (int)((secs / 60) % 60),
219 (int)(secs % 60),
220 (int)((vm_clock_nsec / 1000000) % 1000));
221 return buf;
222}
223
224/* Prints a row of the snapshot table to stdout. */
225static void
226snapshot_info_print( SnapshotInfo *info )
227{
228 char size_buf[8];
229 char date_buf[21];
230 char clock_buf[21];
231
David 'Digit' Turneref009972014-01-29 19:25:54 +0100232 // Note: time_t might be larger than uint32_t.
233 time_t date_sec = info->date_sec;
234
Ot ten Thijeae835ac2010-10-18 13:37:37 +0100235 snapshot_format_size(size_buf, sizeof(size_buf), info->vm_state_size);
David 'Digit' Turneref009972014-01-29 19:25:54 +0100236 snapshot_format_create_date(date_buf, sizeof(date_buf), &date_sec);
Ot ten Thijeae835ac2010-10-18 13:37:37 +0100237 snapshot_format_vm_clock(clock_buf, sizeof(clock_buf), info->vm_clock_nsec);
238
239 printf(" %-10s%-20s%7s%20s%15s\n",
240 info->id_str, info->name, size_buf, date_buf, clock_buf);
241}
242
243/* Prints table of all snapshots recorded in the file 'fd'.
244 */
245static void
246snapshot_print_table( int fd, uint32_t nb_snapshots, uint64_t snapshots_offset )
247{
248 printf(" %-10s%-20s%7s%20s%15s\n",
249 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
250
251 /* skip ahead to snapshot data */
252 seek_or_die(fd, snapshots_offset, SEEK_SET);
253
254 /* iterate over snapshot records */
255 int i;
256 for (i = 0; i < nb_snapshots; i++) {
257 SnapshotInfo *info = snapshot_info_alloc();
258 snapshot_info_read(fd, info);
259 snapshot_info_print(info);
260
261 snapshot_info_free(info);
262 }
263}
264
265/* Validates that 'fd' starts with a correct Qcow 2 header. Prints an error and
266 * exit()s if validation fails.
267 */
268static void
269snapshot_validate_qcow_file( int fd )
270{
271 /* read magic number and qcow version (2x4 bytes) */
272 uint32_t magic, version;
273 read_or_die(fd, &magic, sizeof(magic));
274 read_or_die(fd, &version, sizeof(version));
275 be32_to_cpus(&magic);
276 be32_to_cpus(&version);
277
278 if (magic != QCOW_MAGIC) {
279 derror("Not a valid Qcow snapshot file (expected magic value '%08x', got '%08x').",
280 QCOW_MAGIC, magic);
281 exit(1);
282 }
283 if (version != QCOW_VERSION) {
284 derror("Unsupported Qcow version (need %d, got %d).",
285 QCOW_VERSION, version);
286 exit(1);
287 }
288}
289
290/* Reads snapshot information from a Qcow2 file header.
291 *
292 * C.f. QCowHeader in block/qcow2.h for an exact listing of the header
293 * contents.
294 */
295static void
296snapshot_read_qcow_header( int fd, uint32_t *nb_snapshots, uint64_t *snapshots_offset )
297{
298 snapshot_validate_qcow_file(fd);
299
300 /* skip non-snapshot related metadata (4x8 + 5x4 = 52 bytes)*/
301 seek_or_die(fd, 52, SEEK_CUR);
302
303 read_or_die(fd, nb_snapshots, sizeof(*nb_snapshots));
304 read_or_die(fd, snapshots_offset, sizeof(*snapshots_offset));
305
306 /* convert to host endianness */
307 be32_to_cpus(nb_snapshots);
308 be64_to_cpus(snapshots_offset);
309}
310
311/* Prints a table with information on the snapshots in the qcow2-formatted file
312 * 'snapstorage', then exit()s.
313 */
314void
315snapshot_print_and_exit( const char *snapstorage )
316{
317 /* open snapshot file */
318 int fd = open(snapstorage, O_RDONLY);
David 'Digit' Turnerd80a7862011-05-05 10:24:43 +0200319 if (fd < 0) {
Ot ten Thijeae835ac2010-10-18 13:37:37 +0100320 derror("Could not open snapshot file '%s': %s", snapstorage, strerror(errno));
321 exit(1);
322 }
323
324 /* read snapshot info from file header */
325 uint32_t nb_snapshots;
326 uint64_t snapshots_offset;
327 snapshot_read_qcow_header(fd, &nb_snapshots, &snapshots_offset);
328
329 if (nb_snapshots > 0) {
330 printf("Snapshots in file '%s':\n", snapstorage);
331 snapshot_print_table(fd, nb_snapshots, snapshots_offset);
332 }
333 else {
334 printf("File '%s' contains no snapshots yet.\n", snapstorage);
335 }
336
337 close(fd);
338 exit(0);
339}