用文件挂载目录,将目录的数据写入该文件中,文件拷贝出来后,可在其他机器上再次进行挂载后,就会看到该文件中的数据。

 

[root@localhost file]# dd if=/dev/zero of=/test/part bs=1M count=500   #首先创建一个500M的                                                       文件,写入0,将文件变为分区

500+0 records in

500+0 records out

524288000 bytes (524 MB) copied, 5.15719 s, 102 MB/s

  


[root@localhost file]# mkfs.ext4 /test/part              #指定分区格式,进行格式化

mke2fs 1.41.12 (17-May-2010)

/test/part is not a block special device.

Proceed anyway? (y,n) y   

Filesystem label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

Stride=0 blocks, Stripe width=0 blocks

128016 inodes, 512000 blocks

25600 blocks (5.00%) reserved for the super user

First data block=1

Maximum filesystem blocks=67633152

63 block groups

8192 blocks per group, 8192 fragments per group

2032 inodes per group

Superblock backups stored on blocks: 

8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409


Writing inode tables: done                            

Creating journal (8192 blocks): done

Writing superblocks and filesystem accounting information: done


This filesystem will be automatically checked every 34 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.


[root@localhost file]# mount /test/part /test/file                

mount: /test/part is not a block device (maybe try `-o loop'?)

[root@localhost file]# mount -o loop /test/part /test/file      #将文件挂载时,会提示loop                                                          选项,用此选项


/test/part              /test/file              ext4    loop            0 0

                                                                #如果在/etc/fstab下挂载                                                             时,将loop写入挂载模式中


[root@localhost file1]# blkid /test/part                    #指定查看该文件,可看到UUID

/test/part: UUID="44f2f514-c291-4ce1-a9ab-7b5b631664cc" TYPE="ext4"



[root@localhost file]# touch 123.txt                #为方便观察,我们创建123.txt文件

                    

[root@localhost test]# mount -o loop /test/part /test/file1  #我们将该文件挂载到file1目录


[root@localhost file1]# ls                        #此时,我们看到file1目录中有file目录中                                              的123.txt,这就是part文件中的数据起了                                              作用

123.txt  lost+found



[root@localhost file]# ls                       #此时,file目录中没有了任何数据

[root@localhost file]# 


使用此方法,要注意,数据是写在文件中,而不是在目录上,随着文件的转移挂载,数据更改目录,可以把该文件当做一种移动存储的介质。

注意:7版本的不需要添加loop选项。



[root@localhost file]# losetup /dev/loop3 /test/part      #指定part为loop3

[root@localhost file]# losetup -a                         #查看loop

/dev/loop3: [fd00]:524294 (/test/part)

[root@localhost file]# mkdir /test/part2

[root@localhost file]# mount /dev/loop3 /test/part2        #将loop3挂载到part2目录上

[root@localhost file]# mount                                #查看挂载结果

/dev/mapper/vg0-root on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw)

/dev/sda1 on /boot type ext4 (rw)

/dev/mapper/vg0-usr on /usr type ext4 (rw)

/dev/mapper/vg0-var on /var type ext4 (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

/dev/sdb1 on /test/ceshi type ext4 (rw,nosuid,acl)

/dev/loop3 on /test/part2 type ext4 (rw)









目录挂载目录方法:

mount -B或者--bind /test/file  /test/file1  








分区启用swap

fdisk /dev/sdb

mkswap -L SWAP_SDB1 /dev/sdb1                  #-L是创建卷标

vi /etc/fstab

LABEL=SWAP_SDB1 swap swap defaults 0 0

swapon -s                                     #查看swap分区

swapon -a                                     #挂载写入与mount -a效果一样


swapon /dev/sdb1                              #临时挂载


文件启用swap

dd if=/dev/zero of=/testdir/swapfile bs=1M count=1024      #将文件改为分区

mkswap  /testdir/swapfile 

vi /etc/fstab

/testdir/swapfile   swap swap defaults 0 0

swapon -s cat /proc/swaps

swapon -a


删除swap

swapoff /dev/sdb1 或swapoff /testdir/swapfile

删除/etc/fstab相应记录

删除分区和文件



U盘挂载:

lsmod | grep usb   # 查看U盘模块

sync     #同步三次

sync

sync

umount /mnt/usb  卸载U盘