udev – Linux dynamic device management

ABOUT UDEV

udev - Linux dynamic device management
udev (userspace /dev) is a device manager for the Linux kernel

CONFIG FILE RELATED

# do not edit this file, it will be overwritten on update

ACTION=="remove", GOTO="cdrom_end"
SUBSYSTEM!="block", GOTO="cdrom_end"
KERNEL!="sr[0-9]*|xvd*", GOTO="cdrom_end"
ENV{DEVTYPE}!="disk", GOTO="cdrom_end"

# unconditionally tag device as CDROM
KERNEL=="sr[0-9]*", ENV{ID_CDROM}="1"

# media eject button pressed
ENV{DISK_EJECT_REQUEST}=="?*", RUN+="cdrom_id --eject-media $tempnode", GOTO="cdrom_end"

# import device and media properties and lock tray to
# enable the receiving of media eject button events
IMPORT{program}="cdrom_id --lock-media $tempnode"

LABEL="cdrom_end"
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/lib/udev/rules.d/60-cdrom_id.rules" [readonly] 18L, 584C                                  1,3           All

RELATED SHELL SESSION (cdrom)

[bash]
$cat /proc/sys/dev/cdrom/info
CD-ROM information, Id: cdrom.c 3.20 2003/12/17

drive name: sr0
drive speed: 48
drive # of slots: 1
Can close tray: 1
Can open tray: 1
Can lock tray: 1
Can change speed: 1
Can select disk: 0
Can read multisession: 1
Can read MCN: 1
Reports media changed: 1
Can play audio: 1
Can write CD-R: 1
Can write CD-RW: 1
Can read DVD: 1
Can write DVD-R: 1
Can write DVD-RAM: 1
Can read MRW: 1
Can write MRW: 1
Can write RAM: 1

$

[/bash]

FROM MANUAL

udev  supplies  the system  software  with  device events,  manages permissions of  device nodes and
may create  additional symlinks in the  /dev  directory, or  renames  network  interfaces. The  kernel
usually just assigns unpredictable  device names based on the order of discovery. Meaningful symlinks
or network device names provide away to reliably  identify  devices based  on  their properties  or
current configuration. The udev  daemon, udevd(8),  receives device uevents  directly from the kernel 
whenever  a device is added or  removed from the system, or  it changes its  state. When  udev receives
a device  event, it matches  its  configured  set   of  rules  against  various  device attributes  to
identify the  device. Rules  that match  may provide additional device information to be  stored in the
udev database or to be used to create meaningful symlink names. All  device  in formation  udev  processes
is stored  in  the  udev database and sent out to  possible event subscribers. Access to all stored  data
and  the  event  sources is  provided  by the  library libudev.  

source: Manual

LINKS
https://www.linux.com/news/udev-introduction-device-management-modern-linux-system
https://en.wikipedia.org/wiki/Udev
https://www.thegeekdiary.com/beginners-guide-to-udev-in-linux/

bubble sort testing… 0.00005 Ver — 2000 No.’s related

$time php bubble.php

real	0m2.076s
user	0m1.736s
sys	0m0.052s
$time php bubble.php

real	0m1.8100s
user	0m1.680s
sys	0m0.044s
$time php bubble.php

real	0m1.935s
user	0m1.672s
sys	0m0.052s
$time php bubble.php

real	0m1.861s
user	0m1.676s
sys	0m0.040s
$time php bubble.php

real	0m2.076s
user	0m1.708s
sys	0m0.064s
$time php bubble.php

real	0m1.850s
user	0m1.640s
sys	0m0.060s
$time php bubble.php

real	0m1.872s
user	0m1.664s
sys	0m0.060s
$

What is a dirty inode and what are dirty bytes ?

What is a Dirtied inode

It is a  common computer term that data in a  cache is "clean" when it  has been  synchronized, and  "dirty" when  it is  different.  A dirty inode  has had new data written  into it but it  has not been
written to disk.

source : http://serverfault.com/questions/56374/dirtied-inode
dirty_inode: this method is called by the VFS to mark an inode dirty.

What are dirty_bytes

Contains the amount  of dirty memory at which  a process generating disk writes will itself  start writeback.  Note: dirty_bytes is the counterpart of dirty_ratio. Only one  of them may be specified
at a time.  When one  sysctl is  written  it is  immediately taken  into account to evaluate  the dirty
memory limits and  the other appears as 0 when read.  Note: the minimum value allowed for dirty_bytes is
two  pages (in  bytes); any  value lower  than this  limit  will be ignored and the old configuration
will be retained.

source: http://kernel.org/doc/Documentation/sysctl/vm.txt

