pax_global_header 0000666 0000000 0000000 00000000064 12027041301 0014500 g ustar 00root root 0000000 0000000 52 comment=346b4a80f090c038cb2098ab96d13fb0896e6709
libldm-libldm-0.2.3/ 0000775 0000000 0000000 00000000000 12027041301 0014206 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/.gitignore 0000664 0000000 0000000 00000000646 12027041301 0016204 0 ustar 00root root 0000000 0000000 # Specific build artifacts
/aclocal.m4
/autom4te.cache/
/build-aux/
/config.h
/config.h.in
/config.log
/config.status
/configure
/gtk-doc.make
/ldm-1.0.pc
/libldm-*/
/libldm-*.tar.gz
/libtool
/m4
/stamp-h1
# Generic build artifacts
.libs/
.deps/
Makefile
Makefile.in
*.o
*.la
*.lo
# Random exclusions
.gdb_history
# Generated binaries
/src/ldmtool
/test/partread
/test/ldmread
/libldm.spec
# Temporary files
.*.swp
*~
libldm-libldm-0.2.3/COPYING.gpl 0000664 0000000 0000000 00000104513 12027041301 0016026 0 ustar 00root root 0000000 0000000 GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
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 3 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, see .
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
Copyright (C)
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
libldm-libldm-0.2.3/COPYING.lgpl 0000664 0000000 0000000 00000016743 12027041301 0016211 0 ustar 00root root 0000000 0000000 GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
libldm-libldm-0.2.3/Makefile.am 0000664 0000000 0000000 00000001634 12027041301 0016246 0 ustar 00root root 0000000 0000000 # libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
ACLOCAL_AMFLAGS = -I m4
DISTCHECK_CONFIGURE_FLAGS=--enable-gtk-doc
SUBDIRS = src test docs/reference/libldm docs/reference/ldmtool
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = ldm-1.0.pc
EXTRA_DIST = libldm.spec COPYING.lgpl COPYING.gpl
libldm-libldm-0.2.3/autogen.sh 0000775 0000000 0000000 00000002240 12027041301 0016205 0 ustar 00root root 0000000 0000000 #!/bin/bash
# libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
set -e
set -v
# Create the m4 include directory
mkdir -p m4
# Initialise gtk-doc
gtkdocize
# Initialize autotools
autoreconf -i
CONFIGUREDIR=.
# Run configure in BUILDDIR if it's set
if [ ! -z "$BUILDDIR" ]; then
mkdir -p $BUILDDIR
cd $BUILDDIR
CONFIGUREDIR=..
fi
# If no arguments were specified and configure has run before, use the previous
# arguments
if test $# == 0 && test -x ./config.status; then
./config.status --recheck
else
$CONFIGUREDIR/configure "$@"
fi
libldm-libldm-0.2.3/configure.ac 0000664 0000000 0000000 00000005573 12027041301 0016506 0 ustar 00root root 0000000 0000000 # libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
AC_PREREQ([2.68])
AC_INIT([libldm], [0.2.3], [mbooth@redhat.com])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_SRCDIR([src/ldm.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([foreign])
AC_PATH_PROG([XSLTPROC], [xsltproc])
m4_ifndef([AM_SILENT_RULES], [m4_define([AM_SILENT_RULES],[])])
AM_SILENT_RULES([yes]) # make --enable-silent-rules the default.
AC_CONFIG_MACRO_DIR([m4])
# Checks for programs.
AC_PROG_CC_C99
LT_INIT
AC_CHECK_LIB([readline], [readline], [],
[AC_MSG_ERROR([readline library is missing])])
# Checks for libraries.
AC_CHECK_FUNCS([bzero strerror])
AC_FUNC_MALLOC
# Checks for header files.
AC_CHECK_HEADERS(
[ fcntl.h \
stdint.h \
stdlib.h \
string.h \
sys/ioctl.h \
unistd.h ],
[],
[AC_MSG_ERROR([At least 1 required header file is missing])]
)
AC_HEADER_MAJOR
# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_TYPE_UINT16_T
AC_TYPE_UINT32_T
AC_TYPE_UINT64_T
AC_TYPE_UINT8_T
PKG_CHECK_MODULES([GOBJECT], [gobject-2.0 >= 2.26.0],
[
AC_SUBST([GOBJECT_CFLAGS])
AC_SUBST([GOBJECT_LIBS])
]
)
PKG_CHECK_MODULES([JSON], [json-glib-1.0 >= 0.14.0],
[
AC_SUBST([JSON_CFLAGS])
AC_SUBST([JSON_LIBS])
]
)
PKG_CHECK_MODULES([GIO_UNIX], [gio-unix-2.0 >= 2.32.0],
[
AC_SUBST([GIO_UNIX_CFLAGS])
AC_SUBST([GIO_UNIX_LIBS])
]
)
PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.5],
[
AC_SUBST([ZLIB_CFLAGS])
AC_SUBST([ZLIB_LIBS])
]
)
PKG_CHECK_MODULES([UUID], [uuid >= 2.21.0],
[
AC_SUBST([UUID_CFLAGS])
AC_SUBST([UUID_LIBS])
]
)
PKG_CHECK_MODULES([DEVMAPPER], [devmapper >= 1.0],
[
AC_SUBST([DEVMAPPER_CFLAGS])
AC_SUBST([DEVMAPPER_LIBS])
]
)
# GObject Introspection is not working. See comment in src/Makefile.am
# GOBJECT_INTROSPECTION_CHECK([1.30.0])
GTK_DOC_CHECK([1.14], [--flavour no-tmpl])
# Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([src/Makefile])
AC_CONFIG_FILES([test/Makefile])
AC_CONFIG_FILES([docs/reference/libldm/Makefile])
AC_CONFIG_FILES([docs/reference/ldmtool/Makefile])
AC_CONFIG_FILES([ldm-1.0.pc])
AC_CONFIG_FILES([libldm.spec])
AC_OUTPUT
libldm-libldm-0.2.3/docs/ 0000775 0000000 0000000 00000000000 12027041301 0015136 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/docs/reference/ 0000775 0000000 0000000 00000000000 12027041301 0017074 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/docs/reference/ldmtool/ 0000775 0000000 0000000 00000000000 12027041301 0020546 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/docs/reference/ldmtool/.gitignore 0000664 0000000 0000000 00000000037 12027041301 0022536 0 ustar 00root root 0000000 0000000 # Generated man page
ldmtool.1
libldm-libldm-0.2.3/docs/reference/ldmtool/Makefile.am 0000664 0000000 0000000 00000001563 12027041301 0022607 0 ustar 00root root 0000000 0000000 # libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
man_MANS = ldmtool.1
%.1: %.xml
@XSLTPROC@ -nonet http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $<
BUILT_EXTRA_DIST = $(man_MANS)
EXTRA_DIST = ldmtool.xml
CLEANFILES = ldmtool.1
libldm-libldm-0.2.3/docs/reference/ldmtool/ldmtool.xml 0000664 0000000 0000000 00000047772 12027041301 0022763 0 ustar 00root root 0000000 0000000 ldmtoolMatthewBoothmbooth@redhat.comPrimary developerldmtool1User CommandsldmtoolA tool to manage Microsoft Windows dynamic disksldmtooloptionsldmtooloptionsscandeviceldmtooloptionsshowdiskgroupdisk group GUIDldmtooloptionsshowvolumepartitiondiskdisk group GUIDobject nameldmtooloptionscreateremoveallldmtooloptionscreateremovevolumedisk group GUIDvolume nameOptionsdevice
Automatically scan device.
Descriptionldmtool is a tool for managing Microsoft Windows dynamic
disks, which use Microsoft's LDM metadata. It can inspect them, and also
create and remove device-mapper block devices which can be mounted.
Although a filesystem can be mounted read-write and its contents modified,
ldmtool is not able to modify the LDM metadata itself.
That is, it cannot create, remove or edit dynamic disks.
It is also not able to mount RAID5 volumes which have a partition missing,
although it can mount mirrored volumes with a partition missing. However,
mounting a volume with a missing partition is not recommended, as ldmtool
does not update the LDM metadata in any way. This means Windows will have no
way to determine that the partitions are not synchronised when it
subsequently mounted, which may result in corruption.
Invocationldmtool can be invoked either as a shell to run multiple
actions, or to run a single action and return. If an action is given on the
command line it will run in single action mode. If no action is given it
will launch a shell.
Shell mode
When invoked as a shell, ldmtool will not scan any
block devices by default. If any block devices are given on the command
line with the option, these will be scanned.
Otherwise, block devices must be scanned explicitly with the
action.
Exit shell mode by sending an EOF, or with the
quit or exit actions.
Single action mode
When invoked to run a single action all block devices will be scanned by
default. In this case, if any block devices are specified with the
option, only those block devices will be scanned.
Resultsldmtool returns results as JSON-formatted data. The
precise data returned is described in detail below. If a command fails it
will not return any JSON data, but will instead display an error message. In
shell mode, failure can be detected by the lack of a JSON-formatted result.
In single action mode the caller can additionally check the exit code of the
command.
Actionsscandevice
Scan all devices for LDM metadata.
Returns a list of all known disk group GUIDs. Note that it doesn't just
return newly discovered disk groups.
show diskgroup
GUID
Return detailed information about a single disk group.
Returns:nameThe human-readable name of the disk groupguidThe Windows-assigned GUID of the disk groupvolumes
A list of the names of all volumes in the disk group
disksA list of the name of all disks in the disk groupshow volume
disk group GUIDvolume name
Return detailed information about a volume.
Returns:nameThe name of the volumetype
The volume type. One of:
simplespannedstripedmirroredraid5sizeThe size of the volume in sectorschunk-size
The chunk size, in sectors, used by
striped and raid5
volumes. For other volume types it will be
0.
hint
The volume mounting hint. This value specifies how Windows
expects the volume to be mounted. For a volume with an
assigned drive letter, it might be 'E:'.
partitions
A list of the names of the partitions which constitute this
volume.
show partition
disk group GUIDpartition name
Return detailed information about a partition.
Returns:nameThe name of the partitionstart
The offset, in sectors, of the start of the partition from
the beginning of the disk
sizeThe size of the partition in sectorsdiskThe name of the disk the partition is onshow disk
disk group GUIDdisk name
Return detailed information about a disk.
Returns:nameThe human-readable name of the diskguidThe GUID of the diskpresent
Whether the disk has been discovered during scanning:
true or falseIf the disk is present, additionally returns:deviceThe name of the host device (e.g. /dev/sda)data-start
The start sector of the data portion of the disk
data-size
The size, in sectors, of the data portion of the disk
metadata-start
The start sector of the metadata portion of the disk
metadata-size
The size, in sectors, of the metadata portion of the disk
createvolumedisk group GUIDvolume nameall
Create a device-mapper device for either the specified volume or all
volumes in all detected disk groups.
Returns a list of the device-mapper device names which were created by
this action. Note that if a device already existed for a volume it will
not be returned in this list.
removevolumedisk group GUIDvolume nameall
Remove device-mapper devices for either the specified volume or all
volumes in all detected disk groups.
Returns a list of the device-mapper device names which were removed by
this action. Note that if no device existed for a volume it will not be
returned in this list.
Examples
The following examples form a sequence from a single session of ldmtool
running in shell mode.
Scan all loop devices for LDM metadata:
scan /dev/loop[0-9]*
[
"03c0c4fc-8b6f-402b-9431-4be2e5823b1c",
"06495a84-fbfd-11e1-8cf9-52540061f5db"
]
]]>
Two disk groups were detected. The list contains their GUIDs.
Show detailed information about one of the disk groups:
show diskgroup 06495a84-fbfd-11e1-8cf9-52540061f5db
{
"name" : "WIN-ERRDJSBDAVF-Dg0",
"guid" : "06495a84-fbfd-11e1-8cf9-52540061f5db",
"volumes" : [
"Volume4",
"Volume1",
"Volume2",
"Volume3",
"Volume5"
],
"disks" : [
"Disk1",
"Disk2",
"Disk3",
"Disk4",
"Disk5",
"Disk6",
"Disk7",
"Disk8",
"Disk9"
]
}
]]>
Show detailed information about one of the volumes in the disk group:
show volume 06495a84-fbfd-11e1-8cf9-52540061f5db Volume1
{
"name" : "Volume1",
"type" : "spanned",
"size" : 129024,
"chunk-size" : 0,
"hint" : "E:",
"partitions" : [
"Disk1-01",
"Disk2-01"
]
}
]]>
Show detailed information about one of the partitions in the volume:
show partition 06495a84-fbfd-11e1-8cf9-52540061f5db Disk1-01
{
"name" : "Disk1-01",
"start" : 65,
"size" : 96256,
"disk" : "Disk1"
}
]]>
Show detailed information about the disk containing the partition:
show disk 06495a84-fbfd-11e1-8cf9-52540061f5db Disk1
{
"name" : "Disk1",
"guid" : "06495a85-fbfd-11e1-8cf9-52540061f5db",
"present" : true,
"device" : "/dev/loop15",
"data-start" : 63,
"data-size" : 100289,
"metadata-start" : 100352,
"metadata-size" : 2048
}
]]>
Create a device-mapper device for a volume:
create volume 06495a84-fbfd-11e1-8cf9-52540061f5db Volume1
[
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume1"
]
]]>
A new device-mapper device has been created, called
ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume1. Depending on how
udev is configured, it will probably now be available as
/dev/mapper/ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume1.
Create device-mapper devices for all volumes in both disk groups:
create all
[
"ldm_vol_Red-nzv8x6obywgDg0_Volume2",
"ldm_vol_Red-nzv8x6obywgDg0_Volume1",
"ldm_vol_Red-nzv8x6obywgDg0_Volume4",
"ldm_vol_Red-nzv8x6obywgDg0_Stripe1",
"ldm_vol_Red-nzv8x6obywgDg0_Raid1",
"ldm_vol_Red-nzv8x6obywgDg0_Volume3",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume4",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume2",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume3",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume5"
]
]]>
Note that ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume1 is not in
the list of devices which were created as it already existed.
Remove the device-mapper device for a single volume:
remove volume 06495a84-fbfd-11e1-8cf9-52540061f5db Volume2
[
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume2"
]
]]>
remove all
[
"ldm_vol_Red-nzv8x6obywgDg0_Volume2",
"ldm_vol_Red-nzv8x6obywgDg0_Volume1",
"ldm_vol_Red-nzv8x6obywgDg0_Volume4",
"ldm_vol_Red-nzv8x6obywgDg0_Stripe1",
"ldm_vol_Red-nzv8x6obywgDg0_Raid1",
"ldm_vol_Red-nzv8x6obywgDg0_Volume3",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume4",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume1",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume3",
"ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume5"
]
]]>
Note that ldm_vol_WIN-ERRDJSBDAVF-Dg0_Volume2 is not in
the list of devices which were removed, as it was removed previously.
libldm-libldm-0.2.3/docs/reference/libldm/ 0000775 0000000 0000000 00000000000 12027041301 0020337 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/docs/reference/libldm/.gitignore 0000664 0000000 0000000 00000000504 12027041301 0022326 0 ustar 00root root 0000000 0000000 # gtk-doc spam
/html-build.stamp
/html.stamp
/html/
/*-decl-list.txt
/*-decl.txt
/*-docs.sgml
/*-overrides.txt
/*-sections.txt
/*-undeclared.txt
/*-undocumented.txt
/*-unused.txt
/*.args
/*.hierarchy
/*.interfaces
/*.prerequisites
/*.signals
/*.types
/scan-build.stamp
/setup-build.stamp
/sgml-build.stamp
/sgml.stamp
/xml/
libldm-libldm-0.2.3/docs/reference/libldm/Makefile.am 0000664 0000000 0000000 00000010566 12027041301 0022403 0 ustar 00root root 0000000 0000000 # libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
libname=libldm-1.0.la
lib=$(top_builddir)/src/$(libname)
# We include built sgml and html in dist
dist-hook: sgml-build.stamp html-build.stamp
# We require automake 1.6 at least.
AUTOMAKE_OPTIONS = 1.6
# The name of the module, e.g. 'glib'.
DOC_MODULE=libldm
# Uncomment for versioned docs and specify the version of the module, e.g. '2'.
#DOC_MODULE_VERSION=2
# Introduce an explicit dependency on the library. This ensure that the docs
# will be rebuilt if the library is updated, and that the docs build will
# succeed if the library hasn't been built yet.
# This is required for distcheck to succeed on a fresh checkout.
scan-build.stamp: $(lib)
$(lib):
$(MAKE) $(AM_MAKEFLAGS) -C $(top_srcdir)/src $(libname)
# The top-level SGML file. You can change this if you want to.
DOC_MAIN_SGML_FILE=$(DOC_MODULE)-docs.sgml
# Directories containing the source code.
# gtk-doc will search all .c and .h files beneath these paths
# for inline comments documenting functions and macros.
# e.g. DOC_SOURCE_DIR=$(top_srcdir)/gtk $(top_srcdir)/gdk
DOC_SOURCE_DIR=$(top_srcdir)/src
# Extra options to pass to gtkdoc-scangobj. Not normally needed.
SCANGOBJ_OPTIONS=
# Extra options to supply to gtkdoc-scan.
# e.g. SCAN_OPTIONS=--deprecated-guards="GTK_DISABLE_DEPRECATED"
SCAN_OPTIONS=
# Extra options to supply to gtkdoc-mkdb.
# e.g. MKDB_OPTIONS=--xml-mode --output-format=xml
MKDB_OPTIONS=--xml-mode --output-format=xml
# Extra options to supply to gtkdoc-mktmpl
# e.g. MKTMPL_OPTIONS=--only-section-tmpl
MKTMPL_OPTIONS=
# Extra options to supply to gtkdoc-mkhtml
MKHTML_OPTIONS=
# Extra options to supply to gtkdoc-fixref. Not normally needed.
# e.g. FIXXREF_OPTIONS=--extra-dir=../gdk-pixbuf/html --extra-dir=../gdk/html
FIXXREF_OPTIONS=
# Used for dependencies. The docs will be rebuilt if any of these change.
# e.g. HFILE_GLOB=$(top_srcdir)/gtk/*.h
# e.g. CFILE_GLOB=$(top_srcdir)/gtk/*.c
HFILE_GLOB=$(top_srcdir)/src/ldm.h
CFILE_GLOB=$(top_srcdir)/src/ldm.c
# Extra header to include when scanning, which are not under DOC_SOURCE_DIR
# e.g. EXTRA_HFILES=$(top_srcdir}/contrib/extra.h
EXTRA_HFILES=
# Header files or dirs to ignore when scanning. Use base file/dir names
# e.g. IGNORE_HFILES=gtkdebug.h gtkintl.h private_code
IGNORE_HFILES=gpt.h mbr.h
# Images to copy into HTML directory.
# e.g. HTML_IMAGES=$(top_srcdir)/gtk/stock-icons/stock_about_24.png
HTML_IMAGES=
# Extra SGML files that are included by $(DOC_MAIN_SGML_FILE).
# e.g. content_files=running.sgml building.sgml changes-2.0.sgml
content_files=
# SGML files where gtk-doc abbrevations (#GtkWidget) are expanded
# These files must be listed here *and* in content_files
# e.g. expand_content_files=running.sgml
expand_content_files=
# CFLAGS and LDFLAGS for compiling gtkdoc-scangobj with your library.
# Only needed if you are using gtkdoc-scangobj to dynamically query widget
# signals and properties.
# e.g. GTKDOC_CFLAGS=-I$(top_srcdir) -I$(top_builddir) $(GTK_DEBUG_FLAGS)
# e.g. GTKDOC_LIBS=$(top_builddir)/gtk/$(gtktargetlib)
GTKDOC_CFLAGS=$(DEVMAPPER_LIBS)
GTKDOC_LIBS=$(lib) $(DEVMAPPER_LIBS)
# This includes the standard gtk-doc make rules, copied by gtkdocize.
include $(top_srcdir)/gtk-doc.make
# Other files to distribute
# e.g. EXTRA_DIST += version.xml.in
EXTRA_DIST +=
# Files not to distribute
# for --rebuild-types in $(SCAN_OPTIONS), e.g. $(DOC_MODULE).types
# for --rebuild-sections in $(SCAN_OPTIONS) e.g. $(DOC_MODULE)-sections.txt
#DISTCLEANFILES +=
# Comment this out if you want 'make check' to test you doc status
# and run some sanity checks
if ENABLE_GTK_DOC
TESTS_ENVIRONMENT = cd $(srcdir) && \
DOC_MODULE=$(DOC_MODULE) DOC_MAIN_SGML_FILE=$(DOC_MAIN_SGML_FILE) \
SRCDIR=$(abs_srcdir) BUILDDIR=$(abs_builddir)
#TESTS = $(GTKDOC_CHECK)
endif
-include $(top_srcdir)/git.mk
libldm-libldm-0.2.3/ldm-1.0.pc.in 0000664 0000000 0000000 00000000545 12027041301 0016213 0 ustar 00root root 0000000 0000000 prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: LDM
Description: Microsoft Windows LDM device management library
Requires: gobject-2.0 >= 2.26.0 glib-2.0
Requires.private: json-glib-1.0 >= 0.14.0 gio-unix-2.0 >= 2.32.0 devmapper >= 1.0
Version: @VERSION@
Libs: -L${libdir} -lldm-1.0
Libs.private: -lz -luuid
Cflags:
libldm-libldm-0.2.3/libldm.spec.in 0000664 0000000 0000000 00000002776 12027041301 0016746 0 ustar 00root root 0000000 0000000 Name: libldm
Version: @VERSION@
Release: 1%{?dist}%{?extra_release}
Summary: A tool to manage Windows dynamic disks
License: LGPLv3+ and GPLv3+
URL: https://github.com/mdbooth/libldm
Source0: %{url}/downloads/%{name}-%{version}.tar.gz
BuildRequires: glib2-devel >= 2.26.0
BuildRequires: json-glib-devel >= 0.14.0
BuildRequires: device-mapper-devel >= 1.0
BuildRequires: zlib-devel libuuid-devel readline-devel
%description
libldm is a library for managing Microsoft Windows dynamic disks, which use
Microsoft's LDM metadata. It can inspect them, and also create and remove
device-mapper block devices which can be mounted. It includes ldmtool, which
exposes this functionality as a command-line tool.
libldm is released under LGPLv3+. ldmtool is released under GPLv3+.
%package devel
Summary: Development files for %{name}
Requires: %{name}%{?_isa} = %{version}-%{release}
%description devel
The %{name}-devel package contains libraries and header files for
developing applications that use %{name}.
%prep
%setup -q
%build
%configure --disable-static --enable-gtk-doc
make %{?_smp_mflags}
%install
%make_install
find $RPM_BUILD_ROOT -name '*.la' -exec rm -f {} ';'
%post -p /sbin/ldconfig
%postun -p /sbin/ldconfig
%files
%doc COPYING.lgpl COPYING.gpl
%{_libdir}/*.so.*
%{_bindir}/ldmtool
%{_mandir}/man1/ldmtool.1.gz
%files devel
%{_includedir}/*
%{_libdir}/*.so
%{_libdir}/pkgconfig/ldm-1.0.pc
%{_datadir}/gtk-doc
%changelog
libldm-libldm-0.2.3/src/ 0000775 0000000 0000000 00000000000 12027041301 0014775 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/src/Makefile.am 0000664 0000000 0000000 00000004255 12027041301 0017037 0 ustar 00root root 0000000 0000000 # libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
AM_CFLAGS = -Wall -Werror -Wshadow -Wextra \
-Wno-unused-local-typedefs \
-Wno-unused-parameter
libname = libldm-1.0.la
lib_LTLIBRARIES = $(libname)
include_HEADERS = ldm.h
libldm_1_0_la_SOURCES = mbr.h mbr.c gpt.h gpt.c ldm.h ldm.c
libldm_1_0_la_CFLAGS = $(AM_CFLAGS) $(GOBJECT_CFLAGS) $(ZLIB_CFLAGS) $(UUID_CFLAGS) $(DEVMAPPER_CFLAGS)
libldm_1_0_la_LIBADD = $(ZLIB_LIBS) $(UUID_LIBS) $(GOBJECT_LIBS) $(DEVMAPPER_LIBS)
bin_PROGRAMS = ldmtool
ldmtool_CFLAGS = $(AM_CFLAGS) $(GOBJECT_CFLAGS) $(JSON_CFLAGS) \
$(GIO_UNIX_CFLAGS)
ldmtool_LDADD = -lreadline $(builddir)/$(libname) $(GOBJECT_LIBS) \
$(JSON_LIBS) $(GIO_UNIX_LIBS)
# GObject introspection fails. This seems to be because g-ir-scanner incorrectly
# guesses the symbol prefix as 'l_dm', although explicitly passing in the
# correct prefix causes it not to recognise any of the objects name symbols. I'm
# fairly sure this is going to require a fix in g-ir-scanner.
#include $(INTROSPECTION_MAKEFILE)
#
#INTROSPECTION_GIRS = LDM-1.0.gir
#INTROSPECTION_SCANNER_ARGS = --add-include-path=$(srcdir) --warn-all
#INTROSPECTION_COMPILER_ARGS = --includedir=$(srcdir)
#
#introspection_sources = ldm.h ldm.c
#
#LDM-1.0.gir: $(libname)
#LDM_1_0_gir_INCLUDES = GObject-2.0
#LDM_1_0_gir_CFLAGS = $(INCLUDES) -I$(srcdir)
#LDM_1_0_gir_LIBS = $(libname)
#LDM_1_0_gir_FILES = $(patsubst %,$(srcdir)/%,$(introspection_sources))
#
#girdir = $(datadir)/gir-1.0
#gir_DATA = $(INTROSPECTION_GIRS)
#
#typelibdir = $(libdir)/girepository-1.0
#typelib_DATA = $(INTROSPECTION_GIRS:.gir=.typelib)
libldm-libldm-0.2.3/src/gpt.c 0000664 0000000 0000000 00000013605 12027041301 0015740 0 ustar 00root root 0000000 0000000 /* libldm
* Copyright 2012 Red Hat Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "gpt.h"
struct _gpt_head {
char magic[8];
uint32_t revision;
uint32_t size;
} __attribute__((__packed__));
struct _gpt {
struct _gpt_head head;
uint32_t header_crc;
char padding1[4];
uint64_t current_header_lba;
uint64_t backup_header_lba;
uint64_t first_usable_lba;
uint64_t last_usable_lba;
uuid_t disk_guid;
uint64_t pte_array_start_lba;
uint32_t pte_array_len;
uint32_t pte_size;
uint32_t pte_array_crc;
char padding2[];
} __attribute__((__packed__));
struct _gpt_part {
uuid_t type;
uuid_t guid;
uint64_t first_lba;
uint64_t last_lba;
uint64_t flags;
char name[72];
} __attribute__((__packed__));
struct _gpt_handle {
int fd;
struct _gpt *gpt;
char *pte_array;
iconv_t cd;
};
int
gpt_open_secsize(int fd, const size_t secsize, gpt_handle_t **h)
{
int err = 0;
const off_t gpt_start = secsize;
struct _gpt_head head;
size_t read = 0;
while (read < sizeof(head)) {
ssize_t in = pread(fd, &head + read, sizeof(head) - read,
read + gpt_start);
if (in == 0) return -GPT_ERROR_INVALID;
if (in == -1) return -GPT_ERROR_READ;
read += in;
}
if (memcmp(head.magic, "EFI PART", 8) != 0) return -GPT_ERROR_INVALID;
/* Check the header size. Don't believe anything greater than 4k. */
uint32_t header_size = le32toh(head.size);
if (header_size > 4*1024) return -GPT_ERROR_INVALID;
*h = malloc(sizeof(**h));
if (*h == NULL) abort();
(*h)->gpt = NULL;
(*h)->pte_array = NULL;
(*h)->cd = iconv_open("UTF-8", "UTF-16LE");
(*h)->gpt = malloc(header_size);
if ((*h)->gpt == NULL) abort();
(*h)->fd = fd;
struct _gpt *_gpt = (*h)->gpt;
memcpy(_gpt, &head, sizeof(head));
while (read < le32toh(head.size)) {
ssize_t in = pread(fd, (char *)(_gpt) + read, le32toh(head.size) - read,
read + gpt_start);
if (in == 0) {
err = -GPT_ERROR_INVALID;
goto error;
}
if (in == -1) {
err = -GPT_ERROR_READ;
goto error;
}
read += in;
}
uint32_t header_crc = _gpt->header_crc;
_gpt->header_crc = 0;
uint32_t crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, (Bytef *)_gpt, _gpt->head.size);
if (crc != header_crc) {
err = -GPT_ERROR_INVALID;
goto error;
}
uint32_t pte_array_len = le32toh(_gpt->pte_array_len);
uint32_t pte_size = le32toh(_gpt->pte_size);
/* Sanity check partition entries metadata */
if (pte_array_len > 1024 || pte_size > 1024) {
err = -GPT_ERROR_INVALID;
goto error;
}
const uint32_t pte_array_size = pte_array_len * pte_size;
(*h)->pte_array = malloc(pte_array_size);
if ((*h)->pte_array == NULL) abort();
const off_t pte_array_start = _gpt->pte_array_start_lba * secsize;
read = 0;
while (read < pte_array_size) {
ssize_t in = pread(fd, (*h)->pte_array + read, pte_array_size - read,
read + pte_array_start);
if (in == 0) {
err = -GPT_ERROR_INVALID;
goto error;
}
if (in == -1) {
err = -GPT_ERROR_READ;
goto error;
}
read += in;
}
crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, (Bytef *) (*h)->pte_array, pte_array_size);
if (crc != _gpt->pte_array_crc) {
err = -GPT_ERROR_INVALID;
goto error;
}
return 0;
error:
if ((*h)->cd) iconv_close((*h)->cd);
free((*h)->gpt);
free((*h)->pte_array);
free(*h);
return err;
}
int
gpt_open(int fd, gpt_handle_t **h)
{
int secsize;
if (ioctl(fd, BLKSSZGET, &secsize) == -1) {
secsize = 512;
}
return gpt_open_secsize(fd, secsize, h);
}
void
gpt_close(gpt_handle_t *h)
{
iconv_close(h->cd);
free(h->pte_array);
free(h->gpt);
free(h);
}
void
gpt_get_header(gpt_handle_t *h, gpt_t *gpt)
{
struct _gpt *_gpt = h->gpt;
gpt->first_usable_lba = le64toh(_gpt->first_usable_lba);
gpt->last_usable_lba = le64toh(_gpt->last_usable_lba);
gpt->pte_array_len = le32toh(_gpt->pte_array_len);
gpt->pte_size = le32toh(_gpt->pte_size);
memcpy(gpt->disk_guid, _gpt->disk_guid, sizeof(gpt->disk_guid));
}
int
gpt_get_pte(gpt_handle_t *h, uint32_t n, gpt_pte_t *part)
{
if (n >= le32toh(h->gpt->pte_array_len))
return -GPT_ERROR_INVALID_PART;
struct _gpt_part *_part =
(struct _gpt_part *) (h->pte_array + le32toh(h->gpt->pte_size) * n);
memcpy(part->type, _part->type, sizeof(part->type));
memcpy(part->guid, _part->guid, sizeof(part->guid));
part->first_lba = le64toh(_part->first_lba);
part->last_lba = le64toh(_part->last_lba);
part->flags = le64toh(_part->flags);
char *inbuf = _part->name;
size_t in_rem = sizeof(_part->name);
char *outbuf = part->name;
size_t out_rem = sizeof(part->name);
iconv(h->cd, &inbuf, &in_rem, &outbuf, &out_rem);
return 0;
}
libldm-libldm-0.2.3/src/gpt.h 0000664 0000000 0000000 00000002716 12027041301 0015746 0 ustar 00root root 0000000 0000000 /* libldm
* Copyright 2012 Red Hat Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
#include
#include
typedef enum {
GPT_ERROR_OK,
GPT_ERROR_INVALID,
GPT_ERROR_READ,
GPT_ERROR_INVALID_PART
} gpt_error_t;
typedef struct {
uint64_t first_usable_lba;
uint64_t last_usable_lba;
uuid_t disk_guid;
uint32_t pte_array_len;
uint32_t pte_size;
} gpt_t;
typedef struct {
uuid_t type;
uuid_t guid;
uint64_t first_lba;
uint64_t last_lba;
uint64_t flags;
char name[72];
} gpt_pte_t;
typedef struct _gpt_handle gpt_handle_t;
int gpt_open(int fd, gpt_handle_t **h);
int gpt_open_secsize(int fd, size_t secsize, gpt_handle_t **h);
void gpt_close(gpt_handle_t *h);
void gpt_get_header(gpt_handle_t *h, gpt_t *gpt);
int gpt_get_pte(gpt_handle_t *h, uint32_t n, gpt_pte_t *part);
libldm-libldm-0.2.3/src/ldm.c 0000664 0000000 0000000 00000261764 12027041301 0015735 0 ustar 00root root 0000000 0000000 /* libldm
* Copyright 2012 Red Hat Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mbr.h"
#include "gpt.h"
#include "ldm.h"
#define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-" \
"%02x%02x-%02x%02x%02x%02x%02x%02x"
#define UUID_VALS(uuid) (uuid)[0], (uuid)[1], (uuid)[2], (uuid)[3], \
(uuid)[4], (uuid)[5], (uuid)[6], (uuid)[7], \
(uuid)[8], (uuid)[9], (uuid)[10], (uuid)[11], \
(uuid)[12], (uuid)[13], (uuid)[14], (uuid)[15]
/**
* SECTION:ldm
* @include: ldm.h
*/
/*
* The layout here is mostly derived from:
* http://hackipedia.org/Disk%20formats/Partition%20tables/Windows%20NT%20Logical%20Disk%20Manager/html,%20ldmdoc/index.html
*
* Note that the above reference describes a slightly older version of LDM, but
* the fields it describes remain accurate.
*
* The principal difference from the above version is the addition of support
* for LDM on GPT disks.
*/
/* Fixed structures
*
* These structures don't contain any variable-length fields, and can therefore
* be accessed directly.
*/
struct _privhead
{
char magic[8]; // "PRIVHEAD"
uint32_t unknown_sequence;
uint16_t version_major;
uint16_t version_minor;
uint64_t unknown_timestamp;
uint64_t unknown_number;
uint64_t unknown_size1;
uint64_t unknown_size2;
char disk_guid[64];
char host_guid[64];
char disk_group_guid[64];
char disk_group_name[32];
uint16_t unknown1;
char padding1[9];
uint64_t logical_disk_start;
uint64_t logical_disk_size;
uint64_t ldm_config_start;
uint64_t ldm_config_size;
uint64_t n_tocs;
uint64_t toc_size;
uint32_t n_configs;
uint32_t n_logs;
uint64_t config_size;
uint64_t log_size;
uint32_t disk_signature;
/* Values below aren't set in my data */
char disk_set_guid[16];
char disk_set_guid_dup[16];
} __attribute__((__packed__));
struct _tocblock_bitmap
{
char name[8];
uint16_t flags1;
uint64_t start;
uint64_t size; // Relative to start of DB
uint64_t flags2;
} __attribute__((__packed__));
struct _tocblock
{
char magic[8]; // "TOCBLOCK"
uint32_t seq1;
char padding1[4];
uint32_t seq2;
char padding2[16];
struct _tocblock_bitmap bitmap[2];
} __attribute__((__packed__));
struct _vmdb
{
char magic[4]; // "VMDB"
uint32_t vblk_last;
uint32_t vblk_size;
uint32_t vblk_first_offset;
uint16_t update_status;
uint16_t version_major;
uint16_t version_minor;
char disk_group_name[31];
char disk_group_guid[64];
uint64_t committed_seq;
uint64_t pending_seq;
uint32_t n_committed_vblks_vol;
uint32_t n_committed_vblks_comp;
uint32_t n_committed_vblks_part;
uint32_t n_committed_vblks_disk;
char padding1[12];
uint32_t n_pending_vblks_vol;
uint32_t n_pending_vblks_comp;
uint32_t n_pending_vblks_part;
uint32_t n_pending_vblks_disk;
char padding2[12];
uint64_t last_accessed;
} __attribute__((__packed__));
/* This is the header of every VBLK entry */
struct _vblk_head
{
char magic[4]; // "VBLK"
uint32_t seq;
uint32_t record_id;
uint16_t entry;
uint16_t entries_total;
} __attribute__((__packed__));
/* This is the header of every VBLK record, which may span multiple VBLK
* entries. I.e. if a VBLK record is split across 2 entries, only the first will
* have this header immediately following the entry header. */
struct _vblk_rec_head
{
uint16_t status;
uint8_t flags;
uint8_t type;
uint32_t size;
} __attribute__((__packed__));
/* Array clearing functions */
static void
_unref_object(gpointer const data)
{
g_object_unref(*(GObject **)data);
}
static void
_free_pointer(gpointer const data)
{
g_free(*(gpointer *)data);
}
static void
_free_gstring(gpointer const data)
{
g_string_free(*(GString **)data, TRUE);
}
/* GLIB error handling */
GQuark
ldm_error_quark(void)
{
return g_quark_from_static_string("ldm");
}
GType
ldm_error_get_type(void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
{ LDM_ERROR_INTERNAL, "LDM_ERROR_INTERNAL", "internal" },
{ LDM_ERROR_IO, "LDM_ERROR_IO", "io" },
{ LDM_ERROR_NOT_LDM, "LDM_ERROR_NOT_LDM", "not_ldm" },
{ LDM_ERROR_INVALID, "LDM_ERROR_INVALID", "invalid" },
{ LDM_ERROR_INCONSISTENT, "LDM_ERROR_INCONSISTENT",
"inconsistent" },
{ LDM_ERROR_NOTSUPPORTED, "LDM_ERROR_NOTSUPPORTED",
"notsupported" },
{ LDM_ERROR_MISSING_DISK, "LDM_ERROR_MISSING_DISK",
"missing-disk" },
{ LDM_ERROR_EXTERNAL, "LDM_ERROR_EXTERNAL", "external" }
};
etype = g_enum_register_static("LDMError", values);
}
return etype;
}
/* Macros for exporting object properties */
#define EXPORT_PROP_STRING(object, klass, property) \
gchar * \
ldm_ ## object ## _get_ ## property(const klass * const o) \
{ \
const size_t len = strlen(o->priv->property) + 1; \
gchar * const r = g_malloc(len); \
memcpy(r, o->priv->property, len); \
return r; \
}
#define EXPORT_PROP_GUID(object, klass) \
gchar * \
ldm_ ## object ## _get_guid(const klass * const o) \
{ \
gchar *r = g_malloc(37); \
uuid_unparse(o->priv->guid, r); \
return r; \
}
#define EXPORT_PROP_SCALAR(object, klass, property, type) \
type \
ldm_ ## object ## _get_ ## property(const klass * const o) \
{ \
return o->priv->property; \
}
/* LDM */
#define LDM_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
((obj), LDM_TYPE, LDMPrivate))
struct _LDMPrivate
{
GArray *disk_groups;
};
G_DEFINE_TYPE(LDM, ldm, G_TYPE_OBJECT)
static void
ldm_dispose(GObject * const object)
{
LDM *ldm = LDM_CAST(object);
if (ldm->priv->disk_groups) {
g_array_unref(ldm->priv->disk_groups); ldm->priv->disk_groups = NULL;
}
}
static void
ldm_init(LDM * const o)
{
o->priv = LDM_GET_PRIVATE(o);
bzero(o->priv, sizeof(*o->priv));
}
static void
ldm_class_init(LDMClass * const klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->dispose = ldm_dispose;
g_type_class_add_private(klass, sizeof(LDMPrivate));
}
/* LDMDiskGroup */
#define LDM_DISK_GROUP_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
((obj), LDM_TYPE_DISK_GROUP, LDMDiskGroupPrivate))
struct _LDMDiskGroupPrivate
{
uuid_t guid;
uint32_t id;
char *name;
uint64_t sequence;
/* GObjects */
GArray *disks;
GArray *parts;
GArray *vols;
/* We don't expose components, so they're no GObjects */
uint32_t n_comps;
GArray *comps;
};
G_DEFINE_TYPE(LDMDiskGroup, ldm_disk_group, G_TYPE_OBJECT)
enum {
PROP_LDM_DISK_GROUP_PROP0,
PROP_LDM_DISK_GROUP_GUID,
PROP_LDM_DISK_GROUP_NAME
};
static void
ldm_disk_group_get_property(GObject * const o, const guint property_id,
GValue * const value, GParamSpec * const pspec)
{
LDMDiskGroup * const dg = LDM_DISK_GROUP(o);
LDMDiskGroupPrivate * const priv = dg->priv;
switch (property_id) {
case PROP_LDM_DISK_GROUP_GUID:
{
char guid_str[37];
uuid_unparse(priv->guid, guid_str);
g_value_set_string(value, guid_str);
}
break;
case PROP_LDM_DISK_GROUP_NAME:
g_value_set_string(value, priv->name); break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec);
}
}
EXPORT_PROP_STRING(disk_group, LDMDiskGroup, name)
EXPORT_PROP_GUID(disk_group, LDMDiskGroup)
static void
ldm_disk_group_dispose(GObject * const object)
{
LDMDiskGroup *dg = LDM_DISK_GROUP(object);
if (dg->priv->vols) {
g_array_unref(dg->priv->vols); dg->priv->vols = NULL;
}
if (dg->priv->comps) {
g_array_unref(dg->priv->comps); dg->priv->comps = NULL;
}
if (dg->priv->parts) {
g_array_unref(dg->priv->parts); dg->priv->parts = NULL;
}
if (dg->priv->disks) {
g_array_unref(dg->priv->disks); dg->priv->disks = NULL;
}
}
static void
ldm_disk_group_finalize(GObject * const object)
{
LDMDiskGroup *dg = LDM_DISK_GROUP(object);
g_free(dg->priv->name); dg->priv->name = NULL;
}
static void
ldm_disk_group_class_init(LDMDiskGroupClass * const klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->dispose = ldm_disk_group_dispose;
object_class->finalize = ldm_disk_group_finalize;
object_class->get_property = ldm_disk_group_get_property;
g_type_class_add_private(klass, sizeof(LDMDiskGroupPrivate));
/**
* LDMDiskGroup:guid:
*
* A string representation of the disk group's GUID.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_GROUP_GUID,
g_param_spec_string(
"guid", "GUID",
"A string representation of the disk group's GUID",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMDiskGroup:name:
*
* The name of the disk group.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_GROUP_NAME,
g_param_spec_string(
"name", "Name", "The name of the disk group",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
}
static void
ldm_disk_group_init(LDMDiskGroup * const o)
{
o->priv = LDM_DISK_GROUP_GET_PRIVATE(o);
bzero(o->priv, sizeof(*o->priv));
}
/* LDMVolumeType */
GType
ldm_volume_type_get_type(void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
{ LDM_VOLUME_TYPE_SIMPLE, "LDM_VOLUME_TYPE_SIMPLE", "simple" },
{ LDM_VOLUME_TYPE_SPANNED, "LDM_VOLUME_TYPE_SPANNED", "spanned" },
{ LDM_VOLUME_TYPE_STRIPED, "LDM_VOLUME_TYPE_STRIPED", "striped" },
{ LDM_VOLUME_TYPE_MIRRORED, "LDM_VOLUME_TYPE_MIRRORED",
"mirrored" },
{ LDM_VOLUME_TYPE_RAID5, "LDM_VOLUME_TYPE_RAID5", "RAID5" }
};
etype = g_enum_register_static("LDMVolumeType", values);
}
return etype;
}
/* LDMVolume */
#define LDM_VOLUME_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
((obj), LDM_TYPE_VOLUME, LDMVolumePrivate))
typedef enum {
_VOLUME_TYPE_GEN = 0x3,
_VOLUME_TYPE_RAID5 = 0x4
} _int_volume_type;
struct _LDMVolumePrivate
{
guint32 id;
gchar *name;
gchar *dgname;
guint64 size;
guint8 part_type;
guint8 flags; /* Not exposed: unclear what it means */
gchar *id1; /* Not exposed: unclear what it means */
gchar *id2; /* Not exposed: unclear what it means */
guint64 size2; /* Not exposed: unclear what it means */
gchar *hint;
/* Derived */
LDMVolumeType type;
GArray *parts;
guint64 chunk_size;
/* Only used during parsing */
_int_volume_type _int_type;
guint32 _n_comps;
guint32 _n_comps_i;
};
G_DEFINE_TYPE(LDMVolume, ldm_volume, G_TYPE_OBJECT)
enum {
PROP_LDM_VOLUME_PROP0,
PROP_LDM_VOLUME_NAME,
PROP_LDM_VOLUME_TYPE,
PROP_LDM_VOLUME_SIZE,
PROP_LDM_VOLUME_PART_TYPE,
PROP_LDM_VOLUME_HINT,
PROP_LDM_VOLUME_CHUNK_SIZE
};
static void
ldm_volume_get_property(GObject * const o, const guint property_id,
GValue * const value, GParamSpec *pspec)
{
LDMVolume * const vol = LDM_VOLUME(o);
LDMVolumePrivate * const priv = vol->priv;
switch (property_id) {
case PROP_LDM_VOLUME_NAME:
g_value_set_string(value, priv->name); break;
case PROP_LDM_VOLUME_TYPE:
g_value_set_enum(value, priv->type); break;
case PROP_LDM_VOLUME_SIZE:
g_value_set_uint64(value, priv->size); break;
case PROP_LDM_VOLUME_PART_TYPE:
g_value_set_uint(value, priv->part_type); break;
case PROP_LDM_VOLUME_HINT:
g_value_set_string(value, priv->hint); break;
case PROP_LDM_VOLUME_CHUNK_SIZE:
g_value_set_uint64(value, priv->chunk_size); break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec);
}
}
EXPORT_PROP_STRING(volume, LDMVolume, name)
EXPORT_PROP_SCALAR(volume, LDMVolume, size, guint64)
EXPORT_PROP_SCALAR(volume, LDMVolume, part_type, guint8)
EXPORT_PROP_STRING(volume, LDMVolume, hint)
EXPORT_PROP_SCALAR(volume, LDMVolume, chunk_size, guint64)
/* Sigh... another conflict with glib's _get_type() */
LDMVolumeType
ldm_volume_get_voltype(const LDMVolume * const o)
{
return o->priv->type;
}
static void
ldm_volume_dispose(GObject * const object)
{
LDMVolume * const vol_o = LDM_VOLUME(object);
LDMVolumePrivate * const vol = vol_o->priv;
if (vol->parts) { g_array_unref(vol->parts); vol->parts = NULL; }
}
static void
ldm_volume_finalize(GObject * const object)
{
LDMVolume * const vol_o = LDM_VOLUME(object);
LDMVolumePrivate * const vol = vol_o->priv;
g_free(vol->name); vol->name = NULL;
g_free(vol->dgname); vol->dgname = NULL;
g_free(vol->id1); vol->id1 = NULL;
g_free(vol->id2); vol->id2 = NULL;
g_free(vol->hint); vol->hint = NULL;
}
static void
ldm_volume_class_init(LDMVolumeClass * const klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->dispose = ldm_volume_dispose;
object_class->finalize = ldm_volume_finalize;
object_class->get_property = ldm_volume_get_property;
g_type_class_add_private(klass, sizeof(LDMVolumePrivate));
/**
* LDMVolume:name:
*
* The volume's name.
*/
g_object_class_install_property(
object_class,
PROP_LDM_VOLUME_NAME,
g_param_spec_string(
"name", "Name", "The volume's name",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMVolume:type:
*
* The volume type: simple, spanned, striped, mirrored or raid5.
*/
g_object_class_install_property(
object_class,
PROP_LDM_VOLUME_TYPE,
g_param_spec_enum(
"type", "Type", "The volume type: simple, spanned, striped, "
"mirrored or raid5",
LDM_TYPE_VOLUME_TYPE, LDM_VOLUME_TYPE_SIMPLE,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMVolume:size:
*
* The volume size in sectors.
*/
g_object_class_install_property(
object_class,
PROP_LDM_VOLUME_SIZE,
g_param_spec_uint64(
"size", "Size", "The volume size in sectors",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMVolume:part-type:
*
* A 1-byte type descriptor of the volume's contents. This descriptor has
* the same meaning as for an MBR partition.
*/
g_object_class_install_property(
object_class,
PROP_LDM_VOLUME_PART_TYPE,
g_param_spec_uint(
"part-type", "Partition Type", "A 1-byte type descriptor of the "
"volume's contents. This descriptor has the same meaning as for "
"an MBR partition",
0, G_MAXUINT8, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMVolume:hint:
*
* A hint to Windows as to which drive letter to assign to this volume.
*/
g_object_class_install_property(
object_class,
PROP_LDM_VOLUME_HINT,
g_param_spec_string(
"hint", "Hint", "A hint to Windows as to which drive letter to "
"assign to this volume",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMVolume:chunk-size:
*
* The chunk size of a striped or raided volume.
*/
g_object_class_install_property(
object_class,
PROP_LDM_VOLUME_CHUNK_SIZE,
g_param_spec_uint64(
"chunk-size", "Chunk Size",
"The chunk size of a striped or raided volume",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
}
static void
ldm_volume_init(LDMVolume * const o)
{
o->priv = LDM_VOLUME_GET_PRIVATE(o);
bzero(o->priv, sizeof(*o->priv));
/* We don't have a trivial way to initialize this array to the correct size
* during parsing because we cut out components. We initialize it here and
* allow it to grow dynamically. */
o->priv->parts = g_array_new(FALSE, FALSE, sizeof(LDMPartition *));
g_array_set_clear_func(o->priv->parts, _unref_object);
}
/* We don't expose components externally */
typedef enum {
_COMPONENT_TYPE_STRIPED = 0x1,
_COMPONENT_TYPE_SPANNED = 0x2,
_COMPONENT_TYPE_RAID = 0x3
} _LDMComponentType;
struct _LDMComponent
{
guint32 id;
guint32 parent_id;
_LDMComponentType type;
uint32_t n_parts;
GArray *parts;
guint64 chunk_size;
guint32 n_columns;
};
static void
_cleanup_comp(gpointer const data)
{
struct _LDMComponent * const comp = (struct _LDMComponent *) data;
g_array_unref(comp->parts);
}
/* LDMPartition */
#define LDM_PARTITION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
((obj), LDM_TYPE_PARTITION, LDMPartitionPrivate))
struct _LDMPartitionPrivate
{
guint32 id;
guint32 parent_id;
gchar *name;
guint64 start;
guint64 vol_offset; /* Not exposed: only used for sanity checking */
guint64 size;
guint32 index; /* Not exposed directly: container array is sorted */
guint32 disk_id;
LDMDisk *disk;
};
G_DEFINE_TYPE(LDMPartition, ldm_partition, G_TYPE_OBJECT)
enum {
PROP_LDM_PARTITION_PROP0,
PROP_LDM_PARTITION_NAME,
PROP_LDM_PARTITION_START,
PROP_LDM_PARTITION_SIZE
};
static void
ldm_partition_get_property(GObject * const o, const guint property_id,
GValue * const value, GParamSpec * const pspec)
{
LDMPartition * const part = LDM_PARTITION(o);
LDMPartitionPrivate * const priv = part->priv;
switch (property_id) {
case PROP_LDM_PARTITION_NAME:
g_value_set_string(value, priv->name); break;
case PROP_LDM_PARTITION_START:
g_value_set_uint64(value, priv->start); break;
case PROP_LDM_PARTITION_SIZE:
g_value_set_uint64(value, priv->size); break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec);
}
}
EXPORT_PROP_STRING(partition, LDMPartition, name)
EXPORT_PROP_SCALAR(partition, LDMPartition, start, guint64)
EXPORT_PROP_SCALAR(partition, LDMPartition, size, guint64)
static void
ldm_partition_dispose(GObject * const object)
{
LDMPartition * const part_o = LDM_PARTITION(object);
LDMPartitionPrivate * const part = part_o->priv;
if (part->disk) { g_object_unref(part->disk); part->disk = NULL; }
}
static void
ldm_partition_finalize(GObject * const object)
{
LDMPartition * const part_o = LDM_PARTITION(object);
LDMPartitionPrivate * const part = part_o->priv;
g_free(part->name); part->name = NULL;
}
static void
ldm_partition_class_init(LDMPartitionClass * const klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->dispose = ldm_partition_dispose;
object_class->finalize = ldm_partition_finalize;
object_class->get_property = ldm_partition_get_property;
g_type_class_add_private(klass, sizeof(LDMPartitionPrivate));
/**
* LDMPartition:name:
*
* The name of the partition.
*/
g_object_class_install_property(
object_class,
PROP_LDM_PARTITION_NAME,
g_param_spec_string(
"name", "Name", "The name of the partition",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMPartition:start:
*
* The start sector of the partition.
*/
g_object_class_install_property(
object_class,
PROP_LDM_PARTITION_START,
g_param_spec_uint64(
"start", "Start", "The start sector of the partition",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMPartition:size:
*
* The size of the partition in sectors.
*/
g_object_class_install_property(
object_class,
PROP_LDM_PARTITION_SIZE,
g_param_spec_uint64(
"size", "Size", "The size of the partition in sectors",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
}
static void
ldm_partition_init(LDMPartition * const o)
{
o->priv = LDM_PARTITION_GET_PRIVATE(o);
bzero(o->priv, sizeof(*o->priv));
}
/* LDMDisk */
#define LDM_DISK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE \
((obj), LDM_TYPE_DISK, LDMDiskPrivate))
struct _LDMDiskPrivate
{
guint32 id;
gchar *name;
gchar *dgname;
guint64 data_start;
guint64 data_size;
guint64 metadata_start;
guint64 metadata_size;
uuid_t guid;
gchar *device; // NULL until device is found
};
G_DEFINE_TYPE(LDMDisk, ldm_disk, G_TYPE_OBJECT)
enum {
PROP_LDM_DISK_PROP0,
PROP_LDM_DISK_NAME,
PROP_LDM_DISK_GUID,
PROP_LDM_DISK_DEVICE,
PROP_LDM_DISK_DATA_START,
PROP_LDM_DISK_DATA_SIZE,
PROP_LDM_DISK_METADATA_START,
PROP_LDM_DISK_METADATA_SIZE
};
static void
ldm_disk_get_property(GObject * const o, const guint property_id,
GValue * const value, GParamSpec * const pspec)
{
const LDMDisk * const disk = LDM_DISK(o);
const LDMDiskPrivate * const priv = disk->priv;
switch (property_id) {
case PROP_LDM_DISK_NAME:
g_value_set_string(value, priv->name); break;
case PROP_LDM_DISK_GUID:
{
char guid_str[37];
uuid_unparse(priv->guid, guid_str);
g_value_set_string(value, guid_str);
}
break;
case PROP_LDM_DISK_DEVICE:
g_value_set_string(value, priv->device); break;
case PROP_LDM_DISK_DATA_START:
g_value_set_uint64(value, priv->data_start); break;
case PROP_LDM_DISK_DATA_SIZE:
g_value_set_uint64(value, priv->data_size); break;
case PROP_LDM_DISK_METADATA_START:
g_value_set_uint64(value, priv->metadata_start); break;
case PROP_LDM_DISK_METADATA_SIZE:
g_value_set_uint64(value, priv->metadata_size); break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec);
}
}
EXPORT_PROP_STRING(disk, LDMDisk, name)
EXPORT_PROP_GUID(disk, LDMDisk)
EXPORT_PROP_STRING(disk, LDMDisk, device)
EXPORT_PROP_SCALAR(disk, LDMDisk, data_start, guint64)
EXPORT_PROP_SCALAR(disk, LDMDisk, data_size, guint64)
EXPORT_PROP_SCALAR(disk, LDMDisk, metadata_start, guint64)
EXPORT_PROP_SCALAR(disk, LDMDisk, metadata_size, guint64)
static void
ldm_disk_finalize(GObject * const object)
{
LDMDisk * const disk_o = LDM_DISK(object);
LDMDiskPrivate * const disk = disk_o->priv;
g_free(disk->name); disk->name = NULL;
g_free(disk->dgname); disk->dgname = NULL;
g_free(disk->device); disk->device = NULL;
}
static void
ldm_disk_class_init(LDMDiskClass * const klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = ldm_disk_finalize;
object_class->get_property = ldm_disk_get_property;
g_type_class_add_private(klass, sizeof(LDMDiskPrivate));
/**
* LDMDisk:name:
*
* The name of the disk.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_NAME,
g_param_spec_string(
"name", "Name", "The name of the disk",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMDisk:guid:
*
* The GUID of the disk.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_GUID,
g_param_spec_string(
"guid", "GUID", "The GUID of the disk",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMDisk:device:
*
* The underlying device of this disk. This may be NULL if the disk is
* missing from the disk group.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_DEVICE,
g_param_spec_string(
"device", "Device", "The underlying device of this disk. This may "
"be NULL if the disk is missing from the disk group",
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMDisk:data-start:
*
* The start sector of the data area of the disk.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_DATA_START,
g_param_spec_uint64(
"data-start", "Data Start", "The start sector of the data area of "
"the disk",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMDisk:data-size:
*
* The size, in sectors, of the data area of the disk.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_DATA_SIZE,
g_param_spec_uint64(
"data-size", "Data Size", "The size, in sectors, of the data area "
"of the disk",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMDisk:metadata-start:
*
* The start sector of the metadata area of the disk.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_METADATA_START,
g_param_spec_uint64(
"metadata-start", "Metadata Start", "The start sector of the "
"metadata area of the disk",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
/**
* LDMDisk:metadata-size:
*
* The size, in sectors, of the metadata area of the disk.
*/
g_object_class_install_property(
object_class,
PROP_LDM_DISK_METADATA_SIZE,
g_param_spec_uint64(
"metadata-size", "Metadata Size", "The size, in sectors, of the "
"metadata area of the disk",
0, G_MAXUINT64, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS
)
);
}
static void
ldm_disk_init(LDMDisk * const o)
{
o->priv = LDM_DISK_GET_PRIVATE(o);
bzero(o->priv, sizeof(*o->priv));
}
static gboolean
_find_vmdb(const void * const config, const gchar * const path,
const guint secsize, const struct _vmdb ** const vmdb,
GError ** const err)
{
/* TOCBLOCK starts 2 sectors into config */
const struct _tocblock *tocblock = config + secsize * 2;
if (memcmp(tocblock->magic, "TOCBLOCK", 8) != 0) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Didn't find TOCBLOCK at config offset %" PRIX64,
UINT64_C(0x400));
return FALSE;
}
g_debug("TOCBLOCK: %s\n"
" Sequence1: %" PRIu64 "\n"
" Sequence2: %" PRIu64 "\n"
" Bitmap: %s\n"
" Flags1: %04" PRIo8 "\n"
" Start: %" PRIu64 "\n"
" Size: %" PRIu64 "\n"
" Flags2: %016" PRIo64 "\n"
" Bitmap: %s\n"
" Flags1: %04" PRIo8 "\n"
" Start: %" PRIu64 "\n"
" Size: %" PRIu64 "\n"
" Flags2: %016" PRIo64,
path,
be64toh(tocblock->seq1),
be64toh(tocblock->seq2),
tocblock->bitmap[0].name,
be16toh(tocblock->bitmap[0].flags1),
be64toh(tocblock->bitmap[0].start),
be64toh(tocblock->bitmap[0].size),
be64toh(tocblock->bitmap[0].flags2),
tocblock->bitmap[1].name,
be16toh(tocblock->bitmap[1].flags1),
be64toh(tocblock->bitmap[1].start),
be64toh(tocblock->bitmap[1].size),
be64toh(tocblock->bitmap[1].flags2));
/* Find the start of the DB */
*vmdb = NULL;
for (int i = 0; i < 2; i++) {
const struct _tocblock_bitmap *bitmap = &tocblock->bitmap[i];
if (strcmp(bitmap->name, "config") == 0) {
*vmdb = config + be64toh(tocblock->bitmap[i].start) * secsize;
break;
}
}
if (*vmdb == NULL) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"TOCBLOCK doesn't contain config bitmap");
return FALSE;
}
if (memcmp((*vmdb)->magic, "VMDB", 4) != 0) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Didn't find VMDB at config offset %lX",
(unsigned long int)((void *) (*vmdb) - config));
return FALSE;
}
g_debug("VMDB: %s\n"
" VBLK last: %" PRIu32 "\n"
" VBLK size: %" PRIu32 "\n"
" VBLK first offset: %" PRIu32 "\n"
" Version major: %" PRIu16 "\n"
" Version minor: %" PRIu16 "\n"
" Disk group GUID: %s\n"
" Committed sequence: %" PRIu64 "\n"
" Pending sequence: %" PRIu64 "\n"
" Committed volumes: %" PRIu32 "\n"
" Committed components: %" PRIu32 "\n"
" Committed partitions: %" PRIu32 "\n"
" Committed disks: %" PRIu32 "\n"
" Pending volumes: %" PRIu32 "\n"
" Pending components: %" PRIu32 "\n"
" Pending partitions: %" PRIu32 "\n"
" Pending disks: %" PRIu32,
path,
be32toh((*vmdb)->vblk_last),
be32toh((*vmdb)->vblk_size),
be32toh((*vmdb)->vblk_first_offset),
be16toh((*vmdb)->version_major),
be16toh((*vmdb)->version_minor),
(*vmdb)->disk_group_guid,
be64toh((*vmdb)->committed_seq),
be64toh((*vmdb)->pending_seq),
be32toh((*vmdb)->n_committed_vblks_vol),
be32toh((*vmdb)->n_committed_vblks_comp),
be32toh((*vmdb)->n_committed_vblks_part),
be32toh((*vmdb)->n_committed_vblks_disk),
be32toh((*vmdb)->n_pending_vblks_vol),
be32toh((*vmdb)->n_pending_vblks_comp),
be32toh((*vmdb)->n_pending_vblks_part),
be32toh((*vmdb)->n_pending_vblks_disk));
return TRUE;
}
static gboolean
_read_config(const int fd, const gchar * const path,
const guint secsize, const struct _privhead * const privhead,
void ** const config, GError ** const err)
{
/* Sanity check ldm_config_start and ldm_config_size */
struct stat stat;
if (fstat(fd, &stat) == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_IO,
"Unable to stat %s: %m", path);
return FALSE;
}
uint64_t size = stat.st_size;
if (S_ISBLK(stat.st_mode)) {
if (ioctl(fd, BLKGETSIZE64, &size) == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_IO,
"Unable to get block device size for %s: %m", path);
return FALSE;
}
}
const uint64_t config_start =
be64toh(privhead->ldm_config_start) * secsize;
const uint64_t config_size =
be64toh(privhead->ldm_config_size) * secsize;
if (config_start > size) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"LDM config start (%" PRIX64") is outside file in %s",
config_start, path);
return FALSE;
}
if (config_start + config_size > size) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"LDM config end (%" PRIX64 ") is outside file in %s",
config_start + config_size, path);
return FALSE;
}
*config = g_malloc(config_size);
size_t read = 0;
while (read < config_size) {
ssize_t in = pread(fd, *config + read, config_size - read,
config_start + read);
if (in == 0) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"%s contains invalid LDM metadata", path);
goto error;
}
if (in == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_IO,
"Error reading from %s: %m", path);
goto error;
}
read += in;
}
return TRUE;
error:
g_free(*config); *config = NULL;
return FALSE;
}
static gboolean
_read_privhead_off(const int fd, const gchar * const path,
const uint64_t ph_start,
struct _privhead * const privhead, GError **err)
{
size_t read = 0;
while (read < sizeof(*privhead)) {
ssize_t in = pread(fd, (char *) privhead + read,
sizeof(*privhead) - read,
ph_start + read);
if (in == 0) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"%s contains invalid LDM metadata", path);
return FALSE;
}
if (in == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_IO,
"Error reading from %s: %m", path);
return FALSE;
}
read += in;
}
if (memcmp(privhead->magic, "PRIVHEAD", 8) != 0) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"PRIVHEAD not found at offset %" PRIX64, ph_start);
return FALSE;
}
g_debug("PRIVHEAD: %s\n"
" Version Major: %" PRIu16 "\n"
" Version Minor: %" PRIu16 "\n"
" Disk GUID: %s\n"
" Disk Group GUID: %s\n"
" Logical Disk Start: %" PRIu64 "\n"
" Logical Disk Size: %" PRIu64 "\n"
" LDM Config Start: %" PRIu64 "\n"
" LDM Config Size: %" PRIu64,
path,
be16toh(privhead->version_major),
be16toh(privhead->version_minor),
privhead->disk_guid,
privhead->disk_group_guid,
be64toh(privhead->logical_disk_start),
be64toh(privhead->logical_disk_size),
be64toh(privhead->ldm_config_start),
be64toh(privhead->ldm_config_size));
return TRUE;
}
static gboolean
_read_privhead_mbr(const int fd, const gchar * const path, const guint secsize,
struct _privhead * const privhead, GError ** const err)
{
g_debug("Device %s uses MBR", path);
/* On an MBR disk, the first PRIVHEAD is in sector 6 */
return _read_privhead_off(fd, path, secsize * 6, privhead, err);
}
void _map_gpt_error(const int e, const gchar * const path, GError ** const err)
{
switch (-e) {
case GPT_ERROR_INVALID:
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"%s contains an invalid GPT header", path);
break;
case GPT_ERROR_READ:
g_set_error(err, LDM_ERROR, LDM_ERROR_IO,
"Error reading from %s: %m", path);
break;
case GPT_ERROR_INVALID_PART:
g_error("Request for invalid GPT partition");
default:
g_error("Unhandled GPT return value: %i\n", e);
}
}
static gboolean
_read_privhead_gpt(const int fd, const gchar * const path, const guint secsize,
struct _privhead * const privhead, GError ** const err)
{
g_debug("Device %s uses GPT", path);
int r;
gpt_handle_t *h;
r = gpt_open_secsize(fd, secsize, &h);
if (r < 0) {
_map_gpt_error(r, path, err);
return FALSE;
}
gpt_t gpt;
gpt_get_header(h, &gpt);
static const uuid_t LDM_METADATA = { 0xAA,0xC8,0x08,0x58,
0x8F,0x7E,
0xE0,0x42,
0x85,0xD2,
0xE1,0xE9,0x04,0x34,0xCF,0xB3 };
for (uint32_t i = 0; i < gpt.pte_array_len; i++) {
gpt_pte_t pte;
r = gpt_get_pte(h, 0, &pte);
if (r < 0) {
_map_gpt_error(r, path, err);
gpt_close(h);
return FALSE;
}
if (uuid_compare(pte.type, LDM_METADATA) == 0) {
/* PRIVHEAD is in the last LBA of the LDM metadata partition */
gpt_close(h);
return _read_privhead_off(fd, path, pte.last_lba * secsize,
privhead, err);
}
}
g_set_error(err, LDM_ERROR, LDM_ERROR_NOT_LDM,
"%s does not contain LDM metadata", path);
return FALSE;
}
static gboolean
_read_privhead(const int fd, const gchar * const path, const guint secsize,
struct _privhead * const privhead, GError ** const err)
{
// Whether the disk is MBR or GPT, we expect to find an MBR at the beginning
mbr_t mbr;
int r = mbr_read(fd, &mbr);
if (r < 0) {
switch (-r) {
case MBR_ERROR_INVALID:
g_set_error(err, LDM_ERROR, LDM_ERROR_NOT_LDM,
"Didn't detect a partition table");
return FALSE;
case MBR_ERROR_READ:
g_set_error(err, LDM_ERROR, LDM_ERROR_IO,
"Error reading from %s: %m", path);
return FALSE;
default:
g_error("Unhandled return value from mbr_read: %i", r);
}
}
switch (mbr.part[0].type) {
case MBR_PART_WINDOWS_LDM:
return _read_privhead_mbr(fd, path, secsize, privhead, err);
case MBR_PART_EFI_PROTECTIVE:
return _read_privhead_gpt(fd, path, secsize, privhead, err);
default:
g_set_error(err, LDM_ERROR, LDM_ERROR_NOT_LDM,
"%s does not contain LDM metadata", path);
return FALSE;
}
}
#define PARSE_VAR_INT(func_name, out_type) \
static gboolean \
func_name(const guint8 ** const var, out_type * const out, \
const gchar * const field, const gchar * const type, \
GError ** const err) \
{ \
guint8 i = **var; (*var)++; \
if (i > sizeof(*out)) { \
g_set_error(err, LDM_ERROR, LDM_ERROR_INTERNAL, \
"Found %hhu byte integer for %s:%s", i, field, type); \
return FALSE; \
} \
\
*out = 0; \
for(;i > 0; i--) { \
*out <<= 8; \
*out += **var; (*var)++; \
} \
\
return TRUE; \
}
PARSE_VAR_INT(_parse_var_int32, uint32_t)
PARSE_VAR_INT(_parse_var_int64, uint64_t)
static gchar *
_parse_var_string(const guint8 ** const var)
{
guint8 len = **var; (*var)++;
gchar *ret = g_malloc(len + 1);
memcpy(ret, *var, len); (*var) += len;
ret[len] = '\0';
return ret;
}
static void
_parse_var_skip(const guint8 ** const var)
{
guint8 len = **var; (*var)++;
(*var) += len;
}
static gboolean
_parse_vblk_vol(const guint8 revision, const guint16 flags,
const guint8 * vblk, LDMVolumePrivate * const vol,
GError ** const err)
{
if (revision != 5) {
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Unsupported volume VBLK revision %hhu", revision);
return FALSE;
}
if (!_parse_var_int32(&vblk, &vol->id, "id", "volume", err))
return FALSE;
vol->name = _parse_var_string(&vblk);
/* Volume type: 'gen' or 'raid5'. We parse this elsewhere */
_parse_var_skip(&vblk);
/* Unknown. N.B. Documentation lists this as a single zero, but I have
* observed it to have the variable length string value: '8000000000000000'
*/
_parse_var_skip(&vblk);
/* Volume state */
vblk += 14;
vol->_int_type = *(uint8_t *)vblk; vblk += 1;
switch(vol->_int_type) {
case _VOLUME_TYPE_GEN:
case _VOLUME_TYPE_RAID5:
break;
default:
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Unsupported volume VBLK type %u", vol->_int_type);
return FALSE;
}
/* Unknown */
vblk += 1;
/* Volume number */
vblk += 1;
/* Zeroes */
vblk += 3;
/* Flags */
vol->flags = *(uint8_t *)vblk; vblk += 1;
if (!_parse_var_int32(&vblk, &vol->_n_comps, "n_children", "volume", err))
return FALSE;
/* Commit id */
vblk += 8;
/* Id? */
vblk += 8;
if (!_parse_var_int64(&vblk, &vol->size, "size", "volume", err))
return FALSE;
/* Zeroes */
vblk += 4;
vol->part_type = *((uint8_t *)vblk); vblk++;
/* Volume id */
vblk += 16;
if (flags & 0x08) vol->id1 = _parse_var_string(&vblk);
if (flags & 0x20) vol->id2 = _parse_var_string(&vblk);
if (flags & 0x80 && !_parse_var_int64(&vblk, &vol->size2,
"size2", "volume", err))
return FALSE;
if (flags & 0x02) vol->hint = _parse_var_string(&vblk);
g_debug("Volume: %s\n"
" ID: %" PRIu32 "\n"
" Type: %" PRIi32 "\n"
" Flags: %" PRIu8 "\n"
" Children: %" PRIu32 "\n"
" Size: %" PRIu64 "\n"
" Partition Type: %" PRIu8 "\n"
" ID1: %s\n"
" ID2: %s\n"
" Size2: %" PRIu64 "\n"
" Hint: %s",
vol->name,
vol->id,
vol->_int_type,
vol->flags,
vol->_n_comps,
vol->size,
vol->part_type,
vol->id1,
vol->id2,
vol->size2,
vol->hint);
return TRUE;
}
static gboolean
_parse_vblk_comp(const guint8 revision, const guint16 flags,
const guint8 *vblk, struct _LDMComponent * const comp,
GError ** const err)
{
if (revision != 3) {
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Unsupported component VBLK revision %hhu", revision);
return FALSE;
}
if (!_parse_var_int32(&vblk, &comp->id, "id", "volume", err)) return FALSE;
/* Name */
_parse_var_skip(&vblk);
/* Volume state */
_parse_var_skip(&vblk);
comp->type = *((uint8_t *) vblk); vblk++;
switch (comp->type) {
case _COMPONENT_TYPE_STRIPED:
case _COMPONENT_TYPE_SPANNED:
case _COMPONENT_TYPE_RAID:
break;
default:
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Component VBLK OID=%u has unsupported type %u",
comp->id, comp->type);
return FALSE;
}
/* Zeroes */
vblk += 4;
if (!_parse_var_int32(&vblk, &comp->n_parts, "n_parts", "component", err))
return FALSE;
comp->parts = g_array_sized_new(FALSE, FALSE,
sizeof(LDMPartition *), comp->n_parts);
/* All members of the component's partition array will be passed to the
* parent volume's partition array after initial parsing. We don't unref its
* objects on cleanup, which makes it cleaner to bulk-transfer them. */
/* Log Commit ID */
vblk += 8;
/* Zeroes */
vblk += 8;
if (!_parse_var_int32(&vblk, &comp->parent_id,
"parent_id", "component", err))
return FALSE;
/* Zeroes */
vblk += 1;
if (flags & 0x10) {
if (!_parse_var_int64(&vblk, &comp->chunk_size,
"chunk_size", "component", err))
return FALSE;
if (!_parse_var_int32(&vblk, &comp->n_columns,
"n_columns", "component", err))
return FALSE;
}
g_debug("Component:\n"
" ID: %" PRIu32 "\n"
" Parent ID: %" PRIu32 "\n"
" Type: %" PRIu32 "\n"
" Parts: %" PRIu32 "\n"
" Chunk Size: %" PRIu64 "\n"
" Columns: %" PRIu32,
comp->id,
comp->parent_id,
comp->type,
comp->n_parts,
comp->chunk_size,
comp->n_columns);
return TRUE;
}
static gboolean
_parse_vblk_part(const guint8 revision, const guint16 flags,
const guint8 *vblk, LDMPartitionPrivate * const part,
GError ** const err)
{
if (revision != 3) {
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Unsupported partition VBLK revision %hhu", revision);
return FALSE;
}
if (!_parse_var_int32(&vblk, &part->id, "id", "volume", err)) return FALSE;
part->name = _parse_var_string(&vblk);
/* Zeroes */
vblk += 4;
/* Log Commit ID */
vblk += 8;
part->start = be64toh(*(uint64_t *)vblk); vblk += 8;
part->vol_offset = be64toh(*(uint64_t *)vblk); vblk += 8;
if (!_parse_var_int64(&vblk, &part->size, "size", "partition", err))
return FALSE;
if (!_parse_var_int32(&vblk, &part->parent_id,
"parent_id", "partition", err))
return FALSE;
if (!_parse_var_int32(&vblk, &part->disk_id, "disk_id", "partition", err))
return FALSE;
if (flags & 0x08) {
if (!_parse_var_int32(&vblk, &part->index, "index", "partition", err))
return FALSE;
}
g_debug("Partition: %s\n"
" ID: %" PRIu32 "\n"
" Parent ID: %" PRIu32 "\n"
" Disk ID: %" PRIu32 "\n"
" Index: %" PRIu32 "\n"
" Start: %" PRIu64 "\n"
" Vol Offset: %" PRIu64 "\n"
" Size: %" PRIu64,
part->name,
part->id,
part->parent_id,
part->disk_id,
part->index,
part->start,
part->vol_offset,
part->size);
return TRUE;
}
static gboolean
_parse_vblk_disk(const guint8 revision, const guint16 flags,
const guint8 *vblk, LDMDiskPrivate * const disk,
GError ** const err)
{
if (!_parse_var_int32(&vblk, &disk->id, "id", "volume", err)) return FALSE;
disk->name = _parse_var_string(&vblk);
if (revision == 3) {
char *guid = _parse_var_string(&vblk);
if (uuid_parse(guid, disk->guid) == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Disk %u has invalid guid: %s", disk->id, guid);
g_free(guid);
return FALSE;
}
g_free(guid);
/* No need to parse rest of structure */
}
else if (revision == 4) {
memcpy(&disk->guid, vblk, 16);
/* No need to parse rest of structure */
}
else {
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Unsupported disk VBLK revision %hhu", revision);
return FALSE;
}
g_debug("Disk: %s\n"
" ID: %u\n"
" GUID: " UUID_FMT,
disk->name,
disk->id,
UUID_VALS(disk->guid));
return TRUE;
}
static gboolean
_parse_vblk_disk_group(const guint8 revision, const guint16 flags,
const guint8 *vblk, LDMDiskGroupPrivate * const dg,
GError ** const err)
{
if (revision != 3 && revision != 4) {
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Unsupported disk VBLK revision %hhu", revision);
return FALSE;
}
if (!_parse_var_int32(&vblk, &dg->id, "id", "disk group", err))
return FALSE;
dg->name = _parse_var_string(&vblk);
/* No need to parse rest of structure */
g_debug("Disk Group: %s\n"
" ID: %u",
dg->name,
dg->id);
return TRUE;
}
struct _spanned_rec {
uint32_t record_id;
uint16_t entries_total;
uint16_t entries_found;
int offset;
char data[];
};
static gboolean
_parse_vblk(const void * data, LDMDiskGroup * const dg_o, GArray *comps,
const gchar * const path, const int offset,
GError ** const err)
{
LDMDiskGroupPrivate * const dg = dg_o->priv;
const struct _vblk_rec_head * const rec_head = data;
const guint8 type = rec_head->type & 0x0F;
const guint8 revision = (rec_head->type & 0xF0) >> 4;
data += sizeof(struct _vblk_rec_head);
switch (type) {
case 0x00:
/* Blank VBLK */
break;
case 0x01:
{
LDMVolume * const vol =
LDM_VOLUME(g_object_new(LDM_TYPE_VOLUME, NULL));
g_array_append_val(dg->vols, vol);
if (!_parse_vblk_vol(revision, rec_head->flags, data, vol->priv, err))
return FALSE;
break;
}
case 0x02:
{
g_array_set_size(comps, comps->len + 1);
struct _LDMComponent *comp =
(struct _LDMComponent *) comps->data + comps->len - 1;
if (!_parse_vblk_comp(revision, rec_head->flags, data, comp, err))
return FALSE;
break;
}
case 0x03:
{
LDMPartition * const part =
LDM_PARTITION(g_object_new(LDM_TYPE_PARTITION, NULL));
g_array_append_val(dg->parts, part);
if (!_parse_vblk_part(revision, rec_head->flags, data, part->priv, err))
return FALSE;
break;
}
case 0x04:
{
LDMDisk * const disk =
LDM_DISK(g_object_new(LDM_TYPE_DISK, NULL));
g_array_append_val(dg->disks, disk);
if (!_parse_vblk_disk(revision, rec_head->flags, data, disk->priv, err))
return FALSE;
break;
}
case 0x05:
{
if (!_parse_vblk_disk_group(revision, rec_head->flags, data, dg, err))
return FALSE;
break;
}
default:
g_set_error(err, LDM_ERROR, LDM_ERROR_NOTSUPPORTED,
"Unknown VBLK type %hhi in %s at config offset %X",
type, path, offset);
return FALSE;
}
return TRUE;
}
gint
_cmp_component_parts(gconstpointer a, gconstpointer b)
{
const LDMPartition * const ao = LDM_PARTITION(*(LDMPartition **)a);
const LDMPartition * const bo = LDM_PARTITION(*(LDMPartition **)b);
if (ao->priv->index < bo->priv->index) return -1;
if (ao->priv->index > bo->priv->index) return 1;
return 0;
}
static gboolean
_parse_vblks(const void * const config, const gchar * const path,
const struct _vmdb * const vmdb,
LDMDiskGroup * const dg_o, GError ** const err)
{
LDMDiskGroupPrivate * const dg = dg_o->priv;
GArray *spanned = g_array_new(FALSE, FALSE, sizeof(gpointer));
g_array_set_clear_func(spanned, _free_pointer);
dg->sequence = be64toh(vmdb->committed_seq);
guint32 n_disks = be32toh(vmdb->n_committed_vblks_disk);
guint32 n_parts = be32toh(vmdb->n_committed_vblks_part);
guint32 n_vols = be32toh(vmdb->n_committed_vblks_vol);
guint32 n_comps = be32toh(vmdb->n_committed_vblks_comp);
dg->disks = g_array_sized_new(FALSE, FALSE,
sizeof(LDMDisk *), n_disks);
dg->parts = g_array_sized_new(FALSE, FALSE,
sizeof(LDMPartition *), n_parts);
dg->vols = g_array_sized_new(FALSE, FALSE,
sizeof(LDMVolume *), n_vols);
g_array_set_clear_func(dg->disks, _unref_object);
g_array_set_clear_func(dg->parts, _unref_object);
g_array_set_clear_func(dg->vols, _unref_object);
GArray *comps = g_array_sized_new(FALSE, TRUE,
sizeof(struct _LDMComponent), n_comps);
g_array_set_clear_func(comps, _cleanup_comp);
const guint16 vblk_size = be32toh(vmdb->vblk_size);
const guint16 vblk_data_size = vblk_size - sizeof(struct _vblk_head);
const void *vblk = (void *)vmdb + be32toh(vmdb->vblk_first_offset);
for(;;) {
const int offset = vblk - config;
const struct _vblk_head * const head = vblk;
if (memcmp(head->magic, "VBLK", 4) != 0) break;
/* Sanity check the header */
if (be16toh(head->entries_total) > 0 &&
be16toh(head->entry) >= be16toh(head->entries_total))
{
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"VBLK entry %u has entry (%hu) > total entries (%hu)",
be32toh(head->seq), be16toh(head->entry),
be16toh(head->entries_total));
goto error;
}
vblk += sizeof(struct _vblk_head);
/* Check for a spanned record */
if (be16toh(head->entries_total) > 1) {
/* Look for an existing record */
gboolean found = FALSE;
for (guint i = 0; i < spanned->len; i++) {
struct _spanned_rec * const r =
g_array_index(spanned, struct _spanned_rec *, i);
if (r->record_id == head->record_id) {
r->entries_found++;
memcpy(&r->data[be16toh(head->entry) * vblk_data_size],
vblk, vblk_data_size);
found = TRUE;
break;
}
}
if (!found) {
struct _spanned_rec * const r =
g_malloc0(offsetof(struct _spanned_rec, data) +
head->entries_total * vblk_data_size);
g_array_append_val(spanned, r);
r->record_id = head->record_id;
r->entries_total = be16toh(head->entries_total);
r->entries_found = 1;
r->offset = offset;
memcpy(&r->data[be16toh(head->entry) * vblk_data_size],
vblk, vblk_data_size);
}
}
else {
if (!_parse_vblk(vblk, dg_o, comps, path, offset, err)) goto error;
}
vblk += vblk_data_size;
}
for (guint i = 0; i < spanned->len; i++) {
struct _spanned_rec * const rec =
g_array_index(spanned, struct _spanned_rec *, i);
if (rec->entries_found != rec->entries_total) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Expected to find %hu entries for record %u, but "
"found %hu", rec->entries_total, rec->record_id,
rec->entries_found);
goto error;
}
if (!_parse_vblk(rec->data, dg_o, comps, path, rec->offset, err))
goto error;
}
g_array_unref(spanned); spanned = NULL;
if (dg->disks->len != n_disks) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Expected %u disk VBLKs, but found %u",
n_disks, dg->disks->len);
goto error;
}
if (comps->len != n_comps) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Expected %u component VBLKs, but found %u",
n_comps, comps->len);
goto error;
}
if (dg->parts->len != n_parts) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Expected %u partition VBLKs, but found %u",
n_parts, dg->parts->len);
goto error;
}
if (dg->vols->len != n_vols) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Expected %u volume VBLKs, but found %u",
n_vols, dg->vols->len);
goto error;
}
for (guint32 i = 0; i < n_parts; i++) {
LDMPartition * const part_o =
g_array_index(dg->parts, LDMPartition *, i);
LDMPartitionPrivate * const part = part_o->priv;
/* Look for the underlying disk for this partition */
for (guint32 j = 0; j < n_disks; j++) {
LDMDisk * const disk_o =
g_array_index(dg->disks, LDMDisk *, j);
LDMDiskPrivate * const disk = disk_o->priv;
if (disk->id == part->disk_id) {
part->disk = disk_o;
g_object_ref(disk_o);
break;
}
}
if (part->disk == NULL) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Partition %u references unknown disk %u",
part->id, part->disk_id);
goto error;
}
/* Look for the parent component */
gboolean parent_found = FALSE;
for (guint j = 0; j < comps->len; j++) {
struct _LDMComponent * const comp =
(struct _LDMComponent *)comps->data + j;
if (comp->id == part->parent_id) {
g_array_append_val(comp->parts, part_o);
g_object_ref(part_o);
parent_found = TRUE;
break;
}
}
if (!parent_found) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Didn't find parent component %u for partition %u",
part->parent_id, part->id);
goto error;
}
}
for (guint32 i = 0; i < n_comps; i++) {
struct _LDMComponent * const comp =
(struct _LDMComponent *)comps->data + i;
if (comp->parts->len != comp->n_parts) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Component %u expected %u partitions, but found %u",
comp->id, comp->n_parts, comp->parts->len);
goto error;
}
if (comp->n_columns > 0 && comp->n_columns != comp->parts->len) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Component %u n_columns %u doesn't match number of "
"partitions %u",
comp->id, comp->n_columns, comp->parts->len);
goto error;
}
/* Sort partitions into index order */
g_array_sort(comp->parts, _cmp_component_parts);
gboolean parent_found = FALSE;
for (guint32 j = 0; j < n_vols; j++) {
LDMVolume * const vol_o = g_array_index(dg->vols, LDMVolume *, j);
LDMVolumePrivate * const vol = vol_o->priv;
if (vol->id == comp->parent_id) {
parent_found = TRUE;
g_array_append_vals(vol->parts,
comp->parts->data, comp->parts->len);
vol->chunk_size = comp->chunk_size;
vol->_n_comps_i++;
switch (comp->type) {
case _COMPONENT_TYPE_SPANNED:
if (vol->_int_type != _VOLUME_TYPE_GEN) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Unsupported configuration: SPANNED "
"component has parent volume with type %u",
vol->_int_type);
goto error;
}
if (vol->_n_comps > 1) {
vol->type = LDM_VOLUME_TYPE_MIRRORED;
} else if (comp->n_parts > 1) {
vol->type = LDM_VOLUME_TYPE_SPANNED;
} else {
vol->type = LDM_VOLUME_TYPE_SIMPLE;
}
break;
case _COMPONENT_TYPE_STRIPED:
if (vol->_int_type != _VOLUME_TYPE_GEN) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Unsupported configuration: STRIPED "
"component has parent volume with type %u",
vol->_int_type);
goto error;
}
if (vol->_n_comps != 1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Unsupported configuration: STRIPED "
"component has parent volume with %u "
"child components", vol->_n_comps);
goto error;
}
vol->type = LDM_VOLUME_TYPE_STRIPED;
break;
case _COMPONENT_TYPE_RAID:
if (vol->_int_type != _VOLUME_TYPE_RAID5) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Unsupported configuration: RAID "
"component has parent volume with type %u",
vol->_int_type);
goto error;
}
if (vol->_n_comps != 1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Unsupported configuration: RAID "
"component has parent volume with %u "
"child components", vol->_n_comps);
goto error;
}
vol->type = LDM_VOLUME_TYPE_RAID5;
break;
default:
/* Should be impossible */
g_error("Unexpected component type %u", comp->type);
}
break;
}
}
if (!parent_found) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Didn't find parent volume %u for component %u",
comp->parent_id, comp->id);
goto error;
}
}
for (guint32 i = 0; i < n_vols; i++) {
LDMVolume * const vol_o = g_array_index(dg->vols, LDMVolume *, i);
LDMVolumePrivate * const vol = vol_o->priv;
if (vol->_n_comps_i != vol->_n_comps) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Volume %u expected %u components, but only found %u",
vol->id, vol->_n_comps, vol->_n_comps_i);
goto error;
}
vol->dgname = g_strdup(dg->name);
}
for (guint32 i = 0; i < n_disks; i++) {
LDMDisk * const disk_o = g_array_index(dg->disks, LDMDisk *, i);
LDMDiskPrivate * const disk = disk_o->priv;
disk->dgname = g_strdup(dg->name);
}
g_array_unref(comps);
return TRUE;
error:
if (spanned) g_array_unref(spanned);
if (comps) g_array_unref(comps);
return FALSE;
}
gboolean
ldm_add(LDM * const o, const gchar * const path, GError ** const err)
{
const int fd = open(path, O_RDONLY);
if (fd == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_IO,
"Error opening %s for reading: %m", path);
return FALSE;
}
int secsize;
if (ioctl(fd, BLKSSZGET, &secsize) == -1) {
g_warning("Unable to determine sector size of %s. Assuming 512 byte "
"sectors", path);
secsize = 512;
}
return ldm_add_fd(o, fd, secsize, path, err);
}
gboolean
ldm_add_fd(LDM * const o, const int fd, const guint secsize,
const gchar * const path, GError ** const err)
{
GArray *disk_groups = o->priv->disk_groups;
/* The GObject documentation states quite clearly that method calls on an
* object which has been disposed should *not* result in an error. Seems
* weird, but...
*/
if (!disk_groups) return TRUE;
void *config = NULL;
struct _privhead privhead;
if (!_read_privhead(fd, path, secsize, &privhead, err)) goto error;
if (!_read_config(fd, path, secsize, &privhead, &config, err)) goto error;
const struct _vmdb *vmdb;
if (!_find_vmdb(config, path, secsize, &vmdb, err)) goto error;
uuid_t disk_guid;
uuid_t disk_group_guid;
if (uuid_parse(privhead.disk_guid, disk_guid) == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"PRIVHEAD contains invalid GUID for disk: %s",
privhead.disk_guid);
goto error;
}
if (uuid_parse(privhead.disk_group_guid, disk_group_guid) == -1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"PRIVHEAD contains invalid GUID for disk group: %s",
privhead.disk_group_guid);
goto error;
}
LDMDiskGroup *dg_o = NULL;
LDMDiskGroupPrivate *dg = NULL;
for (guint i = 0; i < disk_groups->len; i++) {
LDMDiskGroup *c = g_array_index(disk_groups,
LDMDiskGroup *, i);
if (uuid_compare(disk_group_guid, c->priv->guid) == 0) {
dg_o = c;
}
}
if (dg_o == NULL) {
dg_o = LDM_DISK_GROUP(g_object_new(LDM_TYPE_DISK_GROUP, NULL));
dg = dg_o->priv;
uuid_copy(dg_o->priv->guid, disk_group_guid);
g_debug("Found new disk group: " UUID_FMT, UUID_VALS(disk_group_guid));
if (!_parse_vblks(config, path, vmdb, dg_o, err)) {
g_object_unref(dg_o); dg_o = NULL;
goto error;
}
g_array_append_val(disk_groups, dg_o);
} else {
dg = dg_o->priv;
/* Check this disk is consistent with other disks */
uint64_t committed = be64toh(vmdb->committed_seq);
if (committed != dg->sequence) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INCONSISTENT,
"Members of disk group " UUID_FMT " are inconsistent: "
"disk %s has committed sequence %" PRIu64 ", "
"group has committed sequence %" PRIu64,
UUID_VALS(disk_group_guid),
path, committed, dg->sequence);
goto error;
}
}
/* Find the disk VBLK for the current disk and add additional information
* from PRIVHEAD */
for (guint i = 0; i < dg->disks->len; i++) {
LDMDisk * const disk_o = g_array_index(dg->disks, LDMDisk *, i);
LDMDiskPrivate * const disk = disk_o->priv;
if (uuid_compare(disk_guid, disk->guid) == 0) {
disk->device = g_strdup(path);
disk->data_start = be64toh(privhead.logical_disk_start);
disk->data_size = be64toh(privhead.logical_disk_size);
disk->metadata_start = be64toh(privhead.ldm_config_start);
disk->metadata_size = be64toh(privhead.ldm_config_size);
break;
}
}
g_free(config);
close(fd);
return TRUE;
error:
g_free(config);
close(fd);
return FALSE;
}
LDM *
ldm_new()
{
LDM *ldm = LDM_CAST(g_object_new(LDM_TYPE, NULL));
ldm->priv->disk_groups = g_array_sized_new(FALSE, FALSE,
sizeof (LDMDiskGroup *), 1);
g_array_set_clear_func(ldm->priv->disk_groups, _unref_object);
return ldm;
}
GArray *
ldm_get_disk_groups(LDM * const o)
{
if (o->priv->disk_groups) g_array_ref(o->priv->disk_groups);
return o->priv->disk_groups;
}
GArray *
ldm_disk_group_get_volumes(LDMDiskGroup * const o)
{
if (o->priv->vols) g_array_ref(o->priv->vols);
return o->priv->vols;
}
GArray *
ldm_disk_group_get_partitions(LDMDiskGroup * const o)
{
if (o->priv->parts) g_array_ref(o->priv->parts);
return o->priv->parts;
}
GArray *
ldm_disk_group_get_disks(LDMDiskGroup * const o)
{
if (o->priv->disks) g_array_ref(o->priv->disks);
return o->priv->disks;
}
GArray *
ldm_volume_get_partitions(LDMVolume * const o)
{
if (o->priv->parts) g_array_ref(o->priv->parts);
return o->priv->parts;
}
LDMDisk *
ldm_partition_get_disk(LDMPartition * const o)
{
if (o->priv->disk) g_object_ref(o->priv->disk);
return o->priv->disk;
}
static GString *
_dm_vol_name(const LDMVolumePrivate * const vol)
{
GString * r = g_string_new("");
g_string_printf(r, "ldm_vol_%s_%s", vol->dgname, vol->name);
return r;
}
/* We catch log messages generated by device mapper with errno != 0 and store
* them here */
static int _dm_err_last_level = 0;
static const char *_dm_err_last_file = NULL;
static int _dm_err_last_line = 0;
static int _dm_err_last_errno = 0;
static char *_dm_err_last_msg = NULL;
struct dm_target {
guint64 start;
guint64 size;
const gchar * type;
GString *params;
};
static struct dm_tree *
_get_device_tree(GError **err)
{
struct dm_tree *tree;
tree = dm_tree_create();
if (!tree) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_tree_create: %s", _dm_err_last_msg);
return NULL;
}
struct dm_task *task;
task = dm_task_create(DM_DEVICE_LIST);
if (!task) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_task_create: %s", _dm_err_last_msg);
goto error;
}
if (!dm_task_run(task)) {
g_set_error_literal(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
_dm_err_last_msg);
goto error;
}
struct dm_names *names;
names = dm_task_get_names(task);
if (!task) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_task_get_names: %s", _dm_err_last_msg);
goto error;
}
if (names->dev != 0) {
for (;;) {
if (!dm_tree_add_dev(tree, major(names->dev), minor(names->dev))) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_tree_add_dev: %s", _dm_err_last_msg);
goto error;
}
if (names->next == 0) break;
names = (struct dm_names *)((char *)names + names->next);
}
}
dm_task_destroy(task);
return tree;
error:
if (tree) dm_tree_free(tree);
if (task) dm_task_destroy(task);
return NULL;
}
static struct dm_tree_node *
_walk_tree(const struct dm_tree * const tree,
const struct dm_tree_node * const node,
const char * const search)
{
void *handle = NULL;
struct dm_tree_node *child;
for (;;) {
child = dm_tree_next_child(&handle, node, 0);
if (!child) return NULL;
const char * const name = dm_tree_node_get_name(child);
if (!name || *name == '\0') continue;
if (g_strcmp0(search, name) == 0) return child;
if (dm_tree_node_num_children(child, 0) > 0) {
struct dm_tree_node * const r_search =
_walk_tree(tree, child, search);
if (r_search) return r_search;
}
}
}
static struct dm_tree_node *
_find_device_tree_node(struct dm_tree * const tree,
const char * const name)
{
const struct dm_tree_node * const root = dm_tree_find_node(tree, 0, 0);
return _walk_tree(tree, root, name);
}
gboolean
_dm_create(const gchar * const name, uint32_t udev_cookie,
const guint n_targets, const struct dm_target * const targets,
GError ** const err)
{
gboolean r = TRUE;
struct dm_task * const task = dm_task_create(DM_DEVICE_CREATE);
if (!task) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_task_create(DM_DEVICE_CREATE) failed: %s",
_dm_err_last_msg);
return FALSE;
}
if (!dm_task_set_name(task, name)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"DM_DEVICE_CREATE: dm_task_set_name(%s) failed: %s",
name, _dm_err_last_msg);
r = FALSE; goto out;
}
for (guint i = 0; i < n_targets; i++) {
const struct dm_target * const target = &targets[i];
if (!dm_task_add_target(task, target->start, target->size,
target->type, target->params->str))
{
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"DM_DEVICE_CREATE: "
"dm_task_add_target(%s, %" PRIu64 ", %" PRIu64 ", "
"%s, %s) failed: %s",
name, target->start, target->size,
target->type, target->params->str, _dm_err_last_msg);
r = FALSE; goto out;
}
}
if (!dm_task_set_cookie(task, &udev_cookie, 0)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"DM_DEVICE_CREATE: dm_task_set_cookie(%08X) failed: %s",
udev_cookie, _dm_err_last_msg);
r = FALSE; goto out;
}
if (!dm_task_run(task)) {
g_set_error_literal(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
_dm_err_last_msg);
r = FALSE; goto out;
}
out:
dm_task_destroy(task);
return r;
}
gboolean
_dm_remove(const gchar * const name, uint32_t udev_cookie, GError ** const err)
{
gboolean r = TRUE;
struct dm_task * const task = dm_task_create(DM_DEVICE_REMOVE);
if (!task) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_task_create(DM_DEVICE_REMOVE) failed: %s",
_dm_err_last_msg);
r = FALSE; goto out;
}
if (!dm_task_set_name(task, name)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"DM_DEVICE_REMOVE: dm_task_set_name(%s) failed: %s",
name, _dm_err_last_msg);
r = FALSE; goto out;
}
if (udev_cookie && !dm_task_set_cookie(task, &udev_cookie, 0)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"DM_DEVICE_REMOVE: dm_task_set_cookie(%08X) failed: %s",
udev_cookie, _dm_err_last_msg);
r = FALSE; goto out;
}
/* If a remove operation fails, try again in case it was only opened
* transiently. */
dm_task_retry_remove(task);
if (!dm_task_run(task)) {
if (_dm_err_last_errno == EBUSY) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"Device is still mounted");
} else {
g_set_error_literal(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
_dm_err_last_msg);
}
r = FALSE; goto out;
}
out:
dm_task_destroy(task);
return r;
}
static GString *
_dm_create_part(const LDMPartitionPrivate * const part, uint32_t cookie,
GError ** const err)
{
const LDMDiskPrivate * const disk = part->disk->priv;
if (!disk->device) {
g_set_error(err, LDM_ERROR, LDM_ERROR_MISSING_DISK,
"Disk %s required by partition %s is missing",
disk->name, part->name);
return NULL;
}
struct dm_target target;
target.start = 0;
target.size = part->size;
target.type = "linear";
target.params = g_string_new("");
g_string_printf(target.params, "%s %" PRIu64,
disk->device, disk->data_start + part->start);
/* Ensure we sanitise table names */
char * dgname_esc =
g_uri_escape_string(disk->dgname,
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT,
FALSE);
char * partname_esc =
g_uri_escape_string(part->name,
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT,
FALSE);
GString *name = g_string_new("");
g_string_printf(name, "ldm_part_%s_%s", dgname_esc, partname_esc);
g_free(dgname_esc);
g_free(partname_esc);
if (!_dm_create(name->str, cookie, 1, &target, err)) {
g_string_free(name, TRUE);
name = NULL;
}
g_string_free(target.params, TRUE);
return name;
}
static GString *
_dm_create_spanned(const LDMVolumePrivate * const vol, GError ** const err)
{
static GString *name = NULL;
guint i = 0;
struct dm_target *targets = g_malloc(sizeof(*targets) * vol->parts->len);
uint64_t pos = 0;
for (; i < vol->parts->len; i++) {
const LDMPartition * const part_o =
g_array_index(vol->parts, const LDMPartition *, i);
const LDMPartitionPrivate * const part = part_o->priv;
const LDMDiskPrivate * const disk = part->disk->priv;
if (!disk->device) {
g_set_error(err, LDM_ERROR, LDM_ERROR_MISSING_DISK,
"Disk %s required by spanned volume %s is missing",
disk->name, vol->name);
goto out;
}
/* Sanity check: current position from adding up sizes of partitions
* should equal the volume offset of the partition */
if (pos != part->vol_offset) {
g_set_error(err, LDM_ERROR, LDM_ERROR_INVALID,
"Partition volume offset does not match sizes of "
"preceding partitions");
goto out;
}
struct dm_target *target = &targets[i];
target->start = pos;
target->size = part->size;
target->type = "linear";
target->params = g_string_new("");
g_string_append_printf(target->params, "%s %" PRIu64,
disk->device,
disk->data_start + part->start);
pos += part->size;
}
uint32_t cookie;
if (!dm_udev_create_cookie(&cookie)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_udev_create_cookie: %s", _dm_err_last_msg);
goto out;
}
name = _dm_vol_name(vol);
if (!_dm_create(name->str, cookie, vol->parts->len, targets, err)) {
g_string_free(name, TRUE);
name = NULL;
}
dm_udev_wait(cookie);
out:
for (; i > 0; i--) {
struct dm_target *target = &targets[i-1];
g_string_free(target->params, TRUE);
}
g_free(targets);
return name;
}
static GString *
_dm_create_striped(const LDMVolumePrivate * const vol, GError ** const err)
{
static GString *name = NULL;
struct dm_target target;
target.start = 0;
target.size = vol->size;
target.type = "striped";
target.params = g_string_new("");
g_string_printf(target.params, "%" PRIu32 " %" PRIu64,
vol->parts->len, vol->chunk_size);
for (guint i = 0; i < vol->parts->len; i++) {
const LDMPartition * const part_o =
g_array_index(vol->parts, const LDMPartition *, i);
const LDMPartitionPrivate * const part = part_o->priv;
const LDMDiskPrivate * const disk = part->disk->priv;
if (!disk->device) {
g_set_error(err, LDM_ERROR, LDM_ERROR_MISSING_DISK,
"Disk %s required by striped volume %s is missing",
disk->name, vol->name);
goto out;
}
g_string_append_printf(target.params, " %s %" PRIu64,
disk->device,
disk->data_start + part->start);
}
uint32_t cookie;
if (!dm_udev_create_cookie(&cookie)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_udev_create_cookie: %s", _dm_err_last_msg);
goto out;
}
name = _dm_vol_name(vol);
if (!_dm_create(name->str, cookie, 1, &target, err)) {
g_string_free(name, TRUE);
name = NULL;
}
dm_udev_wait(cookie);
out:
g_string_free(target.params, TRUE);
return name;
}
static GString *
_dm_create_mirrored(const LDMVolumePrivate * const vol, GError ** const err)
{
GString *name = NULL;
struct dm_target target;
target.start = 0;
target.size = vol->size;
target.type = "raid";
target.params = g_string_new("");
g_string_printf(target.params, "raid1 1 128 %u", vol->parts->len);
GArray * devices = g_array_new(FALSE, FALSE, sizeof(GString *));
g_array_set_clear_func(devices, _free_gstring);
uint32_t cookie;
if (!dm_udev_create_cookie(&cookie)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_udev_create_cookie: %s", _dm_err_last_msg);
goto out;
}
int found = 0;
for (guint i = 0; i < vol->parts->len; i++) {
const LDMPartition * const part_o =
g_array_index(vol->parts, const LDMPartition *, i);
const LDMPartitionPrivate * const part = part_o->priv;
GString * chunk = _dm_create_part(part, cookie, err);
if (chunk == NULL) {
if (err && (*err)->code == LDM_ERROR_MISSING_DISK) {
g_warning((*err)->message);
g_error_free(*err); *err = NULL;
g_string_append(target.params, " - -");
continue;
} else {
goto out;
}
}
found++;
g_array_append_val(devices, chunk);
g_string_append_printf(target.params, " - /dev/mapper/%s", chunk->str);
}
if (found == 0) {
g_set_error(err, LDM_ERROR, LDM_ERROR_MISSING_DISK,
"Mirrored volume is missing all partitions");
return FALSE;
}
/* Wait until all partitions have been created */
dm_udev_wait(cookie);
if (!dm_udev_create_cookie(&cookie)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_udev_create_cookie: %s", _dm_err_last_msg);
goto out;
}
name = _dm_vol_name(vol);
if (!_dm_create(name->str, cookie, 1, &target, err)) {
g_string_free(name, TRUE);
name = NULL;
}
dm_udev_wait(cookie);
g_array_unref(devices); devices = NULL;
out:
if (devices) {
GError *cleanup_err = NULL;
for (int i = devices->len; i > 0; i--) {
GString *device = g_array_index(devices, GString *, i - 1);
if (!_dm_remove(device->str, 0, &cleanup_err)) {
g_warning(cleanup_err->message);
g_error_free(cleanup_err); cleanup_err = NULL;
}
}
g_array_unref(devices); devices = NULL;
}
g_string_free(target.params, TRUE);
return name;
}
static GString *
_dm_create_raid5(const LDMVolumePrivate * const vol, GError ** const err)
{
GString *name = NULL;
struct dm_target target;
target.start = 0;
target.size = vol->size;
target.type = "raid";
target.params = g_string_new("");
g_string_append_printf(target.params, "raid5_ls 1 %" PRIu64 " %" PRIu32,
vol->chunk_size, vol->parts->len);
GArray * devices = g_array_new(FALSE, FALSE, sizeof(GString *));
g_array_set_clear_func(devices, _free_gstring);
uint32_t cookie;
if (!dm_udev_create_cookie(&cookie)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_udev_create_cookie: %s", _dm_err_last_msg);
goto out;
}
guint n_found = 0;
for (guint i = 0; i < vol->parts->len; i++) {
const LDMPartition * const part_o =
g_array_index(vol->parts, const LDMPartition *, i);
const LDMPartitionPrivate * const part = part_o->priv;
GString * chunk = _dm_create_part(part, cookie, err);
if (chunk == NULL) {
if (err && (*err)->code == LDM_ERROR_MISSING_DISK) {
g_warning((*err)->message);
g_error_free(*err); *err = NULL;
g_string_append(target.params, " - -");
continue;
} else {
goto out;
}
}
n_found++;
g_array_append_val(devices, chunk);
g_string_append_printf(target.params, " - /dev/mapper/%s", chunk->str);
}
if (n_found < vol->parts->len - 1) {
g_set_error(err, LDM_ERROR, LDM_ERROR_MISSING_DISK,
"RAID5 volume is missing more than 1 component");
goto out;
}
/* Wait until all partitions have been created */
dm_udev_wait(cookie);
if (!dm_udev_create_cookie(&cookie)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_udev_create_cookie: %s", _dm_err_last_msg);
goto out;
}
name = _dm_vol_name(vol);
if (!_dm_create(name->str, cookie, 1, &target, err)) {
g_string_free(name, TRUE); name = NULL;
goto out;
}
dm_udev_wait(cookie);
g_array_unref(devices); devices = NULL;
out:
if (devices) {
GError *cleanup_err = NULL;
for (int i = devices->len; i > 0; i--) {
GString *device = g_array_index(devices, GString *, i - 1);
if (!_dm_remove(device->str, 0, &cleanup_err)) {
g_warning(cleanup_err->message);
g_error_free(cleanup_err); cleanup_err = NULL;
}
}
g_array_unref(devices); devices = NULL;
}
g_string_free(target.params, TRUE);
return name;
}
static void
_dm_log_fn(const int level, const char * const file, const int line,
const int dm_errno, const char *f, ...)
{
if (dm_errno == 0) return;
_dm_err_last_level = level;
_dm_err_last_file = file;
_dm_err_last_line = line;
/* device-mapper doesn't set dm_errno usefully (it only seems to use
* EUNCLASSIFIED), so we capture errno directly and cross our fingers */
_dm_err_last_errno = errno;
if (_dm_err_last_msg) {
free(_dm_err_last_msg);
_dm_err_last_msg = NULL;
}
va_list ap;
va_start(ap, f);
if (vasprintf(&_dm_err_last_msg, f, ap) == -1) {
g_error("vasprintf");
}
va_end(ap);
}
GString *
ldm_volume_dm_get_name(const LDMVolume * const o)
{
return _dm_vol_name(o->priv);
}
gboolean
ldm_volume_dm_create(const LDMVolume * const o, GString **created,
GError ** const err)
{
const LDMVolumePrivate * const vol = o->priv;
/* We should really store the previous logging function and restore it
* afterwards, but the API doesn't allow this. */
dm_log_with_errno_init(_dm_log_fn);
/* Check if the device already exists */
struct dm_tree *tree = _get_device_tree(err);
if (!tree) return FALSE;
GString *name = _dm_vol_name(vol);
struct dm_tree_node *node = _find_device_tree_node(tree, name->str);
if (node) {
dm_tree_free(tree); tree = NULL;
return TRUE;
}
dm_tree_free(tree); tree = NULL;
g_string_free(name, TRUE);
switch (vol->type) {
case LDM_VOLUME_TYPE_SIMPLE:
case LDM_VOLUME_TYPE_SPANNED:
name = _dm_create_spanned(vol, err);
break;
case LDM_VOLUME_TYPE_STRIPED:
name = _dm_create_striped(vol, err);
break;
case LDM_VOLUME_TYPE_MIRRORED:
name = _dm_create_mirrored(vol, err);
break;
case LDM_VOLUME_TYPE_RAID5:
name = _dm_create_raid5(vol, err);
break;
default:
/* Should be impossible */
g_error("Unexpected volume type: %u", vol->type);
}
dm_log_with_errno_init(NULL);
if (created && name) *created = name;
return name == NULL ? FALSE : TRUE;
}
gboolean
ldm_volume_dm_remove(const LDMVolume * const o, GString **removed,
GError ** const err)
{
const LDMVolumePrivate * const vol = o->priv;
/* We should really store the previous logging function and restore it
* afterwards, but the API doesn't allow this. */
dm_log_with_errno_init(_dm_log_fn);
struct dm_tree *tree = _get_device_tree(err);
if (!tree) return FALSE;
gboolean r = FALSE;
GString *name = _dm_vol_name(vol);
struct dm_tree_node *node = _find_device_tree_node(tree, name->str);
if (node) {
uint32_t cookie;
if (!dm_udev_create_cookie(&cookie)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"dm_udev_create_cookie: %s", _dm_err_last_msg);
g_string_free(name, TRUE); name = NULL;
goto out;
}
if (!_dm_remove(name->str, cookie, err)) {
g_string_free(name, TRUE); name = NULL;
goto out;
}
dm_tree_set_cookie(node, cookie);
if (!dm_tree_deactivate_children(node, NULL, 0)) {
g_set_error(err, LDM_ERROR, LDM_ERROR_EXTERNAL,
"removing children: %s", _dm_err_last_msg);
g_string_free(name, TRUE); name = NULL;
goto out;
}
dm_udev_wait(cookie);
} else {
g_string_free(name, TRUE); name = NULL;
}
r = TRUE;
out:
dm_tree_free(tree);
if (removed && name) *removed = name;
dm_log_with_errno_init(NULL);
return r;
}
libldm-libldm-0.2.3/src/ldm.h 0000664 0000000 0000000 00000035771 12027041301 0015737 0 ustar 00root root 0000000 0000000 /* libldm
* Copyright 2012 Red Hat Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
#ifndef LIBLDM_LDM_H__
#define LIBLDM_LDM_H__
#include
G_BEGIN_DECLS
#define LDM_ERROR ldm_error_quark()
GQuark ldm_error_quark(void);
/**
* LDMError:
* @LDM_ERROR_INTERNAL: An internal error
* @LDM_ERROR_IO: There was an IO error accessing a device
* @LDM_ERROR_NOT_LDM: The device is not part of an LDM disk group
* @LDM_ERROR_INVALID: The LDM metadata is corrupt
* @LDM_ERROR_INCONSISTENT: Detected two disks from the same disk group with
* inconsistent metadata
* @LDM_ERROR_NOTSUPPORTED: Unsupported LDM metadata
* @LDM_ERROR_MISSING_DISK: A disk is missing from a disk group
* @LDM_ERROR_EXTERNAL: An error reported by an external library
*/
typedef enum {
LDM_ERROR_INTERNAL,
LDM_ERROR_IO,
LDM_ERROR_NOT_LDM,
LDM_ERROR_INVALID,
LDM_ERROR_INCONSISTENT,
LDM_ERROR_NOTSUPPORTED,
LDM_ERROR_MISSING_DISK,
LDM_ERROR_EXTERNAL
} LDMError;
#define LDM_TYPE_ERROR (ldm_error_get_type())
GType ldm_error_get_type(void);
/* LDM */
#define LDM_TYPE (ldm_get_type())
#define LDM_CAST(obj) (G_TYPE_CHECK_INSTANCE_CAST \
((obj), LDM_TYPE, LDM))
#define LDM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
((klass), LDM_TYPE, LDMClass))
#define IS_LDM(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
((obj), LDM_TYPE))
#define IS_LDM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE \
((klass), LDM_TYPE))
#define LDM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
((obj), LDM_TYPE, LDMClass))
typedef struct _LDMPrivate LDMPrivate;
/**
* LDM:
*
* An LDM metadata scanner
*/
typedef struct _LDM LDM;
struct _LDM
{
GObject parent;
LDMPrivate *priv;
};
typedef struct
{
GObjectClass parent_class;
} LDMClass;
/* LDMDiskGroup */
#define LDM_TYPE_DISK_GROUP (ldm_disk_group_get_type())
#define LDM_DISK_GROUP(obj) (G_TYPE_CHECK_INSTANCE_CAST \
((obj), LDM_TYPE_DISK_GROUP, LDMDiskGroup))
#define LDM_DISK_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
((klass), LDM_TYPE_DISK_GROUP, LDMDiskGroupClass))
#define LDM_IS_DISK_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
((obj), LDM_TYPE_DISK_GROUP))
#define LDM_IS_DISK_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE \
((klass), LDM_TYPE_DISK_GROUP))
#define LDM_DISK_GROUP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
((obj), LDM_TYPE_DISK_GROUP, LDMDiskGroupClass))
typedef struct _LDMDiskGroupPrivate LDMDiskGroupPrivate;
/**
* LDMDiskGroup:
*
* An LDM Disk Group
*/
typedef struct _LDMDiskGroup LDMDiskGroup;
struct _LDMDiskGroup
{
GObject parent;
LDMDiskGroupPrivate *priv;
};
typedef struct
{
GObjectClass parent_class;
} LDMDiskGroupClass;
/* LDMVolumeType */
/**
* LDMVolumeType:
* @LDM_VOLUME_TYPE_SIMPLE: A simple volume
* @LDM_VOLUME_TYPE_SPANNED: A spanned volume
* @LDM_VOLUME_TYPE_STRIPED: A striped volume
* @LDM_VOLUME_TYPE_MIRRORED: A mirrored volume
* @LDM_VOLUME_TYPE_RAID5: A raid5 volume
*/
typedef enum {
LDM_VOLUME_TYPE_SIMPLE,
LDM_VOLUME_TYPE_SPANNED,
LDM_VOLUME_TYPE_STRIPED,
LDM_VOLUME_TYPE_MIRRORED,
LDM_VOLUME_TYPE_RAID5
} LDMVolumeType;
#define LDM_TYPE_VOLUME_TYPE (ldm_volume_type_get_type())
GType ldm_volume_type_get_type(void);
/* LDMVolume */
#define LDM_TYPE_VOLUME (ldm_volume_get_type())
#define LDM_VOLUME(obj) (G_TYPE_CHECK_INSTANCE_CAST \
((obj), LDM_TYPE_VOLUME, LDMVolume))
#define LDM_VOLUME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
((klass), LDM_TYPE_VOLUME, LDMVolumeClass))
#define LDM_IS_VOLUME(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
((obj), LDM_TYPE_VOLUME))
#define LDM_IS_VOLUME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE \
((klass), LDM_TYPE_VOLUME))
#define LDM_VOLUME_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
((obj), LDM_TYPE_VOLUME, LDMVolumeClass))
typedef struct _LDMVolumePrivate LDMVolumePrivate;
/**
* LDMVolume:
* @parent: The parent #GObject
* @priv: Private data
*
* An LDM Volume.
*/
typedef struct _LDMVolume LDMVolume;
struct _LDMVolume
{
GObject parent;
LDMVolumePrivate *priv;
};
typedef struct
{
GObjectClass parent_class;
} LDMVolumeClass;
/* LDMPartition */
#define LDM_TYPE_PARTITION (ldm_partition_get_type())
#define LDM_PARTITION(obj) (G_TYPE_CHECK_INSTANCE_CAST \
((obj), LDM_TYPE_PARTITION, LDMPartition))
#define LDM_PARTITION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
((klass), LDM_TYPE_PARTITION, LDMPartitionClass))
#define LDM_IS_PARTITION(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
((obj), LDM_TYPE_PARTITION))
#define LDM_IS_PARTITION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE \
((klass), LDM_TYPE_PARTITION))
#define LDM_PARTITION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
((obj), LDM_TYPE_PARTITION, LDMPartitionClass))
typedef struct _LDMPartitionPrivate LDMPartitionPrivate;
/**
* LDMPartition:
* @parent: The parent #GObject
* @priv: Private data
*
* An LDM Parition.
*/
typedef struct _LDMPartition LDMPartition;
struct _LDMPartition
{
GObject parent;
LDMPartitionPrivate *priv;
};
typedef struct
{
GObjectClass parent_class;
} LDMPartitionClass;
/* LDMDisk */
#define LDM_TYPE_DISK (ldm_disk_get_type())
#define LDM_DISK(obj) (G_TYPE_CHECK_INSTANCE_CAST \
((obj), LDM_TYPE_DISK, LDMDisk))
#define LDM_DISK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \
((klass), LDM_TYPE_DISK, LDMDiskClass))
#define LDM_IS_DISK(obj) (G_TYPE_CHECK_INSTANCE_TYPE \
((obj), LDM_TYPE_DISK))
#define LDM_IS_DISK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE \
((klass), LDM_TYPE_DISK))
#define LDM_DISK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \
((obj), LDM_TYPE_DISK, LDMDiskClass))
typedef struct _LDMDiskPrivate LDMDiskPrivate;
/**
* LDMDisk:
* @parent: The parent #GObject
* @priv: Private data
*
* An LDM Disk.
*/
typedef struct _LDMDisk LDMDisk;
struct _LDMDisk
{
GObject parent;
LDMDiskPrivate *priv;
};
typedef struct
{
GObjectClass parent_class;
} LDMDiskClass;
GType ldm_get_type(void);
GType ldm_disk_group_get_type(void);
/**
* ldm_new:
*
* Instantiate a new LDM object. LDM scans devices and stores detected metadata.
*
* Returns: (transfer full): a new #LDM object
*/
LDM *ldm_new();
/**
* ldm_add:
* @o: An #LDM object
* @path: The path of the device
* @err: A #GError to receive any generated errors
*
* Scan device @path and add its metadata to LDM object @o.
*
* Returns: true on success, false on error
*/
gboolean ldm_add(LDM *o, const gchar *path, GError **err);
/**
* ldm_add_fd:
* @o: An #LDM object
* @fd: A file descriptor for reading from the device
* @secsize: The size of a sector on the device
* @path: The path of the device (for messages)
* @err: A #GError to receive any generated errors
*
* Scan a device which has been previously opened for reading and add its
* metadata to LDM object @o.
*
* Returns: true on success, false on error
*/
gboolean ldm_add_fd(LDM *o, int fd, guint secsize, const gchar *path,
GError **err);
/**
* ldm_get_disk_groups:
* @o: An #LDM object
*
* Get an array of discovered disk groups.
*
* Returns: (element-type LDMDiskGroup)(transfer container):
* An array of disk groups
*/
GArray *ldm_get_disk_groups(LDM *o);
/**
* ldm_disk_group_get_volumes:
* @o: An #LDMDiskGroup
*
* Get an array of all volumes in a disk group.
*
* Returns: (element-type LDMVolume)(transfer container):
* An array of volumes
*/
GArray *ldm_disk_group_get_volumes(LDMDiskGroup *o);
/**
* ldm_disk_group_get_partitions:
* @o: An #LDMPartition
*
* Get an array of all partitions in a disk group.
*
* Returns: (element-type LDMPartition)(transfer container):
* An array of partitions
*/
GArray *ldm_disk_group_get_partitions(LDMDiskGroup *o);
/**
* ldm_disk_group_get_disks:
* @o: An #LDMDiskGroup
*
* Get an array of all disks in a disk group.
*
* Returns: (element-type LDMDisk)(transfer container):
* An array of disks
*/
GArray *ldm_disk_group_get_disks(LDMDiskGroup *o);
/**
* ldm_disk_group_get_name:
* @o: An #LDMDiskGroup
*
* Get the Windows-assigned name of a disk group.
*
* Returns: (transfer full): The name
*/
gchar *ldm_disk_group_get_name(const LDMDiskGroup *o);
/**
* ldm_disk_group_get_guid:
* @o: An #LDMDiskGroup
*
* Get the Windows-assigned GUID of a disk group.
*
* Returns: (transfer full): The string representation of the GUID
*/
gchar *ldm_disk_group_get_guid(const LDMDiskGroup *o);
/**
* ldm_volume_get_partitions:
* @o: An #LDMVolume
*
* Get an array of all partitions in a volume.
*
* Returns: (element-type LDMPartition)(transfer container):
* An array of partitions
*/
GArray *ldm_volume_get_partitions(LDMVolume *o);
/**
* ldm_volume_get_name:
* @o: An #LDMVolume
*
* Get the Windows-assigned name of a volume.
*
* Returns: (transfer full): The name
*/
gchar *ldm_volume_get_name(const LDMVolume *o);
/**
* ldm_volume_get_voltype:
* @o: An #LDMVolume
*
* Get the volume type. This can be:
* - %LDM_VOLUME_TYPE_SIMPLE
* - %LDM_VOLUME_TYPE_SPANNED
* - %LDM_VOLUME_TYPE_STRIPED
* - %LDM_VOLUME_TYPE_MIRRORED
* - %LDM_VOLUME_TYPE_RAID5
*
* Returns: The volume type
*/
LDMVolumeType ldm_volume_get_voltype(const LDMVolume *o);
/**
* ldm_volume_get_size:
* @o: An #LDMVolume
*
* Get the volume size in sectors.
*
* Returns: The volume size in sectors
*/
guint64 ldm_volume_get_size(const LDMVolume *o);
/**
* ldm_volume_get_part_type:
* @o: An #LDMVolume
*
* Get the 'partition type' of the volume. This is the same 8-bit value that is
* used to describe partition types on an MBR disk. It is 0x07 for NTFS volumes.
*
* Returns: The partition type
*/
guint8 ldm_volume_get_part_type(const LDMVolume *o);
/**
* ldm_volume_get_hint:
* @o: An #LDMVolume
*
* Get the volume mounting hint. This value specifies how Windows expects the
* volume to be mounted. For a volume with an assigned drive letter, it might be
* 'E:'.
*
* Returns: (transfer full): The mounting hint
*/
gchar *ldm_volume_get_hint(const LDMVolume *o);
/**
* ldm_volume_get_chunk_size:
* @o: An #LDMVolume
*
* Get the chunk size, in sectors, used by striped and raid5 volumes. For other
* volume types it will be 0.
*
* Returns: The chunk size in sectors
*/
guint64 ldm_volume_get_chunk_size(const LDMVolume *o);
/**
* ldm_volume_dm_get_name:
* @o: An #LDMVolume
*
* Get the name of the device mapper device which will be created for this
* volume.
*
* Returns: (transfer full): The device mapper name
*/
GString *ldm_volume_dm_get_name(const LDMVolume *o);
/**
* ldm_volume_dm_create:
* @o: An #LDMVolume
* @created: (out): The name of the created device, if any
* @err: A #GError to receive any generated errors
*
* Create a device mapper device for a volume. If this function is called for
* volume whose device already exists it will still return success. However,
* @created will not be set.
*
* Returns: True if, following the call, the device exists. False if it doesn't.
* @created will only be set if the call actually created the device.
*/
gboolean ldm_volume_dm_create(const LDMVolume *o, GString **created,
GError **err);
/**
* ldm_volume_dm_remove:
* @o: An #LDMVolume
* @removed: (out): The name of the removed device, if any
* @err: A #GError to receive any generated errors
*
* Remove a device mapper device for a volume. If this function is called for a
* volume whose device does not already exist, it will still return success.
* However, @removed will not be set.
*
* Returns: True if, following the call, the device does not exist. False if it
* does. @removed will only be set if the call actually removed the
* device.
*/
gboolean ldm_volume_dm_remove(const LDMVolume *o, GString **removed,
GError **err);
/**
* ldm_partition_get_disk:
* @o: An #LDMPartition
*
* Get the #LDMDisk underlying a partition.
*
* Returns: (transfer full): The underlying disk
*/
LDMDisk *ldm_partition_get_disk(LDMPartition *o);
/**
* ldm_partition_get_name:
* @o: An #LDMPartition
*
* Get the Windows-assigned name of a partition.
*
* Returns: (transfer full): The name
*/
gchar *ldm_partition_get_name(const LDMPartition *o);
/**
* ldm_partition_get_start:
* @o: An #LDMPartition
*
* Get the start sector of a disk, measured from the start of the underlying
* disk.
*
* Returns: The start sector
*/
guint64 ldm_partition_get_start(const LDMPartition *o);
/**
* ldm_partition_get_size:
* @o: An #LDMPartition
*
* Get the size of a partition in sectors.
*
* Returns: The size, in sectors
*/
guint64 ldm_partition_get_size(const LDMPartition *o);
/**
* ldm_disk_get_name
* @o: An #LDMDisk
*
* Get the Windows-assigned name of a disk.
*
* Returns: (transfer full): The name
*/
gchar *ldm_disk_get_name(const LDMDisk *o);
/**
* ldm_disk_get_guid:
* @o: An #LDMDisk
*
* Get the Windows-assigned GUID of a disk.
*
* Returns: (transfer full): The string representation of the GUID
*/
gchar *ldm_disk_get_guid(const LDMDisk *o);
/**
* ldm_disk_get_device:
* @o: An #LDMDisk
*
* Get the name of the host device (e.g. /dev/sda) of a disk. This will be NULL
* if the disk has been identified from metadata on another disk, but has not
* been discovered during scanning.
*
* Returns: The name of the host device, or NULL if the disk is missing
*/
gchar *ldm_disk_get_device(const LDMDisk *o);
/**
* ldm_disk_get_data_start:
* @o: An #LDMDisk
*
* Get the start sector of the data portion of a disk.
*
* Returns: The start sector
*/
guint64 ldm_disk_get_data_start(const LDMDisk *o);
/**
* ldm_disk_get_data_size:
* @o: An #LDMDisk
*
* Get the size, in sectors, of the data portion of a disk.
*
* Returns: The size in sectors
*/
guint64 ldm_disk_get_data_size(const LDMDisk *o);
/**
* ldm_disk_get_metadata_start:
* @o: An #LDMDisk
*
* Get the start sector of the metadata portion of a disk.
*
* Returns: The start sector
*/
guint64 ldm_disk_get_metadata_start(const LDMDisk *o);
/**
* ldm_disk_get_metadata_size:
* @o: An #LDMDisk
*
* Get the size, in sectors, of the metadata portion of a disk.
*
* Returns: The size in sectors
*/
guint64 ldm_disk_get_metadata_size(const LDMDisk *o);
G_END_DECLS
#endif /* LIBLDM_LDM_H__ */
libldm-libldm-0.2.3/src/ldmtool.c 0000664 0000000 0000000 00000055124 12027041301 0016622 0 ustar 00root root 0000000 0000000 /* ldmread
* Copyright 2012 Red Hat Inc.
*
* 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 3 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, see .
*/
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ldm.h"
#define USAGE_SCAN \
" scan []"
#define USAGE_SHOW \
" show diskgroup \n" \
" show volume \n" \
" show partition \n" \
" show disk "
#define USAGE_CREATE \
" create all\n" \
" create volume "
#define USAGE_REMOVE \
" remove all\n" \
" remove volume "
#define USAGE_ALL USAGE_SCAN "\n" USAGE_SHOW "\n" USAGE_CREATE "\n" USAGE_REMOVE
gboolean
usage_show(void)
{
g_warning(USAGE_SHOW);
return FALSE;
}
gboolean usage_create(void)
{
g_warning(USAGE_CREATE);
return FALSE;
}
gboolean usage_remove(void)
{
g_warning(USAGE_REMOVE);
return FALSE;
}
typedef gboolean (*_action_t) (LDM *ldm, gint argc, gchar **argv,
JsonBuilder *jb);
gboolean ldm_scan(LDM *ldm, gint argc, gchar **argv, JsonBuilder *jb);
gboolean ldm_show(LDM *ldm, gint argc, gchar **argv, JsonBuilder *jb);
gboolean ldm_create(LDM *ldm, gint argc, gchar **argv, JsonBuilder *jb);
gboolean ldm_remove(LDM *ldm, gint argc, gchar **argv, JsonBuilder *jb);
typedef struct {
const char * name;
const _action_t action;
} _command_t;
static const _command_t const commands[] = {
{ "scan", ldm_scan },
{ "show", ldm_show },
{ "create", ldm_create },
{ "remove", ldm_remove },
{ NULL }
};
gboolean
do_command(LDM * const ldm, const int argc, char *argv[], gboolean *result,
GOutputStream * const out,
JsonGenerator * const jg, JsonBuilder * const jb)
{
const _command_t *i = commands;
while (i->name) {
if (g_strcmp0(i->name, argv[0]) == 0) {
if ((i->action)(ldm, argc - 1, argv + 1, jb)) {
GError *err = NULL;
json_generator_set_root(jg, json_builder_get_root(jb));
if (!json_generator_to_stream(jg, out, NULL, &err)) {
g_warning("Error writing JSON output: %s",
err ? err->message : "(no detail)");
if (err) { g_error_free(err); err = NULL; }
}
printf("\n");
if (result) *result = TRUE;
} else {
if (result) *result = FALSE;
}
return TRUE;
}
i++;
}
return FALSE;
}
gboolean
_scan(LDM *const ldm, gboolean ignore_errors,
const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
wordexp_t p = {0,};
for (int i = 0; i < argc; i++) {
gchar * const pattern = argv[i];
if (wordexp(pattern, &p, WRDE_REUSE) != 0) {
/* FIXME: diagnose this? */
} else {
for (size_t j = 0; j < p.we_wordc; j++) {
gchar * const path = p.we_wordv[j];
GError *err = NULL;
if (!ldm_add(ldm, path, &err)) {
if (!ignore_errors &&
(err->domain != LDM_ERROR
|| err->code != LDM_ERROR_NOT_LDM)) {
g_warning("Error scanning %s: %s", path, err->message);
}
g_error_free(err); err = NULL;
}
}
}
}
wordfree(&p);
if (jb) {
json_builder_begin_array(jb);
GArray * const dgs = ldm_get_disk_groups(ldm);
for (guint i = 0; i < dgs->len; i++) {
LDMDiskGroup * const dg = g_array_index(dgs, LDMDiskGroup *, i);
gchar *guid = ldm_disk_group_get_guid(dg);
json_builder_add_string_value(jb, guid);
g_free(guid);
}
g_array_unref(dgs);
json_builder_end_array(jb);
}
return TRUE;
}
gboolean
ldm_scan(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
return _scan(ldm, FALSE, argc, argv, jb);
}
void
show_json_array(JsonBuilder * const jb, const GArray * const array,
const gchar * const name)
{
json_builder_set_member_name(jb, name);
json_builder_begin_array(jb);
for (guint i = 0; i < array->len; i++) {
GObject * const o = g_array_index(array, GObject *, i);
gchar *value;
g_object_get(o, "name", &value, NULL);
json_builder_add_string_value(jb, value);
g_free(value);
}
json_builder_end_array(jb);
}
LDMDiskGroup *
find_diskgroup(LDM * const ldm, const gchar * const guid)
{
LDMDiskGroup *dg = NULL;
GArray * const diskgroups = ldm_get_disk_groups(ldm);
for (guint i = 0; i < diskgroups->len; i++) {
LDMDiskGroup * const dg_i =
g_array_index(diskgroups, LDMDiskGroup *, i);
gchar *guid_i = ldm_disk_group_get_guid(dg_i);
if (g_strcmp0(guid_i, guid) == 0) {
dg = dg_i;
g_free(guid_i);
break;
}
g_free(guid_i);
}
if (dg) {
g_object_ref(dg);
} else {
g_warning("No such disk group: %s", guid);
}
g_array_unref(diskgroups);
return dg;
}
gboolean
show_diskgroup(LDM * const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
if (argc != 1) return usage_show();
LDMDiskGroup *dg = find_diskgroup(ldm, argv[0]);
if (!dg) return FALSE;
gchar *name = ldm_disk_group_get_name(dg);
json_builder_begin_object(jb);
json_builder_set_member_name(jb, "name");
json_builder_add_string_value(jb, name);
json_builder_set_member_name(jb, "guid");
json_builder_add_string_value(jb, argv[0]);
g_free(name);
GArray * const volumes = ldm_disk_group_get_volumes(dg);
show_json_array(jb, volumes, "volumes");
g_array_unref(volumes);
GArray * const disks = ldm_disk_group_get_disks(dg);
show_json_array(jb, disks, "disks");
g_array_unref(disks);
json_builder_end_object(jb);
g_object_unref(dg);
return TRUE;
}
gboolean
show_volume(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
if (argc != 2) return usage_show();
LDMDiskGroup *dg = find_diskgroup(ldm, argv[0]);
if (!dg) return FALSE;
GArray * const volumes = ldm_disk_group_get_volumes(dg);
g_object_unref(dg);
gboolean found = FALSE;
for (guint i = 0; i < volumes->len; i++) {
LDMVolume * const vol = g_array_index(volumes, LDMVolume *, i);
gchar *name = ldm_volume_get_name(vol);
LDMVolumeType type = ldm_volume_get_voltype(vol);
guint64 size = ldm_volume_get_size(vol);
guint64 chunk_size = ldm_volume_get_chunk_size(vol);
gchar *hint = ldm_volume_get_hint(vol);
if (g_strcmp0(name, argv[1]) == 0) {
found = TRUE;
json_builder_begin_object(jb);
GEnumValue * const type_v =
g_enum_get_value(g_type_class_peek(LDM_TYPE_VOLUME_TYPE), type);
json_builder_set_member_name(jb, "name");
json_builder_add_string_value(jb, name);
json_builder_set_member_name(jb, "type");
json_builder_add_string_value(jb, type_v->value_nick);
json_builder_set_member_name(jb, "size");
json_builder_add_int_value(jb, size);
json_builder_set_member_name(jb, "chunk-size");
json_builder_add_int_value(jb, chunk_size);
json_builder_set_member_name(jb, "hint");
json_builder_add_string_value(jb, hint);
json_builder_set_member_name(jb, "partitions");
json_builder_begin_array(jb);
GArray * const partitions = ldm_volume_get_partitions(vol);
for (guint j = 0; j < partitions->len; j++) {
LDMPartition * const part =
g_array_index(partitions, LDMPartition *, j);
gchar *partname = ldm_partition_get_name(part);
json_builder_add_string_value(jb, partname);
g_free(partname);
}
g_array_unref(partitions);
json_builder_end_array(jb);
json_builder_end_object(jb);
}
g_free(name);
g_free(hint);
if (found) break;
}
g_array_unref(volumes);
return found;
}
gboolean
show_partition(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
if (argc != 2) return usage_show();
LDMDiskGroup *dg = find_diskgroup(ldm, argv[0]);
if (!dg) return FALSE;
GArray * const parts = ldm_disk_group_get_partitions(dg);
g_object_unref(dg);
for (guint i = 0; i < parts->len; i++) {
LDMPartition * const part = g_array_index(parts, LDMPartition *, i);
gchar *name = ldm_partition_get_name(part);
guint64 start = ldm_partition_get_start(part);
guint64 size = ldm_partition_get_size(part);
gboolean found = FALSE;
if (g_strcmp0(name, argv[1]) == 0) {
found = TRUE;
LDMDisk * const disk = ldm_partition_get_disk(part);
gchar *diskname = ldm_disk_get_name(disk);
g_object_unref(disk);
json_builder_begin_object(jb);
json_builder_set_member_name(jb, "name");
json_builder_add_string_value(jb, name);
json_builder_set_member_name(jb, "start");
json_builder_add_int_value(jb, start);
json_builder_set_member_name(jb, "size");
json_builder_add_int_value(jb, size);
json_builder_set_member_name(jb, "disk");
json_builder_add_string_value(jb, diskname);
json_builder_end_object(jb);
g_free(diskname);
}
g_free(name);
if (found) break;
}
g_array_unref(parts);
return TRUE;
}
gboolean
show_disk(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
if (argc != 2) return usage_show();
LDMDiskGroup *dg = find_diskgroup(ldm, argv[0]);
if (!dg) return FALSE;
GArray * const disks = ldm_disk_group_get_disks(dg);
g_object_unref(dg);
gboolean found = FALSE;
for (guint i = 0; i < disks->len; i++) {
LDMDisk * const disk = g_array_index(disks, LDMDisk *, i);
gchar *name = ldm_disk_get_name(disk);
gchar *guid = ldm_disk_get_guid(disk);
gchar *device = ldm_disk_get_device(disk);
guint64 data_start = ldm_disk_get_data_start(disk);
guint64 data_size = ldm_disk_get_data_size(disk);
guint64 metadata_start = ldm_disk_get_metadata_start(disk);
guint64 metadata_size = ldm_disk_get_metadata_size(disk);
if (g_strcmp0(name, argv[1]) == 0) {
found = TRUE;
json_builder_begin_object(jb);
json_builder_set_member_name(jb, "name");
json_builder_add_string_value(jb, name);
json_builder_set_member_name(jb, "guid");
json_builder_add_string_value(jb, guid);
json_builder_set_member_name(jb, "present");
json_builder_add_boolean_value(jb, device ? TRUE : FALSE);
if (device) {
json_builder_set_member_name(jb, "device");
json_builder_add_string_value(jb, device);
json_builder_set_member_name(jb, "data-start");
json_builder_add_int_value(jb, data_start);
json_builder_set_member_name(jb, "data-size");
json_builder_add_int_value(jb, data_size);
json_builder_set_member_name(jb, "metadata-start");
json_builder_add_int_value(jb, metadata_start);
json_builder_set_member_name(jb, "metadata-size");
json_builder_add_int_value(jb, metadata_size);
}
json_builder_end_object(jb);
}
g_free(name);
g_free(guid);
g_free(device);
if (found) break;
}
g_array_unref(disks);
return TRUE;
}
gboolean
ldm_show(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
if (argc == 0) return usage_show();
if (g_strcmp0(argv[0], "diskgroup") == 0) {
return show_diskgroup(ldm, argc - 1, argv + 1, jb);
} else if (g_strcmp0(argv[0], "volume") == 0) {
return show_volume(ldm, argc - 1, argv + 1, jb);
} else if (g_strcmp0(argv[0], "partition") == 0) {
return show_partition(ldm, argc - 1, argv + 1, jb);
} else if (g_strcmp0(argv[0], "disk") == 0) {
return show_disk(ldm, argc - 1, argv + 1, jb);
}
return usage_show();
}
typedef gboolean (*_usage_t)();
typedef gboolean (*_vol_action_t)(const LDMVolume *, GString **, GError **);
static gboolean
_ldm_vol_action(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb,
const gchar * const action_desc,
_usage_t const usage, _vol_action_t const action)
{
json_builder_begin_array(jb);
if (argc == 1) {
if (g_strcmp0(argv[0], "all") != 0) return (*usage)();
GArray *dgs = ldm_get_disk_groups(ldm);
for (guint i = 0; i < dgs->len; i++) {
LDMDiskGroup * const dg = g_array_index(dgs, LDMDiskGroup *, i);
GArray *volumes = ldm_disk_group_get_volumes(dg);
for (guint j = 0; j < volumes->len; j++) {
LDMVolume * const vol = g_array_index(volumes, LDMVolume *, j);
GError *err = NULL;
GString *device = NULL;
if (!(*action)(vol, &device, &err)) {
gchar *vol_name = ldm_volume_get_name(vol);
gchar *dg_guid = ldm_disk_group_get_guid(dg);
g_warning("Unable to %s volume %s in disk group %s: %s",
action_desc, vol_name, dg_guid, err->message);
g_free(vol_name);
g_free(dg_guid);
g_error_free(err); err = NULL;
}
if (device) {
json_builder_add_string_value(jb, device->str);
g_string_free(device, TRUE);
}
}
}
g_array_unref(dgs);
}
else if (argc == 3) {
if (g_strcmp0(argv[0], "volume") != 0) return (*usage)();
LDMDiskGroup * const dg = find_diskgroup(ldm, argv[1]);
if (!dg) return FALSE;
GArray * const volumes = ldm_disk_group_get_volumes(dg);
LDMVolume *vol = NULL;
for (guint i = 0; i < volumes->len; i++) {
LDMVolume * const o = g_array_index(volumes, LDMVolume *, i);
gchar *name = ldm_volume_get_name(o);
if (g_strcmp0(name, argv[2]) == 0) vol = o;
g_free(name);
if (vol) break;
}
g_object_unref(dg);
g_array_unref(volumes);
if (!vol) {
g_warning("Disk group %s doesn't contain volume %s",
argv[1], argv[2]);
return FALSE;
}
GError *err = NULL;
GString *device = NULL;
if (!(*action)(vol, &device, &err)) {
g_warning("Unable to %s volume %s in disk group %s: %s",
action_desc, argv[2], argv[1], err->message);
g_error_free(err); err = NULL;
return FALSE;
}
if (device) {
json_builder_add_string_value(jb, device->str);
g_string_free(device, TRUE);
}
}
else {
return (*usage)();
}
json_builder_end_array(jb);
return TRUE;
}
gboolean
ldm_create(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
return _ldm_vol_action(ldm, argc, argv, jb,
"create", usage_create, ldm_volume_dm_create);
}
gboolean
ldm_remove(LDM *const ldm, const gint argc, gchar ** const argv,
JsonBuilder * const jb)
{
return _ldm_vol_action(ldm, argc, argv, jb,
"remove", usage_remove, ldm_volume_dm_remove);
}
gboolean
shell(LDM * const ldm, gchar ** const devices,
JsonGenerator * const jg, GOutputStream * const out)
{
int history_len = 0;
rl_readline_name = "ldmtool";
char histfile[1024] = "";
const char * const home = getenv("HOME");
if (home) {
snprintf(histfile, sizeof(histfile), "%s/.ldmtool", home);
using_history();
int r = read_history(histfile);
if (r != 0 && r != ENOENT) {
g_warning("Unable to read history from %s: %s",
histfile, strerror(r));
}
}
JsonBuilder *jb = json_builder_new();
for (;;) {
char * line = readline("ldm> ");
if (!line) {
printf("\n");
break;
}
GError *err = NULL;
gint argc = 0;
gchar **argv = NULL;
if (!g_shell_parse_argv(line, &argc, &argv, &err)) {
if (err->domain != G_SHELL_ERROR ||
err->code != G_SHELL_ERROR_EMPTY_STRING)
{
g_warning("Error parsing command: %s", err->message);
}
g_error_free(err); err = NULL;
}
if (argc == 0) {
free(line);
continue;
}
add_history(line);
history_len++;
free(line);
gboolean result;
if (!do_command(ldm, argc, argv, &result, out, jg, jb)) {
if (g_strcmp0("quit", argv[0]) == 0 ||
g_strcmp0("exit", argv[0]) == 0)
{
g_strfreev(argv);
break;
}
if (g_strcmp0("help", argv[0]) == 0) {
printf("Usage:\n%s\n", USAGE_ALL);
} else {
printf("Unrecognised command: %s\n", argv[0]);
}
}
if (result) json_builder_reset(jb);
g_strfreev(argv);
}
g_object_unref(jb);
if (histfile[0] != '\0') {
/* append_history requires the file to exist already */
int fd = open(histfile, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0600);
if (fd == -1) {
g_warning("Unable to create history file %s: %m", histfile);
} else {
close(fd);
int r = append_history(history_len, histfile);
if (r != 0) {
g_warning("Unable to save history to %s: %s",
histfile, strerror(r));
}
}
}
return TRUE;
}
void
_array_free(gpointer data)
{
char **str_p = data;
g_free(*str_p);
}
GArray *
get_devices(void)
{
DIR *dir = opendir("/sys/block");
if (dir == NULL) {
g_warning("Unable to open /sys/block: %m");
return NULL;
}
GArray *ret = g_array_new(TRUE, FALSE, sizeof(char *));
g_array_set_clear_func(ret, _array_free);
struct dirent *entry;
for (;;) {
entry = readdir(dir);
if (entry == NULL) break;
if (g_strcmp0(entry->d_name, ".") == 0 ||
g_strcmp0(entry->d_name, "..") == 0) continue;
char *device;
if (asprintf(&device, "/dev/%s", entry->d_name) == -1) {
g_error("malloc failure in asprintf");
}
g_array_append_val(ret, device);
}
closedir(dir);
return ret;
}
gboolean
cmdline(LDM * const ldm, gchar **devices,
JsonGenerator * const jg, GOutputStream * const out,
const int argc, char *argv[])
{
GArray * scanned = NULL;
if (!devices) {
scanned = get_devices();
devices = (gchar **) scanned->data;
}
JsonBuilder *jb = NULL;
if (!_scan(ldm, TRUE, g_strv_length(devices), devices, NULL)) goto error;
jb = json_builder_new();
gboolean result;
if (!do_command(ldm, argc, argv, &result, out, jg, jb)) {
g_warning("Unrecognised command: %s", argv[0]);
goto error;
}
if (scanned) g_array_unref(scanned);
g_object_unref(jb);
return result;
error:
if (scanned) g_array_unref(scanned);
if (jb) g_object_unref(jb);
return FALSE;
}
void
ldmtool_log(const gchar * const log_domain, const GLogLevelFlags log_level,
const gchar * const message, gpointer const user_data)
{
if (log_level & G_LOG_LEVEL_DEBUG) {
/* Ignore debug messages */
} else if (log_level & (G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE)) {
printf("%s\n", message);
} else {
fprintf(stderr, "%s\n", message);
}
}
int
main(int argc, char *argv[])
{
static gchar **devices = NULL;
static const GOptionEntry entries[] =
{
{ "device", 'd', 0, G_OPTION_ARG_FILENAME_ARRAY,
&devices, "Block device to scan for LDM metadata", NULL },
{ NULL }
};
GError *err = NULL;
g_log_set_handler(NULL, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE |
G_LOG_LEVEL_INFO, ldmtool_log, NULL);
GOptionContext *context = g_option_context_new("[command ]");
g_option_context_set_summary(context, "Available commands:\n" USAGE_ALL);
g_option_context_add_main_entries(context, entries, NULL);
if(!g_option_context_parse(context, &argc, &argv, &err)) {
g_warning("option parsing failed: %s", err->message);
return 1;
}
g_option_context_free(context);
g_type_init();
LDM * const ldm = ldm_new(&err);
int ret = 0;
GOutputStream *out = g_unix_output_stream_new(STDOUT_FILENO, FALSE);
JsonGenerator *jg = json_generator_new();
json_generator_set_pretty(jg, TRUE);
json_generator_set_indent(jg, 2);
if (argc > 1) {
if (!cmdline(ldm, devices, jg, out, argc - 1, argv + 1)) {
ret = 1;
}
} else {
if (!shell(ldm, devices, jg, out)) {
ret = 1;
}
}
g_object_unref(jg);
g_strfreev(devices);
g_object_unref(ldm);
if (!g_output_stream_close(out, NULL, &err)) {
g_warning("Error closing output stream: %s", err->message);
}
g_object_unref(out);
return ret;
}
libldm-libldm-0.2.3/src/mbr.c 0000664 0000000 0000000 00000004464 12027041301 0015731 0 ustar 00root root 0000000 0000000 /* libldm
* Copyright 2012 Red Hat Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
#include
#include
#include "mbr.h"
struct _part {
uint8_t status;
uint8_t first_head;
uint8_t first_cs[2];
uint8_t type;
uint8_t last_head;
uint8_t last_cs[2];
uint32_t first_lba;
uint32_t n_sectors;
} __attribute__((__packed__));
struct _mbr {
char code[446];
struct _part part[4];
uint8_t magic[2];
} __attribute__((__packed__));
int mbr_read(int fd, mbr_t *mbr)
{
struct _mbr _mbr;
size_t read = 0;
while (read < sizeof(_mbr)) {
ssize_t in = pread(fd, &_mbr + read, sizeof(struct _mbr) - read, read);
if (in == 0) return -MBR_ERROR_INVALID;
if (in == -1) return -MBR_ERROR_READ;
read += in;
}
if (_mbr.magic[0] != 0x55 || _mbr.magic[1] != 0xAA)
return -MBR_ERROR_INVALID;
for (int i = 0; i < 4; i++) {
struct _part *_part = &_mbr.part[i];
mbr_part_t *part = &mbr->part[i];
part->status = _part->status;
part->first_head = _part->first_head;
part->first_sector = _part->first_cs[0] & 0x3F;
part->first_cylinder = (uint16_t) (_part->first_cs[0] & 0xC0) +
(uint16_t) (_part->first_cs[1]);
part->type = _part->type;
part->last_head = _part->last_head;
part->last_sector = _part->last_cs[0] & 0x3F;
part->last_cylinder = (uint16_t) (_part->last_cs[0] & 0xC0) +
(uint16_t) (_part->last_cs[1]);
part->first_lba = le32toh(_part->first_lba);
part->n_sectors = le32toh(_part->n_sectors);
}
return 0;
}
libldm-libldm-0.2.3/src/mbr.h 0000664 0000000 0000000 00000004267 12027041301 0015737 0 ustar 00root root 0000000 0000000 /* libldm
* Copyright 2012 Red Hat Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
#include
typedef enum {
MBR_ERROR_OK,
MBR_ERROR_READ,
MBR_ERROR_INVALID
} mbr_error_t;
typedef enum {
MBR_PART_EMPTY = 0x00,
MBR_PART_FAT16 = 0x04,
MBR_PART_EXTENDED = 0x05,
MBR_PART_FAT16B = 0x06,
MBR_PART_NTFS = 0x07,
MBR_PART_FAT32 = 0x0B,
MBR_PART_FAT32_LBA = 0x0C,
MBR_PART_FAT16_LBA = 0x0E,
MBR_PART_EXTENDED_LBA = 0x0F,
MBR_PART_NTFS_HIDDEN = 0x27,
MBR_PART_WINDOWS_LDM = 0x42,
MBR_PART_LINUX = 0x83,
MBR_PART_EXTENDED_LINUX = 0x85,
MBR_PART_LINUX_LVM = 0x8E,
MBR_PART_EFI_PROTECTIVE = 0xEE
} mbr_part_type_t;
typedef struct _mbr_ext_t mbr_ext_t;
typedef struct {
uint8_t status;
uint8_t first_head;
uint16_t first_cylinder; /* Only 10 bits are usable */
uint8_t first_sector;
mbr_part_type_t type;
uint8_t last_head;
uint16_t last_cylinder; /* Only 10 bits are usable */
uint8_t last_sector;
uint32_t first_lba;
uint32_t n_sectors;
mbr_ext_t *ext;
} mbr_part_t;
typedef struct {
char code[440];
uint32_t signature;
mbr_part_t part[4];
} mbr_t;
struct _mbr_ext_t {
char code[446];
mbr_part_t part[4];
mbr_part_t *_parent;
uint32_t _start;
};
int mbr_read(int fd, mbr_t *mbr);
//int mbr_read_extended(int fd, mbr_part *part, mbr_ext *ext);
libldm-libldm-0.2.3/test/ 0000775 0000000 0000000 00000000000 12027041301 0015165 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/test/.gitignore 0000664 0000000 0000000 00000000566 12027041301 0017164 0 ustar 00root root 0000000 0000000 2003R2_MIRRORED
2003R2_MIRRORED_partial_1
2003R2_MIRRORED_partial_2
2003R2_RAID5
2003R2_RAID5_partial_1
2003R2_RAID5_partial_2
2003R2_RAID5_partial_3
2003R2_SIMPLE
2003R2_SPANNED
2003R2_STRIPED
2008R2_MIRRORED
2008R2_MIRRORED_partial_1
2008R2_MIRRORED_partial_2
2008R2_RAID5
2008R2_RAID5_partial_1
2008R2_RAID5_partial_2
2008R2_RAID5_partial_3
2008R2_SPANNED
2008R2_STRIPED
libldm-libldm-0.2.3/test/Makefile.am 0000664 0000000 0000000 00000011474 12027041301 0017230 0 ustar 00root root 0000000 0000000 # libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
AM_CFLAGS = -Wall -Werror
EXTRA_DIST = checkmount.pl data/ldm-data.tar.xz
check_PROGRAMS = partread ldmread
partread_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/src
partread_LDADD = $(top_builddir)/src/libldm-1.0.la
ldmread_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/src $(GOBJECT_CFLAGS)
ldmread_LDADD = $(top_builddir)/src/libldm-1.0.la $(GOBJECT_LIBS)
2003R2_DG = 03c0c4fc-8b6f-402b-9431-4be2e5823b1c
2008R2_DG = 06495a84-fbfd-11e1-8cf9-52540061f5db
2003R2_SIMPLE = \
data/ldm-2003r2-simple-1.img
2003R2_SIMPLE_volume = $(2003R2_DG) Volume1
2003R2_SPANNED = \
data/ldm-2003r2-spanned-1.img \
data/ldm-2003r2-spanned-2.img
2003R2_SPANNED_volume = $(2003R2_DG) Volume2
2003R2_STRIPED = \
data/ldm-2003r2-striped-1.img \
data/ldm-2003r2-striped-2.img
2003R2_STRIPED_volume = $(2003R2_DG) Stripe1
2003R2_MIRRORED = \
data/ldm-2003r2-mirrored-1.img \
data/ldm-2003r2-mirrored-2.img
2003R2_MIRRORED_partial_1 = \
data/ldm-2003r2-mirrored-1.img
2003R2_MIRRORED_partial_2 = \
data/ldm-2003r2-mirrored-2.img
2003R2_MIRRORED_volume = $(2003R2_DG) Volume3
2003R2_MIRRORED_partial_1_volume = $(2003R2_DG) Volume3
2003R2_MIRRORED_partial_2_volume = $(2003R2_DG) Volume3
2003R2_RAID5 = \
data/ldm-2003r2-raid5-1.img \
data/ldm-2003r2-raid5-2.img \
data/ldm-2003r2-raid5-3.img
2003R2_RAID5_partial_1 = \
data/ldm-2003r2-raid5-1.img \
data/ldm-2003r2-raid5-2.img
2003R2_RAID5_partial_2 = \
data/ldm-2003r2-raid5-2.img \
data/ldm-2003r2-raid5-3.img
2003R2_RAID5_partial_3 = \
data/ldm-2003r2-raid5-1.img \
data/ldm-2003r2-raid5-3.img
2003R2_RAID5_volume = $(2003R2_DG) Raid1
2003R2_RAID5_partial_1_volume = $(2003R2_DG) Raid1
2003R2_RAID5_partial_2_volume = $(2003R2_DG) Raid1
2003R2_RAID5_partial_3_volume = $(2003R2_DG) Raid1
2008R2_SPANNED = \
data/ldm-2008r2-spanned-1.img \
data/ldm-2008r2-spanned-2.img
2008R2_SPANNED_volume = $(2008R2_DG) Volume1
2008R2_STRIPED = \
data/ldm-2008r2-striped-1.img \
data/ldm-2008r2-striped-2.img
2008R2_STRIPED_volume = $(2008R2_DG) Volume2
2008R2_MIRRORED = \
data/ldm-2008r2-mirrored-1.img \
data/ldm-2008r2-mirrored-2.img
2008R2_MIRRORED_partial_1 = \
data/ldm-2008r2-mirrored-1.img
2008R2_MIRRORED_partial_2 = \
data/ldm-2008r2-mirrored-2.img
2008R2_MIRRORED_volume = $(2008R2_DG) Volume3
2008R2_MIRRORED_partial_1_volume = $(2008R2_DG) Volume3
2008R2_MIRRORED_partial_2_volume = $(2008R2_DG) Volume3
2008R2_RAID5 = \
data/ldm-2008r2-raid5-1.img \
data/ldm-2008r2-raid5-2.img \
data/ldm-2008r2-raid5-3.img
2008R2_RAID5_partial_1 = \
data/ldm-2008r2-raid5-1.img \
data/ldm-2008r2-raid5-2.img
2008R2_RAID5_partial_2 = \
data/ldm-2008r2-raid5-2.img \
data/ldm-2008r2-raid5-3.img
2008R2_RAID5_partial_3 = \
data/ldm-2008r2-raid5-1.img \
data/ldm-2008r2-raid5-3.img
2008R2_RAID5_volume = $(2008R2_DG) Volume4
2008R2_RAID5_partial_1_volume = $(2008R2_DG) Volume4
2008R2_RAID5_partial_2_volume = $(2008R2_DG) Volume4
2008R2_RAID5_partial_3_volume = $(2008R2_DG) Volume4
img_files = \
$(2003R2_SIMPLE) \
$(2003R2_SPANNED) \
$(2003R2_STRIPED) \
$(2003R2_MIRRORED) \
$(2003R2_RAID5) \
$(2008R2_SPANNED) \
$(2008R2_STRIPED) \
$(2008R2_MIRRORED) \
$(2008R2_RAID5)
$(img_files): data/ldm-data.tar.xz Makefile
mkdir -p $(builddir)/data
tar -C $(builddir)/data -SJxf $< && touch $(img_files)
data: $(img_files)
# The RAID5 partial tests aren't passing. Kernel error message is:
# md/raid:mdX: cannot start dirty degraded array.
TESTS = \
2003R2_SIMPLE \
2003R2_SPANNED \
2003R2_STRIPED \
2003R2_MIRRORED \
2003R2_RAID5 \
2008R2_SPANNED \
2008R2_STRIPED \
2008R2_MIRRORED \
2008R2_RAID5 \
2003R2_MIRRORED_partial_1 \
2003R2_MIRRORED_partial_2 \
2008R2_MIRRORED_partial_1 \
2008R2_MIRRORED_partial_2 \
#2003R2_RAID5_partial_1 \
#2003R2_RAID5_partial_2 \
#2003R2_RAID5_partial_3 \
#2008R2_RAID5_partial_1 \
#2008R2_RAID5_partial_2 \
#2008R2_RAID5_partial_3
$(TESTS): Makefile.am checkmount.pl $(img_files)
echo "#!/bin/sh" > $@
echo "sudo $(srcdir)/checkmount.pl $(top_builddir)/src $($@_volume) $($@)" >> $@
chmod 755 $@
.PHONY: data
CLEANFILES = $(TESTS) $(img_files)
libldm-libldm-0.2.3/test/checkmount.pl 0000775 0000000 0000000 00000004115 12027041301 0017666 0 ustar 00root root 0000000 0000000 #!/usr/bin/perl
# libldm
# Copyright 2012 Red Hat Inc.
#
# 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 3 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, see .
use strict;
use warnings;
use File::Temp qw(tempdir);
use JSON::PP;
my @loops;
my @dm_devices;
my $mount = tempdir(CLEANUP => 1);
my $mounted = 0;
my $builddir = shift @ARGV;
my $dg = shift @ARGV;
my $vol = shift @ARGV;
sub umount
{
my $dir = shift;
my $tries = 0;
do {
return if (system('umount', $mount) == 0);
sleep(1);
$tries++;
} while ($tries < 3)
}
END {
my $err = $?;
umount($mount) if (defined($mount) && $mounted);
while(@dm_devices) {
system('dmsetup', 'remove', pop(@dm_devices));
}
while(@loops) {
system('losetup', '-d', pop(@loops));
}
$? = $err;
}
my @loop_args;
foreach my $arg (@ARGV) {
my $loop = `losetup -f --show $arg`;
chomp($loop);
push(@loops, $loop);
push(@loop_args, '-d', $loop);
}
my $json = JSON::PP->new();
open(JSON, '-|', "$builddir/ldmtool", @loop_args, 'create', 'volume', $dg, $vol)
or die("Unable to run ldmtool");
while() {
$json->incr_parse($_);
}
close(JSON);
my $tested = 0;
foreach my $device (@{$json->incr_parse}) {
system('mount', "/dev/mapper/$device", $mount);
$mounted = 1;
open(TEST, '<', "$mount/test.txt") or die "Test file missing";
my $line = ;
close(TEST);
umount($mount);
$mounted = 0;
`$builddir/ldmtool remove all`;
exit(1) unless $line eq 'Filesystem test';
$tested = 1;
}
exit($tested ? 0 : 1);
libldm-libldm-0.2.3/test/data/ 0000775 0000000 0000000 00000000000 12027041301 0016076 5 ustar 00root root 0000000 0000000 libldm-libldm-0.2.3/test/data/.gitignore 0000664 0000000 0000000 00000000007 12027041301 0020063 0 ustar 00root root 0000000 0000000 /*.img
libldm-libldm-0.2.3/test/data/ldm-data.tar.xz 0000664 0000000 0000000 00000576720 12027041301 0020752 0 ustar 00root root 0000000 0000000 7zXZ ִF ! t/!f] 6 +%L(ӆ?eztR=TWd2d?v{E'JGt"5E$(g9@p=lC݁hOڤ鵡C4{lJP9DR+CGVItjú5vo[7o{8wsU¦D<[.&1
q{Vד!D\:$^fr4w1"f
xT)ȍRYd6;ޅrav-*4Ls.1s3_i;5^c΄: Qo7[AHwiGnbhB7}.#x[9fJxuDB5{3mm|uSJvO]Tc
+Lfҏs4GleDf
Ge[%W"z _AX UQJnSlRz3Pj7$#\hM K6Z
_l!*W6m)_"4p!xGwyR>~A"ź}>]&uvFOGaYLG>ՍN=sa K8kg˜usk#G6\R({B $`Ҏ֭
r IwZ:81j%[AΩcz!$. b>{@T"eR6s֏9P F2\ћ2CQy" Y/t~I^2z%1z^#
ϕ.{E#Di(tVS)yg@;h'2>)n |6U&tN fi|5
*f'BeystD|^<
]At3'.69&u ѓ͙{@,N:ác8P| K>l¯3]?IʔËR\Vp{ug&/J(u#Jpcs זlp:Yb>:yQef6ֳˇ*ֻ_y6+ 8I +42՝aH=)W{ uLYq請z÷_J63S!Cif$PMQ5$߫TF~~Sk摸ԃk9k(Ҙ\'qtWoAH҃/iuI#+֩ͳ,v4/ˤ8,&ex׀%(|s1b'#Z63,ZqfTz>%BkC &yX4ƾ',yȽQȽ~8$υGkUh9yCU Tݞ_ͭhW=J2({>QS..:.,$d|mx: 6P[_vja)9upvP
J
T2ʼV2
('+vC3GUS"(;DxBcL-`BL)_U%m0G`.vԊ#~
#fFĆ2Ƃ宷ͷ&x|"`OdnhUG~]=1) D˽q֘NOІ`