simple examples of how to

Monday, October 31, 2011

[Android] can't create '/system/bin/busybox': read-only filesystem

You just need to change the read-only option of the mounted filesystem. (FYI it uses yaffs2 filesystem)
In my case,

$ adb shell
$ su
$ mount -o rw,remount /dev/block/mmcblk0p4 /system


application: how to change init.rc

$ mount -o remount,rw rootfs /
and change it!!

[Android] connecting android in linux

first, download android-sdk and use

$ sudo adb start-server # you should start the server as root

$ adb shell # you can connect to the android shell

Saturday, October 29, 2011

[PYTHON] waiting for key input

raw_input("Press Enter to continue")

[Openstack] QEMU at most memory 2047 occurs

try to use euca-run-instance xxx -k key -t m1.tiny

m1.tiny only requests 512MB memory


[Openstack] dnsmasq bind error

If you see bind error while running openstack nova-compute or nova-network,
it probably is the generic dnsmasq launched by /etc/init.d
Just /etc/init.d/dnsmasq stop
and try again

[Openstack] rabbitmq rejects with invalid credentials

When rabbitmq log shows invalid credentials and rejects nova-services to join the cluster,
it probably means that the password is wrong.

nova-services in default use guest/guest (id/pw) for rabbitmq.
you can add (or change) the a user (or pw) with the following commands

add a user guest/guest
$ rabbitmqctl add_user guest guest

change the pw
$ rabbitmqctl change_password guest guest

Friday, October 28, 2011

[Openstack] Suggestion on the tight correlation to the mysql database and nove-service processes

If we stop the service, such as 'stop nova-network'
it would be appreciated if the corresponding service is deleted in mysql services table in nova.

[Openstack] nova.conf configuration

in nova.conf configuration flag, routing_source_ip flag should use 'numbered-ip' such as 192.168.1.1, but not the host name.
I thinkg openstack guys try to tell us by naming the flag as 'routing_source_IP'.
Since it is used as the input param to iptable-restore, thus if you use host name on 'routing_source_ip' nova-network will unexpected die.
If you try to run the nova-network again, it will be on deadlock, since the module that performs this use external lock in /var/log/nova/ and hangs in ps aux.
This makes you think nova-network is working fine but NO IT IS NOT.

It takes half a day to figure out what the heck is going on.

[OpenStack] possible self-deadlock?

Openstack manages external lock in /var/lock/nova directories.

If a service, (in my case, nova-network) dies while it is acquiring the lock, nova-network never be able to run properly since it is waiting for the lock that will be never released. (since the process that acquires the lock was terminated unexpectedly).

Openstack servies should release all locks with 'launch-all' command things like that.

[OpenStack] nova-xxx who registers itself to DB as service?

At function nova/service.py, line around 143

service_ref = db.service_get_by_args(ctxt,
self.host,
self.binary)

function!

Thursday, October 27, 2011

[Python] Starting document for python paste

About WSGI application and WSGI middleware: http://pythonpaste.org/url-parsing-with-wsgi.html

About deployment and ini-configuration : http://pythonpaste.org/deploy/

Wednesday, October 26, 2011

How To Resize ext3 Partitions Without Losing Data


1 Preliminary Note

(I run all the commands in this tutorial as the root user, so make sure you're logged in as root. If you are on a Ubuntu system, you can become root like this:

sudo su

)

I have tested this on a Ubuntu Edgy Eft desktop system that has all files in one large partition (around 10 GB, device /dev/sda1). The partitioning looks like this:

df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.5G 4.1G 4.9G 46% /
varrun 94M 132K 94M 1% /var/run
varlock 94M 0 94M 0% /var/lock
udev 10M 52K 10M 1% /dev
devshm 94M 0 94M 0% /dev/shm
lrm 94M 18M 77M 19% /lib/modules/2.6.17-10-generic/volatile

The partition that is to be resized must be unmounted when we do the resizing; obviously this is not possible if this is the partition that holds all important system files like in this example. Therefore we download a Live Linux-CD such as Knoppix from which we boot later on (if you have physical access to the system). If it is a remote system that you don't have physical access to, you need a rescue system on that system (a lot of hosting companies offer dedicated servers with rescue systems nowadays) that you can boot into (instead of Knoppix), and this rescue system must have the following tools: fdisk, umount, fsck,tune2fs, e2fsck, resize2fs.

