First, the basic operation
Check the capacity of disk and directory
Use the df command to view disk capacity
$ df
/ Dev physical host / sda2 a hard disk partition corresponding to the host, the number indicates the partition numbers, the numbers in front of the letter A represents a few of the hard disk (removable disk may be), if you have multiple hard disks on the host it may also appear / dev / sdb, the form / dev / sdc the disk devices are in the / dev directory files.
Then you will see "1k- block" the strange thing, it represents a disk block size to show the way to the block size used and available capacity expressed in capacity before, you know behind the corresponding Linux file system the bar on the first matter, we should be able to understand you in a way to show:
$ df -h
Now you can use the command to view disk usage of your host.
Use the du command to view the directories capacity
The front command is already used many times:
# 默认同样以 块 的大小展示
$ du
# 加上`-h`参数,以更易读的方式展示
$ du -h
-d
Check parameter specifies the directory depth
# 只查看1级目录的信息
$ du -h -d 0 ~
# 查看2级
$ du -h -d 1 ~
Common parameters
du -h #同--human-readable 以K,M,G为单位,提高信息的可读性。
du -a #同--all 显示目录中所有文件的大小。
du -s #同--summarize 仅显示总计,只列出最后加总的值。
du
(Estimate file space usage) command df
(report file system disk space usage) command only one word, I hope you pay attention not to confuse, so you can get a complete description of the command man page like me, do not remember the full name It will be mixed up.
Second, the simple disk management
The following command involves a certain risk, improper operation may lose your personal data, recommended for beginners to operate in a virtual environment
Under normal circumstances, this section should say directly how to mount unmount the disk, how to format the disk, how to partition, but as you can see, due to the experimental environment, the environment, no additional disk can be mounted, there is no free space test partition command, so first we will create a virtual disk for subsequent exercises operation.
1 Create a virtual disk
dd command Introduction
dd
Commands for converting and copying files, but it's different from the copy cp
. Mentioned before a very important point, about Linux every file that is , on Linux, hardware device drivers (such as hard disk) and a special device files (such as /dev/zero
and /dev/random
) is like an ordinary file, only to realize in the respective driver corresponding features, dd files can be read or written to the file. In this way, dd
it may also be used in the boot sector of the backup hardware, obtaining a certain number of random data or null data tasks. dd
Can process both data replication, e.g. endian conversion, or interchanged between ASCII and EBCDIC encoding.
dd
The command-line statement and other Linux program is different because of its command-line options for the format 选项=值
, rather than the more standard --选项 值
or -选项=值
. dd
The default read from standard input and write to standard output, but you can use option if
(input file, the input file) and of
change (output file, the output file).
Let's try with the dd
command output to a file or from standard input to the standard user 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 read from the standard input a user input device (the default value, may be omitted) and then output to the test file, bs
(Block size) for specifying the block size (Default is Byte, may be assigned as 'K' , 'M', 'G' units, etc.), count
is used to specify the number of blocks. As shown above, I read only designated a total of 10 bytes of data, when I typed "hello shiyanlou" after the carriage return plus space total of 16 bytes (an English one byte characters), it is evident over a set size. Use du
and cat
command to see the actual content of the file write completion really only 10 bytes (percent sign indicate that there is no black line breaks), and other extra input will be intercepted and retained in the standard input.
As mentioned earlier dd
, while the copy of the data conversion may also be implemented, then the following will give a simple example: the output of the English characters to upper case and then written to the file:
$ dd if=/dev/stdin of=test bs=10 count=1 c
2 Create a virtual image file using the dd command
Through the above a bar, you should master the dd
basic use, you use the following dd
command to complete the first step in creating a virtual disk.
From /dev/zero
create a device with a capacity of 256M empty file:
$ dd if=/dev/zero of=virtual.img bs=1M count=256
$ du -h virtual.img
Then we want the file format (written to the file system), where we have to learn a (accurate to say that a group of) new command to complete this requirement.
3 Use the mkfs command to format the disk (in our case a virtual disk image that you have created)
You can enter on the command line sudo mkfs
and press the Tab
button, you can see a number prefixed with mkfs command, these different suffixes in fact represents a different file system, you can use mkfs formatted as file systems.
We can simply use the following command to our virtual disk images formatted as ext4
file systems:
$ sudo mkfs.ext4 virtual.img
You can see the actual mkfs.ext4 is formatted using mke2fs to complete the work. Mke2fs parameters lot, but we will not always format the disk to play, so we grasp this basic use of it, so when you have special needs, and then see the man document solutions.
For more information about file systems, see the wiki: file system ext3 , ext4
If you want to know which file systems supported by Linux you can enter ls -l /lib/modules/$(uname -r)/kernel/fs
(not view our environment) view.
Use the mount command to mount the disk to the directory tree
A user opens a file on a Linux / UNIX machines before, including the file system operation must be mounted, then the user to execute instructions to mount to mount the file system. The instructions are typically used on a USB or other removable storage device, and the root directory would remain mounted state. File And because Linux / UNIX file system may not necessarily correspond to a file if the hardware device, it is possible to mount a file system that contains the directory tree.
mount command Linux / UNIX command line tells the operating system, the corresponding file system is ready to be used, and the file system will correspond to a certain point (called a mount point). Mount good files, directories, device special files and can offer users.
Let's use mount
to view the next host has mounted file system:
$ sudo mount
The results output in each row represents a device or virtual device, the device name is top of each line, and then later on a mount point, followed by type represents file system type, and then followed by mount options (for example, can be mounted when mount as read only setting, etc.).
So how do we actually mount the disk to the directory tree does, mount
the general format of the command is as follows:
mount [options] [source] [directory]
Some common operations:
mount [-o [操作选项]] [-t 文件系统类型] [-w|--rw|--ro] [文件系统源] [挂载点]
Note: Due to environmental constraints laboratory building, mount command to mount and umount unloading can not operate, you can understand these simple steps.
Now we direct to mount virtual disk mirroring to create /mnt
directory:
$ mount -o loop -t ext4 virtual.img /mnt
# 也可以省略挂载类型,很多时候 mount 会自动识别
# 以只读方式挂载
$ mount -o loop --ro virtual.img /mnt
# 或者mount -o loop,ro virtual.img /mnt
Use the umount command to unmount a mounted disk
Note: Due to environmental constraints laboratory building, mount command to mount and umount unloading can not operate, you can understand these simple steps.
# 命令格式 sudo umount 已挂载设备名或者挂载点,如:
$ sudo umount /mnt
But unfortunately, due to the problems of our environment (environment using Linux kernel does not add support for the Loop device at compile time), so you will not be able to mount successful:
Also on the loop device, you may have many questions, then consider the following from Wikipedia / dev / loop explained:
In UNIX-like systems, / dev / loop (or called vnd (vnode disk), lofi (cyclic file Interface)) is a pseudo-device, such a device so that files can be accessed as a general block device.
Before use, recycling equipment must be associated with an existing file on the file system. This association will be provided to a user an application program interface, the interface will allow the file as a block special file (see the device file system) used. Therefore, if the file contains a complete file system, then the file can be mounted as a disk device in general.
Such devices are often used to document CD or disk image. By circulating mount to mount a file containing a file system, then the file in the file system to be accessed. These files will appear in the mount point directory. If you mount the directory itself has documents, which were banned after the mount.
4 Use fdisk to partition
The following example to explain how the physical host for the disk partition.
# 查看硬盘分区表信息
$ sudo fdisk -l
The output at the beginning shows some information on the host disk I, including the capacity number of sectors, sector size, I / O size and other information.
We focused look at the information center of the partition, / dev / sda1, / dev / sda2 partition Windows and Linux-based operating systems are installed, / dev / sda3 swap partition (can be understood as virtual memory), / dev / sda4 to wherein the extended partition comprises / dev / sda5, / dev / sda6, / dev / sda7, / dev / sda8 four logical partitions, because there is a gap between the host several partitions, sector boundaries are not aligned, the partition between the not completely continuous.
# 进入磁盘分区模式
$ sudo fdisk virtual.img
Before proceeding we should first planned our first partitioning scheme, where I will create a master in the use of 30M 128M (available about 127M) virtual disk image partition the remainder of the extended partition containing two logical partitions of about 45M.
After the completion of the input operation p
results are as follows:
Finally, do not forget to enter w
write the partition table.
Use losetup command associated with the loop mirror devices
Note: Due to environmental constraints laboratory building, losetup command can not operate, you can understand these simple steps.
Also because of environmental reasons no physical disk, there is no reason we can not loop device of the lab exercises using the command, and I will be following my example to explain the physical host.
$ sudo losetup /dev/loop0 virtual.img
# 如果提示设备忙你也可以使用其它的回环设备,"ls /dev/loop*"参看所有回环设备
# 解除设备关联
$ sudo losetup -d /dev/loop0
And then use the mkfs
format each partition (in front of us is to format the entire virtual disk image file or disk), but before formatting, we have to establish a virtual device mapping for each partition, use the kpartx
tool, you need to install:
$ sudo apt-get install kpartx
$ sudo kpartx -av /dev/loop0
# 取消映射
$ sudo kpartx -dv /dev/loop0
Then re-formatted, we will all be formatted as ext4:
$ sudo mkfs.ext4 -q /dev/mapper/loop0p1
$ sudo mkfs.ext4 -q /dev/mapper/loop0p5
$ sudo mkfs.ext4 -q /dev/mapper/loop0p6
After the completion of formatting in /media
the new directory for the four empty directory to mount 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
then:
$ df -h
Resources from https://www.shiyanlou.com/courses/1/learning/?id=1