Brain Dump

A place to store my random thoughts and anything else I might find useful.

Archive for the ‘Uncategorized’ Category

Dual boot UEFI Windows 7 and Ubuntu 12.04 (adapted from askubuntu.com)

Posted by mzanfardino on November 20, 2012

In an effort to ensure that I do not forget how to perform the following tasks, I am re-posting them here. This is a near-verbatim copy of the instructions provided at http://askubuntu.com/questions/193144/dual-boot-uefi-windows-7-and-ubuntu-12-04-both-64-bits-w7-entry-doesnt-appea.

Find where the windows bootloader resides by mounting the efi partition, and searching for bootmgfw.efi. My configuration has Windows installed on four (4) separate devices: sda1, sda2, sda3, and sda4. The GRUB scripts find windows installed on sda3, but this does not boot as it’s not the correct EFI entry. In my case, I found bootmgfw.efi on sda1.

$ mount /dev/sda1 ~/tmp/sda1
$ grub-probe --target=fs_uuid ~/tmp/sda1/EFI/Microsoft/Boot/bootmgfw.efi
UUID=2431-71DE

Edit grub custom script to add an entry for UEFI boot for Windows 7:

$ vim /etc/grub.d/40_custom
menuentry "Windows x86_64 UEFI-GPT" {
search --fs-uuid --no-floppy --set=root 2431-71DE
chainloader (${root})/efi/Microsoft/Boot/bootmgfw.efi
}

NOTE: replace the UUID follow ‘–set=root’ with the value returned from grub-probe. Then run update-grub:

$ sudo update-grub

This worked in my case.

NOTES:
The PC I configured for DualBoot is an HP Pavilion pre-installed with Windows 7 Home Premium. I used gparted to resize sda3 to make space before I installed ubuntu. I installed ubuntu from the alternate install CD (64-bit).

Advertisements

Posted in Uncategorized | Leave a Comment »

How-To: Remove errant CR (Carriage return, ^M, ‘\r’, 0x0D, 13 in decimal) from text file.

Posted by mzanfardino on July 5, 2012

Contents

1. Overview
2. Solution
3. Summary
4. References

Overview

There are occasions when one has to work with a text file originating from a DOS/Windows platform on a *nix machine. Opening the file on *nix reveals the addition of a carriage return (^M) appended to the end of each line. This occurs as a result of DOS/Windows utilizing a combination of the CR (Carriage return, ^M, ‘\r’, 0x0D, 13 in decimal) + LF (Line feed, ^L, ‘\n’, 0x0A, 10 in decimal) to indicate a new line while *nix traditionally uses just the LF[1].

Solution

Depending on the depth of the problem there are a number of solutions one might implement. When dealing with an individual file I find that vi/m is the right tool for the job. I imagine emac or other editors are equally suited to the task, but as I’m a vi/m guy, this is what works for me:

Before:

$ vim file_with_errant_cr
This is a test^M
This is also a test^M
Here again, another test^M
What, more tests?^M
Yeah, but this is the last test.^M
:%s/^v^M//g

NOTE: the ^v in this context is the Ctrl-v key-combination and ^M is the Ctrl-m key-combination. These are literals; do not type ^v and ^m. When typed the output will look like:

:%s/^M//g

After:

This is a test
This is also a test
Here again, another test
What, more tests?
Yeah, but this is the last test.

That’s it! Save the file and all the errant CR (^M) are gone.

ED: Additional research has revealed (several) additional, easy solutions to this problem. One of the easiest is to issue the following command in vim:

:%s/.$//

