"Linux@ Basics ~" # disk and file system

Source: Internet
Author: User
Tags ide hard drive

In the previous three articles, we briefly introduced the Linux system user management, file operation and so on, are relatively superficial basic operations. We're going to go into this section, from the filesystem we want to see the disk system. From the disk system we want to see the overall architecture of the operating system. Not much nonsense let's start to learn it!

Disk and File system

1. Disk System 1.1 disk structure

, the disk consists of a sector and a cylinder, the smallest unit of the partition is the cylinder (the column is thickness, this figure is a section), the smallest unit of disk reading is the sector. The MBR (446bytes) partition table for the first sector can contain up to four partitions (64bytes) of information, that is, 4 sets of data from the start cylinder to the end cylinder, 16 bytes per set, 8 bytes per value, which is the maximum bar number of 2^16-1. An extended partition consists of a primary partition, which is called a logical partition by the partition of the extended partition. The primary partition and the extended partition can be up to 4, and the extended partition cannot be formatted until the partition can be formatted as the primary partition and logical partition. The SATA hard disk logical partition from the 5-15,ide hard drive logical partition from 5-63.

1.2 Viewing mounted devices

Linux partitions represent cocoa-mounted devices that can mount the appropriate file system (directory) on each partition

[[email protected] ~]$ df-h [file or directory]
Filesystem Size used Avail use% mounted on
/dev/sda3 16G 2.4G 13G 16%/
Tmpfs 935M 72K 935M 1%/DEV/SHM
/DEV/SDA1 291M 37M 240M 14%/boot

-H Human Read

With Df-h we can view the current file system mounted devices, without adding files/directories to list all file systems by default.

Where size refers to the total disk capacity, use refers to the used, Avail refers to the available remaining capacity, use% usage = use/(use+avail)

1.3 Disk partition 1.31 View all partitions

[Email protected] weijie]# fdisk-l #查看所有分区

disk/dev/sda:21.5 GB, 21474836480 bytes
255 heads, Sectors/track, 2610 cylinders
Units = Cylinders of 16065 * 8225280 bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk identifier:0x000a4289

Device Boot Start End Blocks Id System
/DEV/SDA1 * 1 307200 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 549 4096000 Swap/solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 549 2611 16567296-up Linux

The above information shows that the SDA disk has a 21.5G size (21474836480 bytes) 255 Head Management Read and write, each head management 63 sectors, 512 bytes per sector, a total of 5,005 cylinders.

Device: The file name of the machine, which varies depending on the disk interface/partition location.

Boot: Indicates whether the boot module is powered on * indicates yes

Start,end: Indicates the size of this partition, between which two cylinder numbers can be determined.

Blocks: Is the capacity in 1K units.

Id,system: Represents the partition's file system.

1.32 New/Delete a partition

Fdisk/dev/sda3

Operation Command:

D Delete a partition

n Add a new partition to the condition that the remaining unallocated space or have an extended partition, can be divided into logical partitions.

M Print command prompt

P Display the partition table on the screen

Q Do not save partition modification, exit program

W Save changes and exit

+512m how to use + capacity when setting capacity

1.4 Disk Formatting

Disk formatting primarily uses MKFS and MKE2FS two commands

MKFS [-T file system format] device file name

MKE2FS [-B Block Size] [-I block/node a node allocates how many blocks] [-l volume label] [-C check Disk error] [-j system format is ext3 (with journeal function), not ext2]

1.5 Disk Detection

Check the file system for errors

fsck [-T file system EXT2/EXT3] [-a scan device] [-c check process with histogram] [-a automatically fix problematic sectors] [-y is similar to a, for some file system] device name

Check the hard drive or floppy disk sector for bad lanes

Badblock [-S screen shows Progress] [-V can see progress on screen] [-w test with write] device name

1.6 Disk mounts and uninstalls

When it comes to mounting the first introduction to the/etc/fstab configuration file, let's look at the cat command first:

[Email protected] weijie]# Cat/etc/fstab

#
#/etc/fstab
# Created by Anaconda on Sun Jan 25 07:14:00 2015
#
# Accessible filesystems, by reference, is maintained under '/dev/disk '
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
#
UUID=411DD466-551A-4EF2-A9B4-682B646AA308/EXT4 Defaults 1 1
Uuid=967157df-1cd7-42a6-9b0a-56e94fce0d1a/boot EXT4 Defaults 1 2
Uuid=331eec8a-c9a7-4b86-a63d-3f5e9cf64a71 swap swap defaults 0 0
TMPFS/DEV/SHM TMPFS Defaults 0 0
Devpts/dev/pts devpts gid=5,mode=620 0 0
Sysfs/sys Sysfs Defaults 0 0
PROC/PROC proc Defaults 0 0,

Configuration file Parameter Description:

1=> The disk device file name or the device's volume label

2=> Mount Directory

3=> disk partition file system Ext3,reiserfs,nfs,vfat, etc.

4=> file system parameters separated by commas

Async/sync Synchronous or asynchronous

Auto/noauto is mount-a active test mount

