Moving comments in WordPress

February 7th, 2010

In MySQL, first move the comment:

UPDATE wp_comments SET comment_post_ID = P_TO_ID WHERE comment_ID = C_ID;

P_TO_ID is the ID of the post you are moving it to. C_ID is the ID of the comment you are moving. You can find both of these IDs easily from the dashboard.

Then correct the comment counts for the posts:

UPDATE wp_posts SET comment_count = (SELECT count(*) FROM wp_comments WHERE comment_post_id = id AND comment_approved = 1)
WHERE comment_count != (SELECT count(*) FROM wp_comments WHERE comment_post_id = id AND comment_approved = 1);

The second line in this query is not strictly necessary but it lets you know how many posts were affected.

Terra Nova

January 29th, 2010

So I was at the cinema recently watching a certain disappointing 3D movie and a trailer came up for Battle for Terra, a CGI movie that is already available on Blu-ray in some countries but is only coming to UK cinemas in February 2010. The plot is that humans are the aliens and after destroying earth (crap, another eco-hippie movie) they go to invade an alien planet called Terra. Much like the other disappointing 3D movie with the similar plot, I couldn’t work out if I should be supporting the humans or the aliens.

Anyway, didn’t anyone notice that “Terra” is Latin for “Earth” when they started writing this thing? If it was called Terra Nova (new Earth) or something like that I could understand but apparently these aliens live on a planet that just happens to have exactly the same name or something.

Geek love

January 24th, 2010

On a scale from 1 to 10, you require an arbitrary length integer.

Which came first, the chicken or the egg?

January 23rd, 2010

The egg, fool! Dinosaurs didn’t hatch out of chickens!

Socket 1366 (i7/Xeon) Workstation motherboards

December 30th, 2009

I’m waiting for a decent dual socket Core i7 or same generation Xeon motherboard to come out from a good manufacturer, such as Asus.

It seems to just be Intel, Tyan and Supermicro boards so far. Asus lists a couple of P6T boards (P6T6 WS, P6T7 WS) but nothing dual socket yet.

Come to think of it, I wonder what the limit for the number of sockets/cores/threads you can put in Windows 7 is, since Core i7 is 4 cores/8 threads each so dual socket would be 8 cores, 16 threads.

Anyway, if I’m missing something here someone let me know :)

Update: Puget build some nice stuff but I’m sure you can get roughly the same performance for much less than $16,000.
- $16,000 4 socket 16 core Operteron PC (more photos)
- 8 socket 32 core Opteron PC (via daughter board)

I’m getting envious of other systems and I haven’t even built this one yet.

Convert any audio/video files to mp3 for free

November 24th, 2009

It seems that there is some confusion about audio conversion tools and some people even pay for them.

The truth is that most encoders, even ones you pay for, use ffmpeg internally to do all of the actual work and ffmpeg is free and open source.

You can even get it pre-compiled for Windows, though the version isn’t great.

The only problem with using ffmpeg directly is that it has a command line interface and most novices find it hard to use.

Here’s a batch file for use on Windows which will convert anything you drop onto the file into a 320kbps mp3:

ffmpeg -i %1 -ab 320k -y %1.mp3
@if errorlevel 1 @pause

To create this file, open notepad. Copy and paste the above 2 lines in. Save it as “convert.bat” or something similar (the .bat is important) and select “All files” from the drop down list.

Put the bat file in the same directory as ffmpeg.exe and simply drag anything you want converted on top of the bat file.

You can use almost anything. wav, m4a, m4v, mov, mpg, avi, etc. If you use a file with both audio and video, such as a movie, it will just extract the audio and save that as an mp3 file.

Improve your SSH with GNU Screen

September 11th, 2009

SSH is a wonderful thing. You can log in to a terminal remotely and securely as well as tunnel traffic through that connection. However, one of the biggest problems with SSH is that as soon as you close the session (or are disconnected), any processes running are killed, which can lead to things like loss of work or inconsistent files. It also means that you can’t leave things encoding overnight because as soon as you disconnect, it stops encoding. This is where GNU Screen is useful.

When screen is open, you can disconnect and reconnect to get the same screen back again. You can also have multiple screens open at once and tab between them. Ctrl+a then c creates a new screen, Ctrl+a then n tabs to the next one and Ctrl+a then k kills the selected window.

This also means that you can resize windows and even the previous text is also resized if it was truncated or wrapped before.

Just add

screen -R

to your ~/.profile to have it re-open the same screen every time you connect to SSH or, if there isn’t one running, start one.

You can also add a line to the bottom of your SSH session that tells you which tab you are currently looking at, the date, time and other similar details. This is called the hardstatus line.

I use the following hardstatus line in my ~/.screenrc

hardstatus alwayslastline
hardstatus string "%{= kw}[%{= kg}%H%{= kw}]%-=%-w%{= kr}{%{= kw}%n %t%{= kr}}%{= kw}%+w%-=[%{= kg}%Y-%m-%d %c%{= kw}]"

To explain what each part does, I will first give the uncoloured version. %{= __} sets a colour. For example, “kg” is black background and green foreground.

