Linux operation and maintenance basis - Disk Management

Device file
everything is a file: open (), read () , write (), close ()
device types:
character file: char, c, character access unit, a keyboard
Block equipment: block, b, block access unit, disk
device file: / dev / next linked to a device driver, and thus can be a hardware device corresponding communication
device numbers: major number (identification device type), the minor number (identification of the different devices of the same type)
hard drive interface type:
parallel: IDE, SCSI
port: SATA, SAS, USB

Cp -a copy device file
example: Create a character device at / data: mknod / data / zerofile c 1 5
creates a 10M test file: IF = dd / Data / ZERO test of BS = 1M = 10 COUNT =
the hexdump test -C View binary, which are all 0

CHS dimensional disk
sector sector
head track track head
cylinder cylinders (cylinders equal to the number of tracks)
6bit sector 8bit 10bit head track
using 24bit bit addressing
a maximum addressable space 8GB

LBA (Logical Block Addressing)
LBA is an integer, by converting into a specific address in CHS format through the disk
using 48-bit addressing bit LBA
maximum addressable space 128PB

Use Disk
1. Partition
2. Create a file system, format
3, mounting: distribution directory name

Partition:
Why partition:
to optimize I / O performance
for disk quota limit
increase rate of repair
isolation systems and processes (e.g., database data and logs kept separately in different partitions)
installed OS plurality of
different file system

Two kinds of partition: the MBR, the GPT
the MBR (Master Boot Record): master Boot Record, 32 indicates the number of sectors, partition does not exceed 2T
0 track 0 sector: 512bytes
446bytes: Boot Loader boot the system
64bytes: partition table, identification 16Bytes a partition
2bytes: 55AA, the hard disk partition flag (the hexdump -C / dev / SDA 512 -n -v)
. 4 partitions, a main partition 3 extended partition +1 (N logical partitions)

GPT
the GPI: the GUID (the Globals of Unique Identifiers), Partition Table 128 partitions 64-bit support 8Z (512Byte / block), and 64Z (4096Byte / block)
using 128 UUID (univeersally Unique Identifier) represents the disks and partitions GPT partition tables, automatic backup in two head and tail, and a CRC check bit
UEFI (unified Extensible firmware interface) hardware support GPT, the operating system startup
has to call uuid file on disk: cat / etc / fstab
graphical interface to visualize disk management tools: gnome-disk

BIOS + MBR and UEFI + GPT
boot --- bios --bios initialization self-test --- --- enter the system boot operating system
boot initialization ---- ---- UEFI boot the operating system into the system ---

Manage Partition
lists block devices: lsblk -f file system types can be listed
create a partition used:
the fdisk create MBR partition
centos6 logical partition should be noted that the slot division problems, centos6 is cylindrical units, centos7 in units of sectors is
created gdisk GPT partition
parted advanced partitioning operations
echo -e "n \ n \ n + 2G \ nw" | fdisk / dev / sda &> / dev / null

partprobe- reset the kernel partition table version memory
centos7 synchronous memory partition table: partprobe
centos6 synchronous memory partition table: partx -a / dev / sda (only suitable new zoning use)
centos6 synchronous memory delete partitions: partx -d - -nr 6-8 / dev / sda (6-8 is the partition number)

parted advanced partition operations (Run interactively), all operations with immediate effect, be careful to use
parted / dev / sda print displays disk information
parted / dev / sdb mklabel msdos create an MBR partition
parted / dev / sdb mklabel gpt create a GPT partition
to create a partition: parted / dev / sdb mkpart primary 1 1000 ( in MB)
to view the partition: parted / dev / sdb print or parted -l / dev / sdb
delete the partition: parted / dev / sdb rm 1
on the operation to remove a disk partition: dd if = / dev / zero of = / dev / sdb bs = 1 count = 512
See confirmed: hexdump -C / dev / sdb -n 512

fdisk -l read on the hard disk partition table, lsblk partition table is read from the memory

