Nino\’s Reminders

April 23, 2007

Fortune cookies

Filed under: Miscellaneous — nino @ 12:14

Cookies are kept in a plain text file. You have to do your own text wrap, ie place newlines yourself. Quotes should be divided by an otherwise empty line beginning with a percentage sign (%). After that, invoke strfile textfile textfile.dat (part of fortune package) to create a hash file needed for fortune program. To print fortune cookie every time a user logs in, put a file named zlogin (assuming you use zsh) in /etc with the following line: /usr/games/fortune [fortune database] && echo (echo for better formatting)

September 5, 2006

Set xterm and screen titles

Filed under: Miscellaneous — nino @ 10:34

Ever started a couple of xterms or screens and wondered what you were doing in it? You know, like, you start editing httpd.conf file in one, reading man page of httpd in another, ftp to a server in a third window and so on? I get lost. That’s why you can set the title of xterm window and see in title bar where you are, as well as choose a window from a screen window list.

The following applies to zsh.

In .zshrc file implement following two functions:


local PWD=$(print -P "%~")
precmd () {
echo -n "\e]0;[ $USER@$HOST ] ( $PWD )\a"
}
preexec () {
echo -n "\ek[$USER@$HOST]($PWD) - $1\e\\"
}

This way, you get a nice [myname@mymachinename](mycurrentdir) xterm title. Screen title in window list has a little extra – last executed command in addition to the above (like “vi .zshrc”).

There are other ways and syntaxes to do this; this works for me.

Note: you might need the following line in your .screenrc to make the above work.

termcapinfo xterm 'hs:ts=\E]0;:fs=07:ds=\E]0;screen07'

June 19, 2006

Recover accidentally deleted files

Filed under: FreeBSD — nino @ 16:28

The above is not impossible, but it is very difficult. First of all, the following procedure works only for textual files, but it can still be useful – for example, to recover sources you were programming for the last three months and accidentally deleted. So, how do you go from here?

First, stop writing anything to the partition where deleted files were located!

Second, make a disk dump:
dd if=/dev/[disk and partition label] of=deleted-file bs=1m

Disk and partition label can be found by issuing df. For example, my /usr partition was mounted on /dev/ad0s1d. Don’t forget that output file (of parameter) should reside on another partition. The output file will be equal in size to the size of the partition being dumped. If you don’t have enough space on another partitions of the same disk, insert another disk or mount some resource over the network.

Now you have a raw data file. You can relax a bit now – your data, provided it was still undamaged at the moment you made the image, is safe now, meaning it can’t get overwritten anymore.

Next step is to try and extract the data you need. For that, you need one important thing – a unique string. You have to remember at least some specific word or phrase you used in the lost file(s) in order to recover them in a feasible amount of time. When you have that unique string, you can do the following:

grep -a -B50 -A50 "unique-string" deleted-file

What you are doing now is grep’ing your partition for the deleted file. -a tells grep to treat the input file as text even though it’s binary. -B and -A tell grep to display (in this case) 50 lines of context before and after the found unique string.

That’s the principle, but you won’t do it that way in reality. So, the complete solution:

for (( i = 0 ; i < [nnn] ; i++ ))
do
echo $i > progress && dd if=deleted-file bs=1m skip=${i}00 count=100 2> /dev/null | grep -a -B50 -A50 "unique-string" >> results.output
done

[nnn] should be the size of partition dump file in megabytes divided by 100.

The above loops for [nnn] times, echoes current iteration in a filename called progress, takes a 100MB chunk of data (block size of 1m times count of blocks), skips blocks already read, redirects standard error to null, grep’s the incoming data for unique string and outputs all matches to the results file. Grep has to be fed the data in small chunks (certainly less than 1GB) instead of entire partition image because it otherwise exits with a cryptic “out of memory” error.

May 23, 2006

Mount ISO image

Filed under: FreeBSD — nino @ 10:28

ISO image can be mounted by using:

mdconfig -a -t vnode -f image.iso -u 0

mdconfig configures and enables memory disks. -a attaches the disk, -t defines the type (in this case, vnode which means you can use -f option to specify a file as a backstore for this disk), and -u requests a specific unit number for device (md0 in this case).

mount -t cd9660 /dev/md0 /mnt

This part is pretty self-explanatory, mount it as any other regular device. You can unmount the image with
umount /mnt
and
mdconfig -d -u 0
-d detaches the memory disk, device unit 0 (-u 0).

The same can be done on Linux by using:
mount -o loop -t iso9660 image.iso /mnt

April 14, 2006

Resume screen session

Filed under: Miscellaneous — nino @ 9:45

If you are using screen, you can detach your session by typing

Ctrl+A D (detach).

All your screens remain active and can later be resumed. You do this by typing

screen -r.

If there is only one session to be resumed, it will be resumed automatically; otherwise you need to specify which screen session you want to resume by typing

screen -r [pid].[tty].[host].

There is one more catch: sometimes you get disconnected. It is possible that your session will stay attached. If a session is attached, you can’t attach to it unless you first detach it. But it’s not difficult: just type

screen -d -r [pid].[tty].[host].

-d is for detach, -r is for resume.

April 7, 2006

Working with OpenSSL and certificates

Filed under: FreeBSD — nino @ 14:06

Once you have the configuration file, openssl.cnf, setup properly (in a future article), you can start working with certificates:

openssl req -new -keyout a-key.pem -out a-req.pem -days 365 -config ./openssl.cnf

This will create a certificate signing request along with the private key. Alternatively, you can create a private key with openssl genrsa -out [filename] 1024 and use it as input to openssl req -key [filename] to create a request that corresponds to previously created private key.

openssl ca -config ./openssl.cnf -out a-cert.pem -in a-req.pem

This will sign the specified certificate signing request and output the resulting certificate in the specified output file. Created certificate can be reviewed by issuing the following command:

openssl x509 -text -noout -in a-cert.pem

openssl pkcs12 -export -out a-cert.p12 -in a-cert.pem -inkey a-key.pem

This will export both the public and the private part of the certificate into a single, pkcs12 formatted file that is ready to be imported into the browser.

April 6, 2006

Enable mouse wheel in XWindows

Filed under: Miscellaneous — nino @ 20:28

How do I make my middle mouse button work in XWindows?

You need to edit your xorg.conf configuration file and add something to the section where mouse is defined. It is usually located in /etc/X11/. Needed changes are:

Section "InputDevice"
  Identifier "Mouse1"
  Driver "mouse"
  Option "Protocol" "auto"
  Option "Device" "/dev/sysmouse"
  Option "Buttons" "5"
  Option "ZAxisMapping" "4 5"

EndSection

The bold parts are the added parts; non-bold parts will most likely already be there.

Find syntax example

Filed under: FreeBSD — nino @ 17:52

How do I find files in a directory and subdirectories and execute a command that takes found file(s) as parameter?

Easiest way is to run find command and then pipe the output to the xargs command:
find [start_dir] -name [file_mask] | xargs -I [repl_symbol] [command]

  • find [start_dir] – start from specified directory (find scans subdirectories as well). You will most often put a dot (meaning current directory) here.
  • -name [file_mask]… search files by name (if you use wildcards, be careful to escape them with a backslash – we want to have find command expand the wildcard, not the shell that is running find)
  • xargs – constructs the arguments list and executes the command
  • -I [repl_symbol]… use something as a replacement placeholder. Using percentage sign (%) seems to work ok.
  • [command]… specify a command you want to run on found files. E.g. cp % destdir will copy found file (if % is used as replacement symbol) to the destination directory.

Example
I used this little trick to find all .ko files located in various subdirectories of the current directory and copy them to a specific directory:
find . -name \*.ko | xargs -I % cp % modules/

Create package from locally installed package

Filed under: FreeBSD — nino @ 14:10

How do I create a ready-to-install package from a machine that already has that package installed?

Change the directory to the directory where you want to place created packages. Best way to create a package from locally installed package is:
pkg_create -v -x -b [pkg_name] -R

  • -v… be verbose and write what you are doing
  • -x… use basic regular expressions to determine the package name
  • -b… means create a package from locally installed package
  • -R… create all packages that are required by the specified package

You will most likely use this function to compile and install a port on a fast machine, then create a binary package and install it on a slower machine where compilation would take hours.
Remember to change the current directory before running the command to the directory where you want the resulting packages to be placed. This command always produces the packages in the current directory.

Example:
I used this command to produce a binary package of dvd+rw-tools built from ports on a fast machine. It produced two packages in the current directory, cdrtools-2.01_4.tgz and dvd+rw-tools-6.0_2.tgz.
pkg_create -v -x -b dvd+rw-tools -R

Blog at WordPress.com.

  • Design a site like this with WordPress.com
    Get started