removed unneeded tools
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1038 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
parent
ea2384d36e
commit
a3fb0cf907
13
cow.h
13
cow.h
@ -1,13 +0,0 @@
|
|||||||
/* user mode linux compatible COW file */
|
|
||||||
#define COW_MAGIC 0x4f4f4f4d /* MOOO */
|
|
||||||
#define COW_VERSION 2
|
|
||||||
|
|
||||||
struct cow_header_v2 {
|
|
||||||
uint32_t magic;
|
|
||||||
uint32_t version;
|
|
||||||
char backing_file[1024];
|
|
||||||
int32_t mtime;
|
|
||||||
uint64_t size;
|
|
||||||
uint32_t sectorsize;
|
|
||||||
};
|
|
||||||
|
|
146
qemu-mkcow.c
146
qemu-mkcow.c
@ -1,146 +0,0 @@
|
|||||||
/*
|
|
||||||
* create a COW disk image
|
|
||||||
*
|
|
||||||
* Copyright (c) 2003 Fabrice Bellard
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <getopt.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
|
|
||||||
#include "cow.h"
|
|
||||||
|
|
||||||
#include "bswap.h"
|
|
||||||
|
|
||||||
int cow_create(int cow_fd, const char *image_filename,
|
|
||||||
int64_t image_sectors)
|
|
||||||
{
|
|
||||||
struct cow_header_v2 cow_header;
|
|
||||||
int fd;
|
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
memset(&cow_header, 0, sizeof(cow_header));
|
|
||||||
cow_header.magic = htonl(COW_MAGIC);
|
|
||||||
cow_header.version = htonl(COW_VERSION);
|
|
||||||
if (image_filename) {
|
|
||||||
fd = open(image_filename, O_RDONLY);
|
|
||||||
if (fd < 0) {
|
|
||||||
perror(image_filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
image_sectors = lseek64(fd, 0, SEEK_END);
|
|
||||||
if (fstat(fd, &st) != 0) {
|
|
||||||
close(fd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
image_sectors /= 512;
|
|
||||||
cow_header.mtime = htonl(st.st_mtime);
|
|
||||||
realpath(image_filename, cow_header.backing_file);
|
|
||||||
}
|
|
||||||
cow_header.sectorsize = htonl(512);
|
|
||||||
cow_header.size = image_sectors * 512;
|
|
||||||
#ifndef WORDS_BIGENDIAN
|
|
||||||
cow_header.size = bswap64(cow_header.size);
|
|
||||||
#endif
|
|
||||||
write(cow_fd, &cow_header, sizeof(cow_header));
|
|
||||||
/* resize to include at least all the bitmap */
|
|
||||||
ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3));
|
|
||||||
lseek(cow_fd, 0, SEEK_SET);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void help(void)
|
|
||||||
{
|
|
||||||
printf("qemu-mkcow version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
|
|
||||||
"usage: qemu-mkcow [-h] [-f disk_image] cow_image [cow_size]\n"
|
|
||||||
"Create a Copy On Write disk image from an optional raw disk image\n"
|
|
||||||
"\n"
|
|
||||||
"-f disk_image set the raw disk image name\n"
|
|
||||||
"cow_image the created cow_image\n"
|
|
||||||
"cow_size the create cow_image size in MB if no raw disk image is used\n"
|
|
||||||
"\n"
|
|
||||||
"Once the cow_image is created from a raw disk image, you must not modify the original raw disk image\n"
|
|
||||||
);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
const char *image_filename, *cow_filename;
|
|
||||||
int cow_fd, c, nb_args, simple_image;
|
|
||||||
int64_t image_size;
|
|
||||||
|
|
||||||
image_filename = NULL;
|
|
||||||
image_size = 0;
|
|
||||||
simple_image = 0;
|
|
||||||
for(;;) {
|
|
||||||
c = getopt(argc, argv, "hf:s");
|
|
||||||
if (c == -1)
|
|
||||||
break;
|
|
||||||
switch(c) {
|
|
||||||
case 'h':
|
|
||||||
help();
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
image_filename = optarg;
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
simple_image = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!image_filename)
|
|
||||||
nb_args = 2;
|
|
||||||
else
|
|
||||||
nb_args = 1;
|
|
||||||
if (optind + nb_args != argc)
|
|
||||||
help();
|
|
||||||
|
|
||||||
cow_filename = argv[optind];
|
|
||||||
if (nb_args == 2) {
|
|
||||||
image_size = (int64_t)atoi(argv[optind + 1]) * 2 * 1024;
|
|
||||||
}
|
|
||||||
|
|
||||||
cow_fd = open(cow_filename, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
|
|
||||||
if (!cow_fd < 0)
|
|
||||||
return -1;
|
|
||||||
if (simple_image) {
|
|
||||||
ftruncate64(cow_fd, image_size * 512);
|
|
||||||
} else {
|
|
||||||
if (cow_create(cow_fd, image_filename, image_size) < 0) {
|
|
||||||
fprintf(stderr, "%s: error while formating\n", cow_filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(cow_fd);
|
|
||||||
return 0;
|
|
||||||
}
|
|
72
vmdk.h
72
vmdk.h
@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (C) Matthew Chapman 2003
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation; either version 2 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define SECTOR_BITS 9
|
|
||||||
#define SECTOR_SIZE (1 << SECTOR_BITS)
|
|
||||||
#define SECTOR_MASK (SECTOR_SIZE - 1)
|
|
||||||
|
|
||||||
#define L1_BITS (SECTOR_BITS - 3)
|
|
||||||
#define L1_SIZE (1 << L1_BITS)
|
|
||||||
#define L1_MASK (L1_SIZE - 1)
|
|
||||||
|
|
||||||
#define L2_BITS SECTOR_BITS
|
|
||||||
#define L2_SIZE (1 << L2_BITS)
|
|
||||||
#define L2_MASK (L2_SIZE - 1)
|
|
||||||
|
|
||||||
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
|
|
||||||
|
|
||||||
struct cowdisk_header
|
|
||||||
{
|
|
||||||
uint32_t version;
|
|
||||||
uint32_t flags;
|
|
||||||
uint32_t disk_sectors;
|
|
||||||
uint32_t granularity;
|
|
||||||
uint32_t l1dir_offset;
|
|
||||||
uint32_t l1dir_size;
|
|
||||||
uint32_t file_sectors;
|
|
||||||
uint32_t cylinders;
|
|
||||||
uint32_t heads;
|
|
||||||
uint32_t sectors_per_track;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct cowdisk_header2
|
|
||||||
{
|
|
||||||
uint32_t parent_ts;
|
|
||||||
uint32_t timestamp;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* based on vdk 3.1 10-11-2003 by Ken Kato */
|
|
||||||
|
|
||||||
struct vmdisk_header
|
|
||||||
{
|
|
||||||
uint32_t version;
|
|
||||||
uint32_t flags;
|
|
||||||
|
|
||||||
int64_t capacity;
|
|
||||||
int64_t granularity;
|
|
||||||
int64_t desc_offset;
|
|
||||||
int64_t desc_size;
|
|
||||||
int32_t num_gtes_per_gte;
|
|
||||||
int64_t rgd_offset;
|
|
||||||
int64_t gd_offset;
|
|
||||||
int64_t grain_offset;
|
|
||||||
|
|
||||||
char filler[1];
|
|
||||||
|
|
||||||
char check_bytes[4];
|
|
||||||
};
|
|
261
vmdk2raw.c
261
vmdk2raw.c
@ -1,261 +0,0 @@
|
|||||||
/*
|
|
||||||
vmdk2raw: convert vmware images to raw disk images
|
|
||||||
Copyright (C) Net Integration Technologies 2004
|
|
||||||
Copyright (C) Matthew Chapman 2003
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation; either version 2 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <inttypes.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include "vmdk.h"
|
|
||||||
#include "config-host.h"
|
|
||||||
|
|
||||||
static struct cowdisk_header header;
|
|
||||||
static struct vmdisk_header header4;
|
|
||||||
static off64_t disk_limit;
|
|
||||||
static unsigned int granule_size;
|
|
||||||
static uint32_t *l1dir;
|
|
||||||
|
|
||||||
static unsigned int cached_l2dir;
|
|
||||||
static uint32_t l2dir[L2_SIZE];
|
|
||||||
|
|
||||||
static struct vmdk_prm {
|
|
||||||
uint32_t grain_table_size;
|
|
||||||
uint32_t sectors_per_grain;
|
|
||||||
uint32_t sectors_per_table;
|
|
||||||
uint32_t directory_size;
|
|
||||||
} vdsk;
|
|
||||||
|
|
||||||
static size_t read_physical(int fd, off64_t offset, size_t length, void *buffer)
|
|
||||||
{
|
|
||||||
size_t n;
|
|
||||||
|
|
||||||
if (lseek64(fd, offset, SEEK_SET) == -1) {
|
|
||||||
printf(" error trying to seek lseek to %lld", offset);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
n = read(fd, buffer, length);
|
|
||||||
|
|
||||||
if (n == -1) {
|
|
||||||
printf("read from disk %lld", offset);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int read_l1dir(int fd, size_t offset, int num)
|
|
||||||
{
|
|
||||||
l1dir = malloc(sizeof(*l1dir) * num);
|
|
||||||
if (!l1dir)
|
|
||||||
return -1;
|
|
||||||
return read_physical(fd, offset << SECTOR_BITS, sizeof(*l1dir) * num, (char *)l1dir) != (sizeof(*l1dir) * num);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int read_l2dir(int fd, size_t offset, int num)
|
|
||||||
{
|
|
||||||
return read_physical(fd, offset << SECTOR_BITS, sizeof(l2dir[0]) * num, (char *)l2dir) != sizeof(l2dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t copy_virtual(struct vmdk_prm *dsk, int in_fd, int out_fd, off64_t offset, void *buffer, size_t length)
|
|
||||||
{
|
|
||||||
|
|
||||||
unsigned int granule_offset;
|
|
||||||
unsigned int grain_index;
|
|
||||||
unsigned int sector_map_idx;
|
|
||||||
|
|
||||||
granule_offset = offset % granule_size;
|
|
||||||
length = MIN(length, granule_size - granule_offset);
|
|
||||||
length = MIN(length, disk_limit - offset);
|
|
||||||
|
|
||||||
sector_map_idx = (offset >> SECTOR_BITS) / dsk->sectors_per_table;
|
|
||||||
|
|
||||||
if (sector_map_idx >= dsk->directory_size) {
|
|
||||||
fprintf(stderr, "cannot locate grain table for %d in %d\n", sector_map_idx, dsk->directory_size);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (l1dir[sector_map_idx] == 0)
|
|
||||||
goto zero_fill;
|
|
||||||
|
|
||||||
if (sector_map_idx != cached_l2dir) {
|
|
||||||
if (read_l2dir(in_fd, l1dir[sector_map_idx], dsk->grain_table_size)) {
|
|
||||||
fprintf(stderr, "read failed\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
cached_l2dir = sector_map_idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
grain_index = ((offset >> SECTOR_BITS) % dsk->sectors_per_table) / dsk->sectors_per_grain;
|
|
||||||
|
|
||||||
if (grain_index >= dsk->grain_table_size) {
|
|
||||||
fprintf(stderr, "grain to large");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (l2dir[grain_index] == 0)
|
|
||||||
goto zero_fill;
|
|
||||||
|
|
||||||
if (read_physical(in_fd, (l2dir[grain_index] << SECTOR_BITS) + granule_offset, length, buffer) != length) {
|
|
||||||
fprintf(stderr, "read error 2\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
write(out_fd, buffer, length);
|
|
||||||
return length;
|
|
||||||
|
|
||||||
zero_fill:
|
|
||||||
/* the last chunk of the file can not be sparse
|
|
||||||
* or the file will be truncated */
|
|
||||||
if (offset + length >= disk_limit) {
|
|
||||||
if (lseek64(out_fd, length-1, SEEK_CUR) == (off_t)-1)
|
|
||||||
perror("lseek");
|
|
||||||
/* write the last NULL byte instead of seeking */
|
|
||||||
const char nil = 0;
|
|
||||||
write(out_fd, &nil, 1);
|
|
||||||
} else {
|
|
||||||
if (lseek64(out_fd, length, SEEK_CUR) == (off_t)-1)
|
|
||||||
perror("lseek");
|
|
||||||
}
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int open_vmdk4(int fd)
|
|
||||||
{
|
|
||||||
if (read(fd, &header4, sizeof(header4)) != sizeof(header4)) {
|
|
||||||
perror("read from disk");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
granule_size = header4.granularity << SECTOR_BITS;
|
|
||||||
disk_limit = header4.capacity << SECTOR_BITS;
|
|
||||||
|
|
||||||
cached_l2dir = -1;
|
|
||||||
vdsk.grain_table_size = header4.num_gtes_per_gte;
|
|
||||||
vdsk.sectors_per_grain = header4.granularity;
|
|
||||||
vdsk.sectors_per_table = vdsk.grain_table_size * vdsk.sectors_per_grain;
|
|
||||||
vdsk.directory_size = (header4.capacity + vdsk.sectors_per_table - 1) / vdsk.sectors_per_table + 1;
|
|
||||||
|
|
||||||
if (read_l1dir(fd, header4.rgd_offset, vdsk.directory_size))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int open_vmdk3(int fd)
|
|
||||||
{
|
|
||||||
if (read(fd, &header, sizeof(header)) != sizeof(header)) {
|
|
||||||
perror("read from disk\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
granule_size = header.granularity << SECTOR_BITS;
|
|
||||||
vdsk.sectors_per_grain = header.granularity;
|
|
||||||
vdsk.grain_table_size = L2_SIZE;
|
|
||||||
vdsk.sectors_per_table = vdsk.grain_table_size * vdsk.sectors_per_grain;
|
|
||||||
vdsk.directory_size = L1_SIZE;
|
|
||||||
if (read_l1dir(fd, header.l1dir_offset, L1_SIZE))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
disk_limit = header.disk_sectors << SECTOR_BITS;
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int open_vmdk(const char *filename)
|
|
||||||
{
|
|
||||||
int fd = open(filename, O_RDONLY | O_LARGEFILE);
|
|
||||||
if (fd == -1) {
|
|
||||||
perror(filename);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char magic[4];
|
|
||||||
if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
|
|
||||||
perror("read from disk");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!memcmp(magic, "KDMV", sizeof(magic))) {
|
|
||||||
open_vmdk4(fd);
|
|
||||||
} else if (!memcmp(magic, "COWD", sizeof(magic))) {
|
|
||||||
open_vmdk3(fd);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "%s is not a VMware virtual disk image\n", filename);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
cached_l2dir = -1;
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void help(void)
|
|
||||||
{
|
|
||||||
printf("vmdk2raw\n"
|
|
||||||
"usage: vmdk2raw vmware_image output_image\n"
|
|
||||||
"\n"
|
|
||||||
"vmware_image a vmware cow image\n"
|
|
||||||
"output_image the created disk image\n"
|
|
||||||
);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define BUF_SIZE 0x10000
|
|
||||||
static void copy_disk(in_fd, out_fd)
|
|
||||||
{
|
|
||||||
char buf[BUF_SIZE];
|
|
||||||
off64_t i = 0;
|
|
||||||
int ret;
|
|
||||||
while (i < disk_limit) {
|
|
||||||
ret = copy_virtual(&vdsk, in_fd, out_fd, i, buf, sizeof(buf));
|
|
||||||
if (ret < 0) {
|
|
||||||
fprintf(stderr, "copying failed\n");
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
i += ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
int out_fd, in_fd;
|
|
||||||
|
|
||||||
if (argc < 3)
|
|
||||||
help();
|
|
||||||
|
|
||||||
in_fd = open_vmdk(argv[1]);
|
|
||||||
if (in_fd < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
out_fd = open(argv[2], O_WRONLY | O_LARGEFILE | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
|
||||||
if (out_fd < 0) {
|
|
||||||
perror(argv[2]);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
copy_disk(in_fd, out_fd);
|
|
||||||
close(out_fd);
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user