Wednesday, March 8, 2017

Mount a DD-created Disk Image

I have a lot of old disk images created with simple 'dd' commands. These images include the whole disk (all partitions) and are usually compressed. From time to time, I'll need to add files to those images or access data from them. Ideally, it's just quick to mount them to do that.

First, you need to decompress the image file. For gzip formats, I like to use pigz, since it's much faster on multi-core machines.

Now, you need to understand that you can only mount particular partitions. So, how do you access a particular partition in a single image file? You'll need to use a loopback offset.

Run fdisk to determine the partition table and get the data you'll need to calculate the offset.
# fdisk -l /path/to/IMG.dd
Disk IMG.dd: 120.0 GB, 120034123776 bytes
255 heads,63 sectors/track,14593 cylinders,tot 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002beb4
 Device Boot      Start         End      Blocks   Id  System
IMG.dd1   *        2048   230336511   115167232   83  Linux
IMG.dd2       230338558   234440703     2051073    5  Extended
IMG.dd5       230338560   234440703     2051072   82  Linux swap

Now you need to calculate the offset. In my example above, I'm interested in partition #1, which fdisk reports to start at sector 2048. Since there are 512 bytes in each sector, as reported, multiply 512 X 2048 to get your offset value in bytes. In this case, it's 1048576. You'll use that number in the next step. Setup your loopback device offset, using the value you calculated above. I just used loop0, since it wasn't being used for anything on my system at the time. You should use whatever is available.
# losetup -o 1048576 /dev/loop0 /path/to/IMG.dd
Mount the loopback device.
# mount /dev/loop0 /mnt/img
Now you can use your filesystem as with any other file system you'd mount!

To unmount the file system, you'll need to unmount it like you normally would, but then you need to remove the loopback device.
# umount /mnt/img# losetup -d /dev/loop0

Thursday, February 16, 2017

Fixing Linux Mint Cinnamon Hangs

Every once in a while, my GUI on Linux Mint hangs. The mouse moves, but that's it. Here's the only way I found to get things back up as they were (without losing all of your open windows and applications).

1. Get a terminal (Ctrl + Alt + F2 or similar).
2. Kill all cinnamon processes (killall -HUP cinnamon)
3. Go back to your GUI (Ctrl + Alt + F8).

After a few seconds or minutes, you'll get all your stuff back.

Thursday, February 9, 2017

Configure CentOS 6 VNC Server

One of the first things I like to do when provisioning a CentOS 6 server with a desktop GUI is to configure remote desktop; but the standard/included server (vino) only works when a user is already logged in. I want to usually be able to log-in remotely (in other words, have my VNC session log me in to a machine that has no users logged-in).

