About Me

My photo
I know the last digit of PI

Sunday, November 20, 2016

Convert MBR to GPT and BIOS bootable to UEFI bootable without data loss

Convert MBR to GPT

Instructons for Windows users

  1. Clone the disk in order to have backup data. You can skip this step but if somethings goes wrong the data on the disk may be lost
  2. Identify the disk which will be converted. Open Windows Disk Management and look the number (e.g. Disk 0).
  3. Download gptgen from sourceforge http://sourceforge.net/projects/gptgen This tool will allow you to convert the paritition table without loosing any data
  4. Unzip and start gptgen under elevated CMD (via Run as Administrator)
    gptgen.exe -w \\.\physicaldriveX
    Replace physicaldriveX with the disk number (e.g. physicaldrive0)
Now the disk should be converted to GPT. If you face a problem that the disk doesn't have enough space at the end of the disk, then you can shrink the volume (assuming that you have free space on the volume). This can be done with 3d party tools or with diskpart
  1. Open elevated CMD and run
    diskpart
  2. list volume
  3. select volume
    and choose the number of the corresponding volume (e.g. 0)
  4. shrink
  5. exit
  6. Rerun gptgen

Instructions for Linux

  1. Boot and open terminal. You need parted installed
  2. Make backup of the existing MBR partition table:
    parted /dev/sda unit s print > partition_table.txt
    If you are converting the disk with the operating system you are currently running copy the partition_table.txt file to USB drive or upload it to internet, so you can access it later if something goes wrong.
  3. Create new GPT partition table overriding the existing MBR table
    mktable gpt
    When asked choose to ignore the warnings and to continue (with overwriting). We will recreate the existing partitions on exact sectors they have been before, but with new partition table format.
  4. Recrete partitions by using the information from partition_table.txt file and commands
    unit s
    mkpart
    When asked fill the partition type (e.g. ext4) and partition starting/ending sector (see partition_table.txt)
  5. Optionally set the legacy boot flag to the corresponding partition number.
    set 1 legacy_boot on
  6. Print the partition table and compare it with the partition_table.txt file
    print

Convert BIOS bootable to UEFI bootable for GPT partitioned disk

Since it is difficult to do the conversion while running the operating system I suggest you download a Fedora live CD and create bootable USB drive, so you can boot from it. Make sure that the USB is UEFI bootable - this means when you write it with rufus, choose GPT partition table and FAT32 system. If you plan to convert Windows to UEFI, then you need also a Windows installation DVD or USB. Then change the boot type in BIOS from Legacy BIOS to UEFI - see docs from your commputer manifacturer how to do it.

In order for UEFI to work you need a new partition with type C12A7328-F81F-11D2-BA4B-00A0C93EC93B - EFI System Partition (ESP). The new partition should be formatted with FAT32 system. The partition should be the first one. It will contain the files required to boot the operating system(s).

Windows

In GPT Windows needs additional partition called Microsoft Reserved Partition (MSR). In standard MBR partition scheme Windows used hidden sectors to store system data, but GPT doesn't allow hidden sectors, so MSR partition is used instead. It should not be formatted with any file system, because its sectors will be used directly by the operating system. The partition type should be E3C9E316-0B5C-4DB8-817D-F92DF00215AE.

How to create the ESP and MSR under Windows

This guide is adapted from Technet article
  1. Boot from Windows installation DVD or USB
  2. Choose repair and open command prompt
  3. Start diskpart
  4. Select the disk where the ESR and MSR partitions will be created
    list disk
    select disk X
    Replace X with the disk number (e.g. 0)
  5. Usually you have one small bootable partition used to boot Windows. We are going to delete it and use the space to create a new ESR and MSR
    list partition
    select partition 1
    delete partition
  6. Now create ESR
    create partition EFI size=100 offset=1
    format quick fs=fat32 label="System"
    assign letter=S
  7. Now create MSR
    create partition msr size=128 offset=103424
  8. Find out the windows installation drive letter.
    list volume
    You can also reassign it if needed (Usually it should be C:)
    select volume 3
    assign letter=C
  9. Exit disk part
    exit
  10. Generate boot partiton data
    bcdboot c:\windows /s s: /f UEFI
    Replace C: with your Windows installation letter

Linux

TODO : (draft version) 1. Delete /dev/sda1 2. Shring /boot and move it at the end, so the begining of the disk is free space 3. Create ESR at begining 4./etc/fstab -> mount UUID=xxxxx to /boot/efi 5.copy livecd /boot/efi to /dev/sda1 (UUID=xxxx)

Sunday, October 30, 2016

Keyboard back light under linux.

For some reason my fedora is turning the keyboard back light off. Even if I press the keyboard shorcut to turn the light on, it doesn't work. For a long time I thought that the reason is hardware problem, until I found this post and it turns out to be software issue. Here is the script that I used on my machine to restore the keyboard light. Execute following command line several times until it outputs "100" (percents)
kb-light.py +
or
kb-light.py --up
The python script:
#!/usr/bin/env python3
# coding: utf-8

from sys import argv
import dbus


def kb_light_set(delta):
    bus = dbus.SystemBus()
    kbd_backlight_proxy = bus.get_object('org.freedesktop.UPower', '/org/freedesktop/UPower/KbdBacklight')
    kbd_backlight = dbus.Interface(kbd_backlight_proxy, 'org.freedesktop.UPower.KbdBacklight')

    current = kbd_backlight.GetBrightness()
    maximum = kbd_backlight.GetMaxBrightness()
    new = max(0, current + delta)

    if new >= 0 and new <= maximum:
        current = new
        kbd_backlight.SetBrightness(current)

    # Return current backlight level percentage
    return 100 * current / maximum

