How To Mount Virtualbox VDI With LVM Partition On Ubuntu

October 1, 2015 Leave a comment

First, install the QEMU tools. In Ubuntu, you’ll find them in the qemu-kvm package. Whatever package your distribution ships which contains the qemu-nbd binary should be fine and also install the lvm2

sudo apt-get install qemu-kvm

sudo apt-get install lvm2

Load the nbd kernel module. Yes, I’m serious, the network block device module!

sudo modprobe nbd max_part=16

Then run qemu-nbd, which is a user space loopback block device server for QEMU-supported disk images. Basically, it knows all about weird disk image formats, and presents them to the kernel via nbd, and ultimately to the rest of the system as if they were a normal disk.

sudo qemu-nbd -c /dev/nbd0 <vdi_file_location>

Ex: sudo qemu-nbd -c /dev/nbd0 /home/testuser/Desktop/SharedFolder/xubuntu_1404-disk2.vdi

Re-read The Partition Table Without Rebooting Linux System using partprobe command

sudo partprobe /dev/nbd0

Once installed, run pvscan to scan all disks for physical volume. this to make sure your LVM harddisk is detected by Ubuntu


After that run vgscan to scan disks for volume groups.


Reading all physical volumes. This may take a while…
Found volume group “VolGroup00” using metadata type lvm2

Activate all volume groups available.

vgchange -a y

Run lvscan to scan all disks for logical volume. You can see partitions inside the hard disk now active.