64-byte partition table backup: dd if = / dev / sdb of = / data / partition bs = 1 count64 skip = 446
destroyed 64-byte partition table: dd if = / dev / zero of = / dev / sdd bs = 1 count = 64 seek = 446

Backup partition table: IF = dd / dev / SDA of = / Data / mbr_bak COUNT = 512. 1 BS =
the hexdump -C / Data / or mbr_bak hexdump -C / dev / sda -n512 -v
to copy files elsewhere: scp / data / mbr_bak 192.168.30.233:/data/
destroy the partition table: dd if = / dev / zero of = / dev / sda bs = 1 count = 512
the destruction of the partition table can be viewed: ls / dev / sda *, cat / proc / partitions
destroy the last partition flag 2: dd if of = / = / dev / zero dev / sdd bs = 1 count = 2 skip = 510 seek = 510
with destroy the partition table the same effect as
the device does not restart conditions: dd if = / data / centos7_mbr_bak of = / dev / sda bs = 1 count = 512
restart, then there are two ways to fix:
First, remove the hard disk to other devices to repair and restore the partition table
two, into rescue mode recovery:
the manual card with address: ifconfig ens33 192.168.33.6/24

File system:

Operating system responsible for managing and storing software file structure information is called a document management system
from a system perspective, the file system is a file storage space is organized and allocation, file responsible for file storage and deposit protection and retrieval system, in particular, the establishment of user files, stored, read, modify access, dump files, control files, security control, logging, compression, encryption,
view the current system supported file systems: ls / lib / modules / uname -r/ Kernel / FS
Linux file systems: ext2 (Extended file system), ext3, ext4, xfs (SGI), btrfs (Oracle), reiserfs, jfs (AIX), swap
CD: iso9960
Windows: FAT32, exFAT, NTFS
network file system: NFS, CIFS
cluster file systems: GFS2, OCFS2 (oracle)
distributed file system: fastdfs, moosefs, MogileFS, Luster
RAW: untreated or unformatted file generated by the system

File classification system
based on whether it supports journal feature:
journaling file systems: ext3, ext4, xfs ...
non-journaled file systems: ext2, vfat
part of the file system:
kernel module: ext4, xfs, vfat
by vfs passed to the kernel
user space management tools: mkfs.ext4, mkfs.xfs, mkfs.vfat

free command inside buffers and cached,
Buffer (buffer) usually do write buffer memory used, cache is a read cache on the disk

Check the file system:
blkid / dev / sdb1
-U uuid, -L label name
by UUID check the corresponding partition:
blkid -U cat /etc/fstab|grep -E /data|grep -oE "[a-z0-9-]+"|head -1
blkid -U sed -nr '/data/s@^(.*)=([0-9a-z-]+).*@\2@p' /etc/fstab
df -T to view the current file system with the system, you can only display the mounted
creation file system automatically generates uuid, uuidgen uuid may be generated (128-bit)
change UUID: tune2fs -U 'uuid =' ( or · uuidgen ·) / dev / sdb2
after recommendation script represented by partition uuid

Create the file system: mkfs.ext4 -t ext4 or the mkfs
-b {1024 | 2048 | 4096} specify the block size
-i #: Create a data space for the inode number of bytes each; this size should not be smaller than the size of the block
-N #: inode number to create the specified partition
-l a footprint the size of the inode (123 --- 4096)
-O specifies the characteristics
-m #; default percentage of 5% of the total space is reserved space manager, -m 0.1 ( 1/1000)
or mkfs.ext4 / dev / sdb2 1024 -b -m 0.1
mke2fs / dev / sdb1 create an ext2 file system or mke2fs -t ext2 | ext3 | ext4 / dev / sdb2 creates ext4 file system
mkfs -t ext4 / dev / sdb1 equivalent to mkfs.ext4 / dev / sdb1
sdb1 create band / data / tag mysql partition mkfs.ext4 -L / data / mysql / dev /
specified when mkfs -t ext4 / dev / sdb1 -b 1024 specify the file system block size
mkfs command will be prompted to
create a partition by way of a script: echo -e "n \ np \ n \ n \ n + 2G \ nw \ n" | fdisk dev / sdbc
multi-line redirection: fdisk -l / dev / sdc << EOF