if __name__ == '__main__':
    if len(argv[1:]) == 1:
        if argv[1] == "--up" or argv[1] == "+":
            # ./kb-light.py (+|--up) to increment
            print(kb_light_set(1))
        elif argv[1] == "--down" or argv[1] == "-":
            # ./kb-light.py (-|--down) to decrement
            print(kb_light_set(-1))
        else:
            print("Unknown argument:", argv[1])
    else:
        print("Script takes exactly one argument.", len(argv[1:]), "arguments provided.")

Sunday, October 02, 2016

How to monitor progress of linux command dd.

If you start a dd command from the command prompt, there is no progress output. The easiest way to get the current dd progress is by executing following commmand from different terminal
sudo kill -USR1 $(pgrep ^dd)
The progress will be printed in the dd terminal. To print the progress every 5 seconds use
watch -n5 'sudo kill -USR1 $(pgrep ^dd)'
For dd version 8.24 and above the following command line will print the progress
dd status=progress  if=xxx of=yyy

Thursday, June 30, 2016

NGINX Reverse proxy and load balancer for Java servers

This is a simple script for the load balancing and reverse proxy configuration for an application called "myAppName" which runs on two servers "https://xxxxx:8080/myAppName" and "https://yyyyy:8080/myAppName". The script is designed for Java servers (tomcat, glassfish, jboss, etc.)

However please note that since NGINX open source doesn't support sticky it is not suitable for productive environments load balancing, because it uses ip_hash directive, which tells nginx that all request from particular IP to be served by one and the same server. If that IP is the external IP of large internal LAN (via NAT), then it means only one server will be loaded, which may crash that server. If you are not willing to pay $1900/yr then stick to apache httpd.

There are two options - the reverse proxy machine is serving all requests on the root "/" or "/myAppName".

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    
    # Load balancing    
    upstream myAppName {        
        ip_hash;
        server xxxxx:8080;
        server yyyyy:8081;
    }
    
    server {
        listen       1080;
        server_name  localhost;

        location / {
            rewrite ^/myAppName/(.*)$  /myAppName/$1 break;
            rewrite ^(.*)$  /myAppName/$1 break;
            proxy_pass http://myAppName;
            proxy_cookie_path / /myAppName;    
        }
        
        #location /myAppName {            
        #    proxy_pass http://myAppName;
        #    proxy_cookie_path /myAppName/ /myAppName;    
        #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #    proxy_set_header Host $http_host;
        #    proxy_set_header X-Real-IP $remote_addr;
        #    proxy_set_header X-Forwarded-Proto https;
        #    proxy_redirect off;
        #    proxy_connect_timeout      240;
        #    proxy_send_timeout         240;
        #    proxy_read_timeout         240;
        #    if ($http_cookie ~* "jsessionid=([^;]+)(?:;|$)") {
        #        set $co "jsessionid=$1";
        #    }
        #    proxy_set_header Cookie "$co";            
        #}
    }    
}

Monday, March 28, 2016

Eclipse is too big under Gnome (Fedora 23)

To make eclipse looks more nicer (similar look as on Windows machines) perform following steps:
  1. Create new eclipse launcher and pass GTK version 2 as parameter
    [Desktop Entry]
    Encoding=UTF-8
    Name=Eclipse IDE
    Exec=/opt/eclipse.4.5.2/eclipse --launcher.GTK_version 2
    Icon=/opt/eclipse.4.5.2/icon.xpm
    Type=Application
    Categories=Development;
    
  2. Create configuration file ~/.gtkrc-2.0 with following content:
    style "gtkcompact" { 
      font_name="Sans 8"
      GtkButton::defaultborder={0,0,0,0} 
      GtkButton::defaultoutsideborder={0,0,0,0} 
      GtkButtonBox::childminwidth=0 
      GtkButtonBox::childminheigth=0 
      GtkButtonBox::childinternalpadx=0 
      GtkButtonBox::childinternalpady=0 
      GtkMenu::vertical-padding=1 
      GtkMenuBar::internalpadding=0 
      GtkMenuItem::horizontalpadding=4
      GtkToolbar::internal-padding=0 
      GtkToolbar::space-size=0 
      GtkOptionMenu::indicatorsize=0 
      GtkOptionMenu::indicatorspacing=2 
      GtkPaned::handlesize=4 
      GtkRange::troughborder=0 
      GtkRange::stepperspacing=0 
      GtkScale::valuespacing=0 
      GtkScrolledWindow::scrollbarspacing=0 
      GtkExpander::expandersize=10 
      GtkExpander::expanderspacing=0 
      GtkTreeView::vertical-separator=0 
      GtkTreeView::horizontal-separator=0 
      GtkTreeView::expander-size=8 
      GtkTreeView::fixed-height-mode=TRUE 
      GtkWidget::focuspadding=1 
    } 
    class "GtkWidget" style "gtkcompact"
    
    style "gtkcompactextra" { 
      xthickness=2 ythickness=2 
    } 
    class "GtkButton" style "gtkcompactextra"
    class "GtkToolbar" style "gtkcompactextra"
    class "GtkPaned" style "gtkcompactextra"
    

Thursday, January 21, 2016