Thinkpad还是一如既往的疯狂
最近在折腾硬盘保护的事情,说实话,弄了好久,还没搞定
不过意外中看到一个非常有趣的东西
转载自:http://www.ibm.com/developerworks/library/os-linuxthinkpad/index.html
Shake some sense into your Linux ThinkPadUse open source software and accelerometer-enabled ThinkPads to listen for percussive impacts and shaking to kick-off user commands or a reset |
Level: Intermediate Nathan Harrington (harrington.nathan@gmail.com), Programmer, IBM 07 Nov 2006 Place your computer on the leading edge of cathartic interfaces by modifying the kernel to reset your Linux® laptop automatically when shaken during a kernel panic. Implement a shake-detection algorithm in the kernel and user space to perform automatic shutdowns and restarts when certain kinetic conditions are met. In 2003, IBM® started selling ThinkPad laptop computers with integrated accelerometers and associated software for a commercial operating systems to protect the hard disks when the ThinkPad is dropped. Enterprising hackers from IBM and elsewhere have worked to develop modules for the Linux kernel to take advantage of these sensors. On-screen display orientation, desktop switching, even game control and real-time 3-D models of the tilt of the laptop are now available. In mid-2006, knock-based commands for Linux laptops became available with user-space Perl scripts (as opposed to C-based code buried in kernel space), allowing users to run arbitrary commands based on specific knock sequences. This article describes the process of modifying the Linux kernel to add an oft-demanded feature: feedback on physical input. When the Linux kernel panics, the user can shake the computer (or perform any number of other developer-configurable physical movements of the laptop), and the machine will reset. Also covered are methods for performing a normal shutdown in nonpanic mode. For example, if the user inadvertently places the computer in a laptop bag still turned on, we want the computer to detect a normal walking or driving motion and turn the computer off. Many IBM ThinkPads manufactured in 2003 and later sport hdaps hardware. If you are unsure of your hardware configuration, check out the technical details on Lenovo's Web site. You need a ThinkPad for this code to function. Some MacBooks have the accelerometers and the same general methods available to access them through the kernel. However, the code here was not tested on Apple hardware and was developed and tested on two IBM ThinkPad T42p models. See Resources for links on how to find ThinkPad hardware that will support your desire to get physical with your laptop. This article assumes familiarity with the kernel build process, as well as experience with the inconsistencies among distributions when it comes to kernel compilation. For an introduction to the kernel build process, as well as some great examples on how to get started, see Resources. As of kernel V2.6.15, the hdaps driver is included in the Linux kernel. Grab the latest kernel for the sake of simplicity. For ease of development and administration, this article was developed on Fedora Core V5. The following instructions used to set up the kernel build environment are specific to Fedora Core, but the general principles are applicable to any Linux distribution. Kernel configuration, compilation, and testing To modify the kernel, follow the instructions from the excellent release notes. Open a Web browser and start following the instructions in section 8.6: "Preparing for Kernel Development." When you get to section 2, you may have a problem with the second command: When you get to step 5, select the basic i686 default config with the command: We have now completed the relevant portions of the kernel configuration from the Fedora Core V5 Release Notes document. The remaining steps are standard to any kernel build process. I recommend building and installing your new kernel, modules, and RAM disk setup now to make sure things are working as expected. You can skip the following steps and head right to the kernel modification section if you are confident about your new kernel configuration. Build your new kernel with the Listing 1. grub configuration title Fedora Core (2.6.15hdaps) Modification of panic.c and hdaps.c Now you are ready to start some shake-and-bake kernel hacking. Make sure you are in the ~/rpmbuild/BUILD/kernel-2.6.15/linux-2.6.15.i686 directory. The first priority is to include the hdaps module as a built-in component of the kernel, so it will be ready to provide for shake detection anywhere in the machine's run modes. Use the command Open the file drivers/hwmon/hdaps.c and add the following text to the includes section: Listing 2. Full panicShake subroutine from hdaps.c /* With our shake-detection routine in place, we need to call it upon system panic. Open the kernel/panic.c file and place a call to the Listing 3. panicShake variables int ret, x, y; // return value and x,y from hdaps Of particular note is the deviation threshold parameter and the dimensional shift parameters. These may require tuning based on the unique characteristics of the motion you are trying to detect. For example, if you feel the urge to shake your computer like you were performing a basketball pass, try decreasing the The selections of Next, we consider the infinite loop statement and conditionals. Listing 4. panicShake hdaps read and base setup while(1) The code works as follows: For the rest of time, read the current accelerometer readings from the hdaps sensor. Frequently, the read will be unsuccessful or both values will be equal to 0,0, which is unusable data. We need to avoid these spurious 0,0 readings, as about one out of every 10 readings at any orientation of the sensor will be 0,0 -- invalid data, indeed. If it's our first "successful" read, set the base parameters equal to the first x and y values. This will allow us to more robustly detect shaking or other movements if the panic occurred while the machine was on a nonflat surface, such as a person's knees. The remainder of the subroutine is the implementation of the simple shake-detection algorithm. Listing 5. panicShake shake detection if( abs(baseX - x) > dimShiftX || abs(baseY - y) > dimShiftY ) If the dimensional shift in either direction is greater than our previously set threshold, increment the total deviation by the amount moved in both directions. Then set the current base to the existing level of acceleration. This repeated reinitialization of the base values will require the user to continuously exceed the dimensional shift values to increment the total deviation detected. This is useful for allowing the user to move and store the ThinkPad in panic mode as they track down the systems administrator. Remove the reinitialization assignments if you want simply setting the ThinkPad on its side or tilting it and holding it there to trigger a restart. Testing your panicShake() kernel To initiate a panic, we need to call the panic subroutine in the kernel. Create the following makefile: Listing 6. panicCall.c kernel module source /* As root, compile the panicCall module with the command Listing 7. kernel panic stack strace panicCall: module license 'unspecified' taints kernel. Now pick up your computer and give it a good shake, and it will print out the "shaking substrate" message and perform a restart. If you're the sort who would rather not shake a potentially active disk drive, issue the following commands as root: Listing 8. RAM disk creation, module copying mkdir /tmp/ramdisk0 You now have the two files you need to insert a module into the kernel located on a RAM disk. Update the Listing 9. modification of /etc/init.d/halt echo "disks now mounted in readonly mode, spin down in 5 seconds"; Execute the command User space shutdown and motion detection Many an IT administrator has yearned for the ability to know the physical history of hardware. With the same simple shake-detection algorithm above, a Perl script, and a monitoring policy, administrators will be better able to track the status of their hardware. For example, we will use the Perl script below to shut down the machine gracefully when shaken by the user. Modifications can be made easily to send an e-mail, flash the "ThinkLight," or play a sound file based on the user's manipulation of the ThinkPad. Listing 10. Perl script for shake detection, Part 1 #!/usr/bin/perl -w As you can see, the initial program setup is nearly identical to the hdaps kernel code. The regular expressions and Listing 11. Perl script for shake detection, Part 2 if( $x != 0 >> $y != 0 ) Note the Modifying the dimensional shift parameters and the deviation threshold can provide for additional useful monitoring of physical activities outside kernel space. For example, to acquire a "walking" behavior, set the dimensional shift parameters to around 20 and set the deviation threshold to around 5000. This will pick up about 63 dual-axis dimensional shifts, consistent with the laptop being running while in an over-the-shoulder typical laptop bag. After this long walk is detected (as opposed to a short walk between cube and conference), the machine will enter shutdown to prevent overheating while the airflow is confined in the carrying case. Modify the dimensional shift parameters to be highly sensitive, and every significant bump, drop, or shake can be recorded. With these simple algorithms for user space and kernel-level code, you now have the ability to detect, log, and respond to the full range of physical input from the user. With these code examples, you can do everything from modifying hard-drive performance parameters based on computed altitude from continuous acceleration to tracking the number of steps from your cubicle to the conference room and mail it to your space planner. Learn
Get products and technologies
Discuss
|
0 comments:
发表评论