tune2fs: file system reset ext series adjustable parameter value
Centos6 view ext series file system metadata (ext series can only view): the tune2fs -l / dev / sdb2
-L 'the LABEL': modifying label
or modify e2label ext series label: e2label / dev / sb2 / data / mysql read / the Data / MySQL
-O file system attributes to enable or disable, -O has_journal ^
-o adjust the default file system mount options, -o acl ^
-m # : modify the space reserved for the administrator's percentage
-U specified UUID: tune2fs -U uuidgen/ dev / sdb2
tune2fs -c # / dev / sdb2, change the maximum number of mounted trigger fsck
tune2fs # -C / dev / sdb2, change the current hang Uploads
Centos6 change the default mount options: tune2fs -o acl / dev / sdb1
Centos6 default partition has acl function, no manually created
after the implementation of tune2fs -o acl / dev / sdb2 must be unmounted and then remounted to take effect,
cancel acl: tune2fs -o ^ acl / dev / sdb2
or mount -o acl / dev / sdb1 / data / sdb1
not cancel case mounted canceled ACL: Mount the noacl -o, the remount / dev / sdb1 / Data / sdb1
Centos7 The default file system has acl function
findfs: find the partition
Corresponding apparatus for known UUID:
findfs UUID = 8e8e4838-15c0-400d-8349-e203659c41c5
BLKID 8e8e4838-15c0-400d -U-8349-e203659c41c5

See Centos6 block device (super block): dumpe2fs / dev / sda1, for ext series, the odd-numbered blocks 0-10 are backup block
Centos7 See block device: xfs_info / dev / sda1
page display block information: dumpe2fs / dev / sdb2 | less
dumpe2fs -h / dev / sdb2

Repair the file system
fsck can fix automatically repair the file system -a, -r interactive repair, actually invoke e2fsck
Clean indicates that the file system is normal, noclean file system problems

Destructive operations: IF = dd / dev / ZERO of = / dev / sdb1 BS = 1M COUNT = 20 is
the tune2fs -l / dev / sdb2 view, the
repair partition must ensure that the file system is not in the mounted state, and the fsck is e2fsck ext file system repair,
XFS file system repair with fsck can do, will automatically call xfs_repair command
fsck / dev / sdb1 -y can repair damaged file system
e2fsck / dev / sdb1 -f Force fixing

Mount mount
the device to the associated folder to use
a mount point (directory) with a time device can only mount a
partition can mount multiple folders directory, see things the same, avoid circular mount
mount point folder is best to empty folders, otherwise you can not access the file before, causing junk files
mount point directory must exist, you can write to mount the device name, you can also write labels, you can also write uuid
ordinary users can not mount

See Mount:
DF, Mount, CAT / etc / mtab (updated automatically mount case), cat / proc / mounts kernel recording apparatus mounted
See ext4 file system metadata: tune2fs -l / dev / sdb2, there Mounting the number of
tune2fs -c # / dev / sdb2, change the maximum number of mounted trigger fsck
tune2fs # -C / dev / sdb2, change the current number of mount
mount command
-a automatically mounted read / etc / fstab configuration file
- r mounted read only, read-write mount -w (default RW),
-n: not updated / etc / mtab, mount invisible, hidden mount, centos7 ineffective (pointing file in proc)
-L ' LABEL ': volume label specified mount device
-B (- bind) binding directory to another directory
-o followed by mount properties:
the async: asynchronous mode, the default
sync: sync mode, memory changes, while writing to disk
atime / noatime: contains directories and files, website files can be set
diratime / nodiratime: directory access timestamp
auto / noauto: whether to support the auto-mount, whether to support the -a option
exec / noexec: whether to support the run on the file system applications
dev / nodev: whether to support the use of device files on the file system
suid / nosui d: whether to support the suid and sgid permissions
remount: remount
ro: read only, rw: read-write
user / nouser: whether to allow ordinary users to mount the device, / etc / fstab use
acl: acl enable function on this file system
loop: loop devices using
_netdev only when the network is available to mount the device on the network, such as NFS system
defaults: equivalent to rw, suid, dev, exec, auto, nouser, async

