Archive

Archive for the ‘Linux’ Category

File Type Detection in PHP

December 24th, 2009 Ivan Villareal No comments

I’m working on a project where the users can upload files, and I need to know the type of file they are uploading, a simple solution would be to check the extensions of the files, but this has many flaws, so I was looking for a reliable way to detect the type of file, and I found a PECL extension called file info this extension is enabled by default in PHP 5.3, unfortunately I’m runing PHP 5.2.10, and the prod server also has this version.

So what I did to install this on ubuntu 9.10 karmic was the following:

 

root@mini:/etc/php5/apache2# apt-get install php5-dev libmagic-dev php-pear
....
root@mini:/etc/php5/apache2# pear channel-update pear.php.net
...
root@mini:/etc/php5/apache2# pecl install Fileinfo
....
Build process completed successfully
Installing '/usr/lib/php5/20060613+lfs/fileinfo.so'
install ok: channel://pear.php.net/Fileinfo-1.0.4

 

and finally I added the extension to php.ini

extension=fileinfo.so

I restarted the apache server, and tested with this

          <!--DVFMTSC-->$fileInfo = new finfo(FILEINFO_MIME); <!--DVFMTSC--> 
$mimeType = $fileInfo->buffer(file_get_contents($uploadedFile));

 

Categories: Linux, PHP Tags: , , ,

Mount a remote filesystem using sshfs

December 9th, 2009 Ivan Villareal No comments

I often have to upload files to several servers, and some of them doesn’t have an rsync or ftp server, so if I’m going to make a deployment, I have to use scp, or sftp to upload the files.

This works well when I need to make a quick change or test 1 file, but If I have to synchronize several files it is a very time consuming task.

So what I use in this cases is sshfs it is a pretty cool tool, to allow me to mount any filesystem so I can use any rsync on a remote server without dealing with opening ports configure, the server or do other stuff,

Gentoo is my main OS, so it was really easy to have this working, I had to reconfigure the kernel to support fuse

Symbol: FUSE_FS [=m]
Location:
  -> File systems
      -> Filesystem in Userspace support

and after this was ready I only installed sys-fs/sshfs-fuse

[I] sys-fs/sshfs-fuse
     Available versions:  1.9 ~2.1 2.2
     Installed versions:  2.2(12:33:09 PM 09/08/2009)
     Homepage:            http://fuse.sourceforge.net/sshfs.html
     Description:         Fuse-filesystem utilizing the sftp service.

when it was ready I did the following to mount the remote filesystem:

ivan@gondor ~ $ sshfs phpfix@prestant.citizenhawk.net: preasent/

After this I’m able to use the remote file system as usual.

Categories: Linux Tags: , , , ,

Mysql5 and PHP5 on centos4

October 12th, 2009 Ivan Villareal No comments

I have the need to install php5 and mysql 5 on a centos 4 server, so I found that the Centos Plus repository had this, and this is what I did to get these two working.

First check what is the version of the distro:

