Hey there!


Package managers for UNIX cheat sheet

Aptitude

Update the manifests

apt-get update

Update the packages

apt-get upgrade

or sometimes this is also needed to resolve very complex dependency issues

apt-get dist-upgrade

Install new package

apt-get install foo

Remove a package

apt-get remove foo

or

apt-get purge foo # this will remove the configuration files (some)

Emerge

Update the manifest

Update the packages

Rebuild all packages

Install new package

Remove package

Install binary package

You need to have some binary package server




How to create login info in motd similar to ubuntu server

Ever wondered how could you get this cool login screen you can see when you login to ubuntu server on other distros?

This is a part of proprietary system called landscape. But this thing is too cool to remain proprietary, so I created a similar login info screen here: http://github.com/benapetr/system-info

All you need to do is to append it to your login scripts and here we go:

 




Is internet ready for IPv6?

No, it’s not. Why?

I did an experiment as I was curious how various internet sites and service providers are ready for IPv6 by setting up a computer which supports IPv6 only. That means all sites which were not available through IPv6 just didn’t work.

It was pretty much every site that didn’t work, with few exceptions. Google, facebook and most of linux webpages (like debian.org) work just fine. Except for these, even most major websites such as twitter don’t support IPv6 yet, being completely inaccessible without IPv4 protocol.

Here is a small list of few examples

Major websites that support IPv6:

  • wikipedia.org
  • google.com
  • facebook.com

Major websites that can’t be accessed without IPv4:

  • github.com
  • twitter.com
  • bbc.co.uk
  • microsoft.com
  • ubuntu.com

 What is IPv6 and why should servers support it? From wikipedia:

Internet Protocol version 6 (IPv6) is the most recent version of the Internet Protocol (IP), the communications protocol that provides an identification and location system for computers on networks and routes traffic across the Internet. IPv6 was developed by the Internet Engineering Task Force (IETF) to deal with the long-anticipated problem of IPv4 address exhaustion. IPv6 is intended to replace IPv4.

In a nutshell: it’s a new version of internet protocol that will replace IPv4 someday, the main reason for it is the low number of IPv4 addresses, because its protocol was invented in 1980 by engineers who never expected internet to grow up into this size. IPv4 supports less than 4,294,967,296 network addresses, which may look as enough to some, but is actually very small number, given a number of network enabled devices out there (every modern TV, mobile phone or PC).

It’s totally possible to connect many more devices to IPv4 based internet, but only as long as they are grouped behind NAT servers with shared IP addresses. This will work for regular users who don’t need much from internet, but definitely will not for advanced users who want to use internet in order to provide some services to others, or those who want their devices to be easily reachable from anywhere through internet.

Right now, public IPv4 address is something that actually has some value. You have to pay for it, if you want to have it. IPv6 addresses are so cheap that most of ISP providers would give you whole range of public IPv6 addresses for free.

That means that sooner or later various people will start up services (web servers, game servers and so on) that would be available only over IPv6 because they would need to use public address(es), but they wouldn’t want to pay for them, because they don’t have to.

These IPv6 only servers would not just be available only to people with IPv6, but they would also be only able to connect to machines (and other servers) that support only IPv6. And that is the reason why your system should be IPv6 ready.

How do I check if my website works on IPv6?

It’s easy, just run this ipv6 test

If it doesn’t, don’t worry. Anything like “switchover” to IPv6 isn’t going to happen any time soon, so there is still plenty of time. Regular users will (have to) keep using IPv4 for a very long time…




How to setup debian build environment for both amd64 and x86 platform

This article describes how to setup environment in which you can build both amd64 as well x86 .deb packages. It’s assuming that you already have working debian on amd64 kernel.

First step: install debootstrap

Now you should have fully working x86 debian in /opt/jessie-386

Second step: make a script to switch into i386 version

Create a script anywhere you like, for example /bin/switch_32 with following content:

Then make it executable. Now running it would switch you into fully working debian x86 in which you can install all required packages using apt-get and build your x86 packages.




Howto install xen server on X10SLL-F board

I recently got a new server board from Super micro. It’s quite a nice board except for few issues. One of them was that I figured out that citrix xen server is officially unsupported on this board. Well, why?

The installer which is written in python crashes during startup as it tries to launch some command that dumps serial port information. This command returns non 0 RC and whole installer crashes and that is a reason for it not to work.

There is however super easy work around. The hardware itself is supported by XEN, so from technical point of view there is nothing else that this bug in python installer preventing xen from running there. All you need to do is this super nasty trick:

Install XEN on a supported hardware, it can even be a virtual box if you don’t have spare server. Then put the disk with fully installed OS into server. It should boot up just fine.




PowerShell syntax cheat sheet

Everything is case insensitive, comments with # symbol

