Wednesday, April 22, 2015

NAS Setup: linux Mint 17.1, mdadm, plex, VNC, Headless, Wake on Lan

Install OS

Unplug all Raid drives. (is a bug where ubuntu and mint attempt to install grub to the first drive it finds, one of my raid drives, despite telling it to do otherwise. 

boot from live cd and then install os

Update grub so pc will boot:

by default the system had trouble booting (yup another fucking ubutu bug). to get it to boot edit grub. from the commandline:

sudo gedit /etc/default/grub
comment: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
save and exit
sudo update-grub

Install Plex:

Just install the ubuntu version found on plex site.

Install mdadm: 

sudo apt-get install mdadm
chose not to configure email if prompted
reboot

Edit fstab

get the array to mount at boot by editing fstab as root:
sudo gedit /etc/fstab

Add the following to the bottom of the file: 
UUID=yourraiddeviceuuid /mnt/yourstoragefolder ext3    defaults        0       2

the uuid can be found in the Disks app. yourstoragefolder must be created.  When creating ensure permissions for everyone are set and ensure the directory is shared.

Set Lock Settings

System settings > Screen Locker.
Dissable all forms of screen locking.

Dissable keyring unlock prompts

type "seahorse" at the terminal
Right click on the passwords > default and select Change password
enter old password and leave new ones blank
save and exit

Enable VNC

go to "Desktop Sharing"
check Allow other users to view your desktop
check Allow other users to control your desktop
under show notification are check Never

install dconf-editor
sudo apt-get install dconf-editor
in dconf-editor go to org > gnome > desktop  >remote-access
Uncheck everytihng except enabled

Add Cron Shutdown job

Set Cron to run: "shutdown -h now" as root.

Setup Mediabeaver















Thursday, March 8, 2012

Raid 5, mdadm, ubuntu: lessons learnt

This article documents my experiences setting up a raid 5 array using Ubuntu and mdadm. This article is mainly for my own reference. If you decide to follow any of the steps here in, the risk is all yours etc etc bla bla bla.


Tearing down an array 

While setting up an array and performing experiments you will, no doubt, have a need to tear one down at some stage.

"superblock" information on a drive flags to the OS / mdadm that  a disk is part of a raid array.  to tear down an array you need to halt and remove your array and then delete superblock information on all raid member drives.

execute the following:

mdadm --stop /dev/md0  # to halt the array
mdadm --remove /dev/md0  # to remove the array
mdadm --zero-superblock /dev/sd[abc]1  # delete the superblock from all drives in the array
(edit /etc/mdadm/mdamd.conf to delete any rows related to deleted array)

Note: I have found attempting to tear down an array while the OS is running problematic.  The easiest way is to do it is from the live CD.


Preparing  Advanced Format Drives (Western Digital Green 2TB) for a raid array 


Advanced format drives have a different sector size to previous generation disks.  As a result the primary partition on these disks need to start at a sector that is divisible by 8. I initially configured my array to start at sector 64 but I got a misalignment error: Go figure.  After several attempts, I successfully configured my drives to start at sector 2048.

here are the commands I used to partition my drives and prepare them for my raid array:

$ sudo parted -a optimal /dev/[disk]
(parted) mklabel gpt #creates a GUID partition table
(parted) u s #makes units sectors
(parted) mkpart primary ext4 2048 -1 #Makes a new partition starting on sector 2048 and ending as close to the end of the hard drive as possible. If it tells you it can’t use the last sector and offers another value, just accept that.cat /proc/mdstat
(parted) quit
#Note that you may type the letter p at any time to print the current details of the drive.


Create a raid 5 array using mdadm

this is the easiest part:

sudo mdadm --create /dev/md0 --level=5 --raid-devices=5 --chunk=64 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1

a few things to note here:

 /dev/md0 is the device path to your raid array. you should be able to find this info in Disk Utility if you array has been set up properly

 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1: are all the member drives that make up your array.  again you can find these drives in Disk Utility program.
cat /proc/mdstat cat /proc/mdstat cat /proc/mdstat cat /proc/mdstat cat /proc/mdstat
Chunk: is the size of the stripes on your array.  As I understand it, if your array is going to store large files then you probably want a largish chunk size; for small files you need a small chunk size. for video  for example: 512 kib or above is recommended.

the other options should be self evident

Depending on the number of disks and the size of the array, the array will take quite a while to build.  I have 5 2TB drives and it took me around 6 hours.


Format your array

After creating the array it needs to be formatted. This took about 30 minutes for me.

To do this go into Disk Utility, clicked on my raid array and then clicked on Format Volume.

On my first attempt I formatted my array using ext4. after mounting the volume I found however that jbd2, an ext4 journalling proof lines there that look like this:

cess, was constantly writing to my drives. jbd2 should write to disk once every 8 seconds (i think).  but of-course with 6 disks (including the OS) the amount of IO is ridiculous.  so I decided to go with ext3 instead and hey presto problem solved.

There may be a way round this,  you may be able to configure ext4 to play nice.  but hey who has the time.

at this stage you can mount your drive and start using it.  however in order for the array to be mounted and usable after booting you need to add an entry to fstab and to mdadm.conf


Edit fstab

open fstab as root. from the terminal type:


sudo gedit /etc/fstab

Add the following line to the bottom of the file.

UUID=yourraiddeviceuuid /mnt/yourstoragefolder ext3    defaults        0       2

note: you will need to replace yourraiddeviceuuid withyour array's uuid.  you can find this with the blkid command.  you also need to create a folder under /mnt/ with appropriate permissions.  /mnt/yourstoragefolder will need to point to this directory.  There are plenty of guides on mounting devices so I wont go into details here

save and close the text editor.


Edit mdadm.conf

in the terminal , type:

sudo mdadm --detail --scan

this will print information abof lines there that look like this:

out your array. copy the text that is produced. in my case this was:

ARRAY /dev/md0 metadata=1.2 name=NAS:0 UUID=150099eb:37d941df:fa50f43b:c8d6903b

open mdadm.conf. type:

sudo gedit /etc/mdadm/mdadm.conf


paste the text copied above under the heading "DEVICE partitions".

save and close the text editor.

thats it you should now have a raid array that is configured to start and mount at boot

Testing the array

Initially I tested the array by pulling the data cable from one of my hard drives and attempting to boot. I was unable to boot as a result.  Every time i attempted to boot i ended up at a console (busybox). It appears that the OS does not allow you to boot if you have a degraded array, even if that array is only used for storage and does not have a bootable partition. Go figure.

I eventually got around this problem by commenting GRUB_CMDLINE_LINUX="" in grub and adding the line below


#GRUB_CMDLINE_LINUX=""
GRUB_CMDLINE_LINUX="bootdegraded=true"


after editing grub restart it with


sudo update-grub


Ok so, on to testing the array.  first you need to fail the array. 2 ways to do this

1) pull the data cable on the hard drive. If you have modified your grub as above you should be able to simply pull the data cable on a hard drive.  (Note i did not test this successfully. after my initial problems i ended up resetting my entire array).