cat /etc/*release*

Then make an update:

yum update

After 496 package updates I’ve installed php5

yum --enablerepo=centosplus install php

This were the installed packages:

php-5.1.6-3.el4s1.10
php-cli-5.1.6-3.el4s1.10
php-common-5.1.6-3.el4s1.10

So I’ve created a info.php file, and restarted the apache server:

echo "<?php phpinfo(); ?>" > /var/www/html/info.php && service httpd restart

It was working correctly, so the next thing was to have mysql 5 in it, so I did:

yum --enablerepo=centosplus install mysql-server

I got some problems here, first I got this:

Transaction Check Error: file /etc/my.cnf from install of mysql-libs-5.0.68-1.el4_6 conflicts with file from package mysql-4.1.22-2.el4
file /usr/share/mysql/charsets/Index.xml from install of mysql-libs-5.0.68-1.el4_6 conflicts with file from package mysql-4.1.22-2.el4
file /usr/share/mysql/charsets/README from install of mysql-libs-5.0.68-1.el4_6 conflicts with file from package mysql-4.1.22-2.el4
file /usr/share/mysql/charsets/armscii8.xml from install of mysql-libs-5.0.68-1.el4_6 conflicts with file from package mysql-4.1.22-2.el4
file /usr/share/mysql/charsets/ascii.xml from install of mysql-libs-5.0.68-1.el4_6 conflicts with file from package mysql-4.1.22-2.el4

So what I found was that yum was trying to install mysql.i386 but the architechture is x86_64, to fix this I had to specify the arch

yum --enablerepo=centosplus install mysql.x86_64 mysql-server

After this I was able to install it, but when I tried to start the server I received some errors about error messages not right, I’ve checked the installed packages and it was installed mysqlclient10, so I removed the error messages problem disappeared, however I still couldn’t startup the server, checking at the logs I found this:

InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
091012 11:01:18  InnoDB: Started; log sequence number 0 0                       091012 11:01:18 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist                                                     091012 11:01:18  mysqld ended                                               
\

To fix this I cleared the data dir, that had som files, this was in ‘/var/lib/mysql’ and after that I ran mysql_install_db:

[root@prestant /]# cd /var/lib/mysql 
[root@prestant mysql]# rm -rf *
[root@prestant mysql]# mysql_install_db --user=mysql -ldata=/var/lib/mysql
[root@prestant mysql]# service mysqld start

After the server was up, I changed the root password and added a new user:

[root@prestant mysql]# mysqladmin -u root password NEWPASSWORD
[root@prestant mysql]# mysql -pNEWPASSWORD
 

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.82sp1 Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> CREATE USER 'ivan'@'%' IDENTIFIED BY 'passhere';
Query OK, 0 rows affected (0.00 sec)
 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'ivan'@'%' IDENTIFIED BY 'passhere' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
Query OK, 0 rows affected (0.00 sec)
 
mysql> CREATE USER 'ivan'@'localhost' IDENTIFIED BY 'passhere';
Query OK, 0 rows affected (0.00 sec)
 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'ivan'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit
Bye
 
 

Removing Hybrid feature of an Acomdata drive.

October 1st, 2009 Ivan Villareal No comments


500Gb Acomdata Drive

About 2 years ago I purchased this 500Gb Acomdata drive that was on sale at Fry’s, it was a good deal back then, cost me about 100 bucks, it had an aluminium case and a power off switch, and I was in the need to expand the storage capacity of my multimedia server, which is linux based.

I didn’t researched this product, I didn’t even knew that acomdata was a brand, anyway I remember seeing the “Hybrid Drive” statement, but didn’t care, I was going to wipe this drive an reformat it as ext3, so I was aware that all the “software features” it had were useless to me, like the PushButton™ Backup and the Nomad Mobile Desktop.

After I arrived home, I plugged this thing on my server, which had ivman and a custom script to mount anything, but after few seconds nothing happened, I look at the kernel messages and I started to worry, It showed as a CD drive instead of a Mass Storage Device, I tried unsuccessfully to mount this manually, so I thought this drive must be bad.

The next day I connected this drive to a Windows XP, and after some drivers install, it was working, I noticed that it created a virtual CD drive on Windows it had some software on it, so I thought that this was some kind of partition that would be erased after I formatted the drive, so I did this, but the partition was still there.

I did some research on the internet but didn’t found anything use full, so I was again trying to make this stupid drive worked on linux, I hate when I have to fight with the stuff I buy just to make it work on linux, I was very annoyed by this, finally after looking the kernel messages I’ve found that the drive was acting as a SCSI device, so I activated “Probe all LUNs on each SCSI device” in the kernel and after a kernel restart both partitions were available on Linux, however this still had a problem, because I was using ivman to detect the drive and mounted, ivman  didn’t worked for hard drive partition, just for the virtual CD, I’ve tried unsuccessfully to delete that stupid CD partition on windows and Linux.

I had enough of this and I was going to take this drive back to the store, but in a desperation moment I took my screwdriver and started to tearing apart the drive, I wasn’t even stopped by the warranty void seal, my intention was to take the drive format it and put it back without the cd part, I did this, and the format was successful, the drive was totally empty, but when I put this back to the enclosure the stupid CD partition was there, empty this time, but it was there, so it was a firmware issue and I didn’t found a firmware upgrade to remove this stupid feature.

I did some scripts to detect this unmounted the CD part and mount the hard drive partition, it took me about a week to have this thing working the way I wanted,  this was  unbelievable, why they don’t put the software to remove this “hybryd feature”.

However this drive has been working fine, with the virtual CD partition still there until Yesterday, I was in the need to transfer some large files from work to home, and this was the drive more reachable from all the drives I have, so I bring it her to the office, and as soon as I plugged in the CD partition appeared on my Desktop, so I thought it has been a while now, lets see if I can finally reflash the firmware to remove this so called feature, and I’ve found a forum with several users trying to remove this, and suddenly I’ve found this  post:

You need this software from http://www.mediafire.com/?bgwuwq5xzbm

unzip and click on MP251MFG, click on “configure hdd” and once it says pass just unplug and plug in the device. CD PART gone. 

I quickly downloaded the file in question, it was a zip file called “REMOVE CD PART.zip” with about 1.5Mb in size, it has  a Readme.pdf that describes the MP251MFG program, I booted up in Windows, installed the fsdriver because this drive was an ext3, plugged the drive and run the MP251MFG.exe application this is what I saw:

MP251MFG.exe Interface

I didn’t backed up the drive, I was just exploring the options and I clicked the Configure HDD, a process was started and finished successfully, I was affraid that my data were gone, so I headed to check if the data was there, and it was, I copied the important stuff somewhere else, unplugged the drive and plugged back in, and voilà, the CD partition was gone along with the data in the drive, but that din’t matter the CD partition was really gone :D

The drive was reformatted to FAT32, so I reformatted back to ext3, and now it is working pretty well, 2 years later.

I’ve found that this program works for other drives too, here are some links where this program can be found

For Windows:

For Macintosh:

Setting up ssh keys for passwordless login

September 25th, 2009 Ivan Villareal 1 comment

Most part of the time I’ve spent on my development machine I have several terminals opened, but thanks to screen I only have one per server.

However I’m constantly in the need to login to some servers just to make a quick check of something, so I have to type in my credentials every time I log in, this really annoys me, because when I’m focused on something I don’t want to loose that focus waiting for the login prompt.

So what I did  to fix this annoyance, was to use Key Authentication  instead of Password Authentication, I’ve been using this for several years now under several distros without a problem.

So in short this is what I do, whenever I want to use Key Auth.

gondor .ssh # cd ~/.ssh
gondor .ssh ~ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
53:df:bb:74:24:2d:7f:78:1f:85:66:f0:19:b6:9f:71 root@gondor
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|          . . o  |
|         . . = * |
|        S   . @.E|
|         .   o O=|
|              +o*|
|             . ++|
|              . .|
+-----------------+

I haven’t set a password for the key,  because if I set a password I would have to type the password for the key each time, or setup a program to remember the key password like Gnome Keyring or Putty Pageant in Windows.

After my private and public keys are generated I copy the public key to the server where I want to access without password

gondor .ssh # ssh-copy-id -i id_rsa.pub ivan@odin
The authenticity of host 'odin (192.168.1.201)' can't be established.
RSA key fingerprint is bb:36:b4:0b:05:13:ce:a2:2e:95:97:59:65:f3:f8:a8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'odin,192.168.1.201' (RSA) to the list of known hosts.
Password:
Now try logging into the machine, with "ssh 'ivan@odin'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

gondor .ssh # ssh ivan@odin
Last login: Fri Sep 25 15:50:03 PDT 2009 from gondor.artedigital on ssh
Last login: Fri Sep 25 15:50:07 2009 from gondor.artedigital
ivan@odin ~ $

Categories: Linux Tags: , , , ,