Thursday, 15 July 2010

Filesystems, files


  • ext2 - filesystem without journaling.

  • ext3 - pre-allocates number of inodes at creation time.

  • jfs - journaled file system from IBM

  • xfs

  • reiserfs - is a good choice for handling filesystems with many small files.

  • FAT(12,16,32) - msdos name is used for variant of this filesystem, that supports only 8.3 filenames. vfat name designates version, that supports long names.

  • NTFS

  • HFS, HFS+ - Hierarchical File System from Apple

  • ISO-9660 - The standard filesystem for CD-ROMs, Rock ridge extensions are available

  • Joliet - filesystem for CD-ROM's was created by Microsoft for use by Windows

  • UDF - The Universal Disc Format (UDF) is the next-generation filesystem for optical discs



Determine superblock location:
dumpe2fs /dev/sda1|grep -i superblock

Repair filesystem, using alternative superblock:

e2fsck -f -b 8193 /dev/sda1

XFS tools:
xfs_info - provides info about xfs partition, requires that the filesystem be mounted
xfs_metadump - copies the filesystem’s metadata (filenames, file sizes, and so on) to a file.
xfs_admin - tuning xfs partition

To tune ext2/ext3 filesystems you can use tune2fs utility

debugfs is a swiss army knife for ext2/ext3 file systems,This program provides the abilities of dumpe2fs, tune2fs, and many of Linux’s normal file-manipulation tools all rolled into one. It is possible to undelete files, extract files from damaged systems, get information and much more using this programm.

Get information about filesystem:

dumpe2fs -h /dev/sda1
or

tune2fs -l /dev/sda1

Search for bad blocks:

e2fsck -c
or

mkswap -c



There are several methods of locating files in linux:

  • find - search files using your patterns(size, owner, name, mtime, atime)

  • locate - search only names, it returns all names, containing the specified string. It works from a database that it maintains. Most distributions include a cron job that calls locate with options that cause it to update its database periodically, such as once a night or once a week. (You can also use the updatedb command to do this task at any time.) For this reason, locate may not find recent files, or it may return the names of files that no longer exist. If the database-update utilities omit certain directories, files in them won’t be returned by a locate query.

  • whereis - searches for files in a restricted set of locations, such as standard binary file directories, library directories, and man page directories.

  • which - searches your path for the command that you type and lists the complete path to the first match it finds.

  • type - This command isn’t really a search command; instead, it tells you how a command you type will be interpreted—as a built-in command, an external command, an alias, and so on.


Finding files based on certain permission bits
To search files, containing certain permissions, we use find command:
find path -perm permissions
Permissions can be described in symbolic or octal form. By default find will search the exact permissions, you have defined, for example

find . -perm u=r
will search files with permissions 400. To perform more flexible search you can use / or - symbols. / means match any of permissions. - means exact match. + earlier used, but now is deprecated, / is used instead. For example:

find . -perm /og=r
will search files with o=r or g=r or both. And

find . -perm -og=r
will search only files with g=r and o=r.
Find files larger than 100 kilobytes:

find . -size +100k



Inode
As we know, in Linux everything is a file. Every file is described by inode (index node). Inode consist of:

  • File type (executable, block, character, named pipe, socket, directory, link, etc)

  • Permissions

  • Owner

  • Group

  • File Size

  • File access, change and modification time (UNIX or Linux never stores file creation time)

  • File deletion time

  • Number of links (soft/hard)

  • Extended attribute such as append only or no one can delete file including root user (immutability)

  • Access Control List (ACLs)

To see file's inode you can use command:
ls -i /etc/passwd 131260 /etc/passwd

To see all information you can use stat command:

stat /etc/passwd


Show inode usage of file system: 

df -i /dev/sda1




Timestamps
There are 3 types of types of timestamps:
Access timestamp(atime) - it is set during any read or write operation, you can see access time, using command:

ls -lu file
Change timestamp(ctime) - it is set during changing status of the file, for example changing permissions or ownership, you can see this time using command:

ls -lc file
Modification tymestamp(mtime) - it is set during any write operation, you can see this tyme using command:

ls -l file

Touch program is used to change atime and mtime to current or specified time. If you just run touch without options, it would change atime and mtime of the file to current time. To change only atime -a option is used: touch -a file

To change only mtime -m option is used:

touch -m file

If you want to change timestamps to specified time, you need to use -t or -d options:

touch -t 200812012300.23 file 

touch -d '25 Jan 2008 10:22' file



Tips

Delete file using it's inode:
find . -inum [inode-number] -exec rm -i {} \;

Show directory tree:

tree -d /etc | less

Change file/directory permissions separately:

Directories: 

find . -type d -exec chmod XXX {} \; 

Files: 

find . -type f -exec chmod XXX {} \;

Create hierarchy of directories:

mkdir -p dir1/dir2/dir3

Determine type of the file:

file filename

