Compare commits

...

No commits in common. "master" and "pristine-tar" have entirely different histories.

47 changed files with 4 additions and 3564 deletions

13
.gitignore vendored
View File

@ -1,13 +0,0 @@
.idea
.run
*.iml
hd-idle
build
obj*
debian/hd-idle
debian/debhelper-build-stamp
debian/*.debhelper
debian/files
debian/hd-idle.debhelper.log
debian/hd-idle.substvars
release

674
LICENSE
View File

@ -1,674 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
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.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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 <http://www.gnu.org/licenses/>.
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:
<program> Copyright (C) <year> <name of author>
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
<http://www.gnu.org/licenses/>.
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
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

View File

@ -1,59 +0,0 @@
MAKEFLAGS += --silent
TARGET = hd-idle
PLATFORM := $(shell uname -m)
ARCH :=
ifeq ($(PLATFORM),x86_64)
ARCH = amd64
endif
ifeq ($(PLATFORM),aarch64)
ARCH = arm64
endif
ifeq ($(PLATFORM),armv7l)
ARCH = armhf
endif
GOARCH :=
ifeq ($(ARCH),amd64)
GOARCH = amd64
endif
ifeq ($(ARCH),i386)
GOARCH = 386
endif
ifeq ($(ARCH),arm64)
GOARCH = arm64
endif
ifeq ($(ARCH),armhf)
GOARCH = arm
endif
ifeq ($(GOARCH),)
$(error Invalid ARCH: $(ARCH))
endif
ifdef DESTDIR
# dh_auto_install (Debian) sets this variable
TARGET_DIR = $(DESTDIR)/usr
else
TARGET_DIR ?= /usr/local
endif
all: $(TARGET)
distclean: clean
clean:
rm -f $(TARGET)
install: $(TARGET)
install -Dm755 $(TARGET) $(TARGET_DIR)/sbin/$(TARGET)
install -Dm755 debian/$(TARGET).8 $(TARGET_DIR)/share/man/man8/$(TARGET).8
uninstall:
rm -f $(TARGET_DIR)/sbin/$(TARGET)
$(TARGET):
GOOS=linux GOARCH=$(GOARCH) go build
test:
go test ./... -race -cover

340
README.md
View File

@ -1,340 +0,0 @@
# hd-idle
Reimplementation of _Christian Mueller's_ [hd-idle](http://hd-idle.sf.net) with some extra features.
`hd-idle` is a utility program for spinning-down external disks after a period of idle time.
Since most external IDE disk enclosures don't support setting the IDE idle timer,
a program like `hd-idle` is required to spin down idle disks automatically.
**Index**
* [Extra features](#extra-features)
* [Support ATA commands](#support-ata-commands)
* [Monitor the skew between monitoring cycles](#monitor-the-skew-between-monitoring-cycles)
* [Resolve symlinks in runtime](#resolve-symlinks-in-runtime)
* [Log disk spin up](#log-disk-spin-up)
* [Use disk partitions or device mapper to calculate activity](#use-disk-partitions-or-device-mapper-to-calculate-activity)
* [Install](#Install)
* [Precompiled binaries](#precompiled-binaries)
* [Build from source](#build-from-source)
* [Run hd-idle](#run-hd-idle)
* [Configuration](#Configuration)
* [Understand the logs](#understand-the-logs)
* [Standard log](#standard-log)
* [Log file](#log-file)
* [Warning on spinning down disks](#warning-on-spinning-down-disks)
* [Troubleshot](#Troubleshot)
* [LUKS support](#luks-support)
* [SCSI response not ok](#scsi-response-not-ok)
## Extra features
List of extra features compared to the original `hd-idle`:
### Support ATA commands
The implementation of `hd-idle` written by _Christian Mueller_ relies on the `SCSI` api to work.
When listing the drives by id, disks starting with `usb` stop using the original implementation,
but disk starting with `ata` do not.
$ ls /dev/disk/by-id/
ata-WDC_WD40EZRX-
ata-WDC_WD50EZRX-
usb-WD_My_Book_1140_
[hdparm](https://en.wikipedia.org/wiki/Hdparm) on the other hand always stops the drives without any problems.
It uses `ATA` api calls to send disks to standby. `hd-idle` comes with `ATA` commands support to replicate `hdparm`'s api calls.
### Monitor the skew between monitoring cycles
Identify if the sleep took longer than expected and reset the spun down flag if it waited too long for the main loop sleep.
This should capture suspend events as well as excessive machine load.
### Resolve symlinks in runtime
`hd-idle` can resolve disk symlinks also in runtime. Disks added after application's start won't be hidden.
### Log disk spin up
Show in standard output when disks spin up.
### Use disk partitions or device mapper to calculate activity
The disk activity is calculated by watching read/write changes on partition or device mapper level instead of disk level.
This is required for kernels newer than 5.4 LTS, because disk monitoring tools change read/write values on disk level,
although there's no real activity on the disk itself.
When using LUKS, activity will happen on the device mapper device mapped to the corresponding disk.
## Install
There are various ways of installing `hd-idle`:
### Precompiled binaries
Precompiled binaries for released versions are available in the
[*releases*](https://github.com/adelolmo/hd-idle/releases) section.
### Build from source
To build `hd-idle` from the source code yourself you need to have a working
Go environment with [version 1.16 or greater installed](http://golang.org/doc/install).
Open a terminal and execute these commands:
git clone https://github.com/adelolmo/hd-idle
cd hd-idle
make
On Debian you can also build the package yourself using `dpkg-buildpackage`:
git clone https://github.com/adelolmo/hd-idle.git
cd hd-idle
dpkg-buildpackage -a armhf -us -uc -b
In the example above, the package is built for `armhf`, but you can build it also for the platforms `i386`, `amd64`, and `arm64`
by substituting the parameter `-a`.
Then install the package:
# dpkg -i ../hd-idle*.deb
## Run hd-idle
In order to run `hd-idle`, type:
$ hd-idle
This will start `hd-idle` with the default options, causing all `SCSI`
(read: USB, Firewire, SCSI, ...) hard disks to spin down after 10 minutes of inactivity.
If the Debian package was installed, after editing `/etc/default/hd-idle` and enabling it (`START_HD_IDLE=true`),
run hd-idle with:
# systemctl start hd-idle
To enable `hd-idle` on reboot:
# systemctl enable hd-idle
Please note that `hd-idle` uses */proc/diskstats* to read disk statistics. If
this file is not present, `hd-idle` won't work.
In case of problems, use the debug option *-d* to get further information.
## Configuration
Command line options:
+ -a *name*
Set device name of disks for subsequent idle-time
parameters *-i*. This parameter is optional in the
sense that there's a default entry for all disks
which are not named otherwise by using this
parameter. This can also be a symlink
(e.g. /dev/disk/by-uuid/...)
+ -i *idle_time*
Idle time in seconds for the currently named disk(s)
(-a *name*) or for all disks.
Setting this value to `0` will never spin down the disk(s).
+ -c *command_type*
Api call to stop the device. Possible values are `scsi`
(default value) and `ata`.
+ -p *power_condition*
Power condition to send with the issued SCSI START STOP UNIT command. Possible values
are `0-15` (inclusive). The default value of `0` works fine for disks accessible via the
SCSI layer (USB, IEEE1394, ...), but it will *NOT* work as intended with real SCSI / SAS disks.
A stopped SAS disk will not start up automatically on access, but requires a startup command for reactivation.
Useful values for SAS disks are `2` for idle and `3` for standby.
+ -s *symlink_policy*
Set the policy to resolve symlinks for devices. If set
to `0`, symlinks are resolved only on start. If set to `1`,
symlinks are also resolved on runtime until success.
By default symlinks are only resolved on start. If the
symlink doesn't resolve to a device, the default
configuration will be applied.
+ -l *logfile*
Name of logfile (written only after a disk has spun
up or down). Please note that this option might cause the
disk which holds the logfile to spin up just because
another disk had some activity. On single-disk systems,
this option should not cause any additional spinups.
On systems with more than one disk, the disk where the log
is written will be spun up. On raspberry based systems the
log should be written to the SD card.
Miscellaneous options:
+ -t *disk*
Spin-down the specified disk immediately and exit.
+ -d
Debug mode. It will print debugging info to
stdout/stderr (/var/log/syslog if started with systemctl)
+ -h
Print usage information.
Regarding the parameter *-a*:
The parameter *-a* can be used to set a filter on the disk's device name (omit /dev/)
for subsequent idle-time settings.
1)
A *-i* option before the first *-a* option will set the default idle time.
2)
In order to disable spin-down of disks per default, and then re-enable
spin-down on selected disks, set the default idle time to 0.
Example:
```
hd-idle -i 0 -a sda -i 300 -a sdb -i 1200
```
This example sets the default idle time to 0 (meaning hd-idle will never
try to spin down a disk) and the default api command to `scsi`, then sets explicit
idle times for disks which have the string `sda` or `sdb` in their device name.
3)
The option *-c* allows to set the api call that sends the spindown command.
Possible values are `scsi` (the default value) or `ata`.
Example:
```
hd-idle -i 0 -c ata -a sda -i 300 -a sdb -i 1200 -c scsi
```
This example sets the default idle time to 0 (meaning hd-idle will never
try to spin down a disk) and the default api command to `ata`, then sets explicit
idle times for disks which have the string `sda` or `sdb` in their device name
and sets `sdb` to use `scsi` api command.
## Understand the logs
By default `hd-idle` only logs to the standard output. You can find them in the syslog if the application starts via service.
If you set the log file (`-l` flag) then the application writes extra details to it. (Check the [Configuration](#Configuration) section).
### Standard log
The standard log output registers two kinds of events:
* disk spin up
* disk spin down
```
Aug 8 00:14:55 enterprise hd-idle[9958]: sda spindown
Aug 8 00:14:55 enterprise hd-idle[9958]: sdb spindown
Aug 8 00:14:56 enterprise hd-idle[9958]: sdc spindown
Aug 8 00:17:55 enterprise hd-idle[9958]: sdb spinup
Aug 8 00:28:55 enterprise hd-idle[9958]: sdb spindown
```
### Log file
You can enable the log file with the flag `-l` followed by the log path. (Check the [Configuration](#Configuration) section).
This is the kind of entry shown in the log file:
```
date: 2020-07-30, time: 05:28:01, disk: sdc, running: 601, stopped: 76654
```
Explanation:
* `date` and `time` when the disk spins up.
* `disk` involved.
* `running` seconds the device was running before it spun down the last time.
* `stopped` seconds since last spin down. This is the time the disk was asleep before spinning up.
**Important Note:**
The log file is written after a full cycle of running-stopped-wakeup.
A bit more on `running` explained with the above example:
| timestamp |disk spin| event |new disk spin| running | stopped |
|:-----------------:|:-------:|:-----------:|:-----------:|:------------------------:|:-------------------------------------------------------:|
|2020-07-29 07:59:57| down |disk activity| up | ? | ? |
|2020-07-29 08:09:58| up | go to sleep | down | - | - |
|2020-07-30 05:28:01| down |disk activity| up |08:09:58 - 07:59:57 = 601s|2020-07-30 05:28:01 - 2020-07-29 08:09:58 = ~21h (76654s)|
Explanation:
At 07:59:57 the disk is on standby and hd-idle detects disk activity.
At 08:09:58 the disk is active and hd-idle determines inactivity of the disk and spins it down.
At 05:28:01 on the next day the disk is on standby and hd-idle detects disk activity. It writes on the log file 601s of previous disk spin up and ~21h of standby.
## Warning on spinning down disks
A word of caution: hard disks don't like spinning up too often. Laptop disks
are more robust in this respect than desktop disks but if you set your disks
to spin down after a few seconds you may damage the disk over time due to the
stress the spin-up causes on the spindle motor and bearings. It seems that
manufacturers recommend a minimum idle time of 3-5 minutes, the default in
`hd-idle` is 10 minutes.
You have been warned...
# Troubleshot
This section covers some usual issues that user's face while using `hd-idle`.
## LUKS support
Using encrypted disk or partitions with LUKS is supported by the use of symlinks.
1. Run the following command with you're disk mounted:
`sudo lsblk /dev/sd* -o PATH,FSSIZE,LABEL,UUID,PARTLABEL,PARTUUID,MODEL,SIZE,SERIAL,TYPE,WWN`
```
PATH FSSIZE LABEL UUID PARTLABEL PARTUUID MODEL SIZE SERIAL TYPE WWN
/dev/sde ST400 3.7T ZGY0LB disk 0x5000c500a3d1d419
/dev/sde1 100e952e-0ffb-4b73-bb1a-8401d4fe56c0 dropbox 14a81aa8-c2c9-448e-967b-85d87dc9b488 1T part 0x5000c500a3d1d419
/dev/sde1 100e952e-0ffb-4b73-bb1a-8401d4fe56c0 dropbox 14a81aa8-c2c9-448e-967b-85d87dc9b488 1T part 0x5000c500a3d1d419
/dev/sde2 2.6T three 175e2227-d24f-4ad0-9e42-2ddb8846682d d2792423-3c07-44fe-ab6b-a1aca61c73a5 2.7T part 0x5000c500a3d1d419
/dev/sde2 2.6T three 175e2227-d24f-4ad0-9e42-2ddb8846682d d2792423-3c07-44fe-ab6b-a1aca61c73a5 2.7T part 0x5000c500a3d1d419
/dev/mapper/luks-100e952e-0ffb-4b73-bb1a-8401d4fe56c0
1007.8G dropbox
649dd15e-6750-472c-8185-4d76bffc2490 1024G crypt
```
You have to take symlinks that resolve to disk devices: `/dev/sd*`.
In the example above `/dev/mapper/luks-100e952e-0ffb-4b73-bb1a-8401d4fe56c0` is the Path to the encrypted partition,
which WWN is `0x5000c500a3d1d419`.
2. Run the following command to see which devices the system has identified using `by-id`:
`sudo ls -lv /dev/disk/by-id/`
Output:
```
lrwxrwxrwx 1 root root 9 Jul 18 15:56 ata-ST4000DM005-2DP166_ZGY0LBRB -> ../../sde
lrwxrwxrwx 1 root root 10 Jul 18 16:01 ata-ST4000DM005-2DP166_ZGY0LBRB-part1 -> ../../sde1
lrwxrwxrwx 1 root root 10 Jul 18 15:56 ata-ST4000DM005-2DP166_ZGY0LBRB-part2 -> ../../sde2
lrwxrwxrwx 1 root root 9 Jul 18 15:56 wwn-0x5000c500a3d1d419 -> ../../sde
lrwxrwxrwx 1 root root 10 Jul 18 16:01 wwn-0x5000c500a3d1d419-part1 -> ../../sde1
lrwxrwxrwx 1 root root 10 Jul 18 15:56 wwn-0x5000c500a3d1d419-part2 -> ../../sde2
```
Here we see that we can either use `ata-ST4000DM005-2DP166_ZGY0LBRB` or `wwn-0x5000c500a3d1d419` as symlinks.
3. Edit `/etc/default/hd-idle` to use the symlink you prefer.
In my case, I went with the symlink using WWN (unique storage identifier), yet I could have chosen MODEL (device identifier) instead.
`HD_IDLE_OPTS='-i 0 -c ata -s 1 -l /var/log/hd-idle.log -a /dev/disk/by-id/wwn-0x5000c500a3d1d419 -i 600'`
Or
`HD_IDLE_OPTS='-i 0 -c ata -s 1 -l /var/log/hd-idle.log -a /dev/disk/by-id/ata-ST4000DM005-2DP166_ZGY0LBRB -i 600'`
## SCSI response not ok
You can find information about the issue here: [SCSI-response-not-ok](https://github.com/adelolmo/hd-idle/wiki/SCSI-response-not-ok)
## License
GNU General Public License v3.0, see [LICENSE](https://github.com/adelolmo/hd-idle/blob/master/LICENSE).

8
debian/NEWS vendored
View File

@ -1,8 +0,0 @@
hd-idle (1.21+ds-1) unstable; urgency=medium
Starting from the version 1.21, hd-idle is using a new codebase:
https://github.com/adelolmo/hd-idle - a reimplementation of
Christian Mueller's hd-idle with some extra features.
You might want to check the options and update your configs.
-- Alexandre Mestiashvili <mestia@debian.org> Fri, 22 Mar 2024 15:58:22 +0100

38
debian/changelog vendored
View File

@ -1,38 +0,0 @@
hd-idle (1.21+ds-1) unstable; urgency=medium
[ Debian Janitor ]
* Bump debhelper from old 12 to 13.
* Set debhelper-compat version in Build-Depends.
* Update standards version to 4.4.1, no changes needed.
[ Alexandre Mestiashvili ]
* d/watch, track new upstream
* d/copyright:
+ new upstream source
+ switch to GPL-3+
+ add upstream to debian/* section
* New upstream version 1.21+ds, Closes: #1026327
* d/control
+ update build deps
+ update Homepage and Description
+ Rules-Requires-Root: no
+ bump Policy to 4.6.2
+ introduce XS-Go-Import-Path and Built-Using headers
* d/rules, new build rules
* d/docs, install only README.md
* d/NEWS, new codebase
* add d/lintian-overrides, hd-idle is statically linked
-- root <mario.fetka@disconnected-by-peer.at> Wed, 06 Aug 2025 15:52:26 +0200
hd-idle (1.05+ds-2) unstable; urgency=medium
* No-change, Source-only upload
-- Alexandre Mestiashvili <mestia@debian.org> Thu, 19 Sep 2019 10:13:38 +0200
hd-idle (1.05+ds-1) unstable; urgency=medium
* Initial release. Closes: #924749
-- Alexandre Mestiashvili <mestia@debian.org> Sun, 17 Mar 2019 16:37:34 +0100

32
debian/control vendored
View File

@ -1,32 +0,0 @@
Source: hd-idle
Maintainer: Alexandre Mestiashvili <mestia@debian.org>
Section: admin
Priority: optional
Build-Depends: debhelper-compat (= 12), golang-any, dh-golang
Standards-Version: 4.6.2
Vcs-Browser: https://salsa.debian.org/debian/hd-idle
Vcs-Git: https://salsa.debian.org/debian/hd-idle.git
Rules-Requires-Root: no
Homepage: https://github.com/adelolmo/hd-idle
XS-Go-Import-Path: github.com/adelolmo/hd-idle
Package: hd-idle
Architecture: any
Pre-Depends: ${misc:Pre-Depends}
Depends: ${misc:Depends},
${shlibs:Depends},
Built-Using:
${misc:Built-Using},
Description: Spin down idle [USB] hard disks
hd-idle is a utility program for spinning-down external disks after a period
of idle time. Since most external IDE disk enclosures don't support setting
the IDE idle timer, a program like hd-idle is required to spin down idle disks
automatically.
.
A word of caution: hard disks don't like spinning up too often. Laptop disks
are more robust in this respect than desktop disks but if you set your disks
to spin down after a few seconds you may damage the disk over time due to the
stress the spin-up causes on the spindle motor and bearings. It seems that
manufacturers recommend a minimum idle time of 3-5 minutes, the default in
hd-idle is 10 minutes.

33
debian/copyright vendored
View File

@ -1,33 +0,0 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: hd-idle
Source: https://github.com/adelolmo/hd-idle/
Files-Excluded:
debian/*
Files: *
Copyright: 2018 Andoni del Olmo <andoni.delolmo@gmail.com>
Author: Andoni del Olmo <andoni.delolmo@gmail.com>
License: GPL-3+
Files: debian/*
Copyright: © 2019 Alex Mestiashvili <mestia@debian.org>
© 2007 Christian Mueller <cm1@mumac.de>
@ 2018 Andoni del Olmo <andoni.delolmo@gmail.com>
License: GPL-3+
License: GPL-3+
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 <https://www.gnu.org/licenses/>.
.
On Debian systems, the complete text of the GNU General
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".

1
debian/docs vendored
View File

@ -1 +0,0 @@
README.md

127
debian/hd-idle.8 vendored
View File

@ -1,127 +0,0 @@
.\" Hey, EMACS: -*- nroff -*-
.\" First parameter, NAME, should be all caps
.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection
.\" other parameters are allowed: see man(7), man(1)
.TH HD-IDLE 8 "September 8, 2019"
.\" Please adjust this date whenever revising the manpage.
.\"
.\" Some roff macros, for reference:
.\" .nh disable hyphenation
.\" .hy enable hyphenation
.\" .ad l left justify
.\" .ad b justify to both left and right margins
.\" .nf disable filling
.\" .fi enable filling
.\" .br insert line break
.\" .sp <n> insert n+1 empty lines
.\" for manpage-specific macros, see man(7)
.SH NAME
hd-idle \- spin down idle hard disks
.SH SYNOPSIS
.B hd-idle
.RI [ options ]
.P
.SH DESCRIPTION
hd-idle is a utility program for spinning down external disks after a period
of idle time. Since most external IDE disk enclosures don't support setting
the IDE idle timer, a program like hd-idle is required to spin down idle
disks automatically.
.P
A word of caution: hard disks don't like spinning up too often. Laptop disks
are more robust in this respect than desktop disks but if you set your disks
to spin down after a few seconds you may damage the disk over time due to the
stress the spin-up causes on the spindle motor and bearings. It seems that
manufacturers recommend a minimum idle time of 3-5 minutes, the default in
hd-idle is 10 minutes.
.SH OPTIONS
.TP
.B \-a name
Set device name of disks for subsequent idle-time parameters
.B (-i).
This parameter is optional in the sense that there's a default entry for
all disks which are not named otherwise by using this parameter. This can
also be a symlink (e.g. /dev/disk/by-uuid/...)
.TP
.B \-i idle_time
Idle time in seconds for the currently named disk(s) (-a <name>) or for
all disks.
Setting this value to "0" will never spin down the disk(s).
.TP
.B \-c command_type
Api call to stop the device. Possible values are "scsi" (default value)
and "ata".
.TP
.B \-p power_condition
Power condition to send with the issued SCSI START STOP UNIT command.
Possible values are "0-15" (inclusive). The default value of "0" works fine
for disks accessible via the SCSI layer (USB, IEEE1394, ...), but it will
*NOT* work as intended with real SCSI / SAS disks. A stopped SAS disk will
not start up automatically on access, but requires a startup command for
reactivation. Useful values for SAS disks are "2" for idle and "3" for standby.
.TP
.B \-s symlink_policy
Set the policy to resolve symlinks for devices. If set to "0", symlinks
are resolve only on start. If set to "1", symlinks are also resolved on
runtime until success. By default symlinks are only resolve on start.
If the symlink doesn't resolve to a device, the default configuration
will be applied.
.TP
.B \-l logfile
Name of logfile (written only after a disk has spun up). Please note that
this option might cause the disk which holds the logfile to spin up just
because another disk had some activity. This option should not be used on
systems with more than one disk except for tuning purposes. On single-disk
systems, this option should not cause any additional spinups.
.TP
.B \-t disk
Spin-down the specified disk immediately and exit. It can be used in combination
with
.B \-c
to specify the command type.
.TP
.B \-d
Debug mode. It will print debugging info to stdout/stderr (/var/log/syslog
if started as with systemctl)
.TP
.B \-h
Print usage information.
.SH "DISK SELECTION"
The parameter
.B \-a
can be used to set a filter on the disk's device name (omit /dev/) for
subsequent idle-time settings. The default is all disks:
.P
.TP
.B \1)
A
.B \-i
option before the first
.B \-a
option will set the default idle time.
.TP
.B \2)
In order to disable spin-down of disks per default, and then re-enable
spin-down on selected disks, set the default idle time to 0.
.SH EXAMPLE
hd-idle -i 0 -a sda -i 300 -a sdb -i 1200
.P
This example sets the default idle time to 0 (meaning hd-idle will never
try to spin down a disk) and default "scsi" api command, then sets explicit
idle times for disks which have the string "sda" or "sdb" in their device name.
.SH EXAMPLE
hd-idle -i 0 -c ata -a sda -i 300 -a sdb -i 1200 -c scsi
.P
This example sets the default idle time to 0 (meaning hd-idle will never
try to spin down a disk) and default "ata" api command, then sets explicit
idle times for disks which have the string "sda" or "sdb" in their device name
and sets "sdb" to use "ata" api command.
.P
The option -c allows to set the api call that sends the spindown command.
Possible values are "scsi" (the default value) or "ata".
.SH AUTHOR
hd-idle was written by Andoni del Olmo <andoni.delolmo@gmail> based on Chistian Mueller's <chris@mumac.de> work.
.PP
This manual page was written by Christian Mueller <chris@mumac.de>, for the Debian
project (and may be used by others).
.PP
Modified by Andoni del Olmo <andoni.delolmo@gmail.com>.

View File

@ -1,42 +0,0 @@
# defaults file for hd-idle
# start hd-idle automatically?
START_HD_IDLE=false
# hd-idle command line options
# Options are:
# -a <name> Set device name of disks for subsequent idle-time
# parameters (-i). This parameter is optional in the
# sense that there's a default entry for all disks
# which are not named otherwise by using this
# parameter. This can also be a symlink
# (e.g. /dev/disk/by-uuid/...)
# -i <idle_time> Idle time in seconds.
# -c <command_type> Api call to stop the device. Possible values are "scsi"
# (default value) and "ata".
# -p <power_condition>
# Power condition to send with the issued SCSI START STOP UNIT command. Possible values
# are `0-15` (inclusive). The default value of `0` works fine for disks accessible via the
# SCSI layer (USB, IEEE1394, ...), but it will *NOT* work as intended with real SCSI / SAS disks.
# A stopped SAS disk will not start up automatically on access, but requires a startup command for reactivation.
# Useful values for SAS disks are `2` for idle and `3` for standby.
# -s symlink_policy Set the policy to resolve symlinks for devices.
# If set to "0", symlinks are resolve only on start.
# If set to "1", symlinks are also resolved on runtime
# until success. By default symlinks are only resolve on start.
# If the symlink doesn't resolve to a device, the default
# configuration will be applied.
# -l <logfile> Name of logfile (written only after a disk has spun
# up). Please note that this option might cause the
# disk which holds the logfile to spin up just because
# another disk had some activity. This option should
# not be used on systems with more than one disk
# except for tuning purposes. On single-disk systems,
# this option should not cause any additional spinups.
#
# Options not exactly useful here:
# -t <disk> Spin-down the specified disk immediately and exit.
# -d Debug mode. It will print debugging info to
# stdout/stderr (/var/log/syslog if started as with systemctl)
# -h Print usage information.
#HD_IDLE_OPTS="-i 180 -l /var/log/hd-idle.log"

50
debian/hd-idle.init vendored
View File

@ -1,50 +0,0 @@
#!/bin/sh
set -e
### BEGIN INIT INFO
# Provides: hd-idle
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start hd-idle daemon
# Description: Start hd-idle daemon (spin down idle hard disks)
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/hd-idle
[ -r /etc/default/hd-idle ] && . /etc/default/hd-idle
# See if the daemon is there
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting the hd-idle daemon" "hd-idle"
start-stop-daemon --start --quiet --oknodo --exec $DAEMON -- $HD_IDLE_OPTS
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping the hd-idle daemon" "hd-idle"
start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
log_end_msg $?
;;
restart|force-reload)
$0 stop && sleep 2 && $0 start
;;
status)
status_of_proc $DAEMON hd-idle && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/hd-idle start/stop/restart/force-reload"
exit 1
;;
esac

View File

@ -1 +0,0 @@
hd-idle binary: statically-linked-binary

View File

@ -1,6 +0,0 @@
/var/log/hd-idle.log {
missingok
notifempty
compress
delaycompress
}

View File

@ -1,13 +0,0 @@
[Unit]
Description=hd-idle - spin down idle hard disks
Documentation=man:hd-idle(8)
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
[Service]
Type=simple
EnvironmentFile=/etc/default/hd-idle
ExecStart=/usr/sbin/hd-idle $HD_IDLE_OPTS
Restart=always
[Install]
WantedBy=multi-user.target

15
debian/rules vendored
View File

@ -1,15 +0,0 @@
#!/usr/bin/make -f
export DH_VERBOSE=1
include /usr/share/dpkg/pkg-info.mk
%:
dh $@ --buildsystem=golang --with=golang
override_dh_auto_install:
dh_auto_install -- --no-source
override_dh_install:
mv -vf ${CURDIR}/debian/${DEB_SOURCE}/usr/bin ${CURDIR}/debian/${DEB_SOURCE}/usr/sbin
override_dh_installman:
dh_installman debian/hd-idle.8

View File

@ -1 +0,0 @@
3.0 (quilt)

5
debian/watch vendored
View File

@ -1,5 +0,0 @@
version=4
opts="searchmode=plain, dversionmangle=s/\.ds//, \
filenamemangle=s%.*/v?@ANY_VERSION@%@PACKAGE@-$1.tar.xz%,repacksuffix=+ds,repack" \
https://api.github.com/repos/adelolmo/hd-idle/releases?per_page=50 \
https://api.github.com/repos/[^/]+/[^/]+/tarball/v?@ANY_VERSION@

View File

@ -1,236 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package diskstats
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"regexp"
"strconv"
"strings"
)
/*
https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats
The /proc/diskstats file displays the I/O statistics
of block devices. Each line contains the following 14
fields:
1 - major number
2 - minor mumber
3 - device name
4 - reads completed successfully
5 - reads merged
6 - sectors read
7 - time spent reading (ms)
8 - writes completed
9 - writes merged
10 - sectors written
11 - time spent writing (ms)
12 - I/Os currently in progress
13 - time spent doing I/Os (ms)
14 - weighted time spent doing I/Os (ms)
Kernel 4.18+ appends four more fields for discard
tracking putting the total at 18:
15 - discards completed successfully
16 - discards merged
17 - sectors discarded
18 - time spent discarding
Kernel 5.5+ appends two more fields for flush requests:
19 - flush requests completed successfully
20 - time spent flushing
For more details refer to Documentation/admin-guide/iostats.rst
*/
const (
deviceNameCol = 2 // field 3 - device name
readsCol = 5 // field 6 - sectors read
writesCol = 9 // field 10 - sectors written
)
type DeviceType int
const (
Unknown DeviceType = iota
Disk
Partition
DeviceMapper
)
type ReadWriteStats struct {
Name string
Type DeviceType
Reads uint64
Writes uint64
}
var scsiDiskRegex *regexp.Regexp
var scsiPartitionRegex *regexp.Regexp
var deviceMapperRegex *regexp.Regexp
type diskHolderGetterFunc func(string, string) (string, error)
func init() {
scsiDiskRegex = regexp.MustCompile("sd[a-z]+$")
scsiPartitionRegex = regexp.MustCompile("sd[a-z]+[0-9]+$")
deviceMapperRegex = regexp.MustCompile("dm-.*$")
}
func Snapshot() []ReadWriteStats {
f, err := os.Open("/proc/diskstats")
if err != nil {
log.Fatal(err)
}
defer f.Close()
return readSnapshot(f, getDiskHolder)
}
func readSnapshot(r io.Reader, holderGetter diskHolderGetterFunc) []ReadWriteStats {
diskStatsMap := make(map[string]ReadWriteStats)
partitionStatsMap := make(map[string]ReadWriteStats)
deviceMapperHolderMap := make(map[string]string)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
diskStats, err := statsForDisk(scanner.Text())
if err != nil {
continue
}
if diskStats.Type == Disk {
diskStatsMap[diskStats.Name] = *diskStats
if dmName, err := holderGetter(diskStats.Name, "/sys/class/block/%s/holders/"); err == nil && dmName != "" {
deviceMapperHolderMap[dmName] = diskStats.Name
}
} else {
partitionStatsMap[diskStats.Name] = *diskStats
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
for _, partitionStats := range partitionStatsMap {
var diskName string
var ok bool
switch partitionStats.Type {
case Partition:
diskName = strings.TrimRight(partitionStats.Name,"0123456789")
case DeviceMapper:
if diskName, ok = deviceMapperHolderMap[partitionStats.Name]; !ok {
continue
}
default:
continue
}
var diskStats ReadWriteStats
if diskStats, ok = diskStatsMap[diskName]; !ok {
continue
}
if diskStats.Type == Disk {
// replace disk statistics by partition or holder stats
diskStats.Type = partitionStats.Type
diskStats.Writes = partitionStats.Writes
diskStats.Reads = partitionStats.Reads
} else {
// otherwise, accumulate stats of all partitions and holder if any
diskStats.Writes += partitionStats.Writes
diskStats.Reads += partitionStats.Reads
}
diskStatsMap[diskName] = diskStats
}
return toSlice(diskStatsMap)
}
func getDiskHolder(diskName, pathFormat string) (string, error) {
/* This returns only the first holder. In practice when using LUKS, there is only one holder */
holdersDir := fmt.Sprintf(pathFormat, diskName)
if _, err := os.Stat(holdersDir); os.IsNotExist(err) {
return "", err
}
files, err := os.ReadDir(holdersDir)
if err != nil {
return "", err
}
for _, file := range files {
return file.Name(), nil
}
return "", nil
}
func statsForDisk(rawStats string) (*ReadWriteStats, error) {
reader := strings.NewReader(rawStats)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
cols := strings.Fields(scanner.Text())
name := cols[deviceNameCol]
deviceType := Unknown
reads, _ := strconv.ParseUint(cols[readsCol], 10, 64)
writes, _ := strconv.ParseUint(cols[writesCol], 10, 64)
if scsiDiskRegex.MatchString(name) {
deviceType = Disk
} else if scsiPartitionRegex.MatchString(name) {
deviceType = Partition
} else if deviceMapperRegex.MatchString(name) {
deviceType = DeviceMapper
} else {
continue
}
stats := &ReadWriteStats{
Name: name,
Type: deviceType,
Reads: reads,
Writes: writes,
}
return stats, nil
}
if err := scanner.Err(); err != nil {
return nil, err
}
return nil, errors.New("cannot read disk stats")
}
func toSlice(rws map[string]ReadWriteStats) []ReadWriteStats {
var snapshot []ReadWriteStats
for _, r := range rws {
snapshot = append(snapshot, r)
}
return snapshot
}

View File

@ -1,218 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package diskstats
import (
"os"
"path/filepath"
"sort"
"strings"
"testing"
)
func mockGetDiskHolder(diskName, format string) (string, error) {
if diskName == "sdf" {
return "dm-4", nil
}
return "", nil
}
func TestTakeSnapshot(t *testing.T) {
s := ` 7 0 loop0 0 0 0 0 0 0 0 0 0 0 0
7 1 loop1 0 0 0 0 0 0 0 0 0 0 0
7 2 loop2 0 0 0 0 0 0 0 0 0 0 0
7 3 loop3 0 0 0 0 0 0 0 0 0 0 0
7 4 loop4 0 0 0 0 0 0 0 0 0 0 0
7 5 loop5 0 0 0 0 0 0 0 0 0 0 0
7 6 loop6 0 0 0 0 0 0 0 0 0 0 0
7 7 loop7 0 0 0 0 0 0 0 0 0 0 0
179 0 mmcblk0 133145 53235 6878634 3020910 1544414 1254150 48441345 240124500 0 13439150 243142800
179 1 mmcblk0p1 80 40 1760 210 1 0 1 0 0 140 210
179 2 mmcblk0p2 132931 53195 6874482 3020440 1544413 1254150 48441344 240124500 0 13439000 243278260
8 0 sda 321553 158156 37537568 5961590 50820 94361 10439592 26691430 0 3357150 32650910
8 1 sda1 321454 158156 37536344 5725790 50820 94361 10439592 26691430 0 3121370 32415240
8 32 sdc 52147 2738 6494584 913050 28092 1251 6370936 8938800 0 506360 9852970
8 33 sdc1 52087 2738 6493672 905390 28092 1251 6370936 8938800 0 498700 9892750
8 16 sdb 5650742 34516 727476416 92732820 1728864 35618 404215912 705303450 0 22944140 798112260
8 17 sdb1 5650643 34516 727475192 92673920 1728864 35618 404215912 705303450 0 22893010 798071230
8 16 sdd 982501 110903 37074938 9468348 60870 112203 15682640 17081448 0 2086868 26550788
8 17 sdd1 369 0 39960 1288 0 0 0 0 0 792 1288
8 18 sdd2 443 0 53496 1224 0 0 0 0 0 1204 1224
8 19 sdd3 52 0 2632 76 0 0 0 0 0 76 76
8 21 sdd5 76541 1090 22221672 979636 8845 2335 8316352 14986056 0 470364 15965544
8 22 sdd6 904573 109813 14736818 8485644 51818 109868 7366288 2094080 0 1642436 10580612
8 22 sdd11 904573 109813 1 8485644 51818 109868 1 2094080 0 1642436 10580612
8 32 sde 2891 192 57743 13523 1085550 14035 982686648 9819204 0 5102050 10209416 0 0 0 0 12140 376689
8 34 sdf 207814 251309 3670180 1378314 38505 27680 21787544 926421 0 552176 2325026 0 0 0 0 272 20290
253 4 dm-4 25371 0 206376 195492 1330 0 10640 657392 0 19480 852884 0 0 0 0 0 0
65 160 sdaa 157371 937 11375913 1617587 8304860 2117223236 17004224768 98631435 0 49649267 100249022 0 0 0 0 0 0
65 161 sdaa1 157257 937 11371536 1617417 8304860 2117223236 17004224768 98631435 0 49649104 100248853 0 0 0 0 0 0
65 176 sdab 54244 803 1223811 596585 368 9 3008 1051 0 342387 597828 0 0 0 0 8 191`
stats := readSnapshot(strings.NewReader(s), mockGetDiskHolder)
sort.Slice(stats, func(i, j int) bool {
return stats[i].Name < stats[j].Name
})
expected := []ReadWriteStats{
{Name: "sda", Type: Partition, Reads: 37536344, Writes: 10439592},
{Name: "sdaa", Type: Partition, Reads: 11371536, Writes: 17004224768},
{Name: "sdab", Type: Disk, Reads: 1223811, Writes: 3008},
{Name: "sdb", Type: Partition, Reads: 727475192, Writes: 404215912},
{Name: "sdc", Type: Partition, Reads: 6493672, Writes: 6370936},
{Name: "sdd", Type: Partition, Reads: 37054579, Writes: 15682641},
{Name: "sde", Type: Disk, Reads: 57743, Writes: 982686648},
{Name: "sdf", Type: DeviceMapper, Reads: 206376, Writes: 10640},
}
if len(expected) != len(stats) {
t.Fatalf("Expected %d disks but found %d", len(expected), len(stats))
}
for i := 0; i < len(expected); i++ {
exp := expected[i]
act := stats[i]
if exp != act {
t.Fatalf("Expected %v but found %v", exp, act)
}
}
}
func TestGetDiskHolder(t *testing.T) {
type wantParams struct {
name string
errorMessage string
}
tests := []struct {
name string
diskName string
holderPath string
want wantParams
}{
{
name: "disk not found",
diskName: "sda",
holderPath: "",
want: wantParams{
name: "",
errorMessage: "stat /tmp/sys/class/block/sda/holders/: no such file or directory",
},
}, {
name: "disk found",
diskName: "sda",
holderPath: "/tmp/sys/class/block/sda/holders/dm-0",
want: wantParams{
name: "dm-0",
errorMessage: "",
},
},
}
for _, test := range tests {
err := os.RemoveAll("/tmp/sys")
if err != nil {
panic(err)
}
t.Run(test.name, func(t *testing.T) {
if len(test.holderPath) > 0 {
if err := os.MkdirAll(filepath.Dir(test.holderPath), 0770); err != nil {
panic(err)
}
_, err := os.Create(test.holderPath)
if err != nil {
panic(err)
}
}
got, err := getDiskHolder(test.diskName, "/tmp/sys/class/block/%s/holders/")
if len(test.want.errorMessage) > 0 &&
test.want.errorMessage != err.Error() {
t.Fatalf("Expected %v but found %v", test.want.errorMessage, err.Error())
}
if test.want.name != got {
t.Fatalf("Expected %v but found %v", test.want.name, got)
}
})
}
}
func TestStatsForDisk(t *testing.T) {
type wantParams struct {
name string
deviceType DeviceType
errorMessage string
}
tests := []struct {
name string
line string
want wantParams
}{
{
name: "disk type",
line: "8 0 sda 321553 158156 37537568 5961590 50820 94361 10439592 26691430 0 3357150 32650910",
want: wantParams{
name: "sda",
deviceType: Disk,
},
},
{
name: "partition type",
line: "8 17 sdd1 369 0 39960 1288 0 0 0 0 0 792 1288",
want: wantParams{
name: "sdd1",
deviceType: Partition,
},
},
{
name: "device mapper type",
line: "253 4 dm-4 25371 0 206376 195492 1330 0 10640 657392 0 19480 852884 0 0 0 0 0 0",
want: wantParams{
name: "dm-4",
deviceType: DeviceMapper,
},
},
{
name: "unknown type",
line: "7 1 loop1 0 0 0 0 0 0 0 0 0 0 0",
want: wantParams{
errorMessage: "cannot read disk stats",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, gotError := statsForDisk(test.line)
if test.want.errorMessage != "" && test.want.errorMessage != gotError.Error() {
t.Fatalf("Expected %v but found %v", test.want.errorMessage, gotError.Error())
}
if gotError != nil {
return
}
if test.want.name != got.Name {
t.Fatalf("Expected %v but found %v", test.want.name, got.Name)
}
if test.want.deviceType != got.Type {
t.Fatalf("Expected %v but found %v", test.want.deviceType, got.Type)
}
})
}
}

5
go.mod
View File

@ -1,5 +0,0 @@
module github.com/adelolmo/hd-idle
go 1.16
require github.com/benmcclelland/sgio v0.0.0-20180629175614-f710aebf64c1

2
go.sum
View File

@ -1,2 +0,0 @@
github.com/benmcclelland/sgio v0.0.0-20180629175614-f710aebf64c1 h1:f1AIRyf6d21xBd1DirrIa6fk41O3LB0WvVuVqhPN4co=
github.com/benmcclelland/sgio v0.0.0-20180629175614-f710aebf64c1/go.mod h1:WdrapyVn/Aduwwf/OMW6sEtk9+7BSoMst1kGrx4E4xE=

Binary file not shown.

View File

@ -0,0 +1 @@
4c69bd0c21aeaddc68108925127a56ef960aa836

Binary file not shown.

View File

@ -0,0 +1 @@
7f1d28ec3f71d8ef1a9a10136bd215095351153f

Binary file not shown.

View File

@ -0,0 +1 @@
0be1e19dca3002f0746d94b493ff44bc292b5174

Binary file not shown.

View File

@ -0,0 +1 @@
5cdad8632cc90d07455f66ea5d7ff605572bb1e1

287
hdidle.go
View File

@ -1,287 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"github.com/adelolmo/hd-idle/diskstats"
"github.com/adelolmo/hd-idle/io"
"github.com/adelolmo/hd-idle/sgio"
"log"
"math"
"os"
"time"
)
const (
SCSI = "scsi"
ATA = "ata"
dateFormat = "2006-01-02T15:04:05"
)
type DefaultConf struct {
Idle time.Duration
CommandType string
PowerCondition uint8
Debug bool
LogFile string
SymlinkPolicy int
}
type DeviceConf struct {
Name string
GivenName string
Idle time.Duration
CommandType string
PowerCondition uint8
}
type Config struct {
Devices []DeviceConf
Defaults DefaultConf
SkewTime time.Duration
NameMap map[string]string
}
func (c *Config) resolveDeviceGivenName(name string) string {
if givenName, ok := c.NameMap[name]; ok {
return givenName
}
return name
}
type DiskStats struct {
Name string
GivenName string
IdleTime time.Duration
CommandType string
PowerCondition uint8
Reads uint64
Writes uint64
SpinDownAt time.Time
SpinUpAt time.Time
LastIoAt time.Time
SpunDown bool
}
var previousSnapshots []DiskStats
var now = time.Now()
var lastNow = time.Now()
func ObserveDiskActivity(config *Config) {
actualSnapshot := diskstats.Snapshot()
now = time.Now()
resolveSymlinks(config)
for _, stats := range actualSnapshot {
d := &DiskStats{
Name: stats.Name,
Reads: stats.Reads,
Writes: stats.Writes,
}
updateState(*d, config)
}
lastNow = now
}
func resolveSymlinks(config *Config) {
if config.Defaults.SymlinkPolicy == 0 {
return
}
for i := range config.Devices {
device := config.Devices[i]
if len(device.Name) == 0 {
realPath, err := io.RealPath(device.GivenName)
if err == nil {
config.Devices[i].Name = realPath
logToFile(config.Defaults.LogFile,
fmt.Sprintf("symlink %s resolved to %s", device.GivenName, realPath))
}
if err != nil && config.Defaults.Debug {
fmt.Printf("Cannot resolve sysmlink %s\n", device.GivenName)
}
}
}
}
func updateState(tmp DiskStats, config *Config) {
dsi := previousDiskStatsIndex(tmp.Name)
if dsi < 0 {
previousSnapshots = append(previousSnapshots, initDevice(tmp, config))
return
}
intervalDurationInSeconds := now.Unix() - lastNow.Unix()
if intervalDurationInSeconds > config.SkewTime.Milliseconds()/1000 {
/* we slept too long, assume a suspend event and disks may be spun up */
/* reset spin status and timers */
previousSnapshots[dsi].SpinUpAt = now
previousSnapshots[dsi].LastIoAt = now
previousSnapshots[dsi].SpunDown = false
logSpinupAfterSleep(previousSnapshots[dsi].Name, config.Defaults.LogFile)
}
ds := previousSnapshots[dsi]
if ds.Writes == tmp.Writes && ds.Reads == tmp.Reads {
if !ds.SpunDown {
/* no activity on this disk and still running */
idleDuration := now.Sub(ds.LastIoAt)
if ds.IdleTime != 0 && idleDuration > ds.IdleTime {
fmt.Printf("%s spindown\n", config.resolveDeviceGivenName(ds.Name))
device := fmt.Sprintf("/dev/%s", ds.Name)
if err := spindownDisk(device, ds.CommandType, ds.PowerCondition, config.Defaults.Debug); err != nil {
fmt.Println(err.Error())
}
previousSnapshots[dsi].SpinDownAt = now
previousSnapshots[dsi].SpunDown = true
}
}
} else {
/* disk had some activity */
if ds.SpunDown {
/* disk was spun down, thus it has just spun up */
fmt.Printf("%s spinup\n", config.resolveDeviceGivenName(ds.Name))
logSpinup(ds, config.Defaults.LogFile, config.resolveDeviceGivenName(ds.Name))
previousSnapshots[dsi].SpinUpAt = now
}
previousSnapshots[dsi].Reads = tmp.Reads
previousSnapshots[dsi].Writes = tmp.Writes
previousSnapshots[dsi].LastIoAt = now
previousSnapshots[dsi].SpunDown = false
}
if config.Defaults.Debug {
ds = previousSnapshots[dsi]
idleDuration := now.Sub(ds.LastIoAt)
fmt.Printf("disk=%s command=%s spunDown=%t "+
"reads=%d writes=%d idleTime=%v idleDuration=%v "+
"spindown=%s spinup=%s lastIO=%s\n",
ds.Name, ds.CommandType, ds.SpunDown,
ds.Reads, ds.Writes, ds.IdleTime.Seconds(), math.RoundToEven(idleDuration.Seconds()),
ds.SpinDownAt.Format(dateFormat), ds.SpinUpAt.Format(dateFormat), ds.LastIoAt.Format(dateFormat))
}
}
func previousDiskStatsIndex(diskName string) int {
for i, stats := range previousSnapshots {
if stats.Name == diskName {
return i
}
}
return -1
}
func initDevice(stats DiskStats, config *Config) DiskStats {
idle := config.Defaults.Idle
command := config.Defaults.CommandType
powerCondition := config.Defaults.PowerCondition
deviceConf := deviceConfig(stats.Name, config)
if deviceConf != nil {
idle = deviceConf.Idle
command = deviceConf.CommandType
powerCondition = deviceConf.PowerCondition
}
return DiskStats{
Name: stats.Name,
LastIoAt: time.Now(),
SpinUpAt: time.Now(),
SpunDown: false,
Writes: stats.Writes,
Reads: stats.Reads,
IdleTime: idle,
CommandType: command,
PowerCondition: powerCondition,
}
}
func deviceConfig(diskName string, config *Config) *DeviceConf {
for _, device := range config.Devices {
if device.Name == diskName {
return &device
}
}
return &DeviceConf{
Name: diskName,
CommandType: config.Defaults.CommandType,
PowerCondition: config.Defaults.PowerCondition,
Idle: config.Defaults.Idle,
}
}
func spindownDisk(device, command string, powerCondition uint8, debug bool) error {
switch command {
case SCSI:
if err := sgio.StartStopScsiDevice(device, powerCondition); err != nil {
return fmt.Errorf("cannot spindown scsi disk %s:\n%s\n", device, err.Error())
}
return nil
case ATA:
if err := sgio.StopAtaDevice(device, debug); err != nil {
return fmt.Errorf("cannot spindown ata disk %s:\n%s\n", device, err.Error())
}
return nil
}
return nil
}
func logSpinup(ds DiskStats, file, givenName string) {
now := time.Now()
text := fmt.Sprintf("date: %s, time: %s, disk: %s, running: %d, stopped: %d",
now.Format("2006-01-02"), now.Format("15:04:05"), givenName,
int(ds.SpinDownAt.Sub(ds.SpinUpAt).Seconds()), int(now.Sub(ds.SpinDownAt).Seconds()))
logToFile(file, text)
}
func logSpinupAfterSleep(name, file string) {
text := fmt.Sprintf("date: %s, time: %s, disk: %s, assuming disk spun up after long sleep",
now.Format("2006-01-02"), now.Format("15:04:05"), name)
logToFile(file, text)
}
func logToFile(file, text string) {
if len(file) == 0 {
return
}
cacheFile, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Fatalf("Cannot open file %s. Error: %s", file, err)
}
if _, err = cacheFile.WriteString(text + "\n"); err != nil {
log.Fatalf("Cannot write into file %s. Error: %s", file, err)
}
err = cacheFile.Close()
if err != nil {
log.Fatalf("Cannot close file %s. Error: %s", file, err)
}
}
func (c *Config) String() string {
var devices string
for _, device := range c.Devices {
devices += "{" + device.String() + "}"
}
return fmt.Sprintf("symlinkPolicy=%d, defaultIdle=%v, defaultCommand=%s, defaultPowerCondition=%v, debug=%t, logFile=%s, devices=%s",
c.Defaults.SymlinkPolicy, c.Defaults.Idle.Seconds(), c.Defaults.CommandType, c.Defaults.PowerCondition, c.Defaults.Debug, c.Defaults.LogFile, devices)
}
func (dc *DeviceConf) String() string {
return fmt.Sprintf("name=%s, givenName=%s, idle=%v, commandType=%s, powerCondition=%v",
dc.Name, dc.GivenName, dc.Idle.Seconds(), dc.CommandType, dc.PowerCondition)
}

View File

@ -1,50 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package io
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
func RealPath(path string) (string, error) {
if path[0] != '/' {
return path, nil
}
if !strings.Contains(path, "by-") {
return filepath.Base(path), nil
}
s, err := os.Readlink(path)
if err == nil {
device := filepath.Base(s)
/* remove partition numbers, if any */
for {
i := device[len(device)-1:]
_, err := strconv.Atoi(i)
if err != nil {
break
}
device = device[:len(device)-1]
}
return device, nil
}
return "", fmt.Errorf("cannot find device for %s", path)
}

View File

@ -1,103 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package io
import (
"fmt"
"os"
"testing"
)
func TestRealPath(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want string
symlinkTarget string
expectError bool
}{
{
name: "only device name",
args: args{path: "sda"},
want: "sda",
},
{
name: "full device path",
args: args{path: "/tmp/dev/sda"},
want: "sda",
},
{
name: "wrong symlink by id",
args: args{path: "/tmp/dev/disk/by-id/ata-SAMSUNG_HD103SJ"},
want: "",
expectError: true,
},
{
name: "symlink by id",
args: args{path: "/tmp/dev/disk/by-id/ata-SAMSUNG_HD103SJ"},
want: "sdc",
symlinkTarget: "/tmp/dev/sdc",
},
{
name: "symlink to partition by id",
args: args{path: "/tmp/dev/disk/by-label/disk2"},
want: "sdc",
symlinkTarget: "/tmp/dev/sdc1",
},
}
for _, tt := range tests {
err := os.RemoveAll("/tmp/dev")
if err != nil {
panic(err)
}
err = os.MkdirAll("/tmp/dev/disk/by-id", os.ModePerm)
if err != nil {
panic("cannot create tmp dir")
}
err = os.MkdirAll("/tmp/dev/disk/by-label", os.ModePerm)
if err != nil {
panic("cannot create tmp dir")
}
t.Run(tt.name, func(t *testing.T) {
if len(tt.want) > 0 {
disk := fmt.Sprintf("/tmp/dev/%s", tt.want)
_, err := os.Create(disk)
if err != nil {
panic(err)
}
if len(tt.symlinkTarget) > 0 {
err = os.Symlink(tt.symlinkTarget, tt.args.path)
if err != nil {
panic(err)
}
}
}
got, err := RealPath(tt.args.path)
if err != nil && tt.expectError == false {
panic(err)
}
if got != tt.want {
t.Errorf("RealPath() = %v, want %v", got, tt.want)
}
})
}
}

245
main.go
View File

@ -1,245 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"github.com/adelolmo/hd-idle/io"
"os"
"strconv"
"time"
)
const (
defaultIdleTime = 600 * time.Second
symlinkResolveOnce = 0
symlinkResolveRetry = 1
)
func main() {
if os.Getenv("START_HD_IDLE") == "false" {
fmt.Println("START_HD_IDLE=false exiting now.")
os.Exit(0)
}
singleDiskMode := false
var disk string
defaultConf := DefaultConf{
Idle: defaultIdleTime,
CommandType: SCSI,
PowerCondition: 0,
Debug: false,
SymlinkPolicy: 0,
}
var config = &Config{
Devices: []DeviceConf{},
Defaults: defaultConf,
NameMap: map[string]string{},
}
var deviceConf *DeviceConf
if len(os.Args) == 0 {
usage()
os.Exit(1)
}
for index, arg := range os.Args[1:] {
switch arg {
case "-t":
var err error
disk, err = argument(index)
if err != nil {
fmt.Println("Missing disk argument after -t. Must be a device (e.g. -t sda).")
os.Exit(1)
}
singleDiskMode = true
case "-s":
s, err := argument(index)
if err != nil {
fmt.Println("Missing symlink_policy. Must be 0 or 1.")
os.Exit(1)
}
switch s {
case "0":
config.Defaults.SymlinkPolicy = symlinkResolveOnce
case "1":
config.Defaults.SymlinkPolicy = symlinkResolveRetry
default:
fmt.Printf("Wrong symlink_policy -s %s. Must be 0 or 1.\n", s)
os.Exit(1)
}
case "-a":
if deviceConf != nil {
config.Devices = append(config.Devices, *deviceConf)
}
name, err := argument(index)
if err != nil {
fmt.Println("Missing disk argument after -a. Must be a device (e.g. -a sda).")
os.Exit(1)
}
deviceRealPath, err := io.RealPath(name)
if err != nil {
deviceRealPath = ""
fmt.Printf("Unable to resolve symlink: %s\n", name)
}
deviceConf = &DeviceConf{
Name: deviceRealPath,
GivenName: name,
Idle: config.Defaults.Idle,
CommandType: config.Defaults.CommandType,
PowerCondition: config.Defaults.PowerCondition,
}
config.NameMap[deviceRealPath] = name
case "-i":
s, err := argument(index)
if err != nil {
fmt.Println("Missing idle_time after -i. Must be a number.")
os.Exit(1)
}
idle, err := strconv.Atoi(s)
if err != nil {
fmt.Printf("Wrong idle_time -i %d. Must be a number.", idle)
os.Exit(1)
}
if deviceConf == nil {
config.Defaults.Idle = time.Duration(idle) * time.Second
break
}
deviceConf.Idle = time.Duration(idle) * time.Second
case "-c":
command, err := argument(index)
if err != nil {
fmt.Println("Missing command_type after -c. Must be one of: scsi, ata.")
os.Exit(1)
}
switch command {
case SCSI, ATA:
if deviceConf == nil {
config.Defaults.CommandType = command
break
}
deviceConf.CommandType = command
default:
fmt.Printf("Wrong command_type -c %s. Must be one of: scsi, ata.", command)
os.Exit(1)
}
case "-p":
s, err := argument(index)
if err != nil {
fmt.Println("Missing power condition after -p. Must be a number from 0-15.")
os.Exit(1)
}
powerCondition, err := strconv.ParseUint(s, 0, 4)
if err != nil {
fmt.Printf("Invalid power condition %s: %s", s, err.Error())
os.Exit(1)
}
if deviceConf == nil {
config.Defaults.PowerCondition = uint8(powerCondition)
break
}
deviceConf.PowerCondition = uint8(powerCondition)
case "-l":
logfile, err := argument(index)
if err != nil {
fmt.Println("Missing logfile after -l.")
os.Exit(1)
}
config.Defaults.LogFile = logfile
case "-d":
config.Defaults.Debug = true
case "-h":
usage()
os.Exit(0)
}
}
if singleDiskMode {
if err := spindownDisk(
disk,
config.Defaults.CommandType,
config.Defaults.PowerCondition,
config.Defaults.Debug,
); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
os.Exit(0)
}
if deviceConf != nil {
config.Devices = append(config.Devices, *deviceConf)
}
fmt.Println(config.String())
interval := poolInterval(config.Devices)
config.SkewTime = interval * 3
for {
ObserveDiskActivity(config)
time.Sleep(interval)
}
}
func argument(index int) (string, error) {
argIndex := index + 2
if argIndex >= len(os.Args) {
return "", fmt.Errorf("option requires argument")
}
arg := os.Args[argIndex]
if arg[:1] == "-" {
return "", fmt.Errorf("option requires argument")
}
return arg, nil
}
func usage() {
fmt.Println("usage: hd-idle [-t <disk>] [-s <symlink_policy>] [-a <name>] [-i <idle_time>] " +
"[-c <command_type>] [-p power_condition] [-l <logfile>] [-d] [-h]")
}
func poolInterval(deviceConfs []DeviceConf) time.Duration {
if len(deviceConfs) == 0 {
return defaultIdleTime / 10
}
interval := defaultIdleTime
for _, dev := range deviceConfs {
if dev.Idle == 0 {
continue
}
if dev.Idle < interval {
interval = dev.Idle
}
}
sleepTime := interval / 10
if sleepTime == 0 {
return time.Second
}
return sleepTime
}

View File

@ -1,32 +0,0 @@
package main
import (
"testing"
"time"
)
func TestIntervalWithZeroSecondsIdle(t *testing.T) {
confs := []DeviceConf{{
Name: "test",
GivenName: "test",
Idle: 0,
CommandType: "ata",
}}
interval := poolInterval(confs)
if interval != defaultIdleTime/10 {
t.Fatalf("interval should be the default. it was %d", interval)
}
}
func TestIntervalWith300SecondsIdle(t *testing.T) {
confs := []DeviceConf{{
Name: "test",
GivenName: "test",
Idle: 300 * time.Second,
CommandType: "ata",
}}
interval := poolInterval(confs)
if interval != 30*time.Second {
t.Fatalf("interval should be the 30s. it was %v", interval)
}
}

View File

@ -1,132 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package sgio
import (
"fmt"
"github.com/benmcclelland/sgio"
"os"
)
const (
sgAta16 = 0x85 // ATA PASS-THROUGH(16)
sgAta12 = 0xa1 // ATA PASS-THROUGH (12)
sgAtaProtoNonData = 3 << 1
ataUsingLba = 1 << 6
ataOpStandbyNow1 = 0xe0 // https://wiki.osdev.org/ATA/ATAPI_Power_Management
ataOpStandbyNow2 = 0x94 // Retired in ATA4. Did not coexist with ATAPI.
)
func StopAtaDevice(device string, debug bool) error {
f, err := openDevice(device)
if err != nil {
return err
}
switch NewAtaDevice(device, debug).deviceType() {
case Jmicron:
if err = sendSgio(f, jmicronGetRegisters(), debug); err != nil {
return err
}
if debug {
fmt.Println(" issuing standby command")
}
if err = sendSgio(f, jmicronStandby(), debug); err != nil {
return err
}
return nil
default:
if debug {
fmt.Println(" issuing standby command")
}
if err = sendAtaCommand(f, ataOpStandbyNow1, debug); err != nil {
if err = sendAtaCommand(f, ataOpStandbyNow2, debug); err != nil {
return err
}
}
}
if err := f.Close(); err != nil {
return fmt.Errorf("cannot close file %s. Error: %s", device, err)
}
return nil
}
func jmicronGetRegisters() []uint8 {
cbd := []uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // len 12
cbd[0] = 0xdf
cbd[1] = 0x10 // read
cbd[4] = 0x01
cbd[6] = 0x72
cbd[7] = 0x0f
cbd[11] = 0xfd
return cbd
}
func jmicronStandby() []uint8 {
cbd := []uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // len 12
cbd[0] = 0xdf
cbd[1] = 0x10
cbd[10] = 0xa0 // device port. either 0xa0 or 0xb0
cbd[11] = ataOpStandbyNow1
return cbd
}
func sendAtaCommand(f *os.File, command uint8, debug bool) error {
cbd := []uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // len 16
cbd[0] = sgAta16
cbd[1] = sgAtaProtoNonData
cbd[13] = ataUsingLba
cbd[14] = command
return sendSgio(f, cbd, debug)
}
func sendSgio(f *os.File, inqCmdBlk []uint8, debug bool) error {
senseBuf := make([]byte, sgio.SENSE_BUF_LEN)
ioHdr := &sgio.SgIoHdr{
InterfaceID: 'S', // 0 4
DxferDirection: SgDxferNone, // 4 4
CmdLen: uint8(len(inqCmdBlk)), // 8 1
MxSbLen: sgio.SENSE_BUF_LEN, // 9 1
Cmdp: &inqCmdBlk[0], // 24 8
Sbp: &senseBuf[0], // 32 8
Timeout: 0, // 40 4
}
if debug {
dumpBytes(inqCmdBlk)
}
if err := sgio.SgioSyscall(f, ioHdr); err != nil {
return err
}
if err := sgio.CheckSense(ioHdr, &senseBuf); err != nil {
return err
}
return nil
}
func dumpBytes(p []uint8) {
fmt.Print("outgoing cdb: ")
for i := range p {
fmt.Printf("%02x ", p[i])
}
fmt.Print("\n")
}

View File

@ -1,47 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package sgio
import (
"fmt"
"github.com/benmcclelland/sgio"
"os"
"syscall"
"unsafe"
)
const SgDxferNone = -1
func openDevice(fname string) (*os.File, error) {
f, err := os.OpenFile(fname, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
var version uint32
if (ioctl(f.Fd(), sgio.SG_GET_VERSION_NUM, uintptr(unsafe.Pointer(&version))) != nil) || (version < 30000) {
return nil, fmt.Errorf("device does not appear to be an sg device")
}
return f, nil
}
func ioctl(fd, cmd, ptr uintptr) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, ptr)
if err != 0 {
return err
}
return nil
}