2) set the drive to failed using mdadm:


mdadm --manage /dev/md0 --fail /dev/yourdriveid


after you fail the drive you need to remove it with


mdadm --manage /dev/md0 --remove /dev/yourdriveid


note: I did not need to remove the drive. i restarted my pc straight after failing it.  after restarting, the drive had already been removedlater

finally you need to add the new drive.  (for my test i simply added the same drive back to the array).


adm --manage /dev/md0 --add /dev/yourdriveid



7 months Later

Just found a minor issue.  the device name of my array, md0, has mysteriously changed to md127. this does not appear to have had any impact on the working of the array but is still not ideal. after googling for many hours i could not find any reliable information about why this might have occurred. It  is particularly mysterious when you consider that i have had auto update turned of on my NAS.  

Anywho 1 solution which worked for me was to remove the name parameter in this line 

ARRAY /dev/md0 metadata=1.2 name=NAS:0 UUID=150099eb:37d941df:fa50f43b:c8d6903b

in mdadm.conf. Additionaly, i had to run 
later
sudo update-initramfs -u

Another wonderful Ubuntu mystery :-\
of lines there that look like this:

More that a year Later

1) I am converting my raid 5 to a raid 10. below are the steps 1 followed.
I copied all content to another drive so I could blow away my raid 5 array.

