<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>inode Archives - ITEC4B</title>
	<atom:link href="https://itec4b.com/tag/inode/feed/" rel="self" type="application/rss+xml" />
	<link>https://itec4b.com/tag/inode/</link>
	<description>Information Technology Expert Consulting</description>
	<lastBuildDate>Wed, 15 Feb 2023 20:13:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.3</generator>
	<item>
		<title>Linux Symbolic/Hard Links</title>
		<link>https://itec4b.com/linux-symbolic-hard-links/</link>
		
		<dc:creator><![CDATA[author]]></dc:creator>
		<pubDate>Sun, 12 Feb 2023 20:03:14 +0000</pubDate>
				<category><![CDATA[File System]]></category>
		<category><![CDATA[filesystem]]></category>
		<category><![CDATA[inode]]></category>
		<category><![CDATA[linux]]></category>
		<guid isPermaLink="false">https://itec4b.com/?p=1309</guid>

					<description><![CDATA[To understand links in a file system, you first have to understand what an inode is. In linux there are two types of links :&#8211; Soft/Symbolic Links&#8211; Hard Links Hard Links Every file on the Linux filesystem starts with a single hard link.The link is between the filename and the actual data stored on the &#8230; <p class="link-more"><a href="https://itec4b.com/linux-symbolic-hard-links/" class="more-link">Read more<span class="screen-reader-text"> "Linux Symbolic/Hard Links"</span></a></p>]]></description>
										<content:encoded><![CDATA[
<p><span style="text-decoration: underline;">To understand links in a file system, you first have to understand <a href="https://itec4b.com/linux-filesystem-directory-entries-inodes-datablocks">what an inode is</a></span>.<br><br><span style="text-decoration: underline;"><strong>In linux there are two types of links</strong></span> :<br>&#8211; <strong>Soft/Symbolic Links</strong><br>&#8211; <strong>Hard Links</strong></p>



<h2>Hard Links</h2>



<p><span style="text-decoration: underline;">Every file on the Linux filesystem starts with a single hard link</span>.<br>The link is between the filename and the actual data stored on the filesystem (directory entry &gt; inode &gt; data blocks).<br><br><strong><span style="text-decoration: underline;">When you create a hard link you create a file that gets the same inode as the target file</span></strong>.<br><span style="text-decoration: underline;"><strong>You have different file names/paths for a unique physical file on a partition (pointing to the same inode)</strong></span> </p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><a href="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-hardlink-inodes-datablocks.png"><img decoding="async" src="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-hardlink-inodes-datablocks.png" alt="" class="wp-image-1321" width="697" height="393" srcset="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-hardlink-inodes-datablocks.png 929w, https://itec4b.com/wp-content/uploads/2023/02/linux-schema-hardlink-inodes-datablocks-300x169.png 300w, https://itec4b.com/wp-content/uploads/2023/02/linux-schema-hardlink-inodes-datablocks-768x433.png 768w" sizes="(max-width: 697px) 100vw, 697px" /></a></figure></div>


<p class="has-vivid-red-color has-text-color"><strong><span style="text-decoration: underline;">Hard links can only be created for regular files (not directories or special files) and ONLY within the same filesystem.<br>A hard link cannot span multiple filesystems</span></strong>.</p>



<p class="has-black-color has-text-color">If you delete the <code>"</code>original file<code>"</code>, you can still access it via any remaining hard link having the same inode.<br>Apart from the filename/filepath, you cannot tell which one is the hard link since they share the same inode.</p>



<pre class="wp-block-code"><code>$ ln /path/to/target_file /path/to/hardlink</code></pre>



<pre class="wp-block-code"><code><strong><span style="text-decoration: underline;">List inodes files from &lt;dir>, Recursive, No Sort, date (mtime)</span></strong>
<strong>CMD_LS_RECURSIVE_INODE_MTIME_NOSORT</strong>
<strong><span style="text-decoration: underline;">OUTPUT</span>:</strong>
<strong>&lt;inode> &lt;mode?rwx> &lt;links> &lt;uname> &lt;gname> &lt;size_bytes> &lt;date YYYY-MM-DD> &lt;time hh:mm:ss> &lt;filepath></strong>

$ LC_ALL=C ls -ilR --time-style='+%F %T' &lt;dir> 2>/dev/null | sed -e '/:$/,/^total &#91;0-9]\{1,\}/d' -n -e '/^&#91;0-9]\{1,\} -/p' | tr -s '\n'


<strong><span style="text-decoration: underline;">List inodes with hard link(s) from &lt;dir>, Recursive, Natural Sort (inode first), date (mtime)</span></strong>
<strong><span style="text-decoration: underline;">OUTPUT</span>:</strong>
<strong>&lt;inode> &lt;mode?rwx> &lt;links> &lt;uname> &lt;gname> &lt;size_bytes> &lt;date YYYY-MM-DD> &lt;time hh:mm:ss> &lt;filepath></strong>

$ &lt;CMD_LS_RECURSIVE_INODE_MTIME_NOSORT> | awk '$3 > 1 {print $0}' | sort


<span style="text-decoration: underline;"><strong>Find inodes with hard link(s)</strong></span>
<strong><span style="text-decoration: underline;">OUTPUT</span>:</strong> <strong>&lt;inode></strong>
$ &lt;CMD_LS_RECURSIVE_INODE_MTIME_NOSORT> | awk '$3 > 1 {print $1}' | sort -u</code></pre>



<h2>Soft/Symbolic Links</h2>



<p><strong><span style="text-decoration: underline;">When you create a soft link, you create a new file with a new inode, which points to the target path</span></strong>.<br><strong><span style="text-decoration: underline;">It doesn&#8217;t reference the target inode</span></strong>. <strong><span style="text-decoration: underline;">If the target&#8217;s path/name changes or is deleted, the reference breaks (pointing to a nonexistent file path)</span></strong>.<br><br>Symbolic links can link together non-regular and regular files.<br><span style="text-decoration: underline;"><strong>They also can span multiple filesystems</strong></span>.<br><br><strong><span style="text-decoration: underline;">A symbolic link is identified by the mode lrwxrwxrwx, you cannot change it</span></strong>, so it is easy to identify them.<br>The symbolic link&#8217; size is the target&#8217;s path length.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><a href="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-softlink-inodes-datablocks.png"><img decoding="async" loading="lazy" src="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-softlink-inodes-datablocks.png" alt="" class="wp-image-1322" width="721" height="393" srcset="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-softlink-inodes-datablocks.png 961w, https://itec4b.com/wp-content/uploads/2023/02/linux-schema-softlink-inodes-datablocks-300x164.png 300w, https://itec4b.com/wp-content/uploads/2023/02/linux-schema-softlink-inodes-datablocks-768x419.png 768w" sizes="(max-width: 721px) 100vw, 721px" /></a></figure></div>


<p>Changing owner, group, permissions of a symbolic link only has effect on the target file, in this case target file&#8217;s ctime is updated.<br><br>A change to a symlink name, updates its access time (atime) and status time (ctime).<br>That is the only thing you can change for the symbolic file itself.</p>



<pre class="wp-block-code"><code>$ ln -s /path/to/target_file /path/to/symlink</code></pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Linux Filesystem: Directory Entries, Inodes, Data Blocks</title>
		<link>https://itec4b.com/linux-filesystem-directory-entries-inodes-datablocks/</link>
		
		<dc:creator><![CDATA[author]]></dc:creator>
		<pubDate>Sat, 11 Feb 2023 17:00:39 +0000</pubDate>
				<category><![CDATA[File System]]></category>
		<category><![CDATA[filesystem]]></category>
		<category><![CDATA[inode]]></category>
		<category><![CDATA[linux]]></category>
		<guid isPermaLink="false">https://itec4b.com/?p=1126</guid>

					<description><![CDATA[An inode is a unique number assigned to each Linux file and directory in a filesystem (except for hard links), it is used as an index (Index Node). Inodes store metadata (attributes) about the files they refer to (it is like the "file&#8217;s identity card" without the name)ANDBecause the data of a file is actually &#8230; <p class="link-more"><a href="https://itec4b.com/linux-filesystem-directory-entries-inodes-datablocks/" class="more-link">Read more<span class="screen-reader-text"> "Linux Filesystem: Directory Entries, Inodes, Data Blocks"</span></a></p>]]></description>
										<content:encoded><![CDATA[
<p><span style="text-decoration: underline;"><strong>An inode is a unique number assigned to each Linux file and directory in a filesystem (except for <a href="https://itec4b.com/linux-symbolic-hard-links">hard links</a>), it is used as an index (Index Node)</strong></span>.<br><br><strong><span style="text-decoration: underline;">Inodes store metadata (attributes) about the files they refer to (it is like the <code>"</code>file&#8217;s identity card<code>"</code> without the name)</span></strong><br><strong>AND</strong><br>Because the data of a file is actually stored into data blocks on a physical drive, <strong><span style="text-decoration: underline;">serve as a reference to the disk blocks locations of the data they point to (via data block pointers)</span></strong>.<br><span style="text-decoration: underline;">Note that this information is not directly accessible to the user</span>.<br><br>Thus, an inode is a data structure in a Unix-style filesystem that describes a filesystem object such as a file or a directory.<br><br><span style="text-decoration: underline;">A <strong>block device</strong> is a storage device from which you can read/write data blocks</span>.<br>You create partitions on it and then format it with a <strong>filesystem</strong> that dictates how the files are organized/managed.<br><span style="text-decoration: underline;">Every filesystem needs to split up a partition into data blocks to store files and file parts</span>.<br><br>A <strong>data block</strong> is the basic unit of data storage in a filesystem.<br>It is the smallest unit of data that can be read or written in a single operation.<br>In most filesystems, each data block has a fixed size, typically between 512 and 4096 bytes.<br><span style="text-decoration: underline;"><strong>Today the default is usually 4096 bytes for storage I/O and filesystems</strong></span>.<br><br>With a default filesystem block size of 4096 bytes, if you have a data file of 3 bytes (logical size), it will take away 1 block (4096 bytes: physical size on storage device) from your disk&#8217;s capacity, since that is the smallest unit of the filesystem.<br>If you have a data file of 4097 bytes it will take 2 blocks.</p>



<pre class="wp-block-code"><code><strong><span style="text-decoration: underline;">NOTE</span></strong>:
'stat' command provides 'Size:' and 'Blocks:' informations
'Size:' is the data file' size in bytes (logical size)
'Blocks:' is the real disk usage in blocks of 512 bytes (physical size)

<strong><span style="text-decoration: underline;">List size files from DIR</span></strong>
<strong>OUTPUT: &lt;logical_size_bytes> &lt;physical_size_bytes> &lt;filepath></strong>

$ LC_ALL=C find DIR -type f -exec stat -c '%s %b %B %n' {} + 2>/dev/null | awk '{ fname=""; for (i=4; i &lt;= NF; i++) fname=fname $i " "; print $1" "($2*$3)" "fname }'</code></pre>



<p>Linux ext filesystem uses a default 4096 bytes for a block size because that&#8217;s the default pagesize of CPUs, so there&#8217;s an easy mapping between memory-mapped files and disk blocks.<br>The hardware (specifically, the Memory Management Unit, which is part of the CPU) determines what page sizes are possible. It is the smallest unit of data for memory management in a virtual memory operating system. Almost all architectures support a 4kB page size. Modern architectures support larger pages (and a few also support smaller pages), but <strong><span style="text-decoration: underline;">4kB is a very widespread default</span></strong>.</p>



<pre id="block-5bda9865-bf3c-48ec-b7c1-7ad5af414fbc" class="wp-block-code"><code><span style="text-decoration: underline;">Get the filesystem block size in bytes</span>
(size used internally by kernel, it may be modified by filesystem driver on mount)
# blockdev --getbsz /dev/&lt;device&gt;

<span style="text-decoration: underline;">Get the system's page size</span>
(number of bytes in a memory page, where "page" is a fixed-length block, the unit for memory allocation and file mapping)
$ getconf PAGE_SIZE
$ getconf PAGESIZE</code></pre>



<pre class="wp-block-code"><code><span style="text-decoration: underline;">Print inode's metadata for a specific file/dir using stat command</span>
$ LC_ALL=C stat /path/to/file_or_dir
$ LC_ALL=C stat -c '%i %y %A %U %s %N' /path/to/file_or_dir | sed -e 's;&#91;.]&#91;0-9]\{9\} +&#91;0-9]\{4\};;g'

<span style="text-decoration: underline;">Get inode number(s) with ls -i</span>
$ ls -i1 /path/to/file_or_dir</code></pre>



<pre class="wp-block-code"><code>Get the number of blocks a file uses on disk, so you can calculate disk space really used per file (physical file size).
<span style="text-decoration: underline;"><strong>IMPORTANT</strong></span>:
<strong><span style="text-decoration: underline;">By default 'ls', 'du' and 'df' commands use 1block=1024bytes which may differ from the filesystem unit</span>. <span style="text-decoration: underline;">You can use --block-size option or <a href="https://www.gnu.org/software/coreutils/manual/html_node/Block-size.html">set environment variables</a></span></strong>:
Display values are in units of the first available SIZE from --block-size, DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables. Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

$ du --block-size=4096 /path/to/file
$ ls -s --block-size=4096 /path/to/file

<span style="text-decoration: underline;">ls -l prints the data size in bytes (logical file size), which is less than the actual used space on disk</span>.</code></pre>



<h2><strong>Inodes Metadata</strong></h2>



<pre class="wp-block-code"><code>$ man inode</code></pre>



<p>&#8211; <span style="text-decoration: underline;"><strong>Inode number</strong></span><br>Each file in a filesystem has a unique inode number (except for hard links).<br>Inode numbers are guaranteed to be unique only within a filesystem (i.e. <span style="text-decoration: underline;"><strong>the same inode numbers may be used by different filesystems, which is the reason that hard links may not cross filesystem boundaries</strong></span>).</p>



<p>&#8211; <strong><span style="text-decoration: underline;">Device where inode resides</span></strong><br>Each inode (as well as the associated file) resides in a filesystem that is hosted on a device.<br>That device is identified by the combination of its major ID (which identifies the general class of device) and minor ID (which identifies a specific instance in the general class).<br><br>&#8211; <strong><span style="text-decoration: underline;">Device represented by this inode</span></strong><br>If the current file (inode) represents a device, then the inode records the major and minor ID of that device.<br><br>&#8211; <strong><span style="text-decoration: underline;">Links count</span></strong> (number of hard links to the file)<br><br>&#8211; <strong><span style="text-decoration: underline;">User ID</span></strong> (of the owner of the file)<br><br>&#8211; <strong><span style="text-decoration: underline;">Group ID</span></strong> (of the file)<br><br>&#8211; <strong><span style="text-decoration: underline;">Mode</span></strong>: <strong><span style="text-decoration: underline;">File Type</span></strong> + <strong><span style="text-decoration: underline;">Permissions</span></strong> (read, write and execute permissions of the file for the owner, group and others)<br>The standard Unix file types are regular, directory, symbolic link, FIFO (named pipe), block device, character device, and socket as defined by POSIX.<br><br>&#8211; <span style="text-decoration: underline;"><strong>File size (in bytes)</strong></span><br>This field gives the size of the file (if it is a regular file) in bytes.<br>The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.<br>Default size for a directory is usually one block size (4096 bytes on most ext4 filesystems).<br><br>&#8211; <strong><span style="text-decoration: underline;">Preferred block size for I/O operations (in bytes)</span></strong><br>This field gives the <code>"</code>preferred<code>"</code> block size for efficient filesystem I/O operations.<br>(Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite)<br><br>&#8211; <strong><span style="text-decoration: underline;">Number of blocks allocated to the file</span></strong><br>This field indicates the <strong>number of blocks allocated to the file in 512-byte units</strong><br><br>&#8211; <strong><span style="text-decoration: underline;">File creation (birth) timestamp (btime)</span></strong><br>This is set on file creation and not changed subsequently.<br>The btime timestamp was not historically present on UNIX systems and is not currently supported by most Linux filesystems.<br><br>&#8211; <strong><span style="text-decoration: underline;">Last modification timestamp (mtime)</span></strong><br>This is the file&#8217;s last modification timestamp. It is changed by file modifications (file&#8217;s content: data).<br>Moreover, the mtime timestamp of a directory is changed by the creation or deletion of files in that directory.<br><span style="text-decoration: underline;">The mtime timestamp is not changed for changes in file&#8217;s name, owner, group, hard link count, or mode.</span><br><br>&#8211; <strong><span style="text-decoration: underline;">Last access timestamp (atime)</span></strong><br>It is changed by file accesses.<br><br>&#8211; <strong><span style="text-decoration: underline;">Last status change timestamp (ctime)</span></strong><br>It is changed by modifying file&#8217;s metadata information (i.e. file&#8217;s name, owner, group, link count, mode, etc.).</p>



<p><span style="text-decoration: underline;"><strong>According to The POSIX standard an inode is a <code>"</code>file serial number<code>"</code>, defined as a per-filesystem unique identifier for a file.<br>Combined together with the device ID of the device containing the file, they uniquely identify the file within the whole system</strong></span>.</p>



<p>Two files can have the same inode, but only if they are part of different partitions (except for hard links).<br><strong><span style="text-decoration: underline;">Inodes are only unique on a partition level, not on the whole system</span></strong>.</p>



<h2>Directory Entry</h2>



<p>You may have noticed that inodes do not contain the file&#8217;s name.<br><strong><span style="text-decoration: underline;">The file’s name is not stored in the inode metadata but in its directory structure</span></strong>.<br><strong><span style="text-decoration: underline;">UNIX systems use a directory stream mapping system: directory entries contain the filenames and their inodes number</span></strong>.<br><br><strong>From a user perspective a directory contains files, technically a directory is a structure used to locate other files/directories.<br>In most Unix filesystems, a directory is a mapping from filenames to inode numbers</strong>.<br><strong>There&#8217;s a separate table mapping inode numbers to inode data</strong>.<br><br><span style="text-decoration: underline;">The header file <code><strong>dirent.h</strong></code> describes the format of a directory entry</span>.<br><br>Format of a Directory Entry<br><a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html">https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html</a><br><a href="https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html">https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html</a><br><br><span style="text-decoration: underline;">In the glibc implementation, the <strong>dirent structure</strong> is defined as follows</span>:</p>



<pre class="wp-block-code"><code>struct dirent {
   ino_t   d_ino;              /* Inode number */
   off_t   d_off;              /* Not an offset */
   unsigned short   d_reclen;  /* Length of this record */
   unsigned char   d_type;     /* Type of file; not supported by all filesystem types */
   char d_name&#91;256];           /* Null-terminated filename */
};</code></pre>



<p><strong><span style="text-decoration: underline;">The only fields in the <code>dirent</code> structure that are mandated by POSIX.1 are <code>d_name</code> and <code>d_ino</code></span></strong>.<br>The other fields are unstandardized, and not present on all systems.</p>



<pre class="wp-block-code"><code>/* This is the data type of directory stream objects. */
typedef struct __dirstream DIR;

The DIR data type represents a directory stream.
You shouldn’t ever allocate objects of the struct dirent or DIR data types, since the directory access functions do that for you. Instead, you refer to these objects using the pointers returned by the <a href="https://www.gnu.org/software/libc/manual/html_node/Opening-a-Directory.html">functions</a>.
Directory streams are a high-level interface.</code></pre>



<p><span style="text-decoration: underline;">The design of data block pointers is actually more complex than the schema illustrates below</span>, it also depends on the filesystem. For ext filesystem an inode pointer structure is used to list the addresses of a file&#8217;s data blocks (around 15 direct/indirect data blocks pointers).</p>



<div style="height:18px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><a href="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-dir-inodes-datablocks.jpg"><img decoding="async" loading="lazy" src="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-dir-inodes-datablocks.jpg" alt="" class="wp-image-1160" width="697" height="361" srcset="https://itec4b.com/wp-content/uploads/2023/02/linux-schema-dir-inodes-datablocks.jpg 929w, https://itec4b.com/wp-content/uploads/2023/02/linux-schema-dir-inodes-datablocks-300x155.jpg 300w, https://itec4b.com/wp-content/uploads/2023/02/linux-schema-dir-inodes-datablocks-768x398.jpg 768w" sizes="(max-width: 697px) 100vw, 697px" /></a></figure></div>


<h2>Filesystem</h2>



<p><strong><span style="text-decoration: underline;">Linux uses filesystems to manage data stored on storage devices</span></strong>.<br>The filesystem manages a map (inodes table) to locate each file placed in the storage device.<br>The filesystem divides the partition into blocks: small contiguous areas.<br>The size of these blocks is defined during creation of the filesystem.<br><br><span style="text-decoration: underline;"><strong>Before you can mount a drive partition, you must format it using a filesystem</strong></span>.<br><br><span style="text-decoration: underline;"><strong>The default filesystem used by most Linux distributions is ext4</strong></span>.<br>ext4 filesystem provides journaling, which is a method of tracking data not yet written to the drive in a log file, called the journal. If the system fails before the data can be written to the drive, the journal data can be recovered and stored upon the next system boot.<br><br>After creating a partition, you need to create a filesystem (mkfs program is specifically dedicated for that)</p>



<pre class="wp-block-code"><code>#  LC_ALL=C mkfs -t ext4 /dev/&lt;partition_id&gt;</code></pre>



<p><strong><span style="text-decoration: underline;">Some filesystems (ext4 included), allocate a limited number of inodes when created</span></strong>.<br><strong><span style="text-decoration: underline;">If the filesystem runs out of inode entries in the table, you cannot create any more files, even if there is still space available on the drive: that may happen with a multitude of very small files</span></strong>.<br>When a file is created on the partition or volume, a new entry in the inode table is created.<br>Using the <code>-i</code> option with the <code>df</code> command will show you the percentage of inodes used.<br>It is theoretically possible (although uncommon) that you could run out of available inodes while still having actual space left on your partition, so it’s worth keeping that in mind.<br><br><span style="text-decoration: underline;"><strong>Report file system disk space usage</strong></span></p>



<pre class="wp-block-code"><code><span style="text-decoration: underline;">By blocks (most important)</span>
$ LC_ALL=C df -Th --block-size=4096 -x tmpfs -x devtmpfs -x squashfs 2&gt;/dev/null

<span style="text-decoration: underline;">By inodes</span>
$ LC_ALL=C df -Ti -x tmpfs -x devtmpfs -x squashfs 2&gt;/dev/null</code></pre>



<p>Linux uses <code><strong>e2fsprogs</strong></code> package to provide utilities for working with ext filesystems</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
