Thursday, 25 June 2015

Comparing Files using Find and CMP

I needed to compare two directories containing binary files. Rsync was confusing, Diff only compares text-files.

I tried the following:
#!/bin/bash
# Example: ~/compare.sh FC30-3DA9
# where $1 is the directory name
# requirement: you are in one of the two parent dirs.
# the directories to compare must have the same name

find $1 -type f -exec sh -c '
  for f; do
    cmp "$f" /directory/of/mr/bear/"$f"
  done
' sh {} +
Seemed to work fine.

References

How to compare files in two folders using cmp?
http://www.unix.com/shell-programming-and-scripting/144821-how-compare-files-two-folders-using-cmp.html

Thursday, 18 June 2015

Sum of 1 to 100

Back in school, during Math, one of the arithmetic examples that spoke the most to me about how Powerful Math can be, was the explanation for the adding up of the first one hundred numbers.

The short explanation

When you have a nice distribution of numbers like 1 to 100, it is obvious that the average is right down the middle, which is 50½. And given the fact that we have 100 numbers, the solution becomes obvious.

Okay, so for some people that explanation might have been a bit too short.

The Long Explanation

Determining the average of a series of numbers, \(x_{1}, x_{2}, x_{3}, ... x_{n-1}, x_{n}\) is done by summing them all, and then dividing by the amount of numbers you have.
\[x_{avg} = {\displaystyle\sum_{i=1}^{n} x_i \over n}\]
Therefore, it follows that the sum, i.e. the thing we wish to know, is obviously:
\[{\displaystyle\sum_{i=1}^{n} x_i} = n * x_{avg}\]
For the first 100 numbers, this gives:
\[{\displaystyle\sum_{i=1}^{100} x_i} = 100 * 50{1 \over 2} = 5050\]

The Staircase

Of course, most explanations use a bit more imagination to make things clear2.

Think of the problem as a staircase, with steps reaching from 1 to 100. To make things a bit more simple, and to save my having to draw 100 steps, let's try it with 10 first. With 100 steps the principles are the same.
The interesting part here, is that we can pair different numbers. We will add the numbers at the outside of the figure above, 1 and 10. This gives 11. Next the following outside numbers 2 and 9, also gives 11. In a figure, this would look as follows:
Hence, the answer is the surface of the square, \(5 * 11 = 55\).

Triangles

Obviously the surface area of the square is equal to the surface area of the staircase. If you look at the staircase, you might see a triangle. The surface of the triangle is half the surface of the square containing the triangle (actually containing the two triangles, forming the square).

So you could compute the surface of the staircase also by computing the surface of the square and dividing by two. As it's not a perfect triangle, we need to do some cutting. In geometry, this would look something like the following:

Anecdote

The Anecdote that everybody knows or has heard relates that Carl Friedrich Gauss3, a German mathematician who contributed significantly to many fields, solved this problem in primary school.

References

[1] LaTeX Mathematics
http://en.wikibooks.org/wiki/LaTeX/Mathematics
[2] What is the sum of the first 100 whole numbers?
http://mathcentral.uregina.ca/QQ/database/QQ.02.06/jo1.html
[3] Wikipedia - Carl Friedrich Gauss
http://en.wikipedia.org/wiki/Carl_Friedrich_Gauss

Thursday, 11 June 2015

Creating Math in Blogger

I've decided to add MathJax1 to my blog.

I have tried in the past to use MathML3, but it's support among internet browsers is still too fragmented to be of any use. It looks like this situation is likely to continue. Also, I am a big fan of the Tex notation, instead of the verbose XML I would need to get anything done in MathML.

The difference is obvious when you right-click on the Math Expressions below.

Example

When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c =
0\) and they are \[x = {-b \pm \sqrt{b^2 - 4ac} \over 2a}\]

JavaScript Snippet

I have added the below snippet to my HTML in my Template, as according to source [2].
<!-- Script for installing MathJax -->
<script type='text/x-mathjax-config'>
   //  <![CDATA[
    MathJax.Hub.Config({
      extensions: ["tex2jax.js"],
      showProcessingMessages : true,
      tex2jax: {
          inlineMath: [ ["\\(","\\)"] ],
          displayMath: [ ["\\[","\\]"] ],
          preview : "TeX",
          processEscapes: true
      },
      TeX: {
        Macros: {
            Moon: "{&#9790;}"
   }
   }
    });
   //   ]]>
</script>
<script src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' type='text/javascript'/>
<!-- End Script for installing MathJax -->

References

[1] MathJax
https://www.mathjax.org/
[2] The Astronomy Learner - MathJax on Blogger
http://astro-learned.blogspot.nl/2015/01/mathjax-on-blogger.html
[3] Wikipedia - MathML
http://en.wikipedia.org/wiki/MathML
W3 - MathJax Overview
http://www.w3.org/Math/MJ/Overview.html
Hanselman - Exploring Crossbrowser Math Equations Using MathML or LaTeX With MathJax
http://www.hanselman.com/blog/ExploringCrossbrowserMathEquationsUsingMathMLOrLaTeXWithMathJax.aspx