Change partition label name: e2label / EV / sdb2 / / the Data / sdb2
Mount / dev / sdb2 / the Data / sdb2 or LABEL = Mount / the Data / sdb2 / the Data / sdb2
Mount command to view the mount point, or cat / etc / mtab (the file is automatically updated, recording the current device mounted case)

See who is using directory: lsof / data / html or fuser -v / data / html
forcibly kicked out: fuser -km / data / html, bulletin notice before terminating starting
to unmount umount / data / sdb2 (write label name , mount point will do)

Read-only mount: mount -r / ev / sdb1 / data / sdb1
does not cancel the case mounted remount change the settings: mount -o remount, RO / the Data / sdb1
Centos6 useful and useless to hide 7 mount: mount -n / ev / sdb1 / data / sdb1, can only be viewed by cat / proc / mounts

Directory mount directory: mount -B (- bind) / data / sdb1 / mnt / sdb1
see if the folder directory is mounted status: findmnt / the Data / sdb2
※ see if the directory is the mount state
findmnt / data / sdb2 & > / dev / null || mount / dev / sdb2 / data / sdb2

Create their own block device mount directories:
dd = IF / dev / ZERO of = / the root / 1M COUNT = P1 = 100 BS, mkfs.ext4 P1, P1 BLKID
Mount -o Loop / the root / P1 / mnt, need not Centos7 plus -o loop
you want to permanently save, you need to write / etc / fstab inside, you can not add a uuid, you need to use the device name:
/ root / p1 / mnt / p1 ext4 Loop 0 0

Loop
losetup -a device can view the file and the system automatically assigns the mapping between loop # of
centos6 can mount up to eight blocks of the device, the other may need to add,
the mknod / dev / b loop8 7 8, create their own loop8 equipment
specified loop8 needs artificial association, losetup / dev / loop8 p2
if necessary a lot, it is necessary to change the kernel file, vim /boot/grub/grub.conf, the kernel line max_loop = 100 added at the end
to take effect need to restart the device

Folders mount a folder to save the file:
/ etc / fstab should read: / boot / mnt / boot none bind 0 0, then -a Mount
※ want to permanently mount the CD, can only write device name:
eg: / dev / sr0 / mnt / cdrom iso9660 defaults 0 0, then mount -a mount all the file systems in the / etc / fstab
※ / etc / fstab
can partition linked to a folder, the folder can be linked to a folder to be hanging file folders
can also mount network resources to the directory, the mount that is associated with
the mount will fail after the restart, mount command is a temporary mount
if the entry into force must save the file: / etc / fstab
must first change the configuration file before backup: cp /etc/fstab{,.bak}
the last two digits 1 represents a first back up one day, to check whether the second power 0 is not checked, the other order is to check
if the last non-0, the corresponding device does not exist UUID will cause the system to not start, does not affect the mount point does not exist
after editing and saving the file, run mount -a post will automatically mount
if you modify the fstab inside the parameters, such as adding acl, unmounted need to be linked
not take effect unmounted : mount -o remount / data / sdb2

