Linux create large files and use as file system

  In the process of using Linux systems, sometimes encounter such as the size of a disk partition is not enough, resulting in the file system can not write properly. Also, or is the system swap partition is too small, not enough or does not meet the conditions which led to a series of other problems. If our system mounted on a surplus of disk space, so much easier, draw directly on the existing disk partition, format and mount gets the job done. But sometimes limited conditions, there is no surplus of disk space, how to do it? We can in the existing system, to see if there is relatively large space of other partitions, and points out the latter part, still does not affect the normal use to change the zoning system. If this is the case, you can achieve indirect expansion of disk space.

  1, we need to create a large file on the disk partition has surplus space, the size of the file is determined according to your actual needs and the actual size of the partition space to be set aside. Create a large file, you can use the following three commands:

    1.1 dd

      Create a large file of choice would be the dd command, dd is an acronym for device driver, the device can be used to read the contents of the file, and copied intact to the specified location. So is a copy of the file copy on dd essence,

    It forces you to write each data block, and initializes the contents of the file, but the initialization will take a lot of I / O time, and therefore create large files using the dd command speed is relatively slow. Using the dd command to create large files as follows:

      dd if=/dev/zero of=/home/disk0 bs=2G count=1

    This command will create an empty file called disk0 under / home root directory, which uses the following four options:

      if = filename: Specifies the input file name or device name, if omitted, "if = filename", it indicates that reads from the standard input.

      of = filename: an output file name or device name, if omitted "of = filename", it indicates to standard output.

      bs: block specified block size of a single file.

      count: the number of files you want to create.

    1.2 fallocate

      fallocate function is pre-allocated physical space for the files, instead of generating an empty file, fallocate space allocated on the sector of the disk is continuous, it reduces the read and subsequent write disk seek overhead file;

    Direct and rapid space, but do not want to empty the file as "pretend" War = occupy so much space, so as to ensure along with the use of disk space, but does not appear that insufficient disk space segment. Since fallocate

    Not posix standard interfaces, not all file systems are supported by the current mainstream ext4 and xfs are possible.

      fallocate -l 20G /home/disk00

      -l: - length, length distribution specified file, i.e., the size of the file;

      disk00: The name of the file allocation

    1.3 truncate

      lseek and truncate the end location of the file is "extends to" a fixed position and an empty file generated, that is, a sparse file. The document does not take up actual disk space, but logically it looks so big.

    Use ls -l command to view its logical size, that is allocated size you want. Use the du command can see the actual size of disk space occupied. Use od -c command to see the middle of the file is "\ 0" is filled.

      truncate -s 10G /home/disk000

      Specifies the size of the file: -s

  2, obtained in the first step large format files to the file system and mount

    From the foregoing, if the file size is not to be allocated, such as 2G or less, we can preferably dd command, when the file size is very large, fallocate can be used, because the files are not generated truncate command looks

  It is so big, if not necessary, we try not to choose it.

    For example, we use the dd file or fallocate create a 1G size:
    dd the -if = / dev / ZERO of = / Home / disk00 bs = 1024 COUNT = 1000000

    fallocate -l 1G /home/disk00

    2.1 General extended file system space

      2.1.1 Formatting

      With formatted as ext4 file system as an example

      After mkfs.ext4 / home / disk00 ---- the command is executed, the system will prompt "disk00 is not a block special device", directly enter "y" to confirm to continue on the line.

      2.1.2 mount the file system

      Since disk00 is not a block device, so when mounting the file system does not like a regular disk partition, formatted as directly linked in, we need to use pseudo-device loop. Before performing loop mount, we need

    Verify that the system in which the loopback device, and those who are already in use.

      Confirm that the system has a loopback device:

        ls -l / dev / loop *

      Confirm that loopback is already in use:

        cat /proc/mount

      After confirming the above two items, it can be performed in order to hang below:

        mount -o loop=/dev/loop0 /home/disk00 /opt/backup_DB

        -o loop =: the use of loop mode is used as a file system disk partition or hang

      Mount relationship after the loss of the relationship 2.1.3 will mount a write / etc / fstab, to prevent the system is restarted.

        echo "/home/disk00 /opt/backup_DB ext4 defaults,loop 0 0" >> /etc/fstab

    2.2 Mounting swap partition

      2.2.1 format, turning it into a swap file

        mkswap /home/disk00

      2.2.2 enable this swap file

        swapon /home/disk00

      2.2.3 write / etc / fstab, automatically mount system restart

        echo "/home/disk00 swap swap defaults 0 0" >> /etc/fstab

  3, an example of operation

    Below I have a project in the actual operation as an example:

    Our MySQL database installed in the / home partition, the size of 3.5T, because we do not plug magnetic array, there is no separate backup server, so only the backup of the database is stored locally, and to follow the original backup

  Data can not be placed in the same district, we put the backup in the / opt partition, the partition size is 50G. Our library is currently the size of 12G, the amount of data generated every day between 350-400M, roughly every two weeks, the amount of data increases 1G. Backup policy

  A little is a full backup every week, Tuesday to Sunday and are based on a full incremental backup on Monday (This is intended to facilitate recovery). Backup is a full backup retention policy to retain three weeks, delete all equipment before; weekly increments next Monday

  Preparation of packaged compressed whole move to the next / home / old_DB_backup before. As the volume of data continues to increase, probably when the weekly Saturday / opt footprint had already reached nearly 90%, so we consider the / opt temporarily storing incremental backups

  Directory mount a separate partition for expansion, reduce / opt pressure space partition, the program operates as follows:

    3.1 Since the / home directory partition is very large, so select the / home partition as a split object. Considering the subsequent increase in the amount of data, given spatial spreading 30G:

      fallocate -l 30G /home/old_DB_backup/backup_disk/disk0_in

    3.2 Our file system is ext4, it will change the data file formatted as ext4

      mkfs.ext4 /home/old_DB_backup/backup_disk/disk0_in

    3.3 mount a separate space to be added to the incremental backups directory

      mount -o loop=/dev/loop0 /home/old_DB_backup/backup_disk/disk0_in /opt/DB_backup/incremental_backup

    3.4 Mount relations write / etc / fstab

      echo "/home/old_DB_backup/backup_disk/disk0_in /opt/DB_backup/incremental_backup ext4 defaults,loop, 0 0" >> /etc/fstab

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159947.htm