If the partition that you want to resize doesn't hold any system files (such as /home partitions, partitions for backups, etc.), you don't need a Knoppix Live-CD or a rescue system, because all steps can be run from the original system.

If you want to resize partitions on production systems, please back up your data before, because it is possible you lose all your data if you don't calculate the size of your new partition correctly (especially when shrinking a partition)! You have been warned! Tutorials about backups can be found here:http://www.howtoforge.com/taxonomy_menu/1/34

I'm going to resize /dev/sda1 in this tutorial. If your partition is named differently, please replace /dev/sda1 with your own device (e.g. /dev/hda5, /dev/sdb3, etc.).

2 Shrinking An ext3 Partition

This chapter is about shrinking an ext3 partition. I want to shrink /dev/sda1 in this example. First we gather some details on our original system:

df

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 9859036 4234908 5123304 46% /
varrun 95480 132 95348 1% /var/run
varlock 95480 0 95480 0% /var/lock
udev 10240 52 10188 1% /dev
devshm 95480 0 95480 0% /dev/shm
lrm 95480 17580 77900 19% /lib/modules/2.6.17-10-generic/volatile

df -B 4k

Filesystem 4K-blocks Used Available Use% Mounted on
/dev/sda1 2464759 1058727 1280826 46% /
varrun 23870 33 23837 1% /var/run
varlock 23870 0 23870 0% /var/lock
udev 2560 13 2547 1% /dev
devshm 23870 0 23870 0% /dev/shm
lrm 23870 4395 19475 19% /lib/modules/2.6.17-10-generic/volatile

df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.5G 4.1G 4.9G 46% /
varrun 94M 132K 94M 1% /var/run
varlock 94M 0 94M 0% /var/lock
udev 10M 52K 10M 1% /dev
devshm 94M 0 94M 0% /dev/shm
lrm 94M 18M 77M 19% /lib/modules/2.6.17-10-generic/volatile

fdisk -l


Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1247 10016496 83 Linux
/dev/sda2 1248 1305 465885 5 Extended
/dev/sda5 1248 1305 465853+ 82 Linux swap / Solaris

fdisk -s /dev/sda1

10016496