Conditions

if (condition)
{
   #code
} else if (condition)
{
   #code
} else
{
   #code
}

if (condition1 -And condition2 -And !(condition3))
{}

Negations must be in extra brackets ex:

$x = $true
if ($x) {}
if (!($x)) {}

Parameters

There is special block of code in param, that must be first executable code in function or script. Can be after comments.

Example:

#This is a beginning of script file

param
(
        [string] $PackageName = “blah”,

        [parameter(Mandatory=$true)]     
        [ValidateNotNullOrEmpty()]
        [string] $PackageUrl,

        [parameter(Mandatory=$true)]
        [string] $VariableName
)

You can call the script or function, either with parameters in order in which they are defined like

blah a b c

or with switch

blah -PackageName “a” -PackageUrl “b” -VariableName “c”

Types

[string] $x = “blah”
[bool] $x = $false #or $true
#store output of a command into string
$rev_list = git rev-list HEAD –count | Out-String 
#replace new line
$rev_list = $rev_list.Replace(“n", "").Replace("r”, “”) 

Functions

# Example function with example call of that function

$ErrorActionPreference = “Stop”

function PackageTest
{
    param
    (
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $PackageName,

        [parameter(Mandatory=$true)]     
        [ValidateNotNullOrEmpty()]
        [string] $PackageUrl,

        [parameter(Mandatory=$true)]
        [string] $VariableName
    )

    Write-Host “Looking for $PackageName…    ” -NoNewLine
    if (!(Test-Path $PackageUrl))
    {
        echo “ERROR”
        echo “Unable to find $PackageName at $PackageUrl, you can set the alternative path using -$VariableName=path”
        exit 1
    }
    echo (“OK”);
}

PackageTest “Qt5” “$qt5_path” “qt5_path”




How to install mdadm on citrix xen 6.5

For some reason citrix doesn’t like mdadm so they make everything possible to stop it from working on their xen server.

Here is a guide that would make it work there, but it may not survive system patching

Setup

Connect at least 2 disks to your box. Install a xen server without local storage on first disk.

Installing mdadm

The default install contains mdadm but it doesn’t load raid modules to kernel. In order to enable it, following needs to be done:

Partitioning the disks

Now we create a final schema we want to use on our server on disk /dev/sdb, xen needs to have at least 3 partitions, 1 is for boot loader, second is for OS, I recommend 20gb or more, because this disk is pretty much impossible to extend, although citrix defaults it to 4GB, last partition is for local storage and it should take all remaining space on disk.

Note: Citrix by default creates 3 partitions, 1 for OS, second is empty, same sized as first one and probably used for system upgrade. Third is used for local storage LVM. You don’t have to create second partition for it to work, but system upgrades may not be available if you don’t create it. On other hand system upgrades will likely not work anyway as citrix doesn’t support mdadm installations.

In this guide I will use old MS-DOS partition table because although it’s old, it’s much better supported and it just works. You can also use GPT partitions if you want, but I had some issues getting them work with mdadm and syslinux.

We will have a separate /boot partition for boot loader, because syslinux shipped with xen is having troubles booting from raid device for some reason.

So this is how the layout of sdb should look after we finish the partitioning:

  • /dev/sdb1 (2 GB) for bootloader
  • /dev/sdb2 (20 GB) for OS
  • /dev/sdb3 (rest) for LVM

Now you should be able to boot from /dev/sdb if you are not there is something wrong with the setup, you need to figure out if your problem is with

  • MBR (No bootable device)
  • Boot loader (Missing operating system.)
  • /boot (Linux will start booting but die in progress – try removing quiet and splash from parameters)

 Syncing the disks

Now if you were able to boot up you need to setup the sda disk

Create the same 3 partitions as you did on sdb on sda and then

 




Why writing verbose code is good for self documentation

I recently had an interesting discussion about modern programming languages and I also have seen some, rather weird, opinions of people who believe that being verbose in your source code is a bad thing. I will try to summarize in this post why actually being verbose is a good and useful thing.

First of all, what do I mean by being verbose? I mean to use language elements that can be, but not necessarily need to be explicitly stated in the source code.

Many languages have some implicit definitions that are always used, unless you override them. For example in c# these both lines are identical:

Because every function, if not stated otherwise is private be default. There is no need to use keyword “private”. But well, you can still do that. Why? Because not everybody knows it and that doesn’t necessarily mean that person is a newbie (there are people who simply work in so many languages that they are occasionally unclear about things like this), and even if they were, what is so wrong on writing a source code that is simple to read even by newbies? So in simple words this will make it extra clear that a function is really private. It doesn’t harm anything and may be useful to some people who are reading the code.