ACTIVE ‘/dev/VolGroup00/LogVol00’ [72.44 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01’ [1.94 GB] inherit

Mount the partition to any directory you want, usually to /mnt
mount /dev/VolGroup00/LogVol00 /mnt


Postgres Backup and restore database

August 10, 2015 Leave a comment

Following are some of the commands I use to backup and restore my postgres databases and tables.

  1. pg_dump is a nifty utility designed to output a series of SQL statements that describes the schema and data of your database. You can control what goes into your backup by using additional flags.
    Backup: pg_dump -h localhost -p 5432 -U postgres -d mydb > backup.sql
    Restore: psql -h localhost -p 5432 -U postgres -d mydb < backup.sql
    -h is for host.
    -p is for port.
    -U is for username.
    -d is for database.
  2. Single transaction restore:
    you can use –single-transaction in your restore command. It wraps out entire restore operation in one transaction, if something goes wrong it rollbacks all the changes.
    psql –single-transaction -h localhost -p 5432 -U postgres -d mydb < backup.sql
  3. Compressed Backup:
    If your backup is too big, you can use any compression utility to compress it. I prefer gzip.
    Backup: pg_dump -h localhost -p 5432 -U postgres -d mydb | gzip > backup.gz
    Restore: gunzip -c backup.gz | psql -h localhost -p 5432 -U postgres -d mydb
  4. Split Backup file:
    If you are going to email your backup files or transfer them via any medium on internet I will suggest splitting the files into short files. You can use split utility for splitting the files with a size limit. In the example I am usinf 2mb size limit.
    Backup: pg_dump -h localhost -p 5432 -U postgres -d mydb | split -b 2m – backup.sql
    Restore: cat backup.sql* | psql -h localhost -p 5432 -U postgres -d mydb
  5. Split compressed Backup file:
    This is just a combination of point 3 and 4. We first compress the file then split it instead of splitting the plain file.
    Backup: pg_dump -h localhost -p 5432 -U postgres -d mydb | gzip | split -b 1m – backup.gz
    Restore: cat backup.gz* | gunzip | psql -h localhost -p 5432 -U postgres -d mydb
  6. Parallel Backup:
    You can allow pg_dump to dump the backup data in parallel by including the -j flag. It tells pg_dump the number of tables it can dump in parallel. Parallel backup only works when you use more than one files for writing backup data hence directory. -F d sets the format to directory and -f provides the directory name.
    Backup: pg_dump -F d -f backup -j 20 -h localhost -p 5432 -U postgres -d mydb
    Restore: pg_restore -F d -j 20 -h localhost -p 5432 -U postgres -d mydb backup
  7. Backup of a specific table:
    You can take backup of a specify table by adding -t flag.
    Backup: pg_dump -h localhost -p 5432 -U postgres -d mydb -t my_table > backup.sql
    Restore: psql -h localhost -p 5432 -U postgres -d mydb < backup.sql
  8. Take Backup of all databases:
    pg_dumpall is used to take backup of all of your postgresql database. I think it is just a wrapper around pg_dump. It will ask password for every database.
    Backup: pg_dumpall -h localhost -p 5432 -U postgres > backup.sql
    Restore: psql -h localhost -p 5432 -U postgres < backup.sql
  9. Custom format backup (-F c):
    Keith mentioned in the comment that -F c provides better options at the time of restoring the backup. You can take the backup of whole database and restore only selected tables by using -t flag. It also compresses the backup data for you.All these feature are also provided by the directory format (-F d) too. The main difference between directory and custom format is that custom format generates a single file and directory format generates a directory full of files. A Single file(single stream) gives us many advantages like outputting the backup data over SSH or transferring it to some other service is easy compared to a directory(multiple stream)One more difference is that you can not use parallel backup option (-j) with custom format backup. It is obvious since it uses only one stream.

    So how do you take backup using custom format and restore it.

    Backup: pg_dump -F c -h localhost -p 5432 -U postgres -d mydb > backup.dat
    Restore: pg_restore -F c -h localhost -p 5432 -U postgres -t my_table -d mydb backup.dat

some other useful flags in pg_dump.

-v, –verbose verbose mode
-V, –version output version information, then exit
-?, –help show this help, then exit
-a, –data-only dump only the data, not the schema
-b, –blobs include large objects in dump
-c, –clean clean (drop) database objects before recreating
-C, –create include commands to create database in dump
-E, –encoding=ENCODING dump the data in encoding ENCODING
-n, –schema=SCHEMA dump the named schema(s) only
-N, –exclude-schema=SCHEMA do NOT dump the named schema(s)
-o, –oids include OIDs in dump
-O, –no-owner skip restoration of object ownership in plain-text format
-s, –schema-only dump only the schema, no data
-S, –superuser=NAME superuser user name to use in plain-text format
-t, –table=TABLE dump the named table(s) only
-T, –exclude-table=TABLE do NOT dump the named table(s)
-x, –no-privileges do not dump privileges (grant/revoke)

How to install Virtualbox guest additions on Xubuntu 14.04

October 13, 2014 1 comment

Note: The following instructions are tested in windows 7. Virtualbox installed in windows 7 and xubuntu 14.04 installed in virtualbox .

After installing xubuntu 14.04 we need enable the virtualbox guest additions to get the full screen other features like folder sharing. Following are the steps to installing Guest Additions.

Step 1: Install kernel headers and build tools.

Virtualbox guest additions are compiled for the target system, so it needs the necessary kernel headers and related programs. Install the following 2 packages.

sudo apt-get install build-essential module-assistant

Step 2: Now run following command

sudo m-a prepare

Step 3: Compile virtualbox guest additions

Now click “Devices > Insert guest additions CD image” in the virtualbox window. This will insert the guest additions cd image into the guest OS (xubuntu here). The cd should get mounted automatically inside the /media directory.

The path should be similar to something like this

Mount manually, If it does not mount by itself, then you can manually mount it. Find out the device using blkid and then use the mount command to mount it somewhere in.

Solving quotes issue in Templates while after using django-generate-scffolding

August 6, 2014 Leave a comment

‘url’ requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.
Solution 1:

sed -i -r ‘s/url ([^”]+) %/url “\1” %/g’ template.html

solution 2: Run this one from templates folder.

find . -type f -print0 | xargs -0 sed -i ‘s/{% url \([^” >][^ >]*\)/{% url “\1″/g’

Categories: Django Tags: ,

Integrating Django with a legacy database

August 1, 2014 Leave a comment

After connect to your database.

Step 1:

python inspectdb

Outputs general models from your db that u defined it in settings file.

Step 2:

python inspectdb > ./your_app/

creates file from the output in *nix systems.

Step 3:

You’ll have to do the following manually to clean this up:

Rearrange models order

Make sure each model has one field with primary_key=True

Remove ‘managed = False’ lines if you wish to allow Django to create and delete the table

Step 4:

But it will have some problems like same table will more than one foreign key fields, those filed names should not have the related_name because with out related_name you django can’t figure out which field is connects to foreign key filed. To check this run the following command.

You will see the sqlcustom [appname] but dont run this command because it wont take the

python sqlcustom [appname]





Basic System admin Tips in Debian

December 15, 2011 Leave a comment

How to change Log in message.(MOTD-Message of The Day)

sudo vim /etc/motd

 If you want to avoid /etc/motd to be overwritten with the old version upon reboot you also have to edit the following

sudo vim /etc/default/rcS

in this file change the EDITMOTD “yes” to “no”

Changing the computer name

sudo vim /etc/hostname

Categories: Sys Admin Tags: , ,
%d bloggers like this: