Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Monday, 10 December 2018

Filter failed error for Kyocera printers in Linux is caused by a BUG in the device driver

I recently got a new Kyocera FS-1025MFP unit and the print jobs would stop if there are Chinese characters in the job title. The OS is Ubuntu 16.04. Specifically, the error message was either "filter failed" or "filter crashed". Upon checking the CUPS error messages, I discovered that this was due to an error signal 6 returned by the "rastertokpsl" program shipped with the Kyocera driver.

To correct this problem:

1. Download, and install the Kyocera driver by running install.sh as root. This will copy the printer's PPD file to /usr/share/cups/model/Kyocera/ and the rastertokpsl program to /usr/lib/cups/filter/.

2. Create a wrapper for rastertokpsl by:
sudo nano /usr/lib/cups/filter/rastertokpsl-fixed
and then insert the following text:
#!/bin/bash
jobname=$(echo $3 | egrep -o '[[:alnum:]]' | tr -d '\n' | tail -c 20)
path=/usr/lib/cups/filter
$path/rastertokpsl "$1" "$2" "$jobname" "$4" "$5"
3. Make sure that the wrapper has execution permission:
sudo chmod a+x /usr/lib/cups/filter/rastertokpsl-fixed
4. Edit the PPD file specific to your printer model as found in /usr/share/cups/model/Kyocera/. For instance, mine is an FS-1025MFP and the file name is Kyocera_FS-1025MFPGDI.ppd. The only modification that should be made is the cupsFilter line (line 55 in my case):
Find:
*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/rastertokpsl"
and replace the line with:
*cupsFilter: "application/vnd.cups-raster 0 /usr/lib/cups/filter/rastertokpsl-fixed"
The above steps should fix the problem. Alternatively, you may edit the PPD file shipped with the installation package and create the rastertokpsl-fixed wrapper for the installation files, and add the wrapper into install.sh so that you can be able to install the corrected driver with a single command.

I think the problem also affects FS-1040, FS-1060DN, FS-1020MFP, FS-1120MFP and FS-1125MFP, since they are using the same rastertokpsl program.

Wednesday, 10 January 2018

No module named wheel error while importing wheel from setuptools, in Tensorflow compilation

While compiling TensorFlow from source, you might want to build a .whl file with the build_pip_package script generated by bazel. If you see an error message like this:
File "/home/twang/anaconda2/envs/tensorflow/lib/python2.7/site-packages/setuptools/package_index.py", line 31, in <module>
     from setuptools.wheel import Wheel
ImportError: No module named wheel
It is most likely your setuptools is not current. In particular, setuptools installed by conda by default has version 36.5, which does not support wheel. To upgrade setuptools to 38.4 which supports wheel, run:
conda install -c conda-forge setuptools

Monday, 27 November 2017

Setting up port forwarding on Ubuntu 16.04

Here we demonstrate how to set up port forwarding on a VPS with Ubuntu 16.04, so that we can use this VPS as an Internet traffic forwarding service. This setup is useful when the route between the source and the destination IPs is bad, but the intermediate VPS has good connections to both the source and the destination.

First, set the net.ipv4.ip_forward=1 flag in the /etc/sysctl.conf file with vi, and use the following command to make it effective immediately:
sysctl -p
Next, say we want to use port 50000 to forward both TCP and UDP traffic to 168.10.0.1:40000. We use the following iptables commands:
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 50000 -j DNAT --to-destination 168.10.0.1:40000
iptables -t nat -A PREROUTING -p udp -m udp --dport 50000 -j DNAT --to-destination 168.10.0.1:40000
iptables -t nat -A POSTROUTING -d 168.10.0.1/32 -p tcp -m tcp --dport 40000 -j SNAT --to-source 172.10.0.1
iptables -t nat -A POSTROUTING -d 168.10.0.1/32 -p udp -m udp --dport 40000 -j SNAT --to-source 172.10.0.1
In the command above, 172.10.0.1 is the private IP of the intermediate VPS. We can use the following command to check if we set up the NAT table correctly:
iptables -L -n -t nat
Finally, to make the iptables settings persistent, install:
apt-get install iptables-persistent
To save any additional changes to the NAT table, run:
netfilter-persistent save