Whether the Rw/ro is read-only or readable or writable

Whether the exec/noexec is for execution, or pure data

User/nouser whether the user is allowed to mount (non-root) using mount

Suid/nosuid whether there is suid permission, not the execution file, and this permission is not required

Usrquta boot file system supports disk quotas

Grpquota boot file system to group disk quota mode support

Mode set mount point permission maximum of 666

uid=000 settings allow uid=000 user action

gid=000 Ibid.

Defaults default settings

5=> whether the dump backup command functions 0: Do not back up 1: Daily dump 2: Irregular dump

6=> whether to check sector 0 with fsck: no need to check 1: first check 2: second check

Mount command

Mount [-A] mounts the disks that are not mounted according to the configuration file

Mount [-l] Displays all mount lists

Mount [-t file system] [-l label name] [-o extra option] [-n write/etc/mtab] device file name mount Directory

-O

RO,RW Mount read-only or read-write

Whether the Auto,noauto is automatically mounted by mount-a

Remount Re-mount

EX: We plug the mp3 into the USB port and mount it to the/mnt/mp3player

[Email protected] mnt]# fdisk-l
disk/dev/sdb:3711 MB, 3711959040 bytes New mount directory under MNT mp3player

[Email protected] mnt]# mkdir Mp3player

[Email protected] mnt]# mount-t vfat-o iocharset=cp950/dev/sdb/mnt/mp3player/

VFAT support setting iocharset=cp950 Chinese language display

The mount command is temporarily mounted and fails after a reboot, so the configuration file needs to be modified for permanent mounting.

Same uninstall a Mount Umount [-f Force] [-N not update/etc/mtab] mount device or mount point

[Email protected]/]# umount-fn/media/walkman/

2. File System 2.1 File System overview

The Linux file system is ext2-4, or Inode-block.

Each file and directory has its own inode, each inode has a corresponding block number, through the inode of the file to find the corresponding block. A file may consist of more than one block.

At the same time, each partition can be divided into a block group, each containing a superblock, file system description (description), block corresponding table (block bitmap), Inode table (Inode bitmap), Inode table and data block. As a result, each mounted file system can be considered to be made up of block.

2.2 File System details

Superblock

Superblock records information about the entire file system. Inode/block total, unused and used block and inode totals. Block and Inode size (block size is 1k,2k or 4k, Inode is 128bytes)

The file system mount time, the last time the data was written, the time of the last Test disk (fsck), and so on, the information is first closed. Validbit value, if this filesystem is mounted, then Validbit is 0, or 1 if not mounted.

File system Description

This section describes the start and end block numbers for each block group, as well as a description of the block numbers in each section (Superblock,bitmap,inode,data block) respectively. This section can also be viewed with DUMPE2FS.

[email protected] ~]$ sudo dumpe2fs/dev/sda1
[sudo] password for David:
DUMPE2FS 1.41.12 (17-may-2010)
Filesystem Volume Name: <none>
Last mounted on:/boot
Filesystem UUID:967157DF-1CD7-42A6-9B0A-56E94FCE0D1A
Filesystem Magic number:0xef53
Filesystem Revision #: 1 (dynamic)
Filesystem features:has_journal ext_attr resize_inode dir_index filetype needs_recovery extent FLEX_BG Sparse_super Huge_file UNINIT_BG Dir_nlink extra_isize
Filesystem Flags:signed_directory_hash
Default Mount Options:user_xattr ACL
Filesystem State:clean
Errors behavior:continue
Filesystem OS Type:linux
Inode count:76912 #inode Total
Block count:307200 #block Total
Reserved Block count:15360 #保留block数量
Free blocks:260295 #空闲block
Free inodes:76873 #空闲inode
First Block:1 #第一个block序号
Block size:1024 #block大小
Fragment size:1024
Reserved GDT blocks:256
Blocks per group:8192
Fragments per group:8192
Inodes per group:2024
Inode blocks per group:253
Flex Block Group Size:16
Filesystem Created:sun Jan 25 07:12:55 2015
Last Mount Time:mon Feb 23 21:00:34 2015
Last write Time:mon Feb 23 21:00:34 2015
Mount Count:9
Maximum Mount Count: 1
Last Checked:sun Jan 25 07:12:55 2015
Check interval:0 (<none>)
Lifetime writes:41 MB
Reserved blocks uid:0 (user root)
Reserved blocks gid:0 (group root)
First Inode:11
Inode size:128
Journal Inode:8 #指向journal块的inode号
Default Directory HASH:HALF_MD4
Directory Hash seed:284e552c-ef40-4b71-95e9-9b2ace7b1d52
Journal Backup:inode Blocks
Journal Features: (none)
Journal size:8m #日志文件大小
Journal length:8192 #日志长度
Journal sequence:0x00000036
Journal start:0


