Working Ninja
2017-12-30T20:13:20

Objective:

Enable the "Start up automatically after a power failure" feature found within OS X without OS X installed.

Solution:

  1. Find the LPC controller via lspci.
  2. Reference the LPC controller datasheet to find the register to update.
  3. Update the register with setpci.
  4. Test.
  5. Add the setpci command to crontab.

I'll be performing the following steps on a 2008 MacBook 4,1. Similar steps should be successful with other models.

1. Find the LPC controller via lscpi.

$ lspci | grep LPC
00:1f.0 ISA bridge: Intel Corporation 82801HM (ICH8M) LPC Interface Controller (rev 04)

Note the PCI address (00:1f.0) of our LPC Interface Controller. This will be referenced later with the setpci command.

2. Reference the LPC controller datasheet to find the register to update.

Searching the Internet for "82801HM datasheet" yields this datasheet from Intel. After opening the PDF, find the page for "AFTERG3_EN" via the Appendix A Register Bit Index (at the end of the PDF):

9.8.1.3 GEN_PMCON_3—General PM Configuration 3 Register (PM—D31:F0)  

Offset Address: A4h
Default Value: 00h
Lockable: No
Attribute: R/W, R/WC
Size: 16-bit
Usage: ACPI, Legacy
Power Well: RTC

Bit: 0
Description: AFTERG3_EN — R/W. This bit determines what state to go to when power is re-applied after a power failure (G3 state). This bit is in the RTC well and is not cleared by any type of reset except writes to CF9h or RTCRST#.

0 = System will return to S0 state (boot) after power is re-applied.
1 = System will return to the S5 state (except if it was in S4, in which case it will return to S4). In the S5 state, the only enabled wake event is the Power Button or any enabled wake event that was preserved through the power failure. NOTE: Bit will be set when THRMTRIP#-based shutdown occurs.

From the above, we find that our register location is 0xA4 (A4h) with bit 0 (AFTERG3_EN) being the one that controls whether or not to power on the device after a power failure. Setting this bit to 0 will power on the machine.

3. Update the register with setpci.

Get the current value (should be 1):

$ sudo setpci -v -s 00:1f.0 0xa4.b
01

Set the bit to 0, per our datasheet:

$ sudo setpci -v -s 00:1f.0 0xa4.b=0

This is where I get lost. I do not see how we are setting just bit 0 to 0 with the above line. Searching online yields the same command for similar devices and different commands for others (who's manuals I cannot find online).

Verify that the bit is now set to 0:

$ sudo setpci -v -s 00:1f.0 0xa4.b
00

4. Test.

Shutdown the machine and disconnect the power supply.

5. Final Things

The AFTERG3_EN bit is reset to its default at reboot, so lets add the above command to our crontab (as root):

@reboot /usr/bin/setpci -s 00:1f.0 0xa4.b=0

Source:
https://superuser.com/questions/212434/reboot-after-power-failure-for-mac-mini-running-ubuntu#212533

2017-10-25T17:47:53

Before stumbling upon this module, whenever I connected to my audio receiver via Bluetooth, I had to change the Pulseaudio sink as well as set the default output. Thankfully there is an easy fix that will do both of these when a new sink is available.

Add the following to /etc/pulse/default.pa. Or better yet, if you haven't already, create a local config for Pulse audio at ~/.config/pulse/default.pa:

.include /etc/pulse/default.pa
load-module module-switch-on-connect

Now if only I had stumbled upon this before attempting to write a custom udev rule!

Source: https://askubuntu.com/questions/158241/automatically-change-sound-input-output-device#158250

2017-08-27T08:44:20

Python 2.7 is not provided on CentOS 6 because Yum depends on Python 2.6. Simply installing 2.7 over 2.6 will break Yum. Thankfully, Red Hat provides us with an alternate installation method through their SCL project.

First, get Python 2.7 installed:
https://www.softwarecollections.org/en/scls/rhscl/python27/

Next, add the project path to the top of the script:

import sys
sys.path.insert(0,'/path/to/project')
sys.path.insert(0,'/path/to/project/env/lib/python2.7/site-packages')

Lastly, call the script via Cron:

cat /path/to/project/script.py | scl enable python27 python
2017-07-12T19:50:17

Using IUS (Inline with Upstream Stable)--a nifty repository provided by EPEL--we can upgrade beyond PHP 5.4 by first removing our current version of PHP, installing our preferred version (5.6 in our case), and then restarting Apache:

$ sudo yum remove php-cli mod_php php-common

$ sudo yum install mod_php56u php56u-cli php56u-mysqlnd

$ sudo apachectl restart

Sources:
https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-centos-7
https://ius.io/GettingStarted/

2017-06-30T21:58:27

Renewing an SSL but can't recall exactly how it was filled out last time? Try the following:

openssl req -text -in request.csr

The above command will decode the CSR file and output its contents in human readable text. Enjoy!

More here: https://linux.die.net/man/1/req

2017-03-23T17:32:14

Sometimes it is necessary to resize partitions (e.g. updates to GitLab require additional space on /opt). Here's what we need to do. If you have an LVM parition, you're in luck! Things are pretty straightforward.

  1.   Run vgdisplay (Volume Group Display) to verify we have space to expand our partition.
    •     The line PE Free / Size should have space available.
  2.   Next, add space to the parition with lvresize (Logical Volume Resize):
    sudo lvresize --size +100M /dev/mapper/opt
  3.   Now that the volume has been expanded, let's commit those changes to the filesystem:
    sudo xfs_growfs /dev/mapper/opt

Profit!

2017-03-23T17:22:08

Here's a simple way to count lines of code via the command line that I recently used for a Django project:

find . -name '*.py' -not -path "*/migrations/*" | xargs wc -l

or

wc -l **/*.py

Source: http://stackoverflow.com/questions/1358540/how-to-count-all-the-lines-of-code-in-a-directory-recursively#1358573

2017-03-15T07:38:50
dd if=<path to input file> | pv -s <size e.g. 1377M> | dd of=<path to target device>

Source: https://superuser.com/questions/351814/how-to-copy-an-iso-image-onto-usb-with-dd#351815

2017-02-04T16:02:28

To swap control and command, add the following to your ~/.Xmodmap file:

remove control = Control_L
remove mod4 = Super_L Super_R

keysym Control_L = Super_L
keysym Super_L = Control_L
keysym Super_R = Control_L

add control = Control_L Control_R
add mod4 = Super_L Super_R

The above current works for both an Apple laptop and an Apple keyboard.

Also, to use Command+Tab to cycle windows, make sure to update keyboard shortcuts to use Ctrl+Tab instead of Alt+Tab (for Xfce, Window Manager -> Keyboard). All other keyboard shortcuts should come through (from the Control to Command swap perspective).

Source: https://stackoverflow.com/questions/7099602/cmd-control-keys-swap-in-ubuntu

2016-11-21T23:35:33

The Old Way

import os

os.system('find /home/user/documents -type f -iname "*.doc"')

The New, Recommended Way

import subprocess

subprocess.check_call([
    'find',
    '/home/user/documents',
    '-type', 'f',
    '-iname', '*.doc'
])

Bonus Tidbit

At this point, we can incorporate some nice exception handling:

import subprocess

try:
    subprocess.check_call([
        'find',
        '/home/user/documents',
        '-type', 'f',
        '-iname', '*.doc'
    ])
except subprocess.CallProcessError as e:
    print e.returncode
    print e.cmd
    print e.output

See the official Python Documentation for more usage and recommendations on the subprocess module.