Then we shut down the system and boot into our Knoppix Live-CD (or your rescue system) (if the partition you want to resize doesn't hold any system files, you can do everything from the original system; the steps are the same, just omit booting into Knoppix/your rescue system).

shutdown -r now

After Knoppix has booted, open a terminal and become root by running

su

/dev/sda1 should be unmounted by default, but you can run

umount /dev/sda1

to go sure.

Then run

fsck -n /dev/sda1

The output looks like this:

fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159037/1254176 files, 1095299/2504124 blocks

Next we remove the journal from /dev/sda1, thus turning it into an ext2 partition:

tune2fs -O ^has_journal /dev/sda1

The output looks like this:

tune2fs 1.38 (30-Jun-2005)

Then run

e2fsck -f /dev/sda1

e2fsck 1.38 (30-Jun-2005)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda1: 164178/1254176 files (0.6% non-contiguous), 1051617/2504124 blocks

Now we resize our file system with resize2fs. resize2fs can resize ext2 file systems, but not ext3 file systems, that's why we had to turn /dev/sda1 to ext2. Currently, 4.1GB are used on /dev/sda1 (see the df -h output above), So it's safe to shrink it from 10GB to about 6GB (if you make it smaller than 4.1GB, you will lose data!). Therefore we run

resize2fs /dev/sda1 6000M

The output is as follows:

resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda1 to 1536000 (4k) blocks.
The filesystem on /dev/sda1 is now 1536000 blocks long.

Please take note of the amount of blocks (1536000) and their size (4k). We need that soon.

Now we delete our /dev/sda1 partition (don't be afraid, no data will be lost) and create a new, smaller one (but still big enough to hold our resized file system!). We can do this with fdisk:

fdisk /dev/sda

(Yes, it's /dev/sda, not /dev/sda1.)

The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Type m to get a list of all commands:

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Now we delete partition no. 1 (/dev/sda1):

Command (m for help): d
Partition number (1-5): 1

Next we create a new /dev/sda1 partition. It was a primary partition before, so we choose p again, and again it is our partition no. 1:

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
p
Partition number (1-4): 1

Now comes the crucial part - we are asked about the size of the new partition. The first cylinder is no problem, it is the one from the fdisk -l output at the beginning of this chapter (1).

First cylinder (1-1305, default 1): 1

But we don't have a value for the last cylinder of our new partition. Fortunately, we can specify the size in kilobytes (K), so we calculate the size like this:

We multiply the amount of blocks from the resize2fs output (1536000) by the size of a block (4k), and to go sure the partition is big enough, we add 3 to 5% to it (3% was enough for me, but if you want to go sure take 5%):

1536000 * 4k * 1.03 = 6328320k

So we prepend that value with a + sign and replace the small k with a capital one (K) and enter it:

Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): +6328320K

Our original /dev/sda1 had the bootable flag (see the fdisk -l output from the beginning of this chapter), so we must add it to our new /dev/sda1 again:

Command (m for help): a
Partition number (1-5): 1

Now let's write our new partition table and exit fdisk:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Now we reboot the system, and again we boot into our Knoppix system (rescue system; original system if resized partition doesn't hold system files):

shutdown -r now

Become root again (on Knoppix run

su

)

and then run this:

fsck -n /dev/sda1

The output should look like this:

fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/765536 files, 1047239/1536000 blocks

Then we create the journal on our new /dev/sda1, thus turning it into an ext3 partition again:

tune2fs -j /dev/sda1

tune2fs 1.38 (30-Jun-2005)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.

Now we are done. Shut down the system and boot into the original system:

shutdown -r now

If everything goes well, the original system will boot up, and no data has been lost. Now we can gather some details about our new partitioning and compare them with the information we collected at the beginning of this chapter:

df

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 6047868 4224140 1639408 73% /
varrun 95480 132 95348 1% /var/run
varlock 95480 0 95480 0% /var/lock
udev 10240 52 10188 1% /dev
devshm 95480 0 95480 0% /dev/shm
lrm 95480 17580 77900 19% /lib/modules/2.6.17-10-generic/volatile

df -B 4k

Filesystem 4K-blocks Used Available Use% Mounted on
/dev/sda1 1511967 1056035 409852 73% /
varrun 23870 33 23837 1% /var/run
varlock 23870 0 23870 0% /var/lock
udev 2560 13 2547 1% /dev
devshm 23870 0 23870 0% /dev/shm
lrm 23870 4395 19475 19% /lib/modules/2.6.17-10-generic/volatile

df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 5.8G 4.1G 1.6G 73% /
varrun 94M 132K 94M 1% /var/run
varlock 94M 0 94M 0% /var/lock
udev 10M 52K 10M 1% /dev
devshm 94M 0 94M 0% /dev/shm
lrm 94M 18M 77M 19% /lib/modules/2.6.17-10-generic/volatile

fdisk -l


Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 789 6337611 83 Linux
/dev/sda2 1248 1305 465885 5 Extended
/dev/sda5 1248 1305 465853+ 82 Linux swap / Solaris

fdisk -s /dev/sda1

6337611


Tuesday, October 25, 2011

Saturday, October 22, 2011

[Linux Kernel] The difference between /dev/mem and /dev/kmem

/dev/mem is the character device for physical memory and /dev/kmem is the character device for kernel virtual address space

Also check out /dev/fb* for frame-buffer memory (video).

Would it be possible to change the hostname through accessing /dev/mem?

Friday, October 21, 2011

[Cheating in C] How to know a certain function is called by which function.

When you want to know which function calls a function name A(), and there are many other functions that call A(), here is simple cheating.

First, find out the condition that you want to know who calls A() and make a hard coding there like this.

if( the condition you want ) {
int *a=0;
*a=0;
}

and run with GDB


[Linux] The difference between block and character device

The one big difference is how you handle the device. When you perform open, close, read, write, seek, etc.
For block devices, it reads and writes in the unit of a block of bytes size.
For character devices, it reads and writes in the unit of a byte.
More important thing to remember is that block devices are able to random-access such as hard-disk, but character devices are in stream such as keyboard.

[Linux Kernel] How to study Linux Kernel?

I believe there are tremendous ways for doing that but I think the good way to start is the following.

1) use youtube kernel-related presentations