Group 0: (Blocks 1-8192) [itable_zeroed]
Checksum 0x84ca, unused inodes 2006
Primary superblock at 1, Group descriptors at 2-3
Reserved GDT blocks at 4-259
Block Bitmap at 260 (+259), Inode bitmap at 276 (+275)
Inode table at 292-544 (+291)
3836 free blocks, 2006 free inodes, 2 directories, 2006 unused Inodes
Free blocks:4357-8192
Free inodes:19-2024
Group 1: (Blocks 8193-16384) [Inode_uninit, itable_zeroed]
Checksum 0xe5d9, unused inodes 2024
Backup superblock at 8193, Group descriptors at 8194-8195
Reserved GDT blocks at 8196-8451
Block Bitmap at 261 (+4294959364), Inode bitmap at 277 (+4294959380)
Inode table at 545-797 (+4294959648)
1346 free blocks, 2024 free inodes, 0 directories, 2024 unused inodes
Free blocks:11089-12288, 16239-16384
Free inodes:2025-4048

Block bitmap

Block bitmap records Those blocks that are empty, which blocks are not available, and the block bitmap is also modified after releasing space.

Inode bitmap

Log unused/used inode numbers.

Inode table

Log file attributes and file the actual data placed within the block.

The main records are: File access rights (Read/write/execute), the file's owner and group (owner/group), file size, creation time and state change time, last access time (access times), Last modified (modify time), a flag that defines the attributes of the file, such as setuid; The file really points to

(pointer)-block serial number .

, the structure of recording block number can be divided into 12 direct points, 1 indirect numbers, 1 double indirect numbers, 1 three indirect numbers.

Assuming that the 1 block size is 1k, the total block that an inode can point to is 12+256+256*256+256*256*256=16g.

2.3 Viewing the Inode

ls-i [contents/file] View Inode

783363 Dr-xr-xr-x. 2 root root 4096 Jan 04:26 bin
2 dr-xr-xr-x. 5 root root, Jan 07:45 boot
4 drwxr-xr-x. Root root 3900 Feb 21:01 Dev
130562 Drwxr-xr-x. 101 root root 12288 Feb 21:01 etc
783364 Drwxr-xr-x. 4 root root 4096 Feb 1 05:03 Home
522247 Dr-xr-xr-x. Root root 4096 Jan 07:28 Lib
522244 Dr-xr-xr-x. 9 root root 12288 Jan 04:26 lib64
DRWX------. 2 root root 16384 Jan 07:13 lost+found
783365 Drwxr-xr-x. 4 root root 4096 Feb 21:50 Media
783366 Drwxr-xr-x. 3 root root 4096 Jan 07:47 mnt
522250 Drwxr-xr-x. 3 root root 4096 Jan 23:49 opt
1 dr-xr-xr-x. Root root 0 Feb 20:59 proc
652802 Dr-xr-x---. 3 root root 4096 Feb 7 05:38 Root
783367 Dr-xr-xr-x. 2 root root 12288 Jan 04:26 sbin
1 drwxr-xr-x. 7 root root 0 Feb 20:59 SELinux
522251 Drwxr-xr-x. 2 root root 4096 June (SRV)
1 drwxr-xr-x. Root root 0 Feb 20:59 sys
522242 DRWXRWXRWT. Root root 4096 Feb 21:50 tmp
652805 Drwxr-xr-x. Root root 4096 Jan 07:15 usr
261121 Drwxr-xr-x. Root root 4096 Jan 07:35 var

We can see that the Sys selinux Proc is pointing to the 1th Inode stating that the contents of these three files are exactly identical.

2.4 Viewing file system capacity

1.2 has been introduced to use DF to view the capacity of the file system, there is a command du can see the capacity of the file system, the main difference is that this command will go to the system to find all the file data. Display by default in KB

-m display in mega units

[Email protected] ~]$ du-s
4./.mozilla/extensions
4./.mozilla/plugins
4./.mozilla
4./.gnome2
24.

2.5 Connection File 2.51 hard link

Add a hard link to the/etc/crontab in the root directory

[Email protected] ~]# Ln/etc/crontab./crontab

[Email protected] ~]# ll-i/etc/crontab/root/crontab
132712-rw-r--r--. 2 root root 457 June 3 2011/etc/crontab
132712-rw-r--r--. 2 root root 457 June 3 2011/root/crontab

We can see that the inode of the hard link is the same, the hard-linked two files delete any one, then their inode and block still exist, edit any one file, the final result will be written in the same inode and block.

So we call hard links to be safe! But hard link has two drawbacks 1, cannot cross file system 2, cannot connect to the directory.

2.52 Soft Connect Symbolic link

Soft links We can also become symbolic links, like shortcuts in Windows. It is easy to understand that when the source file is deleted, clicking on the shortcut will show that the file cannot be opened.

We have added a/etc/crontab shortcut to the root directory

[Email protected] ~]# ln-s/ETC/CRONTAB/ROOT/CRONTAB2

A soft connection consumes its own inode and block

[Email protected] ~]# ll-i crontab
132712-rw-r--r--. 2 root root 457 June 3 crontab

-S create a symbolic link

-F Force creation, if the directory already has this file is directly deleted after the new

3. References

"Brother Bird's private dish Linux Basics"

"[email protected" Base Article ~ "# Disk and file system

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.