1 /* Copyright (c) 2008, XenSource Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of XenSource Inc. nor the names of its contributors
12  *       may be used to endorse or promote products derived from this software
13  *       without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Altering operations:
28  *
29  * 1. Change the parent pointer to another file.
30  * 2. Change the size of the file containing the VHD image. This does NOT
31  * affect the VHD disk capacity, only the physical size of the file containing
32  * the VHD. Naturally, it is not possible to set the file size to be less than
33  * the what VHD utilizes.
34  * The operation doesn't actually change the file size, but it writes the
35  * footer in the right location such that resizing the file (manually, as a
36  * separate step) will produce the correct results. If the new file size is
37  * greater than the current file size, the file must first be expanded and then
38  * altered with this operation. If the new size is smaller than the current
39  * size, the VHD must first be altered with this operation and then the file
40  * must be shrunk. Failing to resize the file will result in a corrupted VHD.
41  */
42 
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 
49 #include "libvhd.h"
50 
51 TEST_FAIL_EXTERN_VARS;
52 
53 int
vhd_util_modify(int argc,char ** argv)54 vhd_util_modify(int argc, char **argv)
55 {
56 	char *name;
57 	vhd_context_t vhd;
58 	int err, c, size, parent, parent_raw;
59 	off_t newsize = 0;
60 	char *newparent = NULL;
61 
62 	name       = NULL;
63 	size       = 0;
64 	parent     = 0;
65 	parent_raw = 0;
66 
67 	optind = 0;
68 	while ((c = getopt(argc, argv, "n:s:p:mh")) != -1) {
69 		switch (c) {
70 		case 'n':
71 			name = optarg;
72 			break;
73 		case 's':
74 			size = 1;
75 			errno = 0;
76 			newsize = strtoll(optarg, NULL, 10);
77 			if (errno) {
78 				fprintf(stderr, "Invalid size '%s'\n", optarg);
79 				goto usage;
80 			}
81 			break;
82 		case 'p':
83 			parent = 1;
84 			newparent = optarg;
85 			break;
86 		case 'm':
87 			parent_raw = 1;
88 			break;
89 
90 		case 'h':
91 		default:
92 			goto usage;
93 		}
94 	}
95 
96 	if (!name || optind != argc)
97 		goto usage;
98 
99 	err = vhd_open(&vhd, name, VHD_OPEN_RDWR);
100 	if (err) {
101 		printf("error opening %s: %d\n", name, err);
102 		return err;
103 	}
104 
105 	if (size) {
106 		err = vhd_set_phys_size(&vhd, newsize);
107 		if (err)
108 			printf("failed to set physical size to %"PRIu64":"
109 			       " %d\n", newsize, err);
110 	}
111 
112 	if (parent) {
113 		TEST_FAIL_AT(FAIL_REPARENT_BEGIN);
114 		err = vhd_change_parent(&vhd, newparent, parent_raw);
115 		if (err) {
116 			printf("failed to set parent to '%s': %d\n",
117 					newparent, err);
118 			goto done;
119 		}
120 		TEST_FAIL_AT(FAIL_REPARENT_END);
121 	}
122 
123 done:
124 	vhd_close(&vhd);
125 	return err;
126 
127 usage:
128 	printf("*** Dangerous operations, use with care ***\n");
129 	printf("options: <-n name> [-p NEW_PARENT set parent [-m raw]] "
130 			"[-s NEW_SIZE set size] [-h help]\n");
131 	return -EINVAL;
132 }
133