2) Tear down the array see "Tearing down array above"

3) Repartition the drives. See "Preparing  Advanced Format Drives (Western Digital Green 2TB) for a raid array" above. In addition to following all these steps I also added the following at the end.

(parted) 
set 1 raid on 

Note: I later unset this flag.  As far as I can see it makes no difference to the operation of the array

4) create the raid 10 array with:

sudo mdadm -v --create /dev/md0 --level=raid10 --chunk=64 --raid-devices=6 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 /dev/sdf1

Note: I probably should have given the array a bugger chunk size.

5) Monitor the creation with:

watch cat /proc/mdstat

6) I added

ARRAY /dev/md0 UUID=d94c27f0:d4e3ab95:e5dd0caa:ea902d29

to /etc/mdadm/mdadm.conf

7 ) I then updated initramfs with

 sudo update-initramfs -u

8) I added

/dev/md0p1 /mnt/storage ext3    defaults        0       2

to /etc/fstab














Tuesday, March 29, 2011

Set up Wake on LAN under Ubuntu


Perform the following on the PC you wish to wake:
  1. turn on Wake on Lan, or WOL, in the PC's BIOS.  How to do this will vary widely depending on your PC so you will need to research this one yourself

  2. Get the device id of the Ethernet adapter on which you wish to enable Wake on LAN.

    Open a Terminal' and type ifconfig.  The device id will appear next to a block of text that mentions Ethernet. in the response below eth0 is the Ethernet adapter

    eth0      Link encap:Ethernet  HWaddr 01:23:45:67:89:ab
              inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::215:f2ff:fe6f:3487/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:71495 errors:0 dropped:0 overruns:0 frame:0
              TX packets:76190 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:23164212 (22.0 MiB)  TX bytes:7625016 (7.2 MiB)
              Interrupt:217 Base address:0xd400
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:1290 errors:0 dropped:0 overruns:0 frame:0
              TX packets:1290 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:161182 (157.4 KiB)  TX bytes:161182 (157.4 KiB)

  3. Create a file under /etc/init.d/. This file is going to hook into your PC's boot processes and enable WOL each time your PC is booted. You may give this file whatever name you like.

  4. Inside the file add the code below.  Insert the ID of your Ethernet adapter in place of {adapter Id}.

    #!/bin/bash
    ethtool -s {adapter id} wol g
    exit

  5. Set appropriate permissions on your script file with the command below.  Change {script file} to the name of the script file you created above.

     sudo chmod a+x {script file}

  6. Ensure the script runs at start up with the following command

    update-rc.d -f {script file} defaults

Perform the following on the Wakee

To wake the pc you enabled WOL on above, follow the steps below:

  1. Get the mac address of the PC you wish to wake with the following command.  (obviously you will need to do this on the target PC):

    ifconfig | grep HW
  2. Wake the target PC with the following command:

    wakeonlan {mac address of the pc you are waking}

Thursday, March 24, 2011

Attach a test SSL certificate to an IIS 5.x web site