View File

@ -1,63 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2018 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package sgio
import (
"fmt"
"github.com/benmcclelland/sgio"
)
// https://en.wikipedia.org/wiki/SCSI_command
const startStopUnit = 0x1b
func StartStopScsiDevice(device string, powerCondition uint8) error {
f, err := openDevice(device)
if err != nil {
return err
}
senseBuf := make([]byte, sgio.SENSE_BUF_LEN)
//See https://www.seagate.com/files/staticfiles/support/docs/manual/Interface%20manuals/100293068j.pdf - 3.49 START STOP UNIT command
inqCmdBlk := []uint8{
startStopUnit,
0, //Reserved (7 bit) + IMMED
0, //Reserved (8 bit)
0, //Reserved (4 bit) + POWER CONDITION MODIFER
powerCondition << 4, //POWER CONDITION + Reserved (1 bit) + NO_ FLUSH + LOEJ + LOEJ
0} //CONTROL
ioHdr := &sgio.SgIoHdr{
InterfaceID: 'S',
DxferDirection: SgDxferNone,
Cmdp: &inqCmdBlk[0],
CmdLen: uint8(len(inqCmdBlk)),
Sbp: &senseBuf[0],
MxSbLen: sgio.SENSE_BUF_LEN,
}
if err := sgio.SgioSyscall(f, ioHdr); err != nil {
return err
}
if err := sgio.CheckSense(ioHdr, &senseBuf); err != nil {
return err
}
if err := f.Close(); err != nil {
return fmt.Errorf("cannot close file %s. Error: %s", device, err)
}
return nil
}