This simple but effective alternative replaces the last character of each line (.$) with nothing (//). No need for ^v/^m key combos. However, it is important to note that this does exactly what I said; so let me restate: it replaces the last character of each line regardless of what that character is! So be sure this is what you want to do before doing it!

When dealing with multiple files it is impractical to have to edit each and every file. In this case I would write a simple bash routine which uses sed to find and replace the errant character and save the files to something else. Let’s take for example a group of files named testn.txt where n is some incremental value:


$ for f in test*.txt; do cat ${f} | sed s/^v^M//g > ${f/.txt/.new}; done;

As with vi/m, the ^v is the Ctrl-v key-combination and ^M is the Ctrl-m key-combination. When typed the output will look like:


$ for f in test*.txt; do cat ${f} | sed s/^M//g > ${f/.txt/.new}; done;

The results will be new files created absent the CR with file extension .new. The original files will be left untouched.

Summary

The approaches I’ve taken here were inspired by research I did on the subject and the input I received from Quinn McHenry’s post on Tech Recipes[2]. Quinn also explains clearly that “In UNIX, you can escape a control character by preceeding it with a CONTROL-V”. This was critical to my understanding of how to use sed as well as understanding how vi/m can be used to replace control characters.

References

  1. Newline: From Wikipedia, the free encyclopedia
  2. Remove ^M characters at end of lines in vi by Quinn McHenry

Posted in Uncategorized | Leave a Comment »

How to mirror installed apps on Debian-based linux distros

Posted by mzanfardino on November 22, 2011

I will flesh this out with comments later. Suffice to say, if one needs to mirror the applications installed on one Debian-based linux distro (such as Ubuntu) to another (also Debian-based, preferably running the save release) one merely needs to generate a list of those apps installed on host A and configure host B to install the same apps. This is done using dpkg and apt-get.

Generate the list by logging into host A:

# dpkg --get-selections \* > apps.lst

Copy apps.list to host B and execute the following:

# dpkg --set-selections < apps.lst
# apt-get -u dselect-upgrade

If there are apps which need to be installed you will see the familiar apt-get/aptitude interface displaying the apps to be installed and a prompt to confirm. Once confirmed, the apps will be installed and both machines should be mirrors (from an application standpoint) of each other.

Posted in Uncategorized | Tagged: | 1 Comment »

How-to: mount an uncompressed g4l image

Posted by mzanfardino on April 9, 2010

Occasionally it becomes necessary to mount an uncompressed g4l image (created with g4l explicitly with compression none). This is a simple process that can be accomplished with just a few commands. Firstly you must determine what the offset of the partition within the image you wish to mount. This is done using parted. Once you have the offset of the partition use mount to mount the partition, giving mount the offset discovered by parted.

This example assumes a file named: SMCCAPCON_HMBN_20100106.img

$ parted /home/ftp/img/SMCCAPCON_HMBN_20100106.img unit B print
WARNING: You are not superuser. Watch out for permissions.
Warning: Unable to open /home/ftp/img/SMCCAPCON_HMBN_20100106.img read-write
(Permission denied). /home/ftp/img/SMCCAPCON_HMBN_20100106.img has been opened
read-only.
Warning: Unable to open /home/ftp/img/SMCCAPCON_HMBN_20100106.img read-write
(Permission denied). /home/ftp/img/SMCCAPCON_HMBN_20100106.img has been opened
read-only.
Model: (file)
Disk /home/ftp/img/SMCCAPCON_HMBN_20100106.img: 4098834432B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32256B 3857656319B 3857624064B primary ext3 boot
2 3857656320B 4096189439B 238533120B extended
5 3857688576B 4096189439B 238500864B logical linux-swap

Note that this image contains an entire disk image which has been formatted with a primary partition (#1) which contains root and an extended partition (#5) which contains the swap file.

The next step is to mount the root partition as a loop-back device to a local path. For this example create a mount point at /tmp/mnt (this mount point can of course be anywhere on your file system). Since the image contains both a root partition and the swap partition, it’s critical to use the offset of the root partition as a parameter for the mount command. The offset for root in this example is 32256B.

$ mkdir /tmp/mnt
$ sudo mount -o loop,offset=32256B /home/ftp/img/SMCCAPCON_HMBN_20100106.img /tmp/mnt
The partition is now mounted on /tmp/mnt. To unmount the partition, simply use umount as usual.
$ umount /tmp/mnt

NOTE: If it happens that the image is compressed using lzop, simply uncompress the image first, then take the same actions above. To uncompress a lzop file, use lzop.

$ lzop -x SMCCAPCON_HMBN_20100106.lzo
The resulting file for this example will be SMCCAPCON_HMBN_20100106, not SMCCAPCON_HMBN_20100106.img.
To verify the file type, use the file command.
$ file SMCCAPCON_HMBN_20100106.lzo
SMC_CAPCON_PLM_2010-01-21.lzo: lzop compressed data - version 1.020, LZO1X-1, os: Unix

$ file SMCCAPCON_HMBN_20100106
SMC_CAPCON_PLM_2010-01-21: x86 boot sector; partition 1: ID=0x83, active, starthead 1, startsector 63, 7534422 sectors; partition 2: ID=0x5, starthead 0,
startsector 7534485, 337365 sectors

NOTE: There maybe a need to resize the partitions of an image file. It is possible to resize the partitions using gparted. gparted can be called with the image file as a parameter. However, in my case gparted was unable to resize my partition (could not find the device to resize). I tried mounting the partition to /dev/loop0 using:

$ sudo losetup /dev/loo0 my.img

This succeeded in mounting the image to /dev/loop0 but again gparted could not find the individual partitions to mount (/dev/loop0p1 in my case). This left me to attempt to use ‘parted’. Another road-block, as my partition is ext4 and parted does not support ext4 (at least, the version I’m running).

parted suggested I use resize2fs but again, no individual partition to resize! This is becoming a tricky process.

So what did I need? I needed access to the individual partitions inside the image as accessible devices. Specifically, using the tool ‘kpartx’ I was able to mount each partition in the image to a device mapper such as /dev/mapper/loop0p1. Now I could use ‘resize2fs’ to resize /dev/mapper/loop0p1!

Posted in Uncategorized | 1 Comment »

Unofficial feature for obsolete version of GRUB

Posted by mzanfardino on December 18, 2009

Recently I had been asked to ‘brand’ the GRUB menu for my company’s servers. The company has made the decision to deploy Ubuntu Long Term Support server software. And though we maintain current with security patches, we do not wish to radically alter the distribution that Canonical has release under its LTS program.

As such, we have a number of server running Ubuntu 8.04 LTS and 9.04 LTS. Canonical has seen fit to distribute these distribution with GRUB 0.97. And though GRUB2 is available, it has been decided by the company not to install GRUB2 at this time.

This, coupled with a recent change in the look of the company colors, has cause a problem with ‘branding’ the GRUB menu. It seems that GRUB 0.97 does not support the COLOR attribute if the SPLASHIMAGE attribute is in use. The result of which left me with an untenable problem: when I load the new company SPLASHIMAGE which is red lettering on a white background, I was unable to change the color of the text, which is also white by default (thankfully with black shading, so it’s at least visible).

While researching this issue, I came across bug #16190 filed with the GRUB project (https://savannah.gnu.org/bugs/?16190). In it, a helpful user (adrian15) wrote:

Non-standard grub such as the one from Ubuntu is not supported officially by Grub.

However I am going to help you. The color command when having a background image does not work as you have seen.

Use the following commands instead:

background RRGGBB
foreground RRGGBB
adrian15

I followed adrian15’s suggestion, knowing full-well that this is not an officially supported feature of GRUB. My first attempts failed to yield any results. No matter what color combination I defined it was not reflected in the GRUB menu. I continued to explore this option though, as I was certain that had adrian15 been wrong it would have been noted in the bug.

I found while editing the menu interactively I could in fact set the foreground and background colors to values of my choosing. I concluded then that it must be the order in which these settings appeared in /boot/grub/menu.lst that was the problem. Sure enough, by moving the foreground and background setting after the SPLASHIMAGE setting I was able to achieve my desired results!

In summery, if you are using GRUB 0.97 (I don’t care why) and you have need to customize your GRUB menu with a splash image of your own design and you want to change the color of the text, you can do so with the unofficial settings: FOREGROUND and BACKGROUND. Just remember that you must first specify the SPLASHIMAGE, otherwise the color choice will not appear to take effect.

ex:
...
splashimage=(hd0,0)/boot/grub/images/my_splash_image.xpm.gz
foreground ff0000
background 000000
...

This example will set the foreground color of the text to red and the background color of the text to black.

I have added a comment to the now-closed bug entry so that others may benefit. I must stress however that this is an unofficially feature of an obsolete version of GRUB! Do *NOT* expect to get support from the GRUB team or me if this does not work for you!

Posted in Uncategorized | Leave a Comment »