Ok this took me a while to figure out so I thought it was worth documenting.  I have a web service that I wanted to test on my local pc.  The web service is configured to accept SSL connections only so I needed a way to attach a certificate to my local IIS for the purposes of testing.  This turned out not to be so easy.  Steps to achieve this area detailed below:
  1. Open a command prompt. (Under Win XP go to Start > Run and type cmd)
  2. Create a test certificate and place it in your Personal certificate store by typing the following on the command line:

     cd C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin. 
    makecert -r -pe -n "CN=www.myserver.com" -b 01/01/2000 -e 01/01/2036 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localMachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12

    Note: C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin is the location of makecert on my pc.  do a search on your C drive if you cannot find the program under this location. Note also that you will need to change www.myserver.com to the domain for your server.  if you are installing the certificate on your local PC you can just use localhost.
  3. Add the certificate you just created to your Trusted Root Certification Authorities store.
    1. Access to your certificate stores
      1. go to Start > Run and type mmc
      2. File > Add / Remove snapin
      3. click on Add
      4. click on Certificates 
      5. click on Add
      6. click on Computer account 
      7. click on Next
      8. click on local computer: (the ...
      9. click on Finnish
      10. click on Close
      11. Click on Ok
    2. Open Personal > Certificates
    3. The certificate you created above should appear here. Right click on it.
    4. Click on Copy 
    5. Right click on  Trusted Root Certification Authorities > Certificates
    6. click on Paste
      you should now have a certificate which can be used in your local IIS and which will be trusted by local apps connecting to web apps and services that use the certificate.
  4. Attach the certificate you just created to IIS
    1. open IIS.
    2. right click on the web site node in IIS (obviously there will only be one if you are using IIS 5.x) and then click on Properties
    3. Click on Directory Security
    4. Click on Server Certificate.
    5. click on Next
    6. click on Assign an existing certificate
    7. click on the certificate you created above
    8. click on Next
    9. click on Finnish
Done

Thursday, January 13, 2011

Unit testing fun under Visual Studio 2010 Ulitmate

Recently I encountered a few interesting problem while unit testing an assembly in VS 2010. The assembly I was testing referenced web based code, HttpContext.Current, and was designed for use within a web application.

Unit tests don't run within a web context so testing assemblies like this is obviously problematic. One solution to this problem is to add the following to the head of your test methods:
[TestMethod()]
[HostType("ASP.NET")] 
[UrlToTest("http://localhost/SomeWebApp/Default.aspx")]
public void SomeTestMethod()
{
    ...
These directives instruct Visual Studio to run your unit test inside an  ASP.NET Host.  When the test is run the web app referenced by UrlToTest is fired up and the tests are run within this web site just after the page load event of the referenced web page.

This solution works fine in VS 2008 but when i try this in VS 2010 I get the following error:

The test adapter 'WebHostAdapter' threw an exception while running test 'testWebContext'. The web site could not be configured correctly; getting ASP.NET process information failed. Requesting 'http://localhost/someApp/VSEnterpriseHelper.axd' returned an error: The remote server returned an error: (500) Internal Server Error.
The remote server returned an error: (500) Internal Server Error.


I found the following "solutions" online:
  1. Set the test project and the web project to use version 4.0 of the .Net framework

  2. Add the following to your the Web.config file of your web site:
      <location path="VSEnterpriseHelper.axd">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
  3. Add the following to your the Web.config file of your web site:
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"/>
    
  4. Give the Network Service group permissions to everything under the web root of your web site / application. I gave full permissions to the Internet guest account (pcname\IUSR_pcname), the IIS process account (pcname\IWAM_pcname), the Network Service account.
None of these things worked for me or at least not under IIS. The only thing that worked in the end, after one very frustrating day, was to set my web site to .Net 4.0 and to use the development server that comes bundled with Visual Studio as the host (the one that is used for "File System" web sites).

To use the Development Server you need to add one more directive to your test method:
[AspNetDevelopmentServerHost("D:\\SomeWebApp\""/SomeWebApp")]
Your Method should now look something like this:
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\SomeWebApp\", "/SomeWebApp")]
[UrlToTest("http://localhost:2100/Default.aspx")]
public void SomeTestMethod()
{
     ...
Using the Development server  is not an ideal solution for me, nor is only being able to unit test version 4.0 apps, but this seems to be the only way it will work.

I hope this saves someone else having to bang their head against the wall for a day.

cheers