The other thing which I noticed, is that people usually do not like to use “this” in c++. I am personally using “this” everywhere I can, in every function, just to make it 100% clear that this variable or a function is a non-static member of instance of class in which the function body is defined. It’s not for me, it’s for people who are reading my code, so that they don’t need to scroll potentially hundreds lines of code just to figure out whether variable, which they are looking at is function local or belongs to the class. I know that some IDE’s are showing these in different color, so that their scope is pretty clear, but these people may be reading this code in VI, or notepad. Again, it doesn’t harm anything, there is no difference in binary produced by compiler. In addition, it helps to ensure that your call will not conflict with some other function with same name.

It doesn’t improve the code, it doesn’t make it better nor worse. It just makes it easier to read (not by you, but by someone who doesn’t know the code and wants to understand it).

And what I hate most are languages which doesn’t require explicit type defintion, but are still strong-typed, like python (yes you probably already know I hate python :-)). These languages makes it super hard to figure out which datatype you are looking at, unless you attempt to debug it in some way.

This may be good for obfuscation, but seriously, what’s the real benefit of programming language that allows you to write shortest possible source code in number of characters which is totally unclear and hard to understand? To save space on hard drive? I don’t think so…




How to insert ubuntu PPA’s to debian

Ubuntu created a very interesting service called PPA – personal package archives.

If anyone of you ever used debian (or ubuntu) you may be wondering how cool it could be, if your software could be in official repository so that people could just type:

sudo apt-get install blah

These of you who managed to get through all the bureaucracy and got the packages there, might be wondering how cool it would be, if that package which got build and published there months ago, could be ever updated to latest version of your software ­čÖé

Today, it’s both possible and very easy, thanks to PPA.

I am not going to describe the process of how to submit stuff to PPA, because that is explained on many places, but I am going to explain how PPA work and how you can use this powerful service on debian.

What is PPA

PPA is basically a repository server, where each user can create their own personal aptitude remote repository, using unlimited number of own GPG keys. There is basically no difference between PPA repository and any other apt repository.

How do I make it work on debian

There is no command apt-add-repository on debian, because PPA is ubuntu thing. That makes 2 problems:

* You can’t easily add PPA
* You may have problems with dependencies, as some debian packages are named differently than ubuntu packages

First problem can be overriden easily. You can either get the source code of apt-add-repository and install it localy, OR you can just insert the url of PPA in format of

to your /etc/apt/sources.list where USER is username on PPA and REPOSITORY is PPA. You also need to replace ubuntu version with any ubuntu version which is most close to your debian version, typically some LTS (precise, or lucid).
The other thing you need to do, is installation of GPG key of the user you want to download packages from:

Now you should be able to download and use the PPA repository.

You may be however facing the other problem with dependecies, and there is currently no other solution for it, than rebuilding the source package yourself with correct debian dependencies.




Underscores, camels and insane madness of modern programming styles

So, I went over several articles related to programming style, after I noted that basically every modern project uses some special formatting, which even if I am familiar with, I never liked.

I decided to take programming styles little bit more in a detail from technical perspective and made some interesting conclusions.

In past, when GNU project started, most of programmers followed the so called “ISO” style (which is known as Allman see wikipedia) which I kind of like, even if I could think of better one.

This style uses opening and closing bracket for every function on separate line, for example

Unfortunatelly this style has two disadvantages. I love that allman style put brackets vertically on same position – that makes it just perfectly simple to “parse by eyes” and you never get lost in brackets, unfortunately, GNU does pretty weird indentations, probably because old code was developed in 85×24 terminals, so they really needed to keep it low on width.

Nowadays, most of programmers like to write code using style that makes the code “cute” but awfully hard to read, error prone and ineffective.

It’s not just that K&R put brackets in a way, that you can save one line (like if that was actually needed), but can’t figure out which bracket matches which. But also the letter cases are pretty weird. I discovered this great post: http://whathecode.wordpress.com/2011/02/10/camelcase-vs-underscores-scientific-showdown/ you really should check it out. It perfectly describes pro’s and cons of camelCase over underscore_name and I can tell you, I can see why most of respondents changed their mind after reading the article. Underscores, are clearly better in almost everything, and only argument why camelCase is better was “it is more cute”.

I think this kind of reflects the modern programming trends. Programmers, instead of making effective code, write cute code…

Even if I am not a big fan of Microsoft, I must say that code they use in their examples and Visual Studio has modern and very effective styling which I find extremely easy to read.

They basically use NormalCases and vertically matching brackets for everything, as well as 4 spaces to indent. Which indeed need more space than GNU’s 2 spaces, but make the code easier to read on modern screens with high resolutions. So no matter how people blame Microsoft for everything, this is a style I like and probably going to use in most of my projects, no matter of language. It may not be “cutest” but at least it makes the code easy to read.