Disk Management and Linux driver authoring

Source: Internet
Author: User

Disk Management

I. About the hard drive interface

Install the Linux Red Hat system and discover the hard drive device when partitioning

/DEV/SDA #sata接口设备名

/DEV/SDA1#sda对应的物理分区

/dev/sda2

/dev/sda3

Installation, the hard drive device is named

/dev/hda#IDE接口设备目录

/dev/hda1


What is the difference between SDA and HDA?

HDA is the name of the hard disk that uses the IDE interface. SDA is the name of the hard disk of the SATA interface. In the latest 2.6.19 kernel, all the hard drives are called SDA.

Gerub to fill in SDA.

The hard disk interface is divided into:

IDE (Integrated Drive Electronics) integrates the body and the controller

scsi (small computer system interface)

Fibre Channel Fibre Channel

SATA (Serial ATA (Advanced Technology Attachment))


Two. Division and Mount commands of our hard disk :

Parted-s/DEV/SDA Mklabel GPT

Parted-s/dev/sda Mkpart sashdisk ext3 0 100%

echo "Y" | /sbin/mkreiserfs/dev/sda1


three. disk drive access Preparation steps :

1. Hard disk partitioning, 2 partitions required: One physical partition and one logical partition (FDISK or parted)

2. Format the hard drive: Format the appropriate file system for the hard drive (formatted only as logical partition) (FDISK or parted)

3. Mount the hard drive device/DEV/SDA to a file directory on the Linux system/opt/nsfocus/sda1 (Mount)


four. Naming rules when a hard disk is mapped in Linux :

The hard drive device is named, the SAS interface is Sda/sdb/sdc/...,ide interface for hda/hdb/hdc/...,

The logical partition for one device is named sda1/sda2/..., or/hda1/hda2/...: multiple partitions representing SDA or HDA devices

Fdisk-l View current physical devices and logical and physical partitions

DF View the directory that is mounted under the operating system file system for a physical device that is already mounted


Linux Driver and kernel compilation

First, compile the Linux kernel

1. The Linux version of the current running system must match the Linux version of the system to be compiled

View kernel versions, as well as major and minor versions:

#uname-R

3.5.0-19-generic

To print the system architecture type, i.e. whether the machine is 32-bit or 64-bit, use:

#uname-P

i686


2. Download the same Linux source as the current system

Download various versions of Linux source https://www.kernel.org/

such as machine 3.5.0 version of Source: https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.bz2

After downloading, find a path to unzip, as I extracted to/linux-3.5/


3. Set the minor version number of the source code

Enter the Linux source directory/linux-3.5/, edit makefile, extraversion = Modify to extraversion= -19-generic, the same as the minor version number of the compiled system.

These are to configure the source version number and the system version number, if the source version number and the system version number is inconsistent, when loading the module will appear the following error: Insmod:error inserting ' Hello.ko ':-1 Invalid module format. The reason is clear: the compile-time Hello.ko Kenerl is not the Kenerl version of my PC.


4. Compiling the Linux kernel

Executes the command cp/boot/config-3.5.0-19-generic./config, overwriting the original configuration file.

Go to the Linux source directory, execute make menuconfig configuration kernel, execute make compile kernel.


Ii. Compiling Linux drivers

Drive source code as follows: HELLO.C

/*======================================================================

Asimple kernel module: "Hello World"

======================================================================*/

#include <linux/init.h>

#include <linux/module.h>

Module_license ("ZEROBOUNDARYBSD/GPL");

static int hello_init (void) {

PRINTK (kern_info "Hello World enter\n");

Return0;

}

static void Hello_exit (void) {

PRINTK (kern_info "Hello World exit\n");

}

Module_init (Hello_init);

Module_exit (Hello_exit);

Module_author ("Zeroboundary");

Module_description ("A simple HelloWorld Module");

Module_alias ("a Simplestmodule");


Makefile compiling the source code


Kern_dir =/linux-3.5

All

Make-c $ (kern_dir) m= ' pwd ' modules

Clean

Make-c $ (kern_dir) m= ' pwd ' clean

Obj-m + = hello.o #模块名

MODULE-OBJS: = hello.o [file1.o file2.o] #所有的. o File

1. The compilation kernel will get Hello.ko, copy the Hello.ko to the target system, execute

>insmod Hello.ko #需要root权限, the module will be plugged in and there will be printouts in the kernel log

>dmesg|tail #查看内核日志

>rmmod Hello.ko #可以卸载该驱动

>lsmod#查看已经插入的驱动

>modinfo file

>cat/proc/modules can display the drive module size, the address in the kernel space

>cat/proc/devices displays only the driver's main device number, and is the category display

2.mknod

Insmod after inserting the driver, see if there is a corresponding node under the/dev directory, and if you do not need to create one, then the application can reference

>mknod/dev/hello C 123 0 #创建nod


3.prink output (to terminal)

# CAT/PROC/SYS/KERNEL/PRINTK

6 4 1 7

The first 6 indicates that a message with a level above (less than) 6 will be output to the console, and the second 4 means that if the message level (macro) is not specified when PRINTK is called, the message level is 4, the third 1 indicates that the highest (minimum) level accepted is 1, and fourth 7 indicates that the original initial value of the first 6 is 7

Therefore, if you find that you do not see the output of some PRINTK in your program on the console, use echo 8 >/PROC/SYS/KERNEL/PRINTK to resolve it.


Drive storage location in 4.SAS Linux kernel drivers/scsi/


5. Hard Drive Status View

Lspci-vvv

Fdisk-l


6. How to compile the driver into the kernel

A. Find the directory that the driver will add, such as the SCSI bus driver?/drivers/scsi/

Create a new drive directory under this directory, such as Megaraid1

B. Place all source files associated with the driver in this directory, such as/drivers/scsi/megaraid1

and add makefile and Kconfig 2 files in this directory

C. Add all the. o files and module names in the makefile type, such as


obj-$ (config_megaraid1_sas) + = MEGARAID1_SAS.O

MEGARAID1_SAS-OBJS: = MEGARAID1_SAS_BASE.O megaraid1_sas_fusion.o MEGARAID1_SAS_FP.O


Very simple. Configure whether the driver needs to be compiled into the kernel's menu option under kconf


Config Megaraid1_sas

TriState "LSI Logic MegaRAID1 SAS RAID1 Module"

Depends on PCI && SCSI

Help

Module for LSI Logic ' s SAS based RAID1 controllers.

To compile the driver as a module, choose ' m ' here.

Module'll be called Megaraid1_sas

Also very simple. The TriState is a three-state option: Y is compiled into the kernel, m is manually added, n is not compiled

D. Go back to the/DRIVERS/SCSI directory and configure makefile and kconfig in this directory

You need to add the makefile file path in the MEGARAID1 in makefile

obj-$ (config_megaraid1_sas) + = megaraid1/

You need to add the Kconfig file path in the MEGARAID1 in Kconfig

SOURCE "Drivers/scsi/megaraid1/kconfig"

E.ok, go back to the./directory to make, you will be prompted to configure the module needs to be compiled, as prompted to configure,

You can also configure it manually in the file/include/config/auto.conf.



This article from "Tech record" blog, declined reprint!

Disk Management and Linux driver authoring

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.