In this video, Greg introduces how kernel is developing, who have contributed to kernel development, and how to involve contributing to kernel.


This video is google tech talk which looks a lot like an ad hoc discussion, very fun though.


Kernel work-through from SVLUG (Silicon Valley Linux Users Group).

2) read books to see the overall picture of Linux kernel. I don't recommend you to go deep in the first place with books, just brief and overall understanding. (Understanding Linux Kernel, Linux Device Driver of Orelly; also recommending Essential Linux Device Drivers of Prentice Hall)

3) find a target (such as making a simple driver, adding a new system call, etc) and do that. For better using the APIs of kernels, I recommend you to look up documentations folder in the Linux kernel source code. There are pretty good stuffs.

4) ALWAYS TRY TO READ OPEN SOURCE CODE

[AWK] how to print all string except one field?

If you want to print without 5-th field, you can brute forcely do

bruteforce:
  • echo "1 2 3 4 5 6 7 8 9" | awk '{print $1, $2, $3, $4, $6, $7, $8, $9}'
better way:
  • echo "1 2 3 4 5 6 7 8 9" | awk '{ for (i=1; i<=NF; i++ ) if( i != 5 ) print $i}'
simple cheating:
  • echo "1 2 3 4 5 6 7 8 9" | awk '{$5=""; print $0}'

Thursday, October 20, 2011

How to look up the latitude and longitude of a city

You can look up the latitude and longitude in the site below.
Go to the site and just enter the city name you want to find.

http://universimmedia.pagesperso-orange.fr/geo/loc.htm

try this!

[HTML] how to print < in html

You should use ASCII code

ASCII Printable Characters

ASCII Printable Characters

ASCII CharacterHTML Entity CodeDescription
&#32;space
!&#33;exclamation mark
"&#34;quotation mark
#&#35;number sign
$&#36;dollar sign
%&#37;percent sign
&&#38;ampersand
'&#39;apostrophe
(&#40;left parenthesis
)&#41;right parenthesis
*&#42;asterisk
+&#43;plus sign
,&#44;comma
-&#45;hyphen
.&#46;period
/&#47;slash
0&#48;digit 0
1&#49;digit 1
2&#50;digit 2
3&#51;digit 3
4&#52;digit 4
5&#53;digit 5
6&#54;digit 6
7&#55;digit 7
8&#56;digit 8
9&#57;digit 9
:&#58;colon
;&#59;semicolon
<&#60;less-than
=&#61;equals-to
>&#62;greater-than
?&#63;question mark
@&#64;at sign
A&#65;uppercase A
B&#66;uppercase B
C&#67;uppercase C
D&#68;uppercase D
E&#69;uppercase E
F&#70;uppercase F
G&#71;uppercase G
H&#72;uppercase H
I&#73;uppercase I
J&#74;uppercase J
K&#75;uppercase K
L&#76;uppercase L
M&#77;uppercase M
N&#78;uppercase N
O&#79;uppercase O
P&#80;uppercase P
Q&#81;uppercase Q
R&#82;uppercase R
S&#83;uppercase S
T&#84;uppercase T
U&#85;uppercase U
V&#86;uppercase V
W&#87;uppercase W
X&#88;uppercase X
Y&#89;uppercase Y
Z&#90;uppercase Z
[&#91;left square bracket
\&#92;backslash
]&#93;right square bracket
^&#94;caret
_&#95;underscore
`&#96;grave accent
a&#97;lowercase a
b&#98;lowercase b
c&#99;lowercase c
d&#100;lowercase d
e&#101;lowercase e
f&#102;lowercase f
g&#103;lowercase g
h&#104;lowercase h
i&#105;lowercase i
j&#106;lowercase j
k&#107;lowercase k
l&#108;lowercase l
m&#109;lowercase m
n&#110;lowercase n
o&#111;lowercase o
p&#112;lowercase p
q&#113;lowercase q
r&#114;lowercase r
s&#115;lowercase s
t&#116;lowercase t
u&#117;lowercase u
v&#118;lowercase v
w&#119;lowercase w
x&#120;lowercase x
y&#121;lowercase y
z&#122;lowercase z
{&#123;left curly brace
|&#124;vertical bar
}&#125;right curly brace
~&#126;tilde