RELATED SHELL SESSION EXPOSURE
[bash light=”true”]
$cat /proc/sys/vm/dirty_background_bytes
0
$echo 1 > /proc/sys/vm/dirty_background_bytes
$cat /proc/sys/vm/dirty_background_bytes
1
$echo 1 > /proc/sys/vm/dirty_
dirty_background_bytes dirty_bytes dirty_ratio
dirty_background_ratio dirty_expire_centisecs dirty_writeback_centisecs
$echo 1 > /proc/sys/vm/dirty_background_ratio

$echo 1 > /proc/sys/vm/dirty_background_ratio
$echo 0 > /proc/sys/vm/dirty_background_ratio
$cat /proc/sys/vm/dirty_bytes
0
$echo 1 > /proc/sys/vm/dirty_bytes
$echo 1 > /proc/sys/vm/
block_dump legacy_va_layout numa_zonelist_order
compact_memory lowmem_reserve_ratio oom_dump_tasks
dirty_background_bytes max_map_count oom_kill_allocating_task
dirty_background_ratio memory_failure_early_kill overcommit_memory
dirty_bytes memory_failure_recovery overcommit_ratio
dirty_expire_centisecs min_free_kbytes page-cluster
dirty_ratio min_slab_ratio panic_on_oom
dirty_writeback_centisecs min_unmapped_ratio percpu_pagelist_fraction
drop_caches mmap_min_addr scan_unevictable_pages
extfrag_threshold nr_hugepages stat_interval
hugepages_treat_as_movable nr_hugepages_mempolicy swappiness
hugetlb_shm_group nr_overcommit_hugepages vfs_cache_pressure
laptop_mode nr_pdflush_threads zone_reclaim_mode
$echo 1 > /proc/sys/vm/dirty_expire_centisecs
$echo 1 > /proc/sys/vm/dirty_writeback_centisecs
$

[/bash]
RELATED SOURCE CODE EXPOSURE
[c light=”true”]
/*
* ext4_dirty_inode() is called from __mark_inode_dirty()
*
* We’re really interested in the case where a file is being extended.
* i_size has been changed by generic_commit_write() and we thus need
* to include the updated inode in the current transaction.
*
* Also, dquot_alloc_block() will always dirty the inode when blocks
* are allocated to the file.
*
* If the inode is marked synchronous, we don’t honour that here – doing
* so would cause a commit on atime updates, which we don’t bother doing.
* We handle synchronous inodes at the highest possible level.
*
* If only the I_DIRTY_TIME flag is set, we can skip everything. If
* I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
* to copy into the on-disk inode structure are the timestamp files.
*/
void ext4_dirty_inode(struct inode *inode, int flags)
{
handle_t *handle;

if (flags == I_DIRTY_TIME)
return;
handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
if (IS_ERR(handle))
goto out;

ext4_mark_inode_dirty(handle, inode);

ext4_journal_stop(handle);
out:
return;
}
[/c]
SOURCE CODE TAKEN FROM OFFICIAL LINUX KERNEL
LINKS
https://www.kernel.org/doc/Documentation/filesystems/vfs.txt
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/ext4/inode.c?h=v4.20-rc3

The atime and noatime attribute

The atime and noatime attribute


Linux records  information about when  files were created  and last
modified as  well as  when it  was last accessed.  There is  a cost
associated  with recording  the  last access  time.  The ext2  file
system of Linux has an attribute that allows the super-user to mark
individual  files   such  that  their  last  access   time  is  not
recorded. This may lead  to significant performance improvements on
often accessed  frequently changing files  such as the  contents of
the /var/spool/news directory.


Linux has a  special mount option for file  systems called noatime
that can be  added to each line that addresses  one file system in
the /etc/fstab file.  If a file system has  been mounted with this
option, reading accesses to the  file system will no longer result
in an  update to  the atime information  associated with  the file
like  we  have explained  above.  The  importance  of the  noatime
setting  is that  it eliminates  the need  by the  system  to make
writes  to  the file  system  for  files  which are  simply  being
read. Since writes  can be somewhat expensive, this  can result in
measurable performance gains. Note that the write time information
to a file will continue to  be updated anytime the file is written
to. In  our example below, we  will set the noatime  option to our
/chroot file system.

source : http://tldp.org/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/chap6sec73.html
$lsattr yt-chanrip
------------------- yt-chanrip
$ls -l yt-chanrip
-rw-r--r-- 1 jeffrin jeffrin 230 Sep  2 18:19 yt-chanrip
$chattr +A yt-chanrip
$lsattr yt-chanrip
-------A----------- yt-chanrip
$ls -l yt-chanrip
-rw-r--r-- 1 jeffrin jeffrin 230 Sep  2 18:19 yt-chanrip
$echo "   " >> yt-chanrip
$lsattr yt-chanrip
-------A----------- yt-chanrip
$ls -l yt-chanrip
-rw-r--r-- 1 jeffrin jeffrin 234 Nov 29 22:30 yt-chanrip
$time

