1.7 File system operations and Disk Management (learning process)

Source: Internet
Author: User
Tags virtual environment

Introduction to File system operations and Disk Management experiments

The file system operation of this section of the experiment is very simple and contains only a few parameters of several commands, but mastering these will also help you in the process of learning about other courses in Linux and the lab building.

Because this course is positioned as a primer, as soon as possible, so there is no intention to involve too much theoretical content, the earlier omitted on the Linux file system of some basic knowledge, but also because we are an online experimental environment, so also avoid a few but very important hardware content, We can only expect users to be able to improve their self-learning ability of the mentality of their own to supplement the relevant knowledge.

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

In the lab building environment you will see the following output:

But it will be more like this on the actual physical host:

In general usage, we are more concerned with the content of the first line, that is, in the environment rootfs or on 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 is booted, only one rootfs is present in some embedded systems, or the system that is running in the virtual environment to share the host resource in the context of the situation we are currently experiencing may also 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 will see "1k-blocks" This strange thing, it means to display the capacity as a disk block size, followed by the corresponding block size of the used and usable capacity, before you know the Linux file system This is not the first, we are in a way you should understand to show:

$ df -h

Now you can use the command to see how your host disk is being used. As for the mount point, if you remember the contents of the Linux tree structure described in section 4th above, then you should be able to understand the concept of the mount, and not repeat it here.

To view the capacity of a directory using the du command

This command has actually been used many times before:

# 默认同样以 blocks 的大小展示$ du # 加上`-h`参数,以更易读的方式展示$ du -h

-dparameter specifies the depth of the view directory

# 只查看1级目录的信息$ du -h -d 0 ~# 查看2级$ du -h -d 1 ~

duThe (estimate file space usage) command and df (report file system disk space usage) use only one word, and first you want to be careful not to confuse it so that you can get a full description of the command from the man manual like I do, The full name will not be confused.

Second, the simple Disk Management The following commands are dangerous, improper operation may lose your personal data, beginners recommend operating in a virtual environment

Typically, this section should directly mount the Unmount disk, how to format the disk, and how to partition it, but as you can see, there is nothing in our environment for you to hang, and nothing to give you the lattice and points, so first we will create a virtual disk for subsequent practice operation

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 from and/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:

# 输出到文件$ dd of=test bs=10 count=1 # 或者 dd if=/dev/stdin of=test bs=10 count=1# 输出到标准输出$ dd if=/dev/stdin of=/dev/stdout bs=10 count=1

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:

$ dd if=/dev/stdin of=test bs=10 count=1 conv=ucase

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

To create a virtual image file using the DD command

In the above section, you should be able to master dd the basic use of the following to use the dd command to complete the first step to create a virtual disk.

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

$ dd if=/dev/zero of=virtual.img bs=1M count=256$ du -h virtual.img

Then we're going to format this file (written to the filesystem), and here we have to learn a new command (exactly a set) to complete this requirement.

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

You can enter MKFS in the command line and 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:

virtual.img

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.

For more information on file systems, see Wiki: File System ext3,ext4

If you want to know what file systems Linux supports you can enter ls -l /lib/modules/$(uname -r)/kernel/fs (not viewable in our environment) to 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:

$ sudo mount

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:

mount [options] [source] [directory]

Some common operations:

mount [-o [操作选项]] [-t 文件系统类型] [-w|--rw|--ro] [文件系统源] [挂载点]

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

loop -t ext4 virtual.img /mnt # 也可以省略挂载类型,很多时候 mount 会自动识别# 以只读方式挂载$ mount -o loop --ro virtual.img /mnt# 或者mount -o loop,ro virtual.img /mnt
Uninstalling mounted disks using the Umount command
# 命令格式 sudo umount 已挂载设备名或者挂载点,如:$ sudo umount /mnt

Unfortunately, because of problems with our environment (the Linux kernel used in the environment did not add support for Loop device at compile time), you will not be able to mount the success:

In addition, you may have a lot of questions about loop devices, so look at the following instructions from Wikipedia/dev/loop:

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 (refer to the master boot record for users with unclear concepts about partitioning)

Also because there is no physical disk in the environment and cannot create a virtual disk, we cannot experiment with the command, and I will use my physical host as an example to explain how to partition the disk.

# 查看硬盘分区表信息$ sudo fdisk -l

The output shows some information about the disk on my console, including the number of capacity sectors, sector size, I/o size, and so on.

We 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.

# 进入磁盘分区模式$ sudo fdisk virtual.img

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
$ sudo losetup /dev/loop0 virtual.img# 如果提示设备忙你也可以使用其它的回环设备,"ls /dev/loop*"参看所有回环设备# 解除设备关联$ sudo losetup -d /dev/loop0

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:

$ sudo apt-get install kpartx$ sudo kpart kpartx -av /dev/loop0# 取消映射$ sudo kpart kpartx -dv /dev/loop0

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

$ sudo mkfs.ext4 -q /dev/mapper/loop0p1$ sudo mkfs.ext4 -q /dev/mapper/loop0p5$ sudo mkfs.ext4 -q /dev/mapper/loop0p6

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

mkdir -p /media/virtualdisk_{1..3}
# 挂载磁盘分区$ sudo mount /dev/mapper/loop0p1 /media/virtualdisk_1$ sudo mount /dev/mapper/loop0p5 /media/virtualdisk_2$ sudo mount /dev/mapper/loop0p6 /media/virtualdisk_3# 卸载磁盘分区$ sudo umount /dev/mapper/loop0p1$ sudo umount /dev/mapper/loop0p5$ sudo umount /dev/mapper/loop0p6

And then:

$ df -h

Homework

cowsaycommand that allows you to print a paragraph in the terminal in the form of an animal speaking.

# 安装$ sudo apt-get install cowsay# 默认是一只牛$ cowsay hello shiyanlou# 加上‘-l‘参数打印所有支持的动物(其实不只是动物)种类$ cowsay -l# 使用‘-f‘参数选择动物种类$ cowsay -f elephant hello shiyanlou# 此外它还可以结合我们之前的作业讲过的 fortune 命令一起使用$ fortune | cowsay -f daemon

1.7 File system operations and Disk Management (learning process)

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.