Thursday, 4 June 2015

RAID-1 Degraded - missing device

I have got two hard disks, both the same type/brand. They are two USB2.0 connected WesternDigital drives that work pretty well. I've setup a software-based RAID1 system using them both, by means of the MDADM Linux program. I find it works very well.

Thankfully I noticed something odd about the hard disks I used. It is probably due to Partial RAID1 mounting that I tried recently that I screwed something up.

The raid1 I setup contains two disks, as visible in the lsscsi command:
root@micemouse:~# lsscsi
[0:0:0:0]    disk    ATA      WDC WD5000AAKS-0 01.0  /dev/sda 
[4:0:1:0]    cd/dvd  SAMSUNG  DVD-ROM SDR-430  1.06  /dev/sr0 
[10:0:0:0]   disk    WD       Ext HDD 1021     2021  /dev/sdb 
[11:0:0:0]   disk    WD       Ext HDD 1021     2021  /dev/sdc
As you can see in the printout below, it is set to "Degraded", and we're missing one of the devices.
root@micemouse:~# mdadm --detail /dev/md127
/dev/md127:
        Version : 1.2
  Creation Time : Wed Mar  6 22:16:05 2013
     Raid Level : raid1
     Array Size : 1953380160 (1862.89 GiB 2000.26 GB)
  Used Dev Size : 1953380160 (1862.89 GiB 2000.26 GB)
   Raid Devices : 2
  Total Devices : 1
    Persistence : Superblock is persistent

    Update Time : Sat May 16 15:50:47 2015
          State : clean, degraded 
 Active Devices : 1
Working Devices : 1
 Failed Devices : 0
  Spare Devices : 0

           Name : micemouse:0  (local to host micemouse)
           UUID : ed4531c4:59c132b2:a6bfc3d1:6da3b928
         Events : 3748

    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed
       1       8       33        1      active sync   /dev/sdc1
We are missing /dev/sdb1.

Rebuilding the array

Let's re-add the missing device.
root@micemouse:~# mdadm --remove /dev/md127 /dev/sdb1
mdadm: hot remove failed for /dev/sdb1: No such device or address
root@micemouse:~# mdadm --add /dev/md127 /dev/sdb1
mdadm: added /dev/sdb1
The array is rebuilding itself.
root@micemouse:~# mdadm -D /dev/md127
/dev/md127:
        Version : 1.2
  Creation Time : Wed Mar  6 22:16:05 2013
     Raid Level : raid1
     Array Size : 1953380160 (1862.89 GiB 2000.26 GB)
  Used Dev Size : 1953380160 (1862.89 GiB 2000.26 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Sat May 16 15:54:20 2015
          State : clean, degraded, recovering 
 Active Devices : 1
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 1

 Rebuild Status : 0% complete

           Name : micemouse:0  (local to host micemouse)
           UUID : ed4531c4:59c132b2:a6bfc3d1:6da3b928
         Events : 3750

    Number   Major   Minor   RaidDevice State
       2       8       17        0      spare rebuilding   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1
Trying out "watch -n 2 cat /proc/mdstat" to check on its progress (every two seconds in this case).
Every 2.0s: cat /proc/mdstat                            Sat May 16 16:00:38 2015

Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [ra
id10]
md127 : active raid1 sdb1[2] sdc1[1]
      1953380160 blocks super 1.2 [2/1] [_U]
      [>....................]  recovery =  0.2% (5763072/1953380160) finish=6519
.5min speed=4978K/sec

unused devices: <none>
Recovery in progress, but it looks a long long way away yet.

Much later...

After a very long time, /proc/mdstat mentioned that things were finished:
Every 60.0s: cat /proc/mdstat                           Sun May 17 22:23:29 2015

Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [ra
id10]
md127 : active raid1 sdb1[2] sdc1[1]
      1953380160 blocks super 1.2 [2/2] [UU]

unused devices: <none>
And a closer look at the raid device, shows:
root@micemouse:~# mdadm -D /dev/md127
/dev/md127:
        Version : 1.2
  Creation Time : Wed Mar  6 22:16:05 2013
     Raid Level : raid1
     Array Size : 1953380160 (1862.89 GiB 2000.26 GB)
  Used Dev Size : 1953380160 (1862.89 GiB 2000.26 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Sun May 17 20:46:50 2015
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0

           Name : micemouse:0  (local to host micemouse)
           UUID : ed4531c4:59c132b2:a6bfc3d1:6da3b928
         Events : 5216

    Number   Major   Minor   RaidDevice State
       2       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1
The important part being "State: clean".

References

RAID1 - State : clean, degraded
https://www.howtoforge.com/community/threads/raid1-state-clean-degraded.66744/
Mdadm - recover degraded Array
https://www.thomas-krenn.com/en/wiki/Mdadm_recover_degraded_Array