If we remove all of these we get

"[%H]%-=%-w{%n %t}%+w%-=[%Y-%m-%d %c]"

%H is a host name
%-= is padding
%-w is all of the windows before the current one
%n %t is the current window number and name
%+w is all of the windows after the current one
%Y-%m-%d is the date
%c is the time

Here’s how it looks (as always, click to enlarge):
screen

The downside is that scrolling doesn’t seem to work at all. I don’t know if it has just been replaced by something internal to screen but I generally use something like shift+page up to scroll up or the scroll bar in PuTTY.

How (not) to NAS

September 5th, 2009

This guide is a good way of doing an inexpensive to moderately priced NAS or SAN while delivering almost enterprise grade performance. The “not” in the title refers to my impatience waiting for a case and PSU to be delivered for 2 weeks.

NAS/SAN vs Drobo

Since I have been encoding all of my Blu-ray discs and DVDs for use over my LAN, I have used all of my hard drive space pretty quickly. Computers don’t perform well when they get down to around 10% free space (depending on the operating system). I tried to fix this problem by buying an incredibly overpriced Drobo but this slows down computers even when it has nothing on it (my computer hangs for about 4 minutes when trying to load the 16TB volume over USB and also adds the same amount of time to your boot up if you leave it plugged in). Add this to the insane price of £315 without any hard drives or network capability (to turn it from a DAS into a NAS you need to buy a DroboShare for an additional £140 and this still only gives you USB performance because that’s how it’s connected) and the fact that it has a lot of bugs that you can’t even see before you buy it because a serial number is required to access their community support and you end up with a big shiny black POS.

Compare this £450 lump to what you can build yourself for a NAS. I got a Gigabyte GA-MA785GT-UD3H motherboard (£75), which includes onboard graphics (VGA, DVI and HDMI ports), gigabit ethernet and 6 SATA 3gbps ports as well as an AM3 socket and DDR3 memory slots. I also got an AMD Athlon II X2 240 CPU (£45) and a 2GiB kit of Corsair XMS3 1333MHz 9-9-924 (£37). I would have gone all AM2 for about half the price but I couldn’t find any AM2 CPUs any more.

Any bootable USB stick is fine to put an embedded OS on (I use FreeNAS) and I’m using the Lycom UB-109 internal to external USB adapter to mount it directly onto the motherboard’s internal USB port, though just putting it in one of the rear USB ports would also be OK. This is just tidier and you don’t have to worry about it being pulled out by someone that thinks it was left there accidentally.

So far we are up to just £157 and only need a case and a PSU. If you were to go for a straight comparison, any old case that holds 4 hard drives would do and any 300W or so power supply would also be fine. However, it’s not hard to do better than that. As I have a small rack already, I got a 2U Chenbro RM23212 case (£217). This also meant I needed a 2U PSU. This is a rack mounted case that allows 12 3.5″ SAS/SATA hard drives to be hot swapped in the front. I found that prices for it varied wildly so I got the cheapest one I could find (still not cheap!). I assume this just means it comes with no PSU or backplane but both of those are fine by me, even if it means that hot swapping isn’t so hot. For the PSU I got a SuperMicro PWS-652-2H (£127.55). Also not cheap but I needed a PSU that could fit and potentially power 12 3.5″ hard drives.

That brings us neatly up to £500. £50 more than a Drobo with DroboShare but the same thing could easily be done much cheaper if rack mounts weren’t involved (probably around £200 – cheaper if you have some old parts lying around). You do get a lot more for the money though. As the hard drives are not included in the price of the Drobo, I have not included them here.

Storage

For hard drives, I got 4x 1TB Seagate 7200.11 drives. These were about £55 each and in my experience, transfer data dramatically faster than any other 7200rpm SATA drives. The reason that I only got 4 drives is that I am using ZFS and zpools on FreeNAS. It’s pretty much a form of software RAID so the CPU will be used but this is a NAS box – that’s all the CPU is there for. It means that I can create a raidz with single parity (essentially RAID5 without the write hole – dual parity RAID6 also exists as raidz2) for 4 drives, getting 75% of the space available for data and then add more raidz groups to the pool later. Note that it is still experimental in FreeBSD but the alternative is splashing out on massive RAID cards and either spending much more on RAID10 or dealing with the RAID5 write hole. The only other platform that supports ZFS at all is Solaris.

I could later add another 4x 1TB drives and then, when they are more economical, 4x 2TB drives. All of the drives in a single raidz have to be the same size but the sizes of the raidz groups do not have to match inside the zpool. After I have run out of space for adding new hard drives, I can replace the drives in the raidz and as soon as all 4 have been replaced it will grow to use the new space. This makes the possibilities for expansion almost unlimited. Because it is done in software, adding more drives can be done with either drive multipliers/expanders or dirt cheap RAID cards that have a lot of ports but no real processor or memory.

Getting impatient

I ordered the internal components from scan.co.uk and got them the next day. Two weeks later and I still don’t have a case (ordered from memory-express.co.uk) or PSU (ordered from pconestopshop.com). Needless to say, I will never be buying anything from either of them again. I’ve already called up about the case twice.