real	0m0.000s
user	0m0.000s
sys	0m0.000s
$chattr -A yt-chanrip
$echo "   " >> yt-chanrip
$ls -l yt-chanrip
-rw-r--r-- 1 jeffrin jeffrin 238 Nov 29 22:31 yt-chanrip
$

bubble sort testing… 0.00004 Ver — 1000 No.’s related

$time php bubble.php

real	0m0.846s
user	0m0.388s
sys	0m0.036s
$time php bubble.php

real	0m0.707s
user	0m0.368s
sys	0m0.032s
$time php bubble.php

real	0m0.718s
user	0m0.384s
sys	0m0.024s
$time php bubble.php

real	0m0.768s
user	0m0.372s
sys	0m0.036s
$time php bubble.php

real	0m0.556s
user	0m0.432s
sys	0m0.024s
$time php bubble.php

real	0m0.476s
user	0m0.372s
sys	0m0.040s
$time php bubble.php

real	0m0.438s
user	0m0.356s
sys	0m0.036s
$

block_dump enables block I/O debugging when set to a nonzero value

When this flag is set, Linux reports all disk read and write
operations that take place, and all block dirtyings done to
files. This makes it possible to debug why a disk needs to
spin up, and to increase battery life even more. The output of
block_dump is written to the kernel output, and it can be
retrieved using "dmesg". When you use block_dump and your kernel
logging level also includes kernel debugging messages, you
probably want to turn off klogd, otherwise the output of block_dump
will be logged, causing disk activity that is not normally there.

$cat /proc/sys/vm/block_dump
0
$echo 1 > /proc/sys/vm/block_dump
$cat /proc/sys/vm/block_dump
1
$

chattr – change file attributes on a Linux file system

A UNIX Command

$lsattr test.ogg
------------------- test.ogg
$chattr +u test.ogg
$lsattr test.ogg
-u----------------- test.ogg
$chattr -u test.ogg
$lsattr test.ogg
------------------- test.ogg
$chattr =u test.ogg
$lsattr test.ogg
-u----------------- test.ogg
$chattr +i  test.ogg
chattr: Operation not permitted while setting flags on test.ogg
$sudo chattr +i  test.ogg
[sudo] password for jeffrin:
$lsattr test.ogg
-u--i-------------- test.ogg
$rm test.ogg
rm: remove write-protected regular file `test.ogg'? y
rm: cannot remove `test.ogg': Operation not permitted
$

UNIX Explanation

change file attributes on a Linux file system
The  letters  `acdeijstuADST' select  the  new  attributes for  the
files: append only (a), compressed  (c), no dump (d), extent format
(e), immutable  (i), data journalling (j), secure  deletion (s), no
tail-merging   (t),  undeletable   (u),  no   atime   updates  (A),
synchronous directory  updates (D),  synchro nous updates  (S), and
top of directory hierarchy (T).


bubble sort testing… 0.00003 Ver — 500 No.’s related

$time php bubble.php

real	0m0.366s
user	0m0.212s
sys	0m0.032s
$time php bubble.php

real	0m0.231s
user	0m0.188s
sys	0m0.020s
$time php bubble.php

real	0m0.191s
user	0m0.132s
sys	0m0.040s
$time php bubble.php

real	0m0.192s
user	0m0.148s
sys	0m0.028s
$time php bubble.php

real	0m0.190s
user	0m0.140s
sys	0m0.032s
$time php bubble.php

real	0m0.191s
user	0m0.144s
sys	0m0.028s
$time php bubble.php

real	0m0.189s
user	0m0.132s
sys	0m0.044s
$

bubble sort testing… 0.00002 Ver — 250 No.'s related

$time php bubble.php

real	0m0.318s
user	0m0.068s
sys	0m0.040s
$time php bubble.php

real	0m0.171s
user	0m0.092s
sys	0m0.032s
$time php bubble.php

real	0m0.169s
user	0m0.084s
sys	0m0.036s
$time php bubble.php

real	0m0.153s
user	0m0.072s
sys	0m0.036s
$time php bubble.php

real	0m0.154s
user	0m0.080s
sys	0m0.032s
$time php bubble.php

real	0m0.145s
user	0m0.080s
sys	0m0.024s
$time php bubble.php

real	0m0.182s
user	0m0.076s
sys	0m0.056s
$