View File

@ -1,127 +0,0 @@
// hd-idle - spin down idle hard disks
// Copyright (C) 2023 Andoni del Olmo
//
// 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 <http://www.gnu.org/licenses/>.
package sgio
import (
"fmt"
"os"
"path/filepath"
"strings"
)
const (
Jmicron = iota
Unknown = iota
sysblock = "/sys/block"
jmicron = "152d"
)
type AtaDevice struct {
device string
debug bool
fsRoot string
}
func NewAtaDevice(device string, debug bool) AtaDevice {
return AtaDevice{
device: device,
debug: debug,
fsRoot: sysblock,
}
}
type apt struct {
idVendor, idProduct, bcdDevice string
}
func (a apt) isJmicron() bool {
if a.idVendor != jmicron {
return false
}
switch a.idProduct {
case "2329", "2336", "2338", "2339":
return true
}
return false
}
func (ad AtaDevice) deviceType() int {
a, err := ad.identifyDevice(ad.device)
if err != nil {
if ad.debug {
fmt.Println("APT: Unsupported device")
}
return Unknown
}
if a.isJmicron() {
if ad.debug {
fmt.Println("APT: Found supported device jmicron")
}
return Jmicron
}
if ad.debug {
fmt.Println("APT: Unsupported device")
}
return Unknown
}
func (ad AtaDevice) identifyDevice(device string) (apt, error) {
diskname := strings.Split(device, "/")[2]
sysblockdisk := filepath.Join(ad.fsRoot, diskname)
idVendor, err := findSystemFile(sysblockdisk, "idVendor")
if err != nil {
return apt{}, err
}
idProduct, err := findSystemFile(sysblockdisk, "idProduct")
if err != nil {
return apt{}, err
}
bcdDevice, err := findSystemFile(sysblockdisk, "bcdDevice")
if err != nil {
return apt{}, err
}
if ad.debug {
fmt.Printf("APT: USB ID = 0x%s:0x%s (0x%3s)\n", idVendor, idProduct, bcdDevice)
}
return apt{
idVendor: idVendor,
idProduct: idProduct,
bcdDevice: bcdDevice,
},
nil
}
func findSystemFile(systemRoot, filename string) (string, error) {
_, err := os.ReadFile(filepath.Join(systemRoot, filename))
relativeDir := ""
var content []byte
depth := 0
for depth < 20 {
if err == nil {
return strings.TrimSpace(string(content)), nil
}
relativeDir += "/.."
content, err = os.ReadFile(systemRoot + relativeDir + "/" + filename)
depth++
}
return "", fmt.Errorf("device not found")
}