Getting desperate, as both of my hard drives are full as well as my MythTV/video encoding server, I continued anyway. I stole a PSU from my brother’s PC, which now looks like it’s been passed through a shredder with the amount of parts missing and cables hanging out. I didn’t use a case at all. I used a corsair dominator memory cooler and a big desk fan for cooling. I also laid out the hard drives separately to make sure heat didn’t build up and to avoid any short circuits.

This is how not to NAS.

badnas

Installing FreeNAS

I had major problems trying to install FreeNAS from CD. It seems to only want to install from the primary master IDE even though I was using a SATA drive. I tried using IDE emulation but still had problems unless I plugged it into one specific SATA port after trying all 6. I also had to try all of these with and without the real IDE turned on. There aren’t many things more annoying than loading an installer from a CD and then being told that you have no CD drive. In the end, it finally did find the CD drive but then wouldn’t let me install to the USB drive because it had already mounted it and put a config.xml on it (the default behaviour is to boot off the CD and just use the USB for some persistent storage). It wouldn’t let me unmount the USB drive either.

Another problem was that I was getting a panic: ohci_add_done error. I resolved this simply by unplugging the USB keyboard and plugging it into a PS/2 port. Strangely, removing the keyboard during boot and then plugging it in just when I needed it (to assign a network address) also worked.

The best way I found to install it to the USB drive was to put the CD and the USB drive in a Linux machine, gunzip the FreeNAS embedded image file and then use “dd” to image it on to the USB drive.

cp /media/cdrom/FreeNAS-amd64-embedded.gz .
gunzip FreeNAS-amd64-embedded.gz
dd if=FreeNAS-amd64-embedded of=/dev/sdb

where /dev/sdb was my USB drive – this is very important or you may wipe a hard drive.

I then just put the USB drive back in to the new machine and unplugged the CD drive’s SATA cable. It booted without further problems.

You may need to load the console and set an IP address for the network interface but after this you should not need the console again. If you do, you can always turn on SSH from the web GUI. I unplugged the keyboard and screen at this point.

Setting up these drives is incredibly easy. It can be done from the FreeNAS web UI but I used the following command:

zpool create datapool raidz ad4 ad6 ad8 ad10

Where ad4 to ad10 were my 4 hard drives set to AHCI mode in the BIOS.

Though they can be added later with a similar syntax, creating it with more than one raidz in the pool is also easy:

zpool create datapool raidz da1 da2 da3 da4 raidz da5 da6 da7 da8

You should “synchronize” the web UI after doing this under the ZFS tab, just so that it shows the correct information.

I then added a read-only samba share to /datapool/ and a read/write FTP with a user name and password, unchrooted (though chroot to /datapool would be ideal). This allows me to upload files to the NAS via password protected FTP while still allowing all of the computers in my house easy access to the files without needing user names and passwords and without being able to accidentally damage anything. I changed the file creation umask to 022 (default 077) so that files are created as 0644 and directories are created as 0755. To be honest, these seemingly inverted (XORed?) umasks confuse me but I just stick to 022 and find that ideal.

I get write speeds of roughly 65 to 90MB/s over FTP on copper gigabit Ethernet. The little Athlon II X2 CPU is very busy during these writes but out of the 2GiB of RAM, only a constant 30% is ever used. It is well known that the ZFS code on FreeBSD needs optimising so I will probably check back in the future and try a patch. This also means that write speeds could be increased by adding a faster CPU. I haven’t actually checked if it is using both cores at the moment (SMP) but I assume it is. ZFS performs better on 64bit apparently and that is what I am using. It is also possible that the client uploading the files was causing some bottleneck but I usually get 100 – 110MB/s between the two used.

In the future, I may add a second Ethernet card and use LAGG for load balancing. However, this only really helps for multiple concurrent clients and it requires a switch that supports it. I have a Dell PowerConnect 2716 switch (it was £160 with a £100 off deal at the time) which does support this.

New car

September 3rd, 2009

As some of you may be aware, I have just bought a new 2009 Lotus Elise.

I also spent 2 days gutting a garage that has been used for storage of building materials for the last 20 years and making it ready to hold a car.

car_front

car_rear

DSC02146

DSC02133

DSC02150

Learn English or stop saying you speak it.

August 21st, 2009

A while back on one of my posts on the Tekzilla forum, I was amazed that a certain country started using the word “analyzation”. This is clearly not a word. If you analyse something, you make an analysis not an analyzation but to my disbelief, it managed to get into a dictionary.

Even in my own country, so many people can’t pronounce the word “slippery” that the word “slippy” has found its way into dictionaries. Even “slippier” is there. If that word loses any more random syllables because of stupid people, it’s going to be “sly”.

It turns out that if enough people are wrong, they just change what is right.

While I’m talking about this, it should be “10 items or fewer” not “10 items or less”. If you can count it, it’s “fewer”. This assumes that you can count from 0 to 10, though for the people that make these mistakes, that’s quite a wild assumption.

If you are going to speak English, speak English. If you are not going to speak English, call it something else. I suggest saying that you speak “stoopid”.