Create FAT32 partition:
mkdosfs -F 32 /dev/***

Find files, containing “text”
grep -Hlr "text" /path 



Delete old files:
find /backups/tars -mtime +10 -exec rm {} \;

Shared Libraries in Linux

The main administrative task for shared libraries is to tell programs how to find libraries. It can be done by setting a library path. You can set it systemwide or temporarily.
To change or add path systemwide, you need to add path or conf file, containing path, to /etc/ld.so.conf:/path include /etc/ld.so.conf.d/*.conf
For changes take effect you need to launch ldconfig
/lib and /usr/lib are always in library path, even if they aren't listed in /etc/ld.so.conf
To add or change path temporarily you need to set the LD_LIBRARY_PATH environment variable:
$ export LD_LIBRARY_PATH=/usr/local/testlib:/opt/newlib
You can use ldd command to see what libraries the program links to.

Linux kernel


  • Kernel version consist of Major version.Minor version(even numbers for stable, odd numbers for develpment).Patch version. For example: 2.6.26

  • Modules directory: /lib/modules

  • Tools to control modules:

  • insmod - insmod is a trivial program to insert a module into the kernel. This version of insmod is for kernels 2.5.48 and above. If it detects a kernel with support for old-style modules (for which much of the work was done in userspace), it will attempt to run insmod.old in its place. Most users will want to use modprobe(8) instead, which is cleverer.

  • rmmod - simple program to remove a module from the Linux Kernel. Most users will want to use modprobe(8) instead, with the -r option.

  • lsmod - is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. The lsmod command displays information only about kernel modules, not about drivers that are compiled directly into the Linux kernel.

  • modinfo - program to show information about a Linux Kernel module.

  • modprobe - program to add and remove modules from the Linux Kernel

  • depmode - program to generate modules.dep and map files. Starts with a system.

  • System.map file - is a listing of all symbols along with their address.

  • Config file - kernel configuration file generated by make menuconfig/make xconfig/make gconfig

  • initrd.img file - contains device drivers which are required to boot and load rest of operating system from disk. Usually SCSI and IDE drivers are stored in this file.

  • /etc/modules file contains list of modules, that are loaded during system boot

  • /proc/cmdline - options provided to kernel by bootloader

Linux boot


  • Inside the Linux boot process
  • Directories /etc, /bin, /sbin, /lib, and /dev—should never be placed on separate partitions. These directories host critical system configuration files or files without which a Linux system can’t function. For instance, /etc contains /etc/fstab, the file that specifies what partitions correspond to what directories, and /bin contains the mount utility that’s used to mount partitions on directories.
  • Super GRUB disk
  • System rescue CD

Hardware in Linux

Links




Devices

  • Block devices - is an abstraction layer for any storage device that can be formatted in fixed-size blocks; individual blocks may be accessed independently of access to other blocks. Such access is often called random access.

  • Character device

  • ATA devices - /dev/hdx

  • SATA devices - /dev/hdx or /dev/sdx

  • SCSI devices - /dev/sdx

  • ATA magnetic tape devices - /dev/htx or /dev/nhtx

  • SCSI magnetic tape devices - /dev/stx or /dev/nstx



Information about hardware

  • /proc/partitions - shows all block devices

  • /proc/dma - shows which DMA channels are in use

  • /proc/cpuinfo - all information about CPU

  • The sysfs virtual filesystem, mounted at /sys, exports information about devices so that user space utilities can access the information.

  • HAL -  Daemon The Hardware Abstraction Layer (HAL) Daemon, or hald, is a user space program that runs at all times (that is, as a daemon) that provides other user space programs with information about available hardware.

  • D-Bus - The Desktop Bus (D-Bus) provides a further abstraction of hardware information access. Like hald, D-Bus runs as a daemon. D-Bus enables processes to communicate with each other as well as to register to be notified of events, both by other processes and by hardware (such as the availability of a new USB device).

  • udev -  Traditionally, Linux has created device nodes as conventional files in the /dev directory tree. The existence of hotplug devices and various other issues, however, have motivated the creation of udev: a virtual filesystem, mounted at /dev, which creates dynamic device files as drivers are loaded and unloaded. You can configure udev through files in /etc/udev, but the standard configuration is usually sufficient for common hardware.



Tools



  • lspci - lists all PCI devices.

  • lspnp - display information about PnP devices

  • lsusb - shows all usb devices

  • usbmodules - list kernel driver modules available for a plugged in USB device

  • hotplug - relies on kernel support added with the 2.4.x kernel series. This system uses files stored in /etc/hotplug to control the configuration of specific USB devices. In particular, /etc/hotplug/usb.usermap contains a database of USB device IDs and pointers to scripts in /etc/hotplug/usb that are run when devices are plugged in or unplugged. These scripts might change permissions on USB device files so that ordinary users can access USB hardware, run commands to detect new USB disk devices, or otherwise prepare the system for a new (or newly removed) USB device.

  • usbmgr - a program that runs in the background to detect changes on the USB bus. When it detects changes, it loads or unloads the kernel modules that are required to handle the devices. This package uses configuration files in /etc/usbmgr to handle specific devices and /etc/usbmgr/usbmgr.conf to control the overall configuration.

  • setserial - configures serial interface

  • pnpdump - Dump ISA Plug-And-Play devices resource information

  • sndconfig - easy configuration of sound card

  • scsi_info, sginfo, sg_map - shows information about scsi device

  • hdparm - dispalys information about hard drives and configure them.

  • setpci - utility to directly query and adjust PCI devices’ configurations


Tips
If USB device is self disconnected during boot and you see for example the following in dmesg:
usb 2-1: USB disconnect, address 9
removing kernel module ehci_hcd can help you:
sudo modprobe -r ehci_hcd


Ping does not work

Today I would like to discuss a banal situation: host A is directly connected to host B, ping from host A to host B does not work. What are...