First, install and enable the VNC server:
# yum install tigervnc-server
# yum install xorg-x11-fonts-Type1
# chkconfig vncserver on
Set VNC password for your user:
# su - USER
# vncpasswd
# exit
Add the following lines to the following file:
# vim /etc/sysconfig/vncservers 
VNCSERVERS="1:USER"
VNCSERVERARGS[1]="-geometry 800x600"
Add port 5900 (TCP and UDP) to your firewall allow rules (I just did this graphically at the machine's physical terminal, but you can use iptables in the CLI).

See if VNC server comes up:
# service vncserver restart
Edit the user's vnc configuration to replace the "twm &" line with the following:
# service vncserver stop
# vim /home/USER/.vnc/xstartup 
exec gnome-session &
Test VNC server startup again:
# service vncserver restart
At this point, you should be able to point a VNC client at your machine, and you will automatically be logged-in. Note, it usually takes a little time... up to a minute or so, typically.

Thursday, January 19, 2017

Adding a JAR to an Android Studio Project

I recently needed to add some TFTP functionality to an Android app I've been working on. I opted to leverage the Apache Software Foundation's Commons-Net libraries. If you download the binaries, there is a .jar file you can use in your Android project.

To import the library into your Android Studio project, it's pretty simple.

  1. Create a new directory (if it isn't there) in your project called "libs."
  2. Copy the .jar file to your clipboard.
  3. In Android Studio's "Project" view, expand your project's app/libs branches.
  4. Paste the .jar file to the libs directory.
  5. Right click the .jar file, and then choose "Add As Library."
At this point, Gradle will rebuild, incorporating the new library into your project's builds; so at this point, you should be good to go. Import your library as needed and use it.

Tuesday, December 6, 2016

Migrating VMware Workstation Guests to KVM/qemu

I recently had the need to get one of my VMware Workstation guests (running CentOS 6.4) migrated to a KVM/qemu hypervisor environment. The VM was in the typical multiple file vmdk configuration that VMware Workstation uses.

Note: I've read that if you're migrating a Windows guest or a SCSI-originated guest (or something to that extent), you may get a BSOD or some other boot error. So this may not work for every case.

In summary, the process basically involves converting that multiple file setup into a single file, and then moving that single file into your storage pool for your KVM setup, from which you then import it. Of course, the devil is in the details, so...

Step 1 - Convert VMware multiple file VM into a single file
(note: if you have snapshots, it's best to delete those to ensure you're getting the latest image)
(note: your paths will vary)
# vmware-vdiskmanager -r /home/vm/vmware/c64/c64_disk.vmdk" -t 0 /home/c64.vmdk

During this, I got an error ("VixDiskLib: Invalid configuration file parameter.  Failed to read configuration file.") but I was able to continue on just fine. I assume this is about the VM's configuration, but since I was changing RAM amount and network stuff anyway, I didn't care.

Step 2 - Copy that single file to your storage pool / import location
(note: I'm doing it this way, as the next step is resource-intensive and my destination machine is faster - also, your paths will vary - also, I've setup my storage pool to not be the standard /var/lib/libvirt!)
# rsync -av /home/c64.vmdk [dest-machine]:/home/vm/c64/

Step 3 - Login to destination machine and convert the vmdk into a qemu-compatible format
(note: this will become your VM's main disk image file, so just put it in your pool where you plan on leaving it for the machine)
# qemu-img convert /home/vm/c64/c64.vmdk -O qcow2 /home/vm/c64/c64.img

Step 4 - Start virt-manager and import the VM
# virt-manager
- Create a new machine.
- Choose "Import existing disk image" option.
- Continue configuring it as necessary, and then boot it.

After all is said and done, you will need to tweak a few things (e.g. NIC/MAC, persistent net rules file, etc.), but you should basically be up and running.

Thursday, December 1, 2016

Headless CentOS 7 Virtualization Host: Part 1 - Host Installation & Setup

Here is how I setup a headless (no monitor) VM host on CentOS 7.2

Ensure your host's CPU can support virtualization (the result should be > 0):
# egrep -c '(vmx|svm)' /proc/cpuinfo
Install the KVM hypervisor and its necessary packages (this is competitive with vmware):
# yum install kvm libvirt virt-install qemu-kvm
Make sure the host's kernel is ready to perform NAT with the hypervisor for the VM guests. First we'll enable IP forwarding on the host:
# echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-ipforward.conf
# sysctl -p /etc/sysctl.d/99-ipforward.conf
Then, we'll modify the network adapter's configuration to use a bridge adapter (in my case, I'm using eth1). Open the config file for editing, comment out everything IP-related (IP, gateway, DNS, etc.), and add a link to the bridge configuration file that we'll add next:
# vim /etc/sysconfig/network-scripts/ifcfg-eth1
BRIDGE=virbr0
Next, we need to create a bridge adapter configuration file and add the following lines (your info will probably be same as what you commented out above):
# vim /etc/sysconfig/network-scripts/ifcfg-virbr0 
DEVICE="virbr0"
TYPE=BRIDGE
ONBOOT=yes
BOOTPROTO=static
IPADDR="[YOURS]"
NETMASK="[YOURS]"
GATEWAY="[YOURS]"
DNS1="[YOURS]"
If possible, reboot the machine to ensure kernel modules and network settings load and initialize.

Finally, just verify your installation and configuration:
# lsmod | grep kvm
# ip a show virbr0
# virsh -c qemu:///system list

Tuesday, November 29, 2016

Linux Partitions >2TB

For Linux hard drive partitions larger than 2TB, fdisk just doesn't cut it. You'll have to use parted, instead. But keep in mind that since this parted method uses GPT (part of EFI) instead of the old MBR type supported by most/all BIOS, you'll need to make sure your BIOS supports it (most newer ones should).

The example I'm using in my case was for a brand-new 3TB secondary storage device (no existing partitions), with the primary drive being for boot and of the MBR type on a smaller dedicated hard drive... hence "sdb" in the example below, instead of "sda."

First, verify the size of your drive:
# fdisk -l /dev/sdb
Then, get into the GNU parted shell:
# parted /dev/sdb
Once in the parted shell, issue the following commands...
First, create a GPT partition table:
(parted) mklabel gpt
Second, tell parted you'd like to use TB as the unit:
(parted) unit TB
Then, tell parted to create the 3TB partition:
(parted) mkpart primary 0.00TB 3.00TB
You may verify the partition table and quit:
(parted) print
(parted) quit
After that, all you have to do is create your file system and mount the drive.