Linux 14th day: (August 25) Linux Disk Management
Chapter Content
Disk structure
Partition type
Managing partitions
Managing File Systems
Mounting devices
Managing Virtual Memory
Device files
I/O ports:i/o device Address
All documents:
Open (), read (), write (), close ()
Device Type:
Block devices: block, Access unit "blocks", disk
Character device: Char, Access unit "character", keyboard
Device files: Linked to a device driver to communicate with the corresponding hardware device
Device number:
Main device number: Major numbers, identifying device types
Second device number: Minor numbers, identifying different devices under the same type
Hard Disk interface type
Parallel:
ide:133mb/s
scsi:640mb/s
Serial:
sata:6gbps
sas:6gbps
usb:480mb/s
Rpm:rotations per minute
Device files
Device file name for disk device:/dev/dev_file
IDE:/DEV/HD
SCSI, SATA, SAS, USB:/DEV/SD
Different devices: A-Z
/DEV/SDA,/dev/sdb, ...
Different partitions on the same device: 1, 2, ...
/DEV/SDA1,/dev/sda5
Hard disk storage Terminology
Head: Head
Track: Tracks
Cylinder: Cylinder surface
SECOTR: Fan area, 512bytes
Using partitioned space
Device identification
Device Partitioning
Creating a file system
Tag File system
Create an entry in the/etc/fstab file
Mount a new file system
Disk partitioning
Why a partition?
Optimizing I/O performance
Implementing disk space Quota limits
Improved repair speed
Isolating systems and programs
Install multiple OS
Using different file systems
Partition
Two ways of partitioning: mbr,gpt
Mbr:master boot record,1982 year, use 32 bit to represent sector number, partition not more than 2T
How to partition: Press Cylinder
0 Track 0 Sector: 512bytes
446bytes:boot Loader
64bytes: Partition Table
16bytes: Identify a partition
2bytes:55aa
4 primary partition, 3 primary partition +1 extension (n logical partition)
GPT Partition
Gpt:guid (Globals Unique Identifiers) partition table supports 128 partitions with 64-bit support for 8Z (512byte/block) 64Z (4096byte/block)
Use 128-bit UUID to indicate disk and partition GPT partition table automatically backed up in head and tail two parts, and CRC check Bit
UEFI (Unified extension Firmware interface) hardware supports GPT, enabling the operating system to boot
GPT partition structure
The EFI section can also be divided into 4 areas: EFI Information area (GPT header), partition table, GPT partition, backup area
List the available disk devices
Graphical Disk Management tool: Click "Applications", "System Tools", "Disk" or execute command gnome-disks
Managing partitions
List block devices
Lsblk
To create a partition using:
FDISK creates an MBR partition, also supports GPT, and can manage up to 15 partitions for a hard disk
Gdisk creating a GPT partition
GNU parted advanced partitioning operations (create, copy, resize, etc.)
partprobe-re-setting the kernel partition table version in memory
Fdisk/dev/sdb
GPT Partitioning tool for GFISK/DEV/SDB class Fdisk
# fdisk-l [-u] [device ...]
Sub-command:
P Partition List
t change the partition type
n Create a new partition
D Delete Partition
W Save and exit
Q Do not save and exit
Partitioning tools Fdisk and Gdisk
Synchronizing partitioned Tables
To see if the kernel has identified a new partition:
# cat/proc/partations
Notifies the kernel to re-read the hard disk partition table
New Partition with
Partx-a-N m:n/dev/device
Kpartx-a/dev/device-f: Force
Delete partition with
Partx-d-N m:n/dev/device
Centos6:–nrn-m
CentOS 5, 7: Using Partprobe
Partprobe[/dev/device]
parted command
Parted operations are in real time, use caution
Usage: parted [options] ... [Device [command [parameters] ...] ...]
Parted/dev/sdbmklabelgpt|msdos
Parted/dev/sdbprint
Parted/dev/sdbmkpartprimary 1 200 (default m)
Parted/dev/sdbrm1
Parted-l
File system
A file system is the method and data structure used by the operating system to explicitly store files on a device or partition; The method of organizing files on a storage device. The software structure that manages and stores file information in the operating system is called a file management system, referred to as
File system.
From a system perspective, a file system is a system that organizes and distributes the space of a file storage device, which is responsible for storing files and protecting and retrieving the files deposited. Specifically, it is responsible for creating files for users, depositing, reading, modifying,
Dump files, control file access, security controls, logs, compression, encryption, etc.
File system type
Linux file systems:: ext2 (Extended file system), Ext3, Ext4, XFS (SGI), Btrfs (Oracle), ReiserFS, JFS (AIX), swap
Disc: iso9660
Windows:fat32, NTFS
Unix:ffs (FAST), UFS (Unix), JFS2
Network File system: NFS, CIFS
Clustered file system: GFS2, OCFS2 (Oracle)
Distributed File systems: Ceph, Moosefs, MogileFS, Glusterfs, Lustre
RAW: Unprocessed or unformatted file system
File System classification
Depending on whether it supports the "journal" feature:
journaled file system: ext3, Ext4, XFS, ...
Non-journaled file system: ext2, VFAT
Part of the file system:
Modules in the kernel: Ext4, XFS, VFAT
User space management tools: MKFS.EXT4, Mkfs.xfs,mkfs.vfat
Virtual file system for Linux: VFS
Pre-check supported file systems: Cat/proc/filesystems
Creating a file system
MKFS command:
(1) # MKFS. Fs_type/dev/device
Ext4
Xfs
Btrfs
Vfat
(2) # mkfs-t Fs_type/dev/device
-L ' LABEL ': Set volume label
Create ext File System
Mke2fs:ext Series file system dedicated management tools
-T {EXT2|EXT3|EXT4}
-B {1024|2048|4096}
-L ' LABEL '
-j: Equivalent to-t ext3
MKFS.EXT3 = mkfs-t ext3 = mke2fs-j = Mke2fs-t ext3
-i#: Create an inode for each number of bytes in the data space, which should not be smaller than the block size
-N #: How many inode to create for a data space
-I one inode record size---4096
-M #: Default 5%, Percentage of total space reserved for administrative staff
-O feature[,...] : Enable the specified attribute
-O ^feature: Turn off specified attributes
File System Tags
Another way to point to a device
Device-Independent
Blkid: Block Device properties Information view
Blkid[option] ... [DEVICE]
-U UUID: Find the corresponding device based on the specified UUID
-L Label: finds the corresponding device according to the specified label
E2label: Manage Label for ext series File system
# E2label DEVICE [LABEL]
Findfs: Finding partitions
Findfs[options] Label=<label>
Findfs[options] Uuid=<uuid>
Tune2fs
TUNE2FS: Reset the values of the EXT series file system adjustable parameters
-L: View the specified file system super block information; Super Block
-L ' LABEL ': Modify volume label
-M #: Fixed percentage of space reserved for administrators
-j: Upgrade ext2 to Ext3
-O: File System Properties enabled or disabled, –O ^has_journal
-O: Adjust default mount options for the file system, –o ^acl
-U uuid: Modifying the UUID number
DUMPE2FS:
-H: View Super Block information (grouping information), partition with group management
File system detection and Repair
Often occurs after the crash or abnormal shutdown
Mount for file system labeled "Dirty"
Fsck:file System Check
Fsck. Fs_type
Fsck-t Fs_type
-A: Auto fix error
-r: Interactive fix error
Note: Fs_type must be the same as the file type on the partition;
E2fsck:ext series file-specific detection and repair tool
-Y: Auto Answer Yes
-F: Forced repair
Mount Mount
Mount: An association between an additional file system and an existing directory of the root file system, which makes this directory a gateway to other file accesses
Uninstall: The process for removing this association relationship
To associate a device with a mount point: mount points
Mount
On uninstall: The device can be used or the mount point can be used
Umount
The existing files under mount point are temporarily hidden after the mount is completed
Mount point directory is generally empty
Mount the file system with the Mount command
Mount method: Mount DEVICE Mount_point
Mount: Displays all devices that are currently mounted by viewing the/etc/mtab file
Mount [-FNRSVW] [-t vfstype] [-O options] Device dir
Device: Indicate the equipment to be mounted;
(1) device files: e.g./dev/sda5
(2) Label:-L ' LABEL ', e.g.-l ' MYDATA '
(3) UUID,-u ' uuid ': for example-U ' 0c50523c-43f1-45e7-85c0-a126711d406e '
(4) Pseudo file system name: Proc, SYSFS, Devtmpfs, Configfs
Dir: Mount point
Pre-existing; Recommended use of Empty directory
The device that the process is in use cannot be uninstalled
Mount Common Command Options
-T Vsftype: Specifies the type of file system on the device you want to mount
-r:readonly, read-only Mount
-w:read and write, read-write Mount
-N: Do not update/etc/mtab, equivalent to #mount
-A: Automatically mounts all devices that support automatic mounts (defined in the/etc/fstab file with auto function in the Mount option)
-L ' LABEL ': Specify mount device with volume label
-U ' UUID ': Specifies the device to be mounted with the UUID
-B,--bind: Bind directory to another directory
To view all mounted devices tracked by the kernel:
Cat/proc/mounts
Mount Common Command Options
-O Options: (option to mount file system), multiple options separated by commas
Async: Async Mode
Sync: Synchronous mode, when memory changes, write disk simultaneously
Atime/noatime: Include Directories and files
Diratime/nodiratime: Access timestamp for directory
Auto/noauto: Whether automatic mount is supported, whether the-a option is supported
Exec/noexec: Whether to support running the application on the file system
Dev/nodev: Whether to support the use of device files on this file system
Suid/nosuid: Suid and Sgid permissions not supported
Remount: Re-mount
RO: Read-only
RW: Read and Write
User/nouser: Whether a normal user is allowed to mount this device, the default administrator can mount
ACL: Enable the ACL feature on this file system
Defaults: Equivalent to RW, suid, dev, exec, auto, nouser, async
Uninstall command
To view mount conditions:
#findmntMOUNT_POINT
To view the process that is accessing the specified file system:
#lsofMOUNT_POINT
#fuser-V Mount_point
Terminates all processes that are accessing the specified file system:
# fuser-km Mount_point
Unloading:
# Umountdevice
# Umountmount_point
Mount points and/etc/fstab
Configuration file System Architecture
Used by Mount, FSCK, and other programs
Preserves file system system when system restarts
You can use the file System volume label on the device bar
Use the Mount-a command to mount all file systems in the/etc/fstab
File Mount configuration file
/etc/fstab each row defines a file system to mount;
Device or pseudo file system to be mounted
Mount point
File system type
Mount Options
Dump frequency
Self-Test order
Device or pseudo-file system to mount:
Device file, LABEL (label= "), UUID (uuid=" "), pseudo file system name (proc, SYSFS)
Mount option: Defaults
Dump frequency: 0: Do not make backup
1: Daily dumps
2: Dump every other day
Self-Test sequence: 0: no self-test
1: first self-test; usually only Rootfs 1
Working with swap files and partitions
Swap partition is a complement to system RAM
Basic settings include:
Create a swap partition or file
Write a special signature using Mkswap
Add the appropriate entry in the/etc/fstab file
Activating swap space with swapon-a
Mount Swap Partition
Enabled: Swapon
Swapon[option] ... [DEVICE]
-A: Activates all swap partitions;
-P Priority: Specify precedence
/etc/fstab:pri=value
Disable: Swapoff[option] ... [DEVICE]
Priority of Swap
The user can assign a 0 to 32767 priority to a swap
If the user does not specify, then the core will automatically assign a priority to the swap, which starts at 1, and each addition to a new swap with no user-specified priority will give the priority one reduction.
The default priority for the swap that is added first is higher, unless the user specifies a priority, and the user-specified priority (a positive number) is always higher than the priority specified by the core default (negative).
Optimized performance: Distributed storage, high-performance disk storage
Moving Media
Mounting means making a foreign file system look like part of the home directory tree
Media must be mounted before access
When removed, the media must be unloaded
By default, non-root users can mount only certain devices (optical discs, DVDs, floppy disks, USB, and so on)
Mount points are usually under/media or/mnt
Using discs
Automatically start Mount/run/media/<user>/<label> in a graphical environment
Otherwise, it must be manually mounted.
mount/dev/cdrom/mnt/
Eject command to unmount or eject a disk
Create an ISO file
Cp/dev/cdrom/root/centos7.iso
Mkisofs-r-o/root/etc.iso/etc
Burning discs
Wodim–v–eject Centos.iso
Mounting USB Media
Detected by the kernel as a SCSI device
/dev/sdax,/dev/sdbx, or similar device files
Automatically mounts in a graphical environment
Icon created in the [Computer] window
Mount in/run/media/<user>/<label>
Manual mount
Mount/dev/sdb1/mnt
Common tools
Memory Space usage Status:
free [OPTION]
-M: in megabytes
-G: in gigabytes
Viewing tools for information such as file system space usage:
Df[option] ... [FILE] ...
-H in 1000 units
-T File system type
-h:human-readable
-i:inodesinstead of Blocks
-P: Output in POSIX-compatible format
To view the overall space consumption status of a directory:
Du [OPTION] ... DIR
-h:human-readable
-s:summary
Tool DD
DD command: Convert and copy a file
Usage:
DDIF=/PATH/FROM/SRC Of=/path/to/dest
Bs=#:block size, copying unit sizes
count=#: How many BS do you copy?
Of=file writes to the named file instead of to the standard output
If=file read from the named file instead of the standard input
BS=SIZE Specifies the block size (both IBS and OBS)
Ibs=size read size byte at a time
Obs=size write a size byte at a time
Cbs=size One conversion size a byte
Skip=blocks ignores blocks of IBS-sized blocks from the beginning
Seek=blocks ignoring blocks of obs size from the beginning of a block
Count=n Copy only N records
Conv=conversion[,conversion.] Convert the file with the specified parameters.
Conversion parameters:
ASCII conversion EBCDIC to ASCII
EBCDIC convert ASCII to EBCDIC
The block is converted to a cbs-length record, and the insufficient portions are padded with spaces.
Unblock replaces CBS length with a new line at the end of each line
LCase converting uppercase characters to lowercase characters
UCase converting lowercase characters to uppercase characters
Swab Swap bytes per pair of inputs
NoError does not stop when an error occurs
Notrunc Not truncated output file
Sync fills each input block into IBS bytes, and the less part is padded with empty (NUL) characters
Disk Copy:
Ddif=/dev/sdaof=/dev/sdb
Backup MBR:
ddif=/dev/sdaof=/tmp/mbr.bakbs=512 count=1
Destroy the bootloader in the MBR:
Ddif=/dev/zero of=/dev/sdabs=64 count=1 seek=446
There is a large binary file with 2K Filea. Now want to start reading from the 64th byte position, the size to be read is 128Byts. Another fileb, want to read the above 128Bytes write to the beginning of the 32nd byte position, replace 128Bytes
, how can I achieve it?
#ddif =fileaof=filebbs=1 count=128 skip=63 seek=31 conv=notrunc
Backup:
DD IF=/DEV/SDX OF=/DEV/SDY
Back up the local/DEV/SDX full disk to the/DEV/SDY
DD IF=/DEV/SDX Of=/path/to/image
To back up the/DEV/SDX full data to the image file of the specified path
DD IF=/DEV/SDX | Gzip >/path/to/image.gz
Backup/DEV/SDX full data and compress with gzip tool to save to specified path
Recovery:
DD If=/path/to/image OF=/DEV/SDX
Restore the backup file to the specified disk
gzip-dc/path/to/image.gz | DD OF=/DEV/SDX
Restores a compressed backup file to a specified disk
Copy memory data to hard disk
DD If=/dev/mem Of=/root/mem.bin bs=1024
Copy the in-memory data to the Mem.bin file in the root directory
Copy ISO image from disc
DD If=/dev/cdrom Of=/root/cd.iso
Copy the disc data to the root folder and save it as a Cd.iso file
Destroying disk data
DD If=/dev/urandom OF=/DEV/SDA1
The use of random data to populate the hard disk, in some necessary occasions can be used to destroy data. After you do this, the/dev/sda1 cannot be mounted, and the Create and copy operations cannot be performed.
Get the most appropriate block size
DD If=/dev/zero bs=1024 count=1000000 of=/root/1gb.file
DD If=/dev/zero bs=2048 count=500000 of=/root/1gb.file
DD If=/dev/zero bs=4096 count=250000 of=/root/1gb.file
DD If=/dev/zero bs=8192 count=125000 of=/root/1gb.file
The best block size for the system can be determined by comparing the command execution time shown in the output of the DD instruction.
Test drive Read and write speed
DD If=/root/1gb.file bs=64k | DD Of=/dev/null
DD If=/dev/zero of=/root/1gb.file bs=1024 count=1000000
The read/write speed of the test drive can be calculated by the execution time of the last two command outputs.
Repairing the hard drive
DD IF=/DEV/SDA OF=/DEV/SDA
When the hard disk is not used for a long time (for example, 1, 2 years), a degaussing point is generated on the disk. When the heads read these areas, they encounter difficulties and can cause I/O errors. When this condition affects the first sector of the hard disk, it may cause the
Waste. The above command may bring the data back to the dead. And the process is safe and efficient.
Linux 14th day: (August 25) Linux Disk Management