※ experiment: modify / etc files under the centos6 fstab / unbootable system, enter the root password
to modify the file, because the file system is read-only, you need to enter the command to modify:
Mount -o rw, remount /, you can modify or directly uuid partition name, or to the last digit 0
※ iso image produced using the optical disk (centos6) :( dvd1, dvd2 1 and II)
1, two first optical disc are linked to the DVD 1 directory, the DVD2, first copy of a disc content to the new directory (dvd), then the second copy of the optical disc Packages * .rpm dvd directory to directory Packages;
2, combined TRANS.TBL, cat dvd1 / TRANS.TBL dvd2 / TRANS.TBL | the Sort> DVD / TRANS.TBL
3, make the iso file: mkiso -r -o Centos-6.10- x86_64.iso dvd /, and then mount the iso file (-r is reading all file permissions) mount -o loop Centos -6.10-x86_64.iso mnt / cdrom, centos7 no need to add Loop -o
. 4, the original backup yum profile, create a new profile point to the iso file
5, yum update source: Clean All yum, yum repolist
Home under the root ※ hanging folder to a separate partition
to hang at the root before / home linked to a single The only partition
1, data backup
2 disk partitions, large enough
3, create a file system: mkfs.ext4 / dev / sdb1 -L / Home
4, create a temporary directory / mnt / home, mount the partition to mount / dev / sdb1 / mnt / home
5, init 1 switches to single-user mode, runlevel can view confirm, or notify the user stops using the file, so the file is missing
files under 6, copy / Home, cp -av / Home / ** / mnt / Home
7, make sure the directory have all been migrated, then delete the original home directory
8, vim / etc / fstab, (! blkid / dev / sdb1 generate uuid), added to the file, and then save the entry into force -a Mount
9, the init 3 to switch to the normal mode, you can switch accounts test
10, the temporary directory is unmounted, umount / mnt / home, rm -rf / mnt / home

swap

Check swap usage: free default kb units, free -m, in MB, or free -g, (legible way) -h
proportion of real memory and swap is: if the actual memory is less than 4G: 2: 1 , with more than four G 8 G
the swapon -s (CAT / proc / swaps) can swap partition being used to view and check the priority, priority,
we need to change the / etc / fstab swap partition inside the desired increase in Add later defaults pri = 100, such as defaults, pri = 100
need to swapoff / dev / sdb1 and then swapon -a become effective
buffer (buffer) usually do write buffer memory used, cache is a read cache on the disk
test: dd if = / dev / zero of = / dev / null bs = 2G count = 1

Increasing the swap area, preferably a separate disk
1.fdisk / dev / sdb, the need to change the partition ID, selected from t, then select L, is selected from 82, and then save the exit w
2. Create the file system: mkswap / dev / sdb1 swap_sdb1 -L
. 3, and permanently mounted storage, / etc / fstab,
the UUID = XXX Defaults the swap the swap, PRI 100 0 = 0
. 4, need to run into effect: swapon -a

Swap area increases, with the file to achieve
dd IF = / dev / ZERO of = / = the swapfile BS 2024M / 2G. 1 COUNT =
2. Create the file system: mkswap / swapfile -L swap_file, see BLKID / the swapfile
. 3, and permanently mounted save, / etc / fstab,
/ swapfile swap swap Defaults 0 0
4, to take effect you need to run: swapon -a

Cancel the operation:
1, canceled swap partition: swapoff / dev / sdb1, swapon -s equivalent to the cat / proc / swaps
cancel the swap file: swapoff / swapfile, swapon -s view
2, / etc / fstab increased inside two rows deleted off
3, rm -f / swapfile or delete partitions, fdisk / dev / sdb, d , w

df view the file system footprint and other information (default in K)
-H with 10 units
-h in a readable way
-T displays the file system
-i display the node number
-P to POSTIX standard output format
du overall occupancy view a directory space (default in K)
-s is a total size, without a show
-h is displayed in human-readable
--max-depth = # specify the maximum directory hierarchy
dd: Convert a and File Copy
IF = file read from the named file rather than from the standard input
file of name = file is written, instead of standard output
ibs = size a read of size byte
OBS a write size of size = byte
BS block size = size, specified block size
cbs = size in a conversion of size byte
Skip = bolcks blocks from the start block of a negligible size ibs
seek = blocks two blocks from the start to ignore the fast obs size
count = n Copies n bs

Backup / dev / sdx overall data, using gzip compression, to save a specified path
dd if = / dev / sdx | gzip> /path/to/image.gz

Guess you like

Origin blog.51cto.com/14451011/2429155