Use serial tty as a user

Normally when we connect any boards like arduino, USB to serial converter we end up running applications with sudo. But sometimes we need to run some applications inside scripts which uses these tty’s or simply people think why always I need to run with sudo (for those who comfortable clicking on Application icons and using the App).

The solution to use these tty’s with user permissions is solved bt adding user to dialout and tty group. As mentioned below.

sudo usermod -a -G tty yourUserName

sudo usermod -a -G dialout yourUserName

Now you can run tty based applications with your username without sudo :).  Reference

 

Advertisements

Extract mp3 from youtube-dl

There is a famous command line tool called youtube-dl to download videos from youtube, and its an opensource project written in python and available for Linux, windows and OSX platforms.

In Linux, you can install the tool using:

$ sudo pip install –upgrade youtube-dl

To download a video from a link:

$ youtube-dl <Your Video link>

Example: youtube-dl https://www.youtube.com/watch?v=pRpeEdMmmQ0&list=RDpRpeEdMmmQ0

It will get donwloaded in $PWD.

To download mp3 from a link:

$ youtube-dl –extract-audio –audio-format mp3 <Your Video Link>

Example: youtube-dl –extract-audio –audio-format mp3 https://www.youtube.com/watch?v=krpm0DaeLnI

It will get downloaded in $PWD.

Do “youtube-dl -h” for more help on formats and other features.

 

Download audio from soundcloud through command line

Like youtube-dl there is a command line tool called scdl, which is written in python based on GPLV2 license and works on Linux, windows and OSX.

To install in linux, execute below command:

$ pip install scdl

To download a single song:

$scdl -l <Sound cloud link to the song>

Example: scdl -l scdl -l https://soundcloud.com/trdp/raghu-dixit-jagchanga-lokada

It will download and store in $PWD

Do “scdl -h” to know other options like download complete album, user liked videos etc.

 

 

Installing latest kernel in ubuntu

After a long time I installed latest kernel to my ubuntu 12.04 :). Long time back I use to install and test the latest kernels, so completely forgot the steps. Managed to do it by Googling, so thought of writing a wiki.

Clone latest kernel from Linus Torvalds repository:

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

$ cd linux

If you want to use the previous config, execute below command

$ make localmodconfig

Then execute make ( I am compiling with -j3 to distribute task to 3 cores )

$ make -j3

After successful compilation, needs to install the kernel and modules to the system.

Execute below command to install modules which is compiled.

$ sudo make modules_install

This will copy all the compiled modules to /lib/linux-<version>/ directory.

The next steps are copy kernel, System_map and config to /boot directory.

I am using inter x86 32 bit PC, so my bzImage is located in 

<Kernel_Base_Dir>/arch/x86/boot/bzImage. This will install the kernel.

$ sudo cp arch/x86/boot/bzImage /boot/vmlinuz-<version> 

$ sudo cp <Kernel_Base_Dir>/System.map /boot/System.map-<version>

$ sudo cp <Kernel_Base_Dir>/.config /boot/config-<version>

Above two commands installs the System.map and config to the /boot directory.

Create initramfs:

$ sudo update-initramfs -c -k <version>

Above command will create initramfs file, in my case it creates initrd.img-3.14.0-rc4-00008-g7472e00 file

Update the grub 🙂

$ sudo update-grub2

Above command will update the grub with new kernel :).

 

Thats it!!!! You installed a brand new kernel 🙂

POSIX Shared memory and Semaphore Example programs

Hi,

When I was working on one Linux based project I came across POSIX semaphores and Shared Memory. That time I developed example code to understand the basic concepts. You can find it in below mentioned my github link. When I find time I will update this post by explain the concepts in much more details.

1. Semaphore – POSIX_semaphore

2. Shared memory – POSIX_SharedMemory

3. Both Combined – POSIX_SHaredMemory_Semaphore

While compiling give -lrt for shared memory example and -lpthread for semaphore example and give both for combination

Thanks 🙂

 

Creating sysfs class attribute, how ?

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/sysdev.h>
#include <linux/major.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>

// Global variable for attribute value
static char *gvar = NULL;

// Store and Show functions……
static ssize_t attr1_store(struct class *cls, struct class_attribute *attr, const char *buf, size_t count);
static ssize_t attr1_show(struct class *cls, struct class_attribute *attr, char *buf);

// Attributes declaration: Here I have declared only one attribute attr1
static struct class_attribute pwm_class_attrs[] = {
__ATTR(attr1, S_IRUGO | S_IWUSR , attr1_show, attr1_store), //use macro for permission
__ATTR_NULL
};

//Struct class is basic struct needed to create classs attributes.”dev_jes” is the directory under /sys/class.
static struct class pwm_class =
{
.name = “dev_jes”,
.owner = THIS_MODULE,
.class_attrs = pwm_class_attrs
};

//Module initialization.
static int hello_init(void)
{
char string[] = “nothing”;
gvar = kmalloc(sizeof(char)*strlen(string), GFP_KERNEL);
class_register(&pwm_class);
snprintf(gvar, sizeof(char)*strlen(string)+1, “%s”, string);
printk(“jeshwanth In hello_init function \n”);
return 0;
}

// class attribute show function.
static ssize_t attr1_show(struct class *cls, struct class_attribute *attr, char *buf)
{

printk(“In attr1_show function\n”);
printk(“%s\n”, gvar);
return sprintf(buf, “%s”, gvar);
}

//class attribute store function
static ssize_t attr1_store(struct class *cls, struct class_attribute *attr, const char *buf, size_t count)
{
printk(“the string is : %s count %d\n”, buf, count);
return snprintf(gvar, count, “%s\n”, buf);
}

// module exit function.
static void  hello_exit(void)
{
class_unregister(&pwm_class);
printk(“In hello_exit function \n”);
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“Jeshwanth”);
MODULE_DESCRIPTION(“Learning sysfs”);

Reference:

For basic understanding of sysfs visit:
http://www.kernel.org/pub/linux/kernel/people/mochel/doc/papers/ols-2005/mochel.pdf