Saturday, 15 April 2017

Split contents of a file according to a list

The following script runs through a list in a first file, and check through a second file for lines that start with items in the list. It outputs the matched lines in the second file to a third file.
#!/bin/bash

while read p; do
  grep "^$p" $2 >> $3
done <$1
The script is useful if you have a dataset split file and wanted to extract relevant lines from another file (e.g., a detector's bounding box outputs) according to this split. For example, you may want to run
./split_result.sh train.txt output_trainval.txt output_train.txt
./split_result.sh val.txt output_trainval.txt output_val.txt
Note the script makes no assumption about the ordering of lines in the second file.

Thursday, 18 February 2016

Batch extract tarballs into directories with their respective base names (for ILSVRC 2012 training images)

I encountered this problem when I downloaded the ILSVRC 2012 training images. Thanks to this code, we now have a quick solution.

1. Paste the following code to file tarsubdir.sh:
#!/bin/sh
#usage:
# nameOfScript myArchive.tar.gz
# nameOfScript myArchive.gz
# nameOfScript myArchive.tar
#
# Result:
# myArchive   //folder
fileName="${1%.*}" #extracted filename
echo "file name is: $fileName"
trailingExtension="${fileName##*.}"
if [ "$trailingExtension" == "tar" ]
then
    fileName="${fileName%.*}"
    echo "new file name is: $fileName"
fi
echo "now file name is: $fileName"
mkdir "$fileName"
tar -xf "$1" --strip-components=0 -C "$fileName"
2. Paste the following code to another file untarall.sh:
#!/bin/sh

for file in *.tar
do
  bash ./tarsubdir.sh "$file"
done
3. Run from the bash prompt:
bash ./untarall.sh

Wednesday, 16 October 2013

Installing Linux Mint 13 Maya from hard drive in Windows 7

Here are some simple steps you need to follow if you want to install Linux Mint from your hard drive, without using USB sticks or blank DVDs. We assume you have Windows 7 installed.

1. Download Linux Mint 13 ISO from the official website. Put the ISO file at C:\.

2. Search for and download Grub4dos. Install Grub4dos by copying grldr to C:\. Create a new file C:\menu.lst and put the following contents in:
timeout 10
default 0

title Windows 7
find --set-root --ignore-floppies --ignore-cd /bootmgr
chainloader /bootmgr
3. Search for and download Bootice. Run Bootice and select your hard drive, then click on Process MBR. Choose Grub for DOS and click on Install/Config. This replaces your MBR with the one from Grub4dos.

4. Extract vmlinuz and initrd.lz files from your Linux Mint ISO and put them at C:\. They should be in a folder named casper. It is important that these files should be copied to your hard drive.

5. Restart your machine. You should be able to see the GRUB menu. From that press c for command line. Enter the following commands (for the first command you can press Tab key while entering to see list of files):
find /linuxmint-13-mate-dvd-64bit.iso
kernel (hd0,1)/vmlinuz boot=casper iso-scan/filename=/linuxmint-13-mate-dvd-64bit.iso
initrd (hd0,1)/initrd.lz
boot
When you enter the first command you should see the partition with your Linux Mint ISO file. For all the rest commands you need to change the (hd0,1) part accordingly. Also, you can also use --set-root option to set your root to that partition but that is not necessary.

6. Once Linux Mint Live is booted, go to the terminal and run
sudo umount -l /isodevice
This will avoid errors when you attempt to modify partitions. You can then click on the shortcut on the desktop to install Linux Mint to your hard drive.

Sunday, 13 October 2013

Infinite error loop when using MATLAB with nohup

If you run MATLAB scripts remotely with ssh and nohup (to keep it running in the background), you may find MATLAB entering an infinite loop of the following errors when your script or function finishes:

Warning: Error reading character from command line

and

Bad file descriptor

This has nothing to do with your code, just that you will have to add "</dev/null" before the "&" symbol which is causing the error. For example, you can run a function like this:
nohup matlab -nodisplay -r "myfunc_in_current_dir('params')" > ./mylog.txt </dev/null &
Also, people suggested using the "-nojvm" option. If you are plotting in your script then this option should be avoided.

Friday, 4 October 2013

Password-less ssh logins with RSA key pairs

First, edit your local ~/.bashrc file and add the following lines:
function server1(){
  ssh user@server.com
}
This creates a command named server1 to automatically ssh log in to server.com. However you will still need to enter your password each time you run this command.

Then, run the following commands from the bash shell on your local machine:
ssh-keygen -t rsa
ssh user@server.com "mkdir .ssh"
scp ~/.ssh/id_rsa.pub user@server.com:.ssh/id_rsa.pub
For the first command, press enter three times to create public and private keys without a password. The second command creates a directory named ".ssh" in your remote home directory. The third one copies over the public key.

Finally, run the following commands on the remote machine (by logging on using ssh):
touch ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
If the file ~/.ssh/authorized_keys already exists you can skip the first command. The second command appends id_rsa.pub into authorized_keys.

That's it. Try to type server1 again from your local shell prompt and you'll not be prompted to enter password from now on.

Change MAC address for your Ethernet adapter in Linux

Run the following commands from bash shell (yes you may need to cd to /etc first):
cd /etc
sudo vim rc.local
Add the following lines before "exit 0" (change the MAC address as needed):
/sbin/ifconfig eth2 down
/sbin/ifconfig eth2 hw ether 00:00:00:00:00:00
/sbin/ifconfig eth2 up
Restart your system and it should be OK.

If you want to temporarily change your MAC address, simply run the 3 lines of commands at bash shell prompt.

MATLAB error message during startup on Linux

If you install your MATLAB at /usr/local/, you may not be able to start MATLAB (getting a long error message saying that MATLAB is exiting with a fatal error and no GUI shows up).

Alternatively MATLAB may start with the error message "The desktop configuration was not saved successfully".

In these cases you could try to change the owner of your ~/.matlab folder (change twang to your Linux username):
sudo chown -R twang ~/.matlab
This should fix the problem.

Thursday, 3 October 2013

Redirecting all traffic to add www prefix with Apache

It may be useful to add the "www." prefix automatically for all traffic if your visitors are not typing them. With Apache you need to have mod_rewrite enabled to use this method.

1. Allow mod_rewrite override for your current website. Go to your site configuration file (located at /etc/apache2/sites-enabled/000-default if you are using Ubuntu) and add the following lines into the <VirtualHost *:80> section:
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Order allow,deny
    allow from all
</Directory>
2. Go to your document root (e.g., /var/www), create a new file named .htaccess and with the following content:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
That's it. Restart Apache and it should work by now.

Upgrading/compiling OpenCV on Linux from source code and use with Eclipse

In order to upgrade your OpenCV to the latest version (versions on APT are usually obsolete), or if you modified the source code in OpenCV and need to recompile it, you can follow the following steps as an example:

1. Download the latest source tarball from OpenCV’s website.

2. Extract the .tar.gz file to a directory, say ~/opencv_src. This is just a temporary directory for the source code.

3. Create another directory for build files, say ~/opencv_build. Change to this directory (do not stay in the source folder). Make the build files using CMAKE (you may change the options where necessary):
cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=YES -D TBB_INCLUDE_DIRS=/usr/include/tbb -D CMAKE_INSTALL_PREFIX=/usr -D BUILD_PYTHON_SUPPORT=ON -D WITH_OPENNI=YES ../opencv_src
Note: You could refer to CMakeLists.txt in ~/opencv_src for more options (for example, to install OpenNI support you need to set WITH_OPENNI to YES). Another important thing is to check OpenCV website for required APT packages. Install those packages using apt-get. Otherwise you will get some error information when trying to run the above command.

4. Should the command is executed successfully, you will have the build files. Note that we have set CMAKE_INSTALL_PREFIX to /usr. This is by default /usr/local but if we need to replace the old versions that comes with APT, we need to set the prefix to /usr. This is where apt-get installs programs. For /usr/local, it is usually for self-maintained packages. So actually it is up to you to use /usr or /usr/local.

5. Type “make” and “sudo make install” to install the files to the desired location. If we set prefix to /usr then files will be copied to locations like /usr/lib, /usr/include/opencv. The make command will take a little while.

You should have installed OpenCV now. Following steps are for using OpenCV with Eclipse CDT.

6. Open Eclipse (with CDT installed), create a new C++ project. And then click on Menu -> Project -> Properties -> C/C++ Build -> Settings -> Tool Settings. We need to change:

(1) GCC C++ Compiler -> Includes. Add “/usr/include/opencv”

(2) GCC C++ Linker -> Library search path. Add “/usr/lib”

(3) GCC C++ Linker -> Libraries. Add “opencv_core” and “opencv_highgui” (for a complete list of libraries available, check the libopencv_***.so files located at /usr/lib. When adding their names, simply remove "lib" at front and any extensions. For example, use "opencv_core" for "libopencv_core.so").

7. To give it a go, use the following sample program:
#include <highgui.h>

int main( int argc, char** argv )
{

 IplImage* img = cvLoadImage( "/path/to/an/image.jpg" ); // change this to a valid filename
 cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
 cvShowImage( "Example1", img );
 cvWaitKey(0);
 cvReleaseImage( &img );
 cvDestroyWindow( "Example1" );

}

C++ linking error with Linux 3.x

If you are linking your C/C++ program on Linux 3.x (e.g., Ubuntu 12.04), you may need to put the libraries at the end of the shell command, i.e.,
$(CC) -o $@ $+ $(OPT) $(LIBDIRS) $(LIBS)
(This is like if you use Makefile, same rule applies if you don't use Makefile)

For my example with OpenCV, here
INCLUDES = -I/usr/include/opencv
LIBS = -lcxcore -lcv -lcvaux -lhighgui -lml
LIBDIRS = -L/usr/lib
OPT = -O3 -Wno-deprecated
CC=g++
If you put $(LIBDIRS) and $(LIBS) earlier in this command, it would successfully link with earlier distros (I suppose those with Linux kernel version 2.x, such as Ubuntu 10.04), but not on the newer distros. As a result, you would get the following error message:
*****.cpp:(.text+0x2db3): undefined reference to `cvLoadImage'
*****.cpp:(.text+0x2ef7): undefined reference to `cvCreateImage'
*****.cpp:(.text+0x2feb): undefined reference to `cvCreateImage'
*****.cpp:(.text+0x3041): undefined reference to `cvConvertScale'
*****.cpp:(.text+0x3088): undefined reference to `cvSaveImage'
*****.cpp:(.text+0x3099): undefined reference to `cvReleaseImage'
*****.cpp:(.text+0x30ca): undefined reference to `cvReleaseImage'
*****.cpp:(.text+0x30f9): undefined reference to `cvReleaseImage'
That is, every function call in the libraries will end up with "undefined reference". Pretty tricky! I would appreciate if anyone points to me the stuff under the bonnet here.

Probing system memory info in MATLAB with Linux

I came across a MATLAB library lately which checks for available memory before proceeding, so that the algorithm knows how much memory it should use (e.g., use up to 10% of available memory).

On Windows the function memory() comes in handy for this need, while in Linux we will have to write a few lines of code.
function [ totalmem, freemem ] = linuxMemory()
% Usage: No input arguments needed.
% Returns total and free memory in Kilobytes.

    % probe system memory information
    [~,meminfo] = system('cat /proc/meminfo');

    % get total memory
    tokens = regexpi(meminfo,'^MemTotal:\s*(\d+)\s', 'tokens');
    totalmem = str2double(tokens{1}{1});

    % get available memory
    tokens = regexpi(meminfo,'^*MemFree:\s*(\d+)\s','tokens');
    freemem = str2double(tokens{1}{1});

end