File system operations and Disk Management

Source: Internet
Author: User
Tags virtual environment

Simple file system Operation 1. View disk and directory capacity use the DF command to view the capacity of a disk

In general use cases, we are only concerned with the environment rootfs or the physical host/dev/sda2

"Rootfs": (Root File system) It is Ramfs (Ramfs is a very simple Linux filesystem used to implement the disk caching mechanism as a dynamically resizable RAM-based file system) or a special instance of TMPFS, which acts as a system startup After the kernel is loaded into memory, a temporary file system is placed before the real disk is mounted. The usual host will be replaced with the file system on disk after the system starts, but only one rootfs will exist in some embedded systems, and the system running the shared host resource in the virtual environment may be used this way.

The/dev/sda2 on the physical host is the partition that corresponds to the host hard disk, the following numbers represent the area code, the letter A in front of the number indicates the first few hard disks (or possibly removable disks), and if you have more than one hard disk on the host, you may also/DEV/SDB,/DEV/SDC these disk devices will be The dev directory is in the form of a file.

Then you'll see the strange thing about "1k-blocks", which shows the capacity as a disk block size, followed by the corresponding used and usable capacity in block size, which we show in a way that we can understand:

To view the capacity of a directory using the du command
$ du
# Plus '-h ' parameter to display in an easier-to-read manner

-dparameter specifies the depth of the view directory

# View information for Level 1 directories only

duThe (estimate file space usage) command and df (report file system disk space usage) use only one word, so be careful not to confuse it, you can get a full description of the command from the Man manual, and you won't confuse it.

Ii. Simple Disk Management 1. Introduction to creating a Virtual Disk DD command (some instructions from the DD (Unix) wiki))

ddcommand is used to convert and copy files, but its replication differs from cp . Before mentioning a very important point about Linux, everything is files , on Linux, hardware device drivers (such as hard disk) and special device files (such as /dev/zero and /dev/random ) are like ordinary files, as long as the respective drivers to achieve the corresponding function, DD You can also read or write to these files. This dd can also be used in tasks such as backing up the boot sector of the hardware, obtaining a certain amount of random data, or empty data. ddprograms can also process data during replication, such as converting a byte order, or swapping between ASCII and EBCDIC encodings.

ddCommand-line statement differs from other Linux programs because it has a command-line option format 选项 = , rather than a more standard -- 选项 or - 选项 = . The dd default is read from the standard input and written to the standard output, but can be changed with the options (input file if , input files) and of (output file).

Let's try the trial dd command. Read user input to standard output or a file from standard input:

# Output to File

The above command reads the user input from the standard input device (the default, so it can be omitted) and then outputs it to the test file, bs (block size) for specifying the block size (the default unit is Byte, or it can be specified as ' K ', ' M ', ' G ', etc.) to count specify the number of blocks. As shown, I specify to read only 10 bytes of data, when I enter "Hello Shiyanlou" and then add a space to enter a total of 16 bytes (an English character in one byte) content, obviously over the set size. du cat The actual content of the write completion file that you see with the and commands is actually only 10 bytes (the black-bottom percent sign means there is no line break), and the rest of the input is truncated and kept in standard input.

In front of the dd copy can also achieve data conversion, then a simple example: the output of the English characters converted to uppercase and then write to the file:

You can man see all the other conversion parameters in the document.

To create a virtual image file using the DD command

/dev/zerocreate an empty file with a capacity of 256M from the device:

This file is then formatted (written to the file system), where we have to learn a new command (exactly a set of) to complete this requirement.

Format the disk with the MKFS command (we are here to create the virtual disk image ourselves)

You can enter the command line sudo mkfs and then press the Tab key, you can see a lot of mkfs-prefixed commands, these different suffixes are actually representing different file systems, can be formatted with the Mkfs file system.

We can simply use the following command to format our vdisk image as a ext4 file system:

You can see that the actual MKFS.EXT4 is using MKE2FS to do the formatting work. MKE2FS has a lot of parameters, but we don't always format the disk to play, so just master this basic usage, and when you have special needs, check out the man document.

If you want to know what file systems Linux supports you can enter ls -l /lib/modules/$(uname -r)/kernel/fs view.

Mount a disk to a directory tree using the Mount command

Before a user opens a file on a Linux/unix machine, the file system that contains the file must first mount the action, at which point the user executes the mount instruction to mount the file system. It is usually used on USB or other removable storage devices, while the root directory needs to remain mounted at all times. And because the Linux/unix file system can correspond to a file and not necessarily a hardware device, you can mount a file containing the file system to the directory tree.

The mount instruction of the Linux/unix command line tells the operating system that the corresponding file system is ready to be used, and that the file system corresponds to a specific point (called a mount point). Mounted files, directories, devices, and special files can be used by users.

Let's use it mount to see the file system that the host has mounted:

Each row in the output represents a device or a virtual device, each line is preceded by a device name, followed by a mount point, followed by the file system type, followed by mount options (such as the ability to mount in a read-only manner when mounted, and so on).

So how do we mount the real disk to the directory tree, the mount general format of the command is as follows:

Some common operations:

We are now going directly to mount the virtual disk image we created to the /mnt directory:

# You can also omit mount types, and many times mount will automatically recognize
Uninstalling mounted disks using the Umount command
# command format sudo umount mounted device name or mount point, such as:

In addition to the loop device, you may have a lot of questions, then please look at the following from Wikipedia/dev/loop description:

In UNIX-like systems,/dev/loop (or VND (Vnode disk), Lofi (circular file interface)) is a pseudo-device that allows files to be accessed as if they were block devices.

Before use, the loop device must be associated with a file on an existing file system. This association will be provided to the user with an application interface that will allow the file to be treated as a block special file (see Device file System) for use. Therefore, if the file contains a complete file system, the file can be mounted as if it were a disk device.

This device file is often used for disc or disk mirroring. A file that contains a file system is mounted by a cyclic mount to allow access to the files in the file system. These files will appear in the mount point directory. If the mount directory itself has files, these files will be banned after being mounted.

Partitioning a disk using Fdisk

You must have a physical disk that cannot be used on a virtual disk.

Some information about the disk on the host is shown at the beginning of the output, including the number of capacity sectors, sector size, I/o size, and so on.

Focus on the middle partition information,/dev/sda1,/dev/sda2 the main partition installed Windows and Linux operating system,/DEV/SDA3 for the swap partition (can be understood as virtual memory),/DEV/SDA4 as an extended partition containing/dev/ SDA5,/DEV/SDA6,/DEV/SDA7,/DEV/SDA8 four logical partitions because there is a gap between several partitions on the host, there are no aligned boundary sectors, so the partitions are not completely contiguous.

Before we proceed, we should first plan our partitioning scheme, where I will create a 30M primary partition with a virtual disk image of 128M (available 127M or so) for the remaining portion of the extended partition containing 2 approximately 45M logical partitions.

After the operation is completed, enter the p following view results:

Finally, do not forget to enter the w write partition table.

Using the Losetup command to establish an association between a mirror and a loopback device
# If you are prompted for a busy device, you can also use a different loopback device, "ls/dev/loop*" See all loopback devices

Then use the mkfs format of each partition (we are formatting the entire virtual disk image file or disk), but before formatting, we also have to establish a virtual device mapping for each partition, using the kpartx tool, we need to first install:

And then it's formatted, and we'll format all of it as EXT4:

After formatting is complete /media , create a new four empty directory in the directory to mount the virtual disk:

And then:

File system operations and Disk Management

Related Article

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.