├── .gitignore ├── CHANGELOG ├── CMakeLists.txt ├── INSTALL ├── LICENSE ├── README.md ├── src ├── disk.cpp ├── disk.hpp ├── main.cpp ├── package.cpp ├── package.hpp ├── progress.cpp ├── progress.hpp ├── readfile.cpp ├── readfile.hpp ├── sha1.cpp ├── sha1.hpp ├── writefile.cpp ├── writefile.hpp ├── xxh64.cpp └── xxh64.hpp └── xva-img.1 /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeCache.txt 2 | CMakeFiles/ 3 | Makefile 4 | cmake_install.cmake 5 | xva-img 6 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | v1.3 2 | 3 | - 2013-07-28 Merged with sourceforge project: Renamed to xva-img 4 | 5 | v1.2 6 | 7 | - 2013-07-28 Imported to github, fixed some compiler errors/warnings 8 | 9 | v1.1 10 | 11 | - 2009-11-19 Fixed some bugs! (non-criticial) 12 | 13 | v1.0 14 | 15 | - 2009-11-29 First Public Release 16 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 3.0) 2 | 3 | LINK_DIRECTORIES(/usr/local/lib) 4 | INCLUDE_DIRECTORIES(SYSTEM 5 | /usr/local/include 6 | ) 7 | 8 | SET(CMAKE_CXX_FLAGS_RELEASE "-Wall -Wextra -Wvla") 9 | SET(CMAKE_BUILD_TYPE Release) 10 | 11 | PROJECT("xva-img") 12 | FIND_PACKAGE(OpenSSL REQUIRED) 13 | SET(TARGET_NAME "xva-img") 14 | FILE(GLOB_RECURSE SOURCE_FILES src/*.cpp) 15 | ADD_EXECUTABLE(${TARGET_NAME} ${SOURCE_FILES}) 16 | TARGET_LINK_LIBRARIES(${TARGET_NAME} 17 | crypto 18 | xxhash 19 | ) 20 | INSTALL(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/xva-img 21 | DESTINATION bin) 22 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | Run "cmake ." followed by "make install". xva-img will be installed in /usr/local/bin/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | In addition, as a special exception, the copyright holders give 342 | permission to link the code of portions of this program with the 343 | OpenSSL library under certain conditions as described in each 344 | individual source file, and distribute linked combinations 345 | including the two. 346 | You must obey the GNU General Public License in all respects 347 | for all of the code used other than OpenSSL. If you modify 348 | file(s) with this exception, you may extend this exception to your 349 | version of the file(s), but you are not obligated to do so. If you 350 | do not wish to do so, delete this exception statement from your 351 | version. If you delete this exception statement from all source 352 | files in the program, then also delete it here. 353 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | xva-img 2 | ======= 3 | 4 | Citrix Xen uses a custom virtual appliance format for import/export called "XVA". it's basically a strangely crafted tar-file. You don't need this program to unpack this tar-file, just use your favourite tar unpacker (tar, gtar, bsdtar). Once unpacked you will end up with a lot of different files, ova.xml (which contains the settings for the virtual appliance, think VMware vmx) and a number of folders called Ref:/, this is your disks. Each of these folders contain hundreds of files named 00000000, 00000001 with a accompanying .CHECKSUM file (SHA1). Each file is a 1MB slice of the disk, but some of the files in the sequence will probably be missing this is because XVA do not use compression; instead it will exclude slices of the disk that only contains zeros (are empty). This tool can assemble the disk for you (you will end up with a RAW disk) that can easily be mounted and modified. It can then also split the file again and generate checksum. Once ready, you will probably want to use the "package" command to rebuild the XVA file. 5 | 6 | Requirements 7 | ============ 8 | 9 | * CMake 10 | * g++ or clang++ 11 | * libssl-dev 12 | * libxxhash-dev 13 | 14 | To build 15 | ======= 16 | Install g++ cmake make libssl-dev 17 | 18 | cmake . 19 | make 20 | make install 21 | 22 | Example 23 | ======= 24 | Extract the XVA file. 25 | 26 | mkdir my-virtual-machine 27 | tar -xf my-virtual-machine.xva -C my-virtual-machine 28 | chmod -R 755 my-virtual-machine 29 | 30 | Extract one of the disks 31 | 32 | xva-img -p disk-export my-virtual-machine/Ref\:1/ disk.raw 33 | 34 | Splice the disk file (remove all files in the folder, required not to make any unnecessary mistakes) 35 | 36 | rm -rf my-virtual-machine/Ref\:1/* 37 | xva-img -p disk-import my-virtual-machine/Ref\:1/ disk.raw 38 | 39 | Rebuild the XVA file 40 | 41 | xva-img -p package my-virtual-machine-fixed.xva my-virtual-machine/ova.xml my-virtual-machine/Ref\:1/ 42 | -------------------------------------------------------------------------------- /src/disk.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | #define _LARGEFILE_SOURCE 33 | #define _LARGEFILE64_SOURCE 34 | #define _FILE_OFFSET_BITS 64 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #include "readfile.hpp" 45 | #include "writefile.hpp" 46 | #include "sha1.hpp" 47 | #include "xxh64.hpp" 48 | #include "progress.hpp" 49 | 50 | #include "disk.hpp" 51 | 52 | using XVA::Disk; 53 | 54 | Disk::Disk(const std::string& path) 55 | : m_path(path) 56 | , m_verbose(false) 57 | , m_sparse(false) 58 | , m_ignore_checksum(false) 59 | { 60 | } 61 | 62 | bool Disk::Export(const std::string& diskpath) 63 | noexcept(false) 64 | { 65 | std::set parts; 66 | 67 | DIR* dp = opendir(m_path.c_str()); 68 | if (!dp) 69 | throw std::runtime_error("unable to open " + m_path); 70 | 71 | struct dirent* de; 72 | while((de=readdir(dp)) != NULL) 73 | { 74 | unsigned int part; 75 | int parsed = -1; 76 | if (strlen(de->d_name) == 8 && sscanf(de->d_name, "%u%n", &part, &parsed) && parsed == 8) 77 | { 78 | parts.insert(part); 79 | } 80 | } 81 | closedir(dp); 82 | 83 | if (parts.empty()) 84 | throw std::runtime_error("directory " + m_path + " is empty"); 85 | 86 | FILE* fp = fopen(diskpath.c_str(), "wb"); 87 | if (!fp) 88 | throw std::runtime_error("unable to open " + diskpath); 89 | 90 | Progress progress("Exporting"); 91 | if (m_verbose) 92 | progress.Start(); 93 | 94 | unsigned int const last = *parts.rbegin(); 95 | for(unsigned int i = 0; i <= last; i++) 96 | { 97 | if (m_verbose) 98 | progress.Update(((float)i / last)*100); 99 | 100 | if (parts.find(i) != parts.end()) 101 | { 102 | char path2[9]; 103 | snprintf(path2, sizeof path2, "%08d", i); 104 | 105 | std::string chunk; 106 | if (!XVA::ReadFile((m_path + "/" + path2), chunk)) 107 | { 108 | fclose(fp); 109 | throw std::runtime_error("unable to open chunk " + 110 | std::string(path2)); 111 | } 112 | 113 | std::string checksum; 114 | if (!XVA::ReadFile( 115 | (m_path + "/" + path2 + ".checksum"), 116 | checksum) 117 | ) 118 | { 119 | if (!XVA::ReadFile( 120 | (m_path + "/" + path2 + ".xxhash"), 121 | checksum) 122 | ) 123 | { 124 | fclose(fp); 125 | throw std::runtime_error("unable to read " + 126 | std::string(path2) + ".checksum and " + 127 | std::string(path2) + ".xxhash"); 128 | } else if (checksum != XVA::XXH64(chunk)) 129 | { 130 | if(!m_ignore_checksum) 131 | { 132 | fclose(fp); 133 | throw std::runtime_error("invalid xxh64 checksum for " + 134 | std::string(path2)); 135 | } 136 | std::cerr << "invalid xxh64 checksum for " << std::string(path2) << std::endl; 137 | } 138 | } else if (checksum != XVA::SHA1(chunk)) 139 | { 140 | if(!m_ignore_checksum) 141 | { 142 | fclose(fp); 143 | throw std::runtime_error("invalid sha1 checksum for " + 144 | std::string(path2)); 145 | } 146 | std::cerr << "invalid sha1 checksum for " << std::string(path2) << std::endl; 147 | } 148 | 149 | if (fwrite(chunk.c_str(), 1, chunk.size(), fp) != chunk.size()) 150 | { 151 | fclose(fp); 152 | throw std::runtime_error("cannot write " + 153 | std::string(path2) + " to output file"); 154 | } 155 | } else { 156 | if (m_sparse) 157 | { 158 | // we skip empty parts, so that the file system may create a "sparse file" 159 | if (fseek(fp, 1048576, SEEK_CUR)) 160 | { 161 | fclose(fp); 162 | throw std::runtime_error("cannot add sparse chunk to " + 163 | diskpath); 164 | } 165 | } else { 166 | char bufnull[1048576] = { 0 }; 167 | if (fwrite(bufnull, 1, sizeof bufnull, fp) != sizeof bufnull) 168 | { 169 | fclose(fp); 170 | throw std::runtime_error("cannot add empty chunk to " + 171 | diskpath); 172 | } 173 | } 174 | } 175 | } 176 | 177 | if (m_verbose) 178 | progress.Finish(); 179 | 180 | fclose(fp); 181 | return true; 182 | } 183 | 184 | bool Disk::Import(const std::string& diskpath) 185 | noexcept(false) 186 | { 187 | DIR* dp = opendir(m_path.c_str()); 188 | if (!dp) 189 | throw std::runtime_error("unable to open " + m_path); 190 | 191 | size_t files = 0; 192 | struct dirent* de; 193 | while((de=readdir(dp)) != NULL) 194 | { 195 | if (strcmp(de->d_name, ".") == 0) continue; 196 | if (strcmp(de->d_name, "..") == 0) continue; 197 | files++; 198 | } 199 | closedir(dp); 200 | 201 | if (files != 0) 202 | throw std::runtime_error("directory " + m_path + " is not empty"); 203 | 204 | FILE* fp = fopen(diskpath.c_str(), "rb"); 205 | if (!fp) 206 | throw std::runtime_error("unable to open " + diskpath); 207 | 208 | char buf[1048576]; 209 | fseek(fp, 0, SEEK_END); 210 | unsigned long long size = ftello(fp); 211 | fseek(fp, 0, SEEK_SET); 212 | 213 | Progress progress("Importing"); 214 | if (m_verbose) 215 | progress.Start(); 216 | 217 | char bufnull[1048576] = { 0 }; 218 | for (size_t i = 0; ; i++) 219 | { 220 | if (m_verbose) 221 | progress.Update(((float)ftello(fp) / size)*100); 222 | 223 | fread(buf, 1, sizeof buf, fp); 224 | if (feof(fp)) break; 225 | 226 | /* skip empty chunks */ 227 | if ((i > 0 && ftello(fp) < (off_t)size) 228 | && 229 | memcmp(buf, bufnull, sizeof bufnull) == 0) 230 | { 231 | continue; 232 | } 233 | 234 | char path2[9]; 235 | snprintf(path2, sizeof path2, "%08zu", i); 236 | 237 | std::string output; 238 | output.append(buf, 1048576); 239 | 240 | if (!XVA::WriteFile(m_path + "/" + path2, output)) 241 | { 242 | fclose(fp); 243 | throw std::runtime_error("cannot write chunk " + 244 | std::string(path2)); 245 | } 246 | 247 | if (!XVA::WriteFile(m_path + "/" + path2 + ".checksum", 248 | XVA::SHA1(output) 249 | )) 250 | { 251 | fclose(fp); 252 | throw std::runtime_error("cannot write checksum for " + 253 | std::string(path2)); 254 | } 255 | } 256 | 257 | if (m_verbose) 258 | progress.Finish(); 259 | 260 | fclose(fp); 261 | return true; 262 | } 263 | 264 | void Disk::Verbose() 265 | { 266 | m_verbose = true; 267 | } 268 | 269 | void Disk::Sparse() 270 | { 271 | m_sparse = true; 272 | } 273 | 274 | void Disk::IgnoreChecksum() 275 | { 276 | m_ignore_checksum = true; 277 | } 278 | -------------------------------------------------------------------------------- /src/disk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #ifndef _XVA_DISK_HPP_ 34 | #define _XVA_DISK_HPP_ 35 | 36 | #include 37 | #include 38 | 39 | namespace XVA { 40 | class Disk 41 | { 42 | public: 43 | Disk(const std::string& path); 44 | bool Export(const std::string& diskpath) 45 | noexcept(false); 46 | bool Import(const std::string& diskpath) 47 | noexcept(false); 48 | 49 | void Verbose(); 50 | void Sparse(); 51 | void IgnoreChecksum(); 52 | private: 53 | std::string m_path; 54 | bool m_verbose; 55 | bool m_sparse; 56 | bool m_ignore_checksum; 57 | }; 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | Sparse file support on export added by Jens-U. Mozdzen 5 | 6 | This program is free software; you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation; either version 2 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program; if not, write to the Free Software 18 | Foundation, Inc., 59 Temple Place - Suite 330, 19 | Boston, MA 02111-1307, USA. 20 | 21 | In addition, as a special exception, the copyright holders give 22 | permission to link the code of portions of this program with the 23 | OpenSSL library under certain conditions as described in each 24 | individual source file, and distribute linked combinations 25 | including the two. 26 | You must obey the GNU General Public License in all respects 27 | for all of the code used other than OpenSSL. If you modify 28 | file(s) with this exception, you may extend this exception to your 29 | version of the file(s), but you are not obligated to do so. If you 30 | do not wish to do so, delete this exception statement from your 31 | version. If you delete this exception statement from all source 32 | files in the program, then also delete it here. 33 | */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #include "package.hpp" 45 | #include "disk.hpp" 46 | 47 | #define _VERSION_ "1.5.0" 48 | 49 | std::string program_name = ""; 50 | 51 | void usage(FILE* fp, int status) 52 | { 53 | fprintf(fp, 54 | "xva-img " _VERSION_ " build on " __DATE__ 55 | " (c) Erik Lax \n" 56 | "Usage: %s [OPTION...] COMMAND ...\n" 57 | "\n" 58 | " Commands\n" 59 | "\n" 60 | " package [ ,... ]\n" 61 | " disk-export \n" 62 | " disk-import \n" 63 | "\n" 64 | " Options\n" 65 | "\n" 66 | " -p, --progress Show progress\n" 67 | " -s, --sparse Create sparse files on export\n" 68 | " -i, --ignore-checksum Only warn on failed checksum\n" 69 | " -h, --help Show this help\n" 70 | " -v, --version Show version\n" 71 | "\n", 72 | program_name.c_str() 73 | ); 74 | exit(status); 75 | } 76 | 77 | int main(int argc, char* argv[]) 78 | { 79 | program_name = std::string(argv[0]); 80 | 81 | if (argc < 2) 82 | usage(stderr, 2); 83 | 84 | bool verbose = false; 85 | bool sparse = false; 86 | bool ignore_checksum = false; 87 | static struct option longopts[] = { 88 | { "help", no_argument, 0x0, 'h' }, 89 | { "version", no_argument, 0x0, 'v' }, 90 | { "progress", no_argument, 0x0, 'p' }, 91 | { "sparse", no_argument, 0x0, 's' }, 92 | { "ignore-checksum", no_argument, 0x0, 'i' }, 93 | { 0x0, 0, 0x0, 0 } 94 | }; 95 | 96 | opterr = 0; 97 | optind = 0; 98 | int ch; 99 | while ((ch = getopt_long(argc, argv, "hvpis", longopts, 0x0)) != -1) { 100 | switch(ch) 101 | { 102 | case 'p': 103 | verbose = true; 104 | break; 105 | case 's': 106 | sparse = true; 107 | break; 108 | case 'i': 109 | ignore_checksum = true; 110 | break; 111 | case 'h': 112 | usage(stdout, 0); 113 | break; 114 | case 'v': 115 | printf("%s\n", _VERSION_); 116 | exit(0); 117 | break; 118 | case 0: break; 119 | default: 120 | usage(stderr, 2); 121 | break; 122 | } 123 | } 124 | 125 | argc -= optind; 126 | argv += optind; 127 | 128 | if (argc == 0) 129 | usage(stderr, 2); 130 | 131 | try { 132 | if (strcmp(*argv, "package") == 0) 133 | { 134 | argv++; 135 | argc--; 136 | char* outfile = *argv; 137 | 138 | argv++; 139 | argc--; 140 | 141 | XVA::XVAPackage package; 142 | if (verbose) 143 | package.Verbose(); 144 | while(argc > 0) 145 | { 146 | struct stat stt; 147 | if (stat(*argv, &stt) != 0) { 148 | throw std::runtime_error("cannot stat " + 149 | std::string(*argv)); 150 | } 151 | 152 | if (stt.st_mode & S_IFREG) 153 | package.AddFile(*argv); 154 | else if (stt.st_mode & S_IFDIR) 155 | package.AddDir(*argv); 156 | else 157 | throw std::runtime_error("unknown file type " + 158 | std::string(*argv)); 159 | 160 | argv++; 161 | argc--; 162 | } 163 | package.Write(outfile); 164 | return 0; 165 | } 166 | 167 | if (strcmp(*argv, "disk-export") == 0) 168 | { 169 | argv++; 170 | argc--; 171 | 172 | if (argc != 2) 173 | usage(stderr, 2); 174 | 175 | XVA::Disk disk(*argv); 176 | if (verbose) 177 | disk.Verbose(); 178 | if (sparse) 179 | disk.Sparse(); 180 | if (ignore_checksum) 181 | disk.IgnoreChecksum(); 182 | argv++; 183 | argc--; 184 | disk.Export(*argv); 185 | return 0; 186 | } 187 | 188 | if (strcmp(*argv, "disk-import") == 0) 189 | { 190 | argv++; 191 | argc--; 192 | 193 | if (argc != 2) 194 | usage(stderr, 2); 195 | 196 | XVA::Disk disk(*argv); 197 | if (verbose) 198 | disk.Verbose(); 199 | argv++; 200 | argc--; 201 | disk.Import(*argv); 202 | return 0; 203 | } 204 | 205 | usage(stderr, 2); 206 | } catch (std::runtime_error& e) { 207 | fprintf(stderr, "%s: %s\n", program_name.c_str(), e.what()); 208 | exit(1); 209 | } 210 | 211 | return 0; 212 | } 213 | -------------------------------------------------------------------------------- /src/package.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | #define _LARGEFILE_SOURCE 33 | #define _LARGEFILE64_SOURCE 34 | #define _FILE_OFFSET_BITS 64 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #include "writefile.hpp" 43 | #include "readfile.hpp" 44 | #include "progress.hpp" 45 | 46 | #include "package.hpp" 47 | 48 | using XVA::XVAPackage; 49 | 50 | XVAPackage::XVAPackage() 51 | : m_verbose(false) 52 | { 53 | } 54 | 55 | /* 56 | * Add a file to the list 57 | */ 58 | void XVAPackage::AddFile(const std::string& file) 59 | { 60 | m_files.push_back(file); 61 | } 62 | 63 | /* 64 | * Add all files in a directory to the file list 65 | */ 66 | void XVAPackage::AddDir(const std::string& path) 67 | noexcept(false) 68 | { 69 | DIR* dp = opendir(path.c_str()); 70 | if (!dp) 71 | throw std::runtime_error("unable to open " + path); 72 | 73 | std::set list; 74 | 75 | bool slash = true; 76 | if (path.substr(path.size()-1) == "/") 77 | slash = false; 78 | 79 | struct dirent* de; 80 | while((de=readdir(dp)) != NULL) 81 | { 82 | if (strcmp(de->d_name, ".") == 0) continue; 83 | if (strcmp(de->d_name, "..") == 0) continue; 84 | list.insert(path + (slash?"/":"") + de->d_name); 85 | } 86 | closedir(dp); 87 | 88 | for(std::set::const_iterator i = list.begin(); 89 | i != list.end(); ++i) 90 | { 91 | m_files.push_back(*i); 92 | } 93 | } 94 | 95 | /* 96 | * Write the XVA Package to file 97 | */ 98 | bool XVAPackage::Write(const std::string& file) 99 | noexcept(false) 100 | { 101 | FILE* fp = fopen(file.c_str(), "wb"); 102 | if (!fp) 103 | throw std::runtime_error("unable to open " + file); 104 | 105 | Progress progress("Packing"); 106 | if (m_verbose) 107 | progress.Start(); 108 | 109 | size_t x = 0; 110 | for(std::list::const_iterator i = m_files.begin(); 111 | i != m_files.end(); ++i, ++x) 112 | { 113 | if (m_verbose) 114 | progress.Update(((float)x / m_files.size())*100); 115 | 116 | std::string data; 117 | if (!XVA::ReadFile(*i, data)) 118 | { 119 | fclose(fp); 120 | throw std::runtime_error("unable to open file " + 121 | *i); 122 | } 123 | 124 | /* 125 | * we should remove all paths up to ova.xml and Ref:4/ for disks 126 | * this code is subject to be changed, when new types of files 127 | * are added to the xva file! 128 | */ 129 | const char* file = strrchr(i->c_str(), '/'); 130 | if (file != NULL) 131 | { 132 | file++; 133 | if (strcmp(file, "ova.xml") == 0) 134 | { /* ova.xml file .. */ } else { 135 | 136 | int len = file - i->c_str(); 137 | 138 | char* str = new char[len]; 139 | strncpy(str, i->c_str(), len - 1); 140 | str[len - 1] = '\0'; 141 | 142 | if (strrchr(str, '/') != NULL) 143 | { 144 | int len = strrchr(str, '/') - str; 145 | file = i->c_str() + len + 1; 146 | } 147 | delete [] str; 148 | } 149 | } 150 | 151 | /* 152 | * build a tar-header 153 | */ 154 | char header[512] = { 0 }; 155 | char* ptr = header; 156 | snprintf(ptr, 100, "%s", file); 157 | ptr += 100; 158 | snprintf(ptr, 8, "%07d", 0); 159 | ptr += 8; 160 | snprintf(ptr, 8, "%07d", 0); 161 | ptr += 8; 162 | snprintf(ptr, 8, "%07d", 0); 163 | ptr += 8; 164 | snprintf(ptr, 12, "%011lo", static_cast(data.size())); 165 | ptr += 12; 166 | snprintf(ptr, 12, "%011d", 0); 167 | ptr += 12; 168 | 169 | memset(ptr, ' ', 8); 170 | unsigned int checksum = 0; 171 | for(int x=0; x < 512; x++) 172 | checksum += header[x]; 173 | snprintf(ptr, 8, "%07o", checksum); 174 | ptr += 8; 175 | 176 | if (fwrite(header, 1, 512, fp) != 512) 177 | { 178 | fclose(fp); 179 | throw std::runtime_error("unable to write header for " + 180 | *i); 181 | } 182 | 183 | if (fwrite(data.c_str(), 1, data.size(), fp) != data.size()) 184 | { 185 | fclose(fp); 186 | throw std::runtime_error("unable to write data for " + 187 | *i); 188 | } 189 | 190 | size_t pad = 512-(data.size()%512); 191 | if (pad < 512) 192 | { 193 | char null[512] = { 0 }; 194 | if (fwrite(null, 1, pad, fp) != pad) 195 | { 196 | fclose(fp); 197 | throw std::runtime_error("unable to write pad for " + 198 | *i); 199 | } 200 | } 201 | } 202 | 203 | char null[1024] = { 0 }; 204 | if (fwrite(null, 1, sizeof null, fp) != sizeof null) 205 | { 206 | fclose(fp); 207 | throw std::runtime_error("unable to footer"); 208 | } 209 | 210 | if (m_verbose) 211 | progress.Finish(); 212 | 213 | fclose(fp); 214 | return true; 215 | } 216 | 217 | void XVAPackage::Verbose() 218 | { 219 | m_verbose = true; 220 | } 221 | -------------------------------------------------------------------------------- /src/package.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #ifndef _XVA_PACKAGE_HPP_ 34 | #define _XVA_PACKAGE_HPP_ 35 | 36 | #include 37 | #include 38 | #include 39 | 40 | namespace XVA 41 | { 42 | class XVAPackage 43 | { 44 | public: 45 | XVAPackage(); 46 | void AddFile(const std::string& file); 47 | void AddDir(const std::string& path) 48 | noexcept(false); 49 | 50 | bool Write(const std::string& file) 51 | noexcept(false); 52 | 53 | void Verbose(); 54 | private: 55 | std::list m_files; 56 | bool m_verbose; 57 | }; 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/progress.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #include 34 | #include "progress.hpp" 35 | 36 | using XVA::Progress; 37 | 38 | Progress::Progress(const std::string& action) 39 | : m_active(false) 40 | , m_error(false) 41 | , m_curval(0) 42 | , m_action(action) 43 | { 44 | } 45 | 46 | Progress::~Progress() 47 | { 48 | /* somehow Finish() wasn't called */ 49 | if (m_active) 50 | { 51 | m_error = true; 52 | Draw(); 53 | } 54 | } 55 | 56 | void Progress::Start() 57 | { 58 | m_active = true; 59 | m_curval = 0; 60 | m_error = false; 61 | 62 | Draw(); 63 | } 64 | 65 | void Progress::Update(float val) 66 | { 67 | m_curval = val; 68 | Draw(); 69 | } 70 | 71 | void Progress::Draw() 72 | { 73 | if (!m_active) return; 74 | 75 | size_t cols = 80; 76 | printf("\r"); 77 | 78 | printf("%s: |", m_action.c_str()); 79 | cols -= m_action.size() + 3; /* [Extracting: |] */ 80 | cols -= 7; /* [/ 100% ] */ 81 | 82 | size_t val = (int)(m_curval * cols/100); 83 | size_t i = 0; 84 | 85 | for(; i < cols; i++) 86 | { 87 | if (i < val) 88 | printf("="); 89 | else 90 | printf(" "); 91 | } 92 | 93 | char spinner[] = { 94 | '|', 95 | '/', 96 | '-', 97 | '\\', 98 | }; 99 | 100 | if (m_error) 101 | { 102 | printf("%c ERROR\n", spinner[(int)m_curval%4]); 103 | m_active = false; 104 | } else { 105 | printf("%c %3.0lf%%", spinner[(int)m_curval%4], m_curval); 106 | 107 | if (val == cols) 108 | { 109 | m_active = false; 110 | printf("\n"); 111 | } 112 | } 113 | 114 | fflush(stdout); 115 | } 116 | 117 | void Progress::Finish() 118 | { 119 | m_curval = 100; 120 | Draw(); 121 | } 122 | -------------------------------------------------------------------------------- /src/progress.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #ifndef _XVA_PROGRESS_HPP_ 34 | #define _XVA_PROGRESS_HPP_ 35 | 36 | #include 37 | 38 | namespace XVA 39 | { 40 | class Progress 41 | { 42 | public: 43 | Progress(const std::string& action = ""); 44 | ~Progress(); 45 | 46 | void Start(); 47 | void Update(float val); 48 | void Finish(); 49 | private: 50 | void Draw(); 51 | 52 | bool m_active; 53 | bool m_error; 54 | float m_curval; 55 | std::string m_action; 56 | }; 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/readfile.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | #define _LARGEFILE_SOURCE 33 | #define _LARGEFILE64_SOURCE 34 | #define _FILE_OFFSET_BITS 64 35 | 36 | #include 37 | #include "readfile.hpp" 38 | 39 | bool XVA::ReadFile(const std::string& path, std::string& result) 40 | { 41 | FILE* fp = fopen(path.c_str(), "rb"); 42 | 43 | if (!fp) 44 | return false; 45 | 46 | char buf[1024]; 47 | size_t r; 48 | while((r = fread(buf, 1, sizeof buf, fp)) > 0) 49 | { 50 | result.append(buf, r); 51 | } 52 | fclose(fp); 53 | 54 | return true; 55 | } 56 | -------------------------------------------------------------------------------- /src/readfile.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #ifndef _XVA_READFILE_HPP_ 34 | #define _XVA_READFILE_HPP_ 35 | 36 | #include 37 | 38 | namespace XVA 39 | { 40 | bool ReadFile(const std::string& path, std::string& result); 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/sha1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #include 34 | #include 35 | #include "sha1.hpp" 36 | 37 | std::string XVA::SHA1(const std::string& input) 38 | { 39 | SHA_CTX ctx; 40 | SHA1_Init(&ctx); 41 | SHA1_Update(&ctx, input.c_str(), input.size()); 42 | 43 | unsigned char hash[SHA_DIGEST_LENGTH]; 44 | SHA1_Final(hash, &ctx); 45 | 46 | std::string result; 47 | char hex[3]; 48 | for(int i = 0; i < SHA_DIGEST_LENGTH; i++) 49 | { 50 | snprintf(hex, sizeof hex, "%02x", hash[i]); 51 | result.append(hex, 2); 52 | } 53 | return result; 54 | } 55 | -------------------------------------------------------------------------------- /src/sha1.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #ifndef _XVA_SHA1_HPP_ 34 | #define _XVA_SHA1_HPP_ 35 | 36 | #include 37 | 38 | namespace XVA 39 | { 40 | std::string SHA1(const std::string& input); 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/writefile.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | #define _LARGEFILE_SOURCE 33 | #define _LARGEFILE64_SOURCE 34 | #define _FILE_OFFSET_BITS 64 35 | 36 | #include 37 | #include "writefile.hpp" 38 | 39 | bool XVA::WriteFile(const std::string& path, const std::string& data) 40 | { 41 | FILE* fp = fopen(path.c_str(), "wb"); 42 | 43 | if (!fp) 44 | return false; 45 | 46 | if (fwrite(data.c_str(), 1, data.size(), fp) != data.size()) 47 | { 48 | fclose(fp); 49 | return false; 50 | } 51 | 52 | fclose(fp); 53 | return true; 54 | } 55 | -------------------------------------------------------------------------------- /src/writefile.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #ifndef _XVA_WRITEFILE_HPP_ 34 | #define _XVA_WRITEFILE_HPP_ 35 | 36 | #include 37 | 38 | namespace XVA 39 | { 40 | bool WriteFile(const std::string& path, const std::string& output); 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/xxh64.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #include 34 | #include 35 | #include "xxh64.hpp" 36 | 37 | std::string XVA::XXH64(const std::string& input) 38 | { 39 | XXH64_state_t* const state = XXH64_createState(); 40 | XXH64_hash_t const seed = 0; 41 | XXH64_reset(state, seed); 42 | 43 | XXH64_update(state, input.c_str(), input.size()); 44 | XXH64_hash_t const int_digest = XXH64_digest(state); 45 | 46 | char digest[8]; 47 | XXH64_canonicalFromHash((XXH64_canonical_t *)digest, int_digest); 48 | XXH64_freeState(state); 49 | 50 | char digest_chars[17]; 51 | for (int i = 0, j = 0; i < 8; i++) { 52 | unsigned char c; 53 | c = (digest[i] >> 4) & 0xf; 54 | c = (c > 9) ? c + 'A' - 10 : c + '0'; 55 | digest_chars[j++] = c; 56 | c = (digest[i] & 0xf); 57 | c = (c > 9) ? c + 'A' - 10 : c + '0'; 58 | digest_chars[j++] = c; 59 | } 60 | digest_chars[16] = '\0'; 61 | return std::string(digest_chars); 62 | } 63 | -------------------------------------------------------------------------------- /src/xxh64.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2009 Erik Lax 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place - Suite 330, 17 | Boston, MA 02111-1307, USA. 18 | 19 | In addition, as a special exception, the copyright holders give 20 | permission to link the code of portions of this program with the 21 | OpenSSL library under certain conditions as described in each 22 | individual source file, and distribute linked combinations 23 | including the two. 24 | You must obey the GNU General Public License in all respects 25 | for all of the code used other than OpenSSL. If you modify 26 | file(s) with this exception, you may extend this exception to your 27 | version of the file(s), but you are not obligated to do so. If you 28 | do not wish to do so, delete this exception statement from your 29 | version. If you delete this exception statement from all source 30 | files in the program, then also delete it here. 31 | */ 32 | 33 | #ifndef _XVA_XXH64_HPP_ 34 | #define _XVA_XXH64_HPP_ 35 | 36 | #include 37 | 38 | namespace XVA 39 | { 40 | std::string XXH64(const std::string& input); 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /xva-img.1: -------------------------------------------------------------------------------- 1 | .\" Text automatically generated by txt2man 2 | .TH xva-img 1 "31 January 2024" "xva-img-1.5.0" "Citrix XenServer .xva disk extraction tool" 3 | .SH NAME 4 | \fBxva-img \fP- Citrix XenServer .xva disk extraction tool 5 | \fB 6 | .SH SYNOPSIS 7 | .nf 8 | .fam C 9 | \fBxva-img\fP [\fIOPTION\fP] [COMMAND]\.\.\. 10 | 11 | .fam T 12 | .fi 13 | .fam T 14 | .fi 15 | .SH DESCRIPTION 16 | xva-image is a tool to generate disk images from Citrix XenServer .xva VM images 17 | as well as to generate .xva VM images from raw disks and the according ova.xml files. 18 | .SH OPTIONS 19 | \fB-h\fP, \fB--help\fP Show summary of options. 20 | .PP 21 | \fB-v\fP, \fB--version\fP Show version of program. 22 | .PP 23 | \fB-p\fP, \fB--progress\fP Show progress. 24 | .PP 25 | \fB-s\fP, \fB--sparse\fP Create sparse files on export. 26 | .PP 27 | \fB-i\fP, \fB--ignore-checksum\fP Only warn on failed checksum. 28 | .SH COMMANDS 29 | package [ ,\.\.\. ] 30 | .PP 31 | disk-export 32 | .PP 33 | disk-import 34 | .SH EXAMPLES 35 | Extract the XVA file: 36 | .PP 37 | .nf 38 | .fam C 39 | $ mkdir my-virtual-machine 40 | $ tar -xf my-virtual-machine.xva -C my-virtual-machine 41 | $ chmod -R 755 my-virtual-machine 42 | 43 | .fam T 44 | .fi 45 | Extract one of the disks: 46 | .PP 47 | .nf 48 | .fam C 49 | $ xva-img -p disk-export my-virtual-machine/Ref\\:1/ disk.raw 50 | 51 | .fam T 52 | .fi 53 | Splice the disk file (remove all files in the folder, required not to make any unnecessary mistakes): 54 | .PP 55 | .nf 56 | .fam C 57 | $ rm -rf my-virtual-machine/Ref\\:1/* 58 | $ xva-img -p disk-import my-virtual-machine/Ref1\\:/ disk.raw 59 | 60 | .fam T 61 | .fi 62 | Rebuild the XVA file: 63 | .PP 64 | .nf 65 | .fam C 66 | $ xva-img -p package my-virtual-machine-fixed.xva my-virtual-machine/ova.xml my-virtual-machine/Ref\\:1/ 67 | 68 | .fam T 69 | .fi 70 | Convert raw image to qcow2: 71 | .PP 72 | .nf 73 | .fam C 74 | $ qemu-img convert -f raw -O qcow2 disk.raw disk.qcow2 75 | 76 | .fam T 77 | .fi 78 | .SH SEE ALSO 79 | \fBchmod\fP(1), \fBmkdir\fP(1), \fBqemu-img\fP(1), \fBrm\fP(1), \fBtar\fP(1). 80 | .SH AUTHORS 81 | \fBxva-img\fP was written by Erik Lax. 82 | .PP 83 | This manpage was written by Francisco Vilmar Cardoso Ruviaro for the Debian project (but may be used by others). 84 | --------------------------------------------------------------------------------