View File

@ -1,80 +0,0 @@
package sgio
import (
"log"
"os"
"path/filepath"
"strings"
"testing"
)
const (
tmpDir = "/tmp/hd-idle/ata"
)
func TestAtaDevice_deviceType(t *testing.T) {
type fields struct {
device string
debug bool
fsRoot string
idVendor, idProduct, bcdDevice string
}
tests := []struct {
name string
fields fields
want int
}{
{
name: "find jmicron controller",
fields: fields{
device: "/dev/sde",
debug: true,
fsRoot: filepath.Join(tmpDir, "sys", "block"),
idVendor: "152d",
idProduct: "2339",
bcdDevice: "100",
},
want: Jmicron,
},
{
name: "unknown device",
fields: fields{
device: "/dev/sde",
debug: true,
fsRoot: filepath.Join(tmpDir, "sys", "block"),
idVendor: "1058",
idProduct: "25a3",
bcdDevice: "1021",
},
want: Unknown,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ad := AtaDevice{
device: tt.fields.device,
debug: tt.fields.debug,
fsRoot: tt.fields.fsRoot,
}
err := os.RemoveAll(tmpDir)
if err != nil {
log.Fatal(err)
}
infoDir := filepath.Join(tmpDir, "/sys/devices/pci0000:00/0000:00:15.0/usb2/2-2/2-2.3/2-2.3.2")
diskname := strings.Split(tt.fields.device, "/")[2]
deviceRoot := infoDir + "/2-2.3.2:1.0/host5/target5:0:0/5:0:0:0/block/" + diskname
_ = os.MkdirAll(deviceRoot, 0755)
_ = os.WriteFile(filepath.Join(infoDir, "idVendor"), []byte(tt.fields.idVendor), 0666)
_ = os.WriteFile(filepath.Join(infoDir, "idProduct"), []byte(tt.fields.idProduct), 0666)
_ = os.WriteFile(filepath.Join(infoDir, "bcdDevice"), []byte(tt.fields.bcdDevice), 0666)
_ = os.MkdirAll(filepath.Join(tmpDir, "/sys/block"), 0755)
if err = os.Symlink(deviceRoot, filepath.Join(tmpDir, "/sys/block", diskname)); err != nil {
log.Fatal(err)
}
if got := ad.deviceType(); got != tt.want {
t.Errorf("deviceType() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,24 +0,0 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2017 Ben McClelland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,28 +0,0 @@
# sgio
golang library for issuing SCSI commands with SG_IO ioctl
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/benmcclelland/sgio)
See TestUnitReady() for example function using SG_IO
example:
```
f, err := OpenScsiDevice("/dev/sg0")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
```
Fill out SgIoHdr for SCSI command
```
ioHdr := &SgIoHdr{...}
err := SgioSyscall(f, ioHdr)
if err != nil {
log.Fatalln(err)
}
err = CheckSense(ioHdr, &senseBuf)
if err != nil {
log.Fatalln(err)
}
```

View File

@ -1,219 +0,0 @@
package sgio
var errmap map[string]string
func GetErrString(a, b byte) string {
return errmap[stringify(a, b)]
}
func init() {
errmap = make(map[string]string)
errmap[stringify(0x00, 0x00)] = "NO ADDITIONAL SENSE INFORMATION"
errmap[stringify(0x00, 0x01)] = "FILEMARK DETECTED"
errmap[stringify(0x00, 0x02)] = "END-OF-PARTITION/MEDIUM DETECTED"
errmap[stringify(0x00, 0x03)] = "SETMARK DETECTED"
errmap[stringify(0x00, 0x04)] = "BEGINNING-OF-PARTITION/MEDIUM DETECTED"
errmap[stringify(0x00, 0x05)] = "END-OF-DATA DETECTED"
errmap[stringify(0x00, 0x06)] = "I/O PROCESS TERMINATED"
errmap[stringify(0x00, 0x11)] = "AUDIO PLAY OPERATION IN PROGRESS"
errmap[stringify(0x00, 0x12)] = "AUDIO PLAY OPERATION PAUSED"
errmap[stringify(0x00, 0x13)] = "AUDIO PLAY OPERATION SUCCESSFULLY COMPLETED"
errmap[stringify(0x00, 0x14)] = "AUDIO PLAY OPERATION STOPPED DUE TO ERROR"
errmap[stringify(0x00, 0x15)] = "NO CURRENT AUDIO STATUS TO RETURN"
errmap[stringify(0x01, 0x00)] = "NO INDEX/SECTOR SIGNAL"
errmap[stringify(0x02, 0x00)] = "NO SEEK COMPLETE"
errmap[stringify(0x03, 0x00)] = "PERIPHERAL DEVICE WRITE FAULT"
errmap[stringify(0x03, 0x01)] = "NO WRITE CURRENT"
errmap[stringify(0x03, 0x02)] = "EXCESSIVE WRITE ERRORS"
errmap[stringify(0x04, 0x00)] = "LOGICAL UNIT NOT READY, CAUSE NOT REPORTABLE"
errmap[stringify(0x04, 0x01)] = "LOGICAL UNIT IS IN PROCESS OF BECOMING READY"
errmap[stringify(0x04, 0x02)] = "LOGICAL UNIT NOT READY, INITIALIZING COMMAND REQUIRED"
errmap[stringify(0x04, 0x03)] = "LOGICAL UNIT NOT READY, MANUAL INTERVENTION REQUIRED"
errmap[stringify(0x04, 0x04)] = "LOGICAL UNIT NOT READY, FORMAT IN PROGRESS"
errmap[stringify(0x05, 0x00)] = "LOGICAL UNIT DOES NOT RESPOND TO SELECTION"
errmap[stringify(0x06, 0x00)] = "REFERENCE POSITION FOUND"
errmap[stringify(0x07, 0x00)] = "MULTIPLE PERIPHERAL DEVICES SELECTED"
errmap[stringify(0x08, 0x00)] = "LOGICAL UNIT COMMUNICATION FAILURE"
errmap[stringify(0x08, 0x01)] = "LOGICAL UNIT COMMUNICATION TIME-OUT"
errmap[stringify(0x08, 0x02)] = "LOGICAL UNIT COMMUNICATION PARITY ERROR"
errmap[stringify(0x09, 0x00)] = "TRACK FOLLOWING ERROR"
errmap[stringify(0x09, 0x01)] = "TRA CKING SERVO FAILURE"
errmap[stringify(0x09, 0x02)] = "FOC US SERVO FAILURE"
errmap[stringify(0x09, 0x03)] = "SPI NDLE SERVO FAILURE"
errmap[stringify(0x0A, 0x00)] = "ERROR LOG OVERFLOW"
errmap[stringify(0x0B, 0x00)] = ""
errmap[stringify(0x0C, 0x00)] = "WRITE ERROR"
errmap[stringify(0x0C, 0x01)] = "WRITE ERROR RECOVERED WITH AUTO REALLOCATION"
errmap[stringify(0x0C, 0x02)] = "WRITE ERROR - AUTO REALLOCATION FAILED"
errmap[stringify(0x0D, 0x00)] = ""
errmap[stringify(0x0E, 0x00)] = ""
errmap[stringify(0x0F, 0x00)] = ""
errmap[stringify(0x10, 0x00)] = "ID CRC OR ECC ERROR"
errmap[stringify(0x11, 0x00)] = "UNRECOVERED READ ERROR"
errmap[stringify(0x11, 0x01)] = "READ RETRIES EXHAUSTED"
errmap[stringify(0x11, 0x02)] = "ERROR TOO LONG TO CORRECT"
errmap[stringify(0x11, 0x03)] = "MULTIPLE READ ERRORS"
errmap[stringify(0x11, 0x04)] = "UNRECOVERED READ ERROR - AUTO REALLOCATE FAILED"
errmap[stringify(0x11, 0x05)] = "L-EC UNCORRECTABLE ERROR"
errmap[stringify(0x11, 0x06)] = "CIRC UNRECOVERED ERROR"
errmap[stringify(0x11, 0x07)] = "DATA RESYCHRONIZATION ERROR"
errmap[stringify(0x11, 0x08)] = "INCOMPLETE BLOCK READ"
errmap[stringify(0x11, 0x09)] = "NO GAP FOUND"
errmap[stringify(0x11, 0x0A)] = "MISCORRECTED ERROR"
errmap[stringify(0x11, 0x0B)] = "UNRECOVERED READ ERROR - RECOMMEND REASSIGNMENT"
errmap[stringify(0x11, 0x0C)] = "UNRECOVERED READ ERROR - RECOMMEND REWRITE THE DATA"
errmap[stringify(0x12, 0x00)] = "ADDRESS MARK NOT FOUND FOR ID FIELD"
errmap[stringify(0x13, 0x00)] = "ADDRESS MARK NOT FOUND FOR DATA FIELD"
errmap[stringify(0x14, 0x00)] = "RECORDED ENTITY NOT FOUND"
errmap[stringify(0x14, 0x01)] = "RECORD NOT FOUND"
errmap[stringify(0x14, 0x02)] = "FILEMARK OR SETMARK NOT FOUND"
errmap[stringify(0x14, 0x03)] = "END-OF-DATA NOT FOUND"
errmap[stringify(0x14, 0x04)] = "BLOCK SEQUENCE ERROR"
errmap[stringify(0x15, 0x00)] = "RANDOM POSITIONING ERROR"
errmap[stringify(0x15, 0x01)] = "MECHANICAL POSITIONING ERROR"
errmap[stringify(0x15, 0x02)] = "POSITIONING ERROR DETECTED BY READ OF MEDIUM"
errmap[stringify(0x16, 0x00)] = "DATA SYNCHRONIZATION MARK ERROR"
errmap[stringify(0x17, 0x00)] = "RECOVERED DATA WITH NO ERROR CORRECTION APPLIED"
errmap[stringify(0x17, 0x01)] = "RECOVERED DATA WITH RETRIES"
errmap[stringify(0x17, 0x02)] = "RECOVERED DATA WITH POSITIVE HEAD OFFSET"
errmap[stringify(0x17, 0x03)] = "RECOVERED DATA WITH NEGATIVE HEAD OFFSET"
errmap[stringify(0x17, 0x04)] = "RECOVERED DATA WITH RETRIES AND/OR CIRC APPLIED"
errmap[stringify(0x17, 0x05)] = "RECOVERED DATA USING PREVIOUS SECTOR ID"
errmap[stringify(0x17, 0x06)] = "RECOVERED DATA WITHOUT ECC - DATA AUTO-REALLOCATED"
errmap[stringify(0x17, 0x07)] = "RECOVERED DATA WITHOUT ECC - RECOMMEND REASSIGNMENT"
errmap[stringify(0x17, 0x08)] = "RECOVERED DATA WITHOUT ECC - RECOMMEND REWRITE"
errmap[stringify(0x18, 0x00)] = "RECOVERED DATA WITH ERROR CORRECTION APPLIED"
errmap[stringify(0x18, 0x01)] = "RECOVERED DATA WITH ERROR CORRECTION & RETRIES APPLIED"
errmap[stringify(0x18, 0x02)] = "RECOVERED DATA - DATA AUTO-REALLOCATED"
errmap[stringify(0x18, 0x03)] = "RECOVERED DATA WITH CIRC"
errmap[stringify(0x18, 0x04)] = "RECOVERED DATA WITH LEC"
errmap[stringify(0x18, 0x05)] = "RECOVERED DATA - RECOMMEND REASSIGNMENT"
errmap[stringify(0x18, 0x06)] = "RECOVERED DATA - RECOMMEND REWRITE"
errmap[stringify(0x19, 0x00)] = "DEFECT LIST ERROR"
errmap[stringify(0x19, 0x01)] = "DEFECT LIST NOT AVAILABLE"
errmap[stringify(0x19, 0x02)] = "DEFECT LIST ERROR IN PRIMARY LIST"
errmap[stringify(0x19, 0x03)] = "DEFECT LIST ERROR IN GROWN LIST"
errmap[stringify(0x1A, 0x00)] = "PARAMETER LIST LENGTH ERROR"
errmap[stringify(0x1B, 0x00)] = "SYNCHRONOUS DATA TRANSFER ERROR"
errmap[stringify(0x1C, 0x00)] = "DEFECT LIST NOT FOUND"
errmap[stringify(0x1C, 0x01)] = "PRIMARY DEFECT LIST NOT FOUND"
errmap[stringify(0x1C, 0x02)] = "GROWN DEFECT LIST NOT FOUND"
errmap[stringify(0x1D, 0x00)] = "MISCOMPARE DURING VERIFY OPERATION"
errmap[stringify(0x1E, 0x00)] = "RECOVERED ID WITH ECC"
errmap[stringify(0x1F, 0x00)] = ""
errmap[stringify(0x20, 0x00)] = "INVALID COMMAND OPERATION CODE"
errmap[stringify(0x21, 0x00)] = "LOGICAL BLOCK ADDRESS OUT OF RANGE"
errmap[stringify(0x21, 0x01)] = "INVALID ELEMENT ADDRESS"
errmap[stringify(0x22, 0x00)] = "ILLEGAL FUNCTION (SHOULD USE 20 00, 24 00, OR 26 00)"
errmap[stringify(0x23, 0x00)] = ""
errmap[stringify(0x24, 0x00)] = "INVALID FIELD IN CDB"
errmap[stringify(0x25, 0x00)] = "LOGICAL UNIT NOT SUPPORTED"
errmap[stringify(0x26, 0x00)] = "INVALID FIELD IN PARAMETER LIST"
errmap[stringify(0x26, 0x01)] = "PARAMETER NOT SUPPORTED"
errmap[stringify(0x26, 0x02)] = "PARAMETER VALUE INVALID"
errmap[stringify(0x26, 0x03)] = "THRESHOLD PARAMETERS NOT SUPPORTED"
errmap[stringify(0x27, 0x00)] = "WRITE PROTECTED"
errmap[stringify(0x28, 0x00)] = "NOT READY TO READY TRANSITION(MEDIUM MAY HAVE CHANGED)"
errmap[stringify(0x28, 0x01)] = "IMPORT OR EXPORT ELEMENT ACCESSED"
errmap[stringify(0x29, 0x00)] = "POWER ON, RESET, OR BUS DEVICE RESET OCCURRED"
errmap[stringify(0x2A, 0x00)] = "PARAMETERS CHANGED"
errmap[stringify(0x2A, 0x01)] = "MODE PARAMETERS CHANGED"
errmap[stringify(0x2A, 0x02)] = "LOG PARAMETERS CHANGED"
errmap[stringify(0x2B, 0x00)] = "COPY CANNOT EXECUTE SINCE HOST CANNOT DISCONNECT"
errmap[stringify(0x2C, 0x00)] = "COMMAND SEQUENCE ERROR"
errmap[stringify(0x2C, 0x01)] = "TOO MANY WINDOWS SPECIFIED"
errmap[stringify(0x2C, 0x02)] = "INVALID COMBINATION OF WINDOWS SPECIFIED"
errmap[stringify(0x2D, 0x00)] = "OVERWRITE ERROR ON UPDATE IN PLACE"
errmap[stringify(0x2E, 0x00)] = ""
errmap[stringify(0x2F, 0x00)] = "COMMANDS CLEARED BY ANOTHER INITIATOR"
errmap[stringify(0x30, 0x00)] = "INCOMPATIBLE MEDIUM INSTALLED"
errmap[stringify(0x30, 0x01)] = "CANNOT READ MEDIUM - UNKNOWN FORMAT"
errmap[stringify(0x30, 0x02)] = "CANNOT READ MEDIUM - INCOMPATIBLE FORMAT"
errmap[stringify(0x30, 0x03)] = "CLEANING CARTRIDGE INSTALLED"
errmap[stringify(0x31, 0x00)] = "MEDIUM FORMAT CORRUPTED"
errmap[stringify(0x31, 0x01)] = "FORMAT COMMAND FAILED"
errmap[stringify(0x32, 0x00)] = "NO DEFECT SPARE LOCATION AVAILABLE"
errmap[stringify(0x32, 0x01)] = "DEFECT LIST UPDATE FAILURE"
errmap[stringify(0x33, 0x00)] = "TAPE LENGTH ERROR"
errmap[stringify(0x34, 0x00)] = ""
errmap[stringify(0x35, 0x00)] = ""
errmap[stringify(0x36, 0x00)] = "RIBBON, INK, OR TONER FAILURE"
errmap[stringify(0x37, 0x00)] = "ROUNDED PARAMETER"
errmap[stringify(0x38, 0x00)] = ""
errmap[stringify(0x39, 0x00)] = "SAVING PARAMETERS NOT SUPPORTED"
errmap[stringify(0x3A, 0x00)] = "MEDIUM NOT PRESENT"
errmap[stringify(0x3B, 0x00)] = "SEQUENTIAL POSITIONING ERROR"
errmap[stringify(0x3B, 0x01)] = "TAPE POSITION ERROR AT BEGINNING-OF-MEDIUM"
errmap[stringify(0x3B, 0x02)] = "TAPE POSITION ERROR AT END-OF-MEDIUM"
errmap[stringify(0x3B, 0x03)] = "TAPE OR ELECTRONIC VERTICAL FORMS UNIT NOT READY"
errmap[stringify(0x3B, 0x04)] = "SLEW FAILURE"
errmap[stringify(0x3B, 0x05)] = "PAPER JAM"
errmap[stringify(0x3B, 0x06)] = "FAILED TO SENSE TOP-OF-FORM"
errmap[stringify(0x3B, 0x07)] = "FAILED TO SENSE BOTTOM-OF-FORM"
errmap[stringify(0x3B, 0x08)] = "REPOSITION ERROR"
errmap[stringify(0x3B, 0x09)] = "READ PAST END OF MEDIUM"
errmap[stringify(0x3B, 0x0A)] = "READ PAST BEGINNING OF MEDIUM"
errmap[stringify(0x3B, 0x0B)] = "POSITION PAST END OF MEDIUM"
errmap[stringify(0x3B, 0x0C)] = "POSITION PAST BEGINNING OF MEDIUM"
errmap[stringify(0x3B, 0x0D)] = "MEDIUM DESTINATION ELEMENT FULL"
errmap[stringify(0x3B, 0x0E)] = "MEDIUM SOURCE ELEMENT EMPTY"
errmap[stringify(0x3C, 0x00)] = ""
errmap[stringify(0x3D, 0x00)] = "INVALID BITS IN IDENTIFY MESSAGE"
errmap[stringify(0x3E, 0x00)] = "LOGICAL UNIT HAS NOT SELF-CONFIGURED YET"
errmap[stringify(0x3F, 0x00)] = "TARGET OPERATING CONDITIONS HAVE CHANGED"
errmap[stringify(0x3F, 0x01)] = "MICROCODE HAS BEEN CHANGED"
errmap[stringify(0x3F, 0x02)] = "CHANGED OPERATING DEFINITION"
errmap[stringify(0x3F, 0x03)] = "INQUIRY DATA HAS CHANGED"
errmap[stringify(0x40, 0x00)] = "RAM FAILURE (SHOULD USE 40 NN)"
//errmap[stringify(0x40, 0xNN)] = "DIAGNOSTIC FAILURE ON COMPONENT NN (80H-FFH)"
errmap[stringify(0x41, 0x00)] = "DATA PATH FAILURE (SHOULD USE 40 NN)"
errmap[stringify(0x42, 0x00)] = "POWER-ON OR SELF-TEST FAILURE (SHOULD USE 40 NN)"
errmap[stringify(0x43, 0x00)] = "MESSAGE ERROR"
errmap[stringify(0x44, 0x00)] = "INTERNAL TARGET FAILURE"
errmap[stringify(0x45, 0x00)] = "SELECT OR RESELECT FAILURE"
errmap[stringify(0x46, 0x00)] = "UNSUCCESSFUL SOFT RESET"
errmap[stringify(0x47, 0x00)] = "SCSI PARITY ERROR"
errmap[stringify(0x48, 0x00)] = "INITIATOR DETECTED ERROR MESSAGE RECEIVED"
errmap[stringify(0x49, 0x00)] = "INVALID MESSAGE ERROR"
errmap[stringify(0x4A, 0x00)] = "COMMAND PHASE ERROR"
errmap[stringify(0x4B, 0x00)] = "DATA PHASE ERROR"
errmap[stringify(0x4C, 0x00)] = "LOGICAL UNIT FAILED SELF-CONFIGURATION"
errmap[stringify(0x4D, 0x00)] = ""
errmap[stringify(0x4E, 0x00)] = "OVERLAPPED COMMANDS ATTEMPTED"
errmap[stringify(0x4F, 0x00)] = ""
errmap[stringify(0x50, 0x00)] = "WRITE APPEND ERROR"
errmap[stringify(0x50, 0x01)] = "WRITE APPEND POSITION ERROR"
errmap[stringify(0x50, 0x02)] = "POSITION ERROR RELATED TO TIMING"
errmap[stringify(0x51, 0x00)] = "ERASE FAILURE"
errmap[stringify(0x52, 0x00)] = "CARTRIDGE FAULT"
errmap[stringify(0x53, 0x00)] = "MEDIA LOAD OR EJECT FAILED"
errmap[stringify(0x53, 0x01)] = "UNLOAD TAPE FAILURE"
errmap[stringify(0x53, 0x02)] = "MEDIUM REMOVAL PREVENTED"
errmap[stringify(0x54, 0x00)] = "SCSI TO HOST SYSTEM INTERFACE FAILURE"
errmap[stringify(0x55, 0x00)] = "SYSTEM RESOURCE FAILURE"
errmap[stringify(0x56, 0x00)] = ""
errmap[stringify(0x57, 0x00)] = "UNABLE TO RECOVER TABLE-OF-CONTENTS"
errmap[stringify(0x58, 0x00)] = "GENERATION DOES NOT EXIST"
errmap[stringify(0x59, 0x00)] = "UPDATED BLOCK READ"
errmap[stringify(0x5A, 0x00)] = "OPERATOR REQUEST OR STATE CHANGE INPUT (UNSPECIFIED)"
errmap[stringify(0x5A, 0x01)] = "OPERATOR MEDIUM REMOVAL REQUEST"
errmap[stringify(0x5A, 0x02)] = "OPERATOR SELECTED WRITE PROTECT"
errmap[stringify(0x5A, 0x03)] = "OPERATOR SELECTED WRITE PERMIT"
errmap[stringify(0x5B, 0x00)] = "LOG EXCEPTION"
errmap[stringify(0x5B, 0x01)] = "THRESHOLD CONDITION MET"
errmap[stringify(0x5B, 0x02)] = "LOG COUNTER AT MAXIMUM"
errmap[stringify(0x5B, 0x03)] = "LOG LIST CODES EXHAUSTED"
errmap[stringify(0x5C, 0x00)] = "RPL STATUS CHANGE"
errmap[stringify(0x5C, 0x01)] = "SPINDLES SYNCHRONIZED"
errmap[stringify(0x5C, 0x02)] = "SPINDLES NOT SYNCHRONIZED"
errmap[stringify(0x5D, 0x00)] = ""
errmap[stringify(0x5E, 0x00)] = ""
errmap[stringify(0x5F, 0x00)] = ""
errmap[stringify(0x60, 0x00)] = "LAMP FAILURE"
errmap[stringify(0x61, 0x00)] = "VIDEO ACQUISITION ERROR"
errmap[stringify(0x61, 0x01)] = "UNABLE TO ACQUIRE VIDEO"
errmap[stringify(0x61, 0x02)] = "OUT OF FOCUS"
errmap[stringify(0x62, 0x00)] = "SCAN HEAD POSITIONING ERROR"
errmap[stringify(0x63, 0x00)] = "END OF USER AREA ENCOUNTERED ON THIS TRACK"
errmap[stringify(0x64, 0x00)] = "ILLEGAL MODE FOR THIS TRACK"
}

View File

@ -1,24 +0,0 @@
package sgio
import (
"bytes"
"encoding/hex"
)
func stringify(a, b byte) string {
return dumpHex(append([]byte{a}, b))
}
func dumpHex(data []byte) string {
var buf bytes.Buffer
var tmp [3]byte
for i := range data {
hex.Encode(tmp[:], data[i:i+1])
tmp[2] = ' '
_, err := buf.Write(tmp[:3])
if err != nil {
return ""
}
}
return buf.String()
}

View File

@ -1,160 +0,0 @@
package sgio
import (
"bytes"
"fmt"
"os"
"syscall"
"unsafe"
)
const (
SG_GET_VERSION_NUM = 0x2282
SG_IO = 0x2285
SG_INFO_OK_MASK = 0x1
SG_INFO_OK = 0x0
SG_DXFER_TO_DEV = -2
SG_DXFER_FROM_DEV = -3
SG_DXFER_TO_FROM_DEV = -4
INQ_CMD_CODE = 0x12
INQ_REPLY_LEN = 96
SENSE_BUF_LEN = 32
TIMEOUT_20_SECS = 20000
)
// pahole for sg_io_hdr_t on amd64
/*
* struct sg_io_hdr {
* int interface_id; // 0 4
* int dxfer_direction; // 4 4
* unsigned char cmd_len; // 8 1
* unsigned char mx_sb_len; // 9 1
* short unsigned int iovec_count; // 10 2
* unsigned int dxfer_len; // 12 4
* void * dxferp; // 16 8
* unsigned char * cmdp; // 24 8
* unsigned char * sbp; // 32 8
* unsigned int timeout; // 40 4
* unsigned int flags; // 44 4
* int pack_id; // 48 4
*
* // XXX 4 bytes hole, try to pack
*
* void * usr_ptr; // 56 8
* // --- cacheline 1 boundary (64 bytes) ---
* unsigned char status; // 64 1
* unsigned char masked_status; // 65 1
* unsigned char msg_status; // 66 1
* unsigned char sb_len_wr; // 67 1
* short unsigned int host_status; // 68 2
* short unsigned int driver_status; // 70 2
* int resid; // 72 4
* unsigned int duration; // 76 4
* unsigned int info; // 80 4
*
* // size: 88, cachelines: 2, members: 22
* // sum members: 80, holes: 1, sum holes: 4
* // padding: 4
* // last cacheline: 24 bytes
* };
*/
// SgIoHdr is our version of sg_io_hdr_t that gets passed to the SG_IO ioctl
type SgIoHdr struct {
InterfaceID int32
DxferDirection int32
CmdLen uint8
MxSbLen uint8
IovecCount uint16
DxferLen uint32
Dxferp *byte
Cmdp *uint8
Sbp *byte
Timeout uint32
Flags uint32
PackID int32
pad0 [4]byte
UsrPtr *byte
Status uint8
MaskedStatus uint8
MsgStatus uint8
SbLenWr uint8
HostStatus uint16
DriverStatus uint16
Resid int32
Duration uint32
Info uint32
}
func TestUnitReady(f *os.File) error {
senseBuf := make([]byte, SENSE_BUF_LEN)
inqCmdBlk := []uint8{0, 0, 0, 0, 0, 0}
ioHdr := &SgIoHdr{
InterfaceID: int32('S'),
CmdLen: uint8(len(inqCmdBlk)),
MxSbLen: SENSE_BUF_LEN,
DxferDirection: SG_DXFER_FROM_DEV,
Cmdp: &inqCmdBlk[0],
Sbp: &senseBuf[0],
Timeout: TIMEOUT_20_SECS,
}
err := SgioSyscall(f, ioHdr)
if err != nil {
return err
}
err = CheckSense(ioHdr, &senseBuf)
if err != nil {
return err
}
return nil
}
func CheckSense(i *SgIoHdr, s *[]byte) error {
var b bytes.Buffer
if (i.Info & SG_INFO_OK_MASK) != SG_INFO_OK {
_, err := b.WriteString(
fmt.Sprintf("SCSI response not ok\n"+
"SCSI status: %v host status: %v driver status: %v",
i.Status, i.HostStatus, i.DriverStatus))
if err != nil {
return err
}
if i.SbLenWr > 0 {
_, err := b.WriteString(
fmt.Sprintf("\nSENSE:\n%v\n%v",
dumpHex(*s), GetErrString((*s)[12], (*s)[13])))
if err != nil {
return err
}
}
return fmt.Errorf(b.String())
}
return nil
}
func SgioSyscall(f *os.File, i *SgIoHdr) error {
return ioctl(f.Fd(), SG_IO, uintptr(unsafe.Pointer(i)))
}
func ioctl(fd, cmd, ptr uintptr) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, ptr)
if err != 0 {
return err
}
return nil
}
func OpenScsiDevice(fname string) (*os.File, error) {
f, err := os.OpenFile(fname, os.O_RDWR, 0)
if err != nil {
return nil, err
}
var version uint32
if (ioctl(f.Fd(), SG_GET_VERSION_NUM, uintptr(unsafe.Pointer(&version))) != nil) || (version < 30000) {
return nil, fmt.Errorf("device does not appear to be an sg device")
}
return f, nil
}

3
vendor/modules.txt vendored
View File

@ -1,3 +0,0 @@
# github.com/benmcclelland/sgio v0.0.0-20180629175614-f710aebf64c1
## explicit
github.com/benmcclelland/sgio