`
javayestome
  • 浏览: 1041163 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

在一个文件里创建一个文件系统,将是一个新的磁盘,有木有!!!

阅读更多

You can take a disk file, format it as an ext2, ext3, or reiser filesystem, and then mount it, just like a physical drive. It's then possible to read and write files to this newly-mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem and mount it with ACL (Access Control Lists), which gives you rights beyond the traditional read (r), write (w), and execute (x) permissions for the three user groups "file", "owner", and "other".

This is an excellent way to investigate different filesystems without having to reformat a physical drive, which means you avoid the hassle of moving all your data. This method is quick -- very quick compared to preparing a physical device. You can then read and write files to the mounted device, but what is truly great about this technique is that you can explore different filesystems such as reiserfs, ext3, or ext2 without having to purchase an additional physical drive. Since the same file can be mounted on more than one mount point, you can investigate sync rates.

Creating a filesystem in this manner allows you to set a hard limit on the amount of space used, which, of course, will be equal to the file size. This can be an advantage if you need to move this information to other servers. Since the contents cannot grow beyond the file, you can easily keep track of how much space is being used.

First, you want to create a 20MB file by executing the following command:

      $ dd if=/dev/zero of=disk-image count=40960
      40960+0 records in
      40960+0 records out

You created a 20 MB file because, by default, dd uses a block size of 512 bytes. That makes the size: 40960*512=20971520.

      $ ls -l disk-image
      -rw-rw-r--    1 chirico  chirico  20971520 Sep  3 14:24 disk-image

Next, to format this as an ext3 filesystem, you just execute the following command:

      $ /sbin/mkfs -t ext3 -q disk-image
      mke2fs 1.32 (09-Nov-2002)
      disk-image is not a block special device.
      Proceed anyway? (y,n) y

You are asked whether to proceed because this is a file, and not a block device. That is OK. We will mount this as a loopback device so that this file will simulate a block device.

Next, you need to create a directory that will serve as a mount point for the loopback device.

      $ mkdir fs

You are now one step away from the last step. You just want to find out what the next available loopback device number is. Normally, loopback devices start at zero (/dev/loop0) and work their way up (/dev/loop1, /dev/loop2, ... /dev/loopn). An easy way for you to find out what loopback devices are being used is to look into /proc/mounts, since the mount command may not give you what you need.

      $ cat /proc/mounts

      rootfs / rootfs rw 0 0
      /dev/root / ext3 rw 0 0
      /proc /proc proc rw,nodiratime 0 0
      none /sys sysfs rw 0 0
      /dev/sda1 /boot ext3 rw 0 0
      none /dev/pts devpts rw 0 0
      /proc/bus/usb /proc/bus/usb usbdevfs rw 0 0
      none /dev/shm tmpfs rw 0 0

On my computer, I have no loopback devices mounted, so I'm OK to start with zero. You must do the next command as root, or with an account that has superuser privileges.

      # mount -o loop=/dev/loop0 disk-image fs

That's it. You just mounted the file as a device. Now take a look at /proc/mounts, you will see this is using /dev/loop0.

      $ cat /proc/mounts

      rootfs / rootfs rw 0 0
      /dev/root / ext3 rw 0 0
      /proc /proc proc rw,nodiratime 0 0
      none /sys sysfs rw 0 0
      /dev/sda1 /boot ext3 rw 0 0
      none /dev/pts devpts rw 0 0
      /proc/bus/usb /proc/bus/usb usbdevfs rw 0 0
      none /dev/shm tmpfs rw 0 0
      /dev/loop0 /home/chirico/junk/fs ext3 rw 0 0

You can now create new files, write to them, read them, and do everything you normally would do on a disk drive. First, I'll give access to the chirico account.

      # chown -R chirico.chirico /home/chirico/junk/fs

Now, under the chirico account, it is possible to create files.

      $ cd /home/chirico/fs
      $ mkdir one two three
      $ ls -l

      total 15
      drwx------    2 chirico  chirico     12288 Sep  3 14:28 lost+found
      drwxrwxr-x    2 chirico  chirico      1024 Sep  3 14:34 one
      drwxrwxr-x    2 chirico  chirico      1024 Sep  3 14:34 three
      drwxrwxr-x    2 chirico  chirico      1024 Sep  3 14:34 two

      $ df -h

      Filesystem            Size  Used Avail Use% Mounted on
      /dev/sda2              17G   11G  4.6G  71% /
      /dev/sda1              99M   83M   11M  89% /boot
      none                   62M     0   62M   0% /dev/shm
      /home/chirico/junk/disk-image
                             20M  1.1M   18M   6% /home/chirico/junk/fs

If you need to umount the filesystem, as root, just issue the umount command. If you need to free the loopback device, execute the losetup command with the -d option. You can execute both commands as follows:

      # umount /home/chirico/junk/fs
      # losetup -d /dev/loop0

Using RWX -- The Old Way To Collaborate

Before we get started with ACL, how would you set up rights on the filesystem so that users could create and save documents that others could modify? For instance, let's say that users chirico and sporkey are collaborating on a project together.

Well, you have to add everyone to the same group. You would execute commands like these:.

      # groupadd sharefs
      # chown -R root.sharefs /home/chirico/junk/fs
      # chmod 2775 /home/chirico/junk/fs
      # usermod -G sharefs sporkey
      # usermod -G sharefs chirico

Note that if these changes do not take effect for your users (for example, if they were logged in when you executed the commands), they'll have to log out and log in again or execute the "$ newgrp sharefs" command. No big deal, right? Well, keep reading, and see how ACL avoids this step.

More importantly, even though theold wayworked for you, at some point, new users may need to be added to the project. What if some of these users only need a subset of the rights? For instance, you have developers, testers, managers, and a few special people. There are limits to what the rwx type rights can do. ACL solves a lot of these problems.

ACL, Reiserfs, and AES Encryption: The 2.6 Kernel

For the next steps, I will assume that you are running Red Hat Fedora Core 2. If not, reference the 2.6 kernel upgrade section below. Four things will be covered in this section:

  • Create A File With Random Data
  • Set Up An AES Encrypted Loopback Device With Password
  • Build A Reiser Filesystem On The Loopback Device
  • Mount With ACL Capabilities

Your installation of Fedora Core 2, by default, will be configured for loop, cryptoloop, and aes, but it is highly unlikely that you will have all of these modules loaded. So, execute the following commands to load these modules (you will need to do this as root):

      # modprobe loop
      # modprobe cryptoloop
      # modprobe aes

Next, create a directory to store the files. The Reiser filesystem will require more space than the ext3 filesystem.

      # mkdir /home/diskimg
      # cd /home/diskimg

Instead of creating the file zeroed out, like you did with the ext3 filesystem, this one is going to contain random bits, which may add a little extra security.

      # dd if=/dev/urandom of=disk-aes count=102400

We need to encrypt the loop device, so you need to use losetup. You will be prompted for a password, which you will need to remember when you mount the device.

      # losetup -e aes /dev/loop1 ./disk-aes
        Password:

This step is new also. Instead of formating the file directly, you will format the loop device. The file stays encrypted. Again, you will be prompted to continue, so just enter "y".

      # mkfs -t reiserfs /dev/loop1

      mkfs.reiserfs 3.6.13 (2003 www.namesys.com)                                                
                                                                                           
      A pair of credits:                                                                   
      Elena Gryaznova performed testing and benchmarking.                                  
                                                                                           
      The  Defense  Advanced  Research  Projects Agency (DARPA, www.darpa.mil) is the      
      primary sponsor of Reiser4.  DARPA  does  not  endorse  this project; it merely      
      sponsors it.                                                                         
                                                                                           
                                                                                           
      Guessing about desired format.. Kernel 2.6.8-1.521 is running.                       
      Format 3.6 with standard journal                                                     
      Count of blocks on the device: 12800                                                 
      Number of blocks consumed by mkreiserfs formatting process: 8212                     
      Blocksize: 4096                                                                      
      Hash function used to sort names: "r5"                                               
      Journal Size 8193 blocks (first block 18)                                            
      Journal Max transaction length 1024                                                  
      inode generation number: 0                                                           
      UUID: 435e3495-5e2e-489d-bf55-1b5f9a44b670                                           
      ATTENTION: YOU SHOULD REBOOT AFTER FDISK!                                            
              ALL DATA WILL BE LOST ON '/dev/loop1'!                                       

      Continue (y/n):y                                                                     
      Initializing journal - 0%....20%....40%....60%....80%....100%                        
      Syncing..ok                                                                          
                                                                                           
      Tell your friends to use a kernel based on 2.4.18 or later, and especially not a     
      kernel based on 2.4.9, when you use reiserFS. Have fun.                              
                                                                                           
      ReiserFS is successfully created on /dev/loop1.                                      

Create the mount point /fs, and mount this device. Note that you will be entering the acl option as well. Plus, you will prompted for a password.

      # mkdir /fs
      # mount -o loop,encryption=aes,acl ./disk-aes /fs
        Password:

Ok, now take a look at the mount command. It should show up as the Reiser filesystem, encrypted, using ACL. Note that it says loop2; it mounted it on /dev/loop2, which is one above what losetup specified, /dev/loop1.

      $ mount
      /home/diskimg/disk-aes on /fs type reiserfs (rw,loop=/dev/loop2,encryption=aes,acl)

Exploring ACL

With ACL (Access Control Lists), you have finer control over access permissions. With the rwx permission scheme, you cannot easily change rights without creating new groups to handle the users. With ACL, you can set user permissions without creating a group, and individual users can add or remove access.

These rights are set with thesetfaclcommand. The command below will give the users donkey, chirico, and bozo2 access to this new filesystem that we mounted. Again, I'm assuming that you are using Fedora Core 2, or some distribution that is set up for ACL.

# setfacl -R -m d:u:donkey:rwx,d:u:chirico:rwx,d:u:bozo2:rwx /fs

Next, create a few directories as one of the users. The example below was done as the user chirico.

      $ mkdir /fs/one
      $ touch /fs/one/stuff
      $ ls -l /fs/one/stuff
      -rw-rw----+ 1 chirico chirico 0 Sep  3 17:48 /fs/one/stuff

Notice the plus sign in the last line. It tells us a little about who has access. So, as user chirico, the getfacl command can be executed:

      $ getfacl /fs/one/stuff                                    

      getfacl: Removing leading '/' from absolute path names     
      # file: fs/one/stuff                                       
      # owner: chirico                                           
      # group: chirico                                           
      user::rw-                                                  
      user:chirico:rwx                #effective:rw-             
      user:donkey:rwx                 #effective:rw-             
      user:bozo2:rwx                  #effective:rw-             
      group::r-x                      #effective:r--             
      mask::rw-                                                  
      other::---                                                 

We now see that donkey, chirico, and bozo2 have effective rights on this file. Chirico has enough rights to remove bozo2.

      $ setfacl -x u:bozo2 /fs/one/stuff
      $ getfacl /fs/one/stuff
      getfacl: Removing leading '/' from absolute path names
      # file: fs/one/stuff
      # owner: chirico
      # group: chirico
      user::rw-
      user:chirico:rwx
      user:donkey:rwx
      group::r-x
      mask::rwx
      other::---

This is just scratching the surface of what can be done with ACL. For more information, see some of the references below.

2.6 Kernel Upgrade

This articlewill get you started with the 2.6 kernel if you are currently running Red Hat 8 or 9. You may want to take a look at it to see what is involved. If you decide to upgrade, you will need to configure your kernel for the following:

      CONFIG_BLK_DEV_LOOP
      CONFIG_BLK_DEV_CRYPTOLOOP
      CONFIG_CRYPTO_AES_586

This is done in the .config file, and you can download my config filehere. Just look for kernel-2.6.8.1-i686-chirico-reiserfsacl.config in the tar.gz.

In addition to upgrading the kernel, you will need the latest version ofthe Linux utilities. Currently, there is no need to patch this version. In the past, there was a patch, but this version worked fine for me.

分享到:
评论

相关推荐

    数据库系统原理第五版答案.pdf

    含泪奉献啊,第五版啊!!虽然有一半题目都没有答案,但是lz我真的大奉献...有木有啊!!第五版找不到找第四版啊!!第四版再找不到还有第三版啊!!有木有啊!!还是新手都不敢多要资源分啊!!有用得着的就拿去吧!!

    altium_designer_10_破解文件

    altium_designer_10_破解文件.rar 最近在CSDN下载里面发现有人将这个破解文件索要资源分 10!! 简直就是强盗!!有木有?!!我把这个低价提供!希望大家成为AD高手!试过了可以用!

    程序员超级喜欢陆毅有木有

    在描述中提到的“程序员超级喜欢陆毅有木有”,这似乎是一个与明星陆毅相关的项目或活动。在Web开发中,有时开发者会使用他们喜欢的人物、事物作为示例或者项目主题,以此增加工作乐趣。在这个情况下,程序员们可以...

    ASP.NETAJAX经典范例168VC#版 源码下载带数据库

    ASP.NET AJAX经典范例168 VC#版 章立民 大爷写的书!!!!非常经典,ajax的.NET方面的资料基本上就是这个大爷写的!!!相当的犀利啊!!鉴于网上我找了一年多了都没有iso...一会去电驴上发布一个iso的去。反正都一样。

    华硕转ROM.rar

    PS:有些同学救砖实在新手,会忽略一个情况,就是:当你把文件通过编程器刷写进去以后,电脑会开机很久不亮屏,其实是因为新文件第一次运行需要一段时间,但最长不超过5分钟,可以适当多等一下,只要风扇一直转,...

    ios-一句话实现分段控制器的切换,有木有感觉到最强封装... 下载了不给STAR以后不分享了..zip

    这个压缩包文件"ios-一句话实现分段控制器的切换,有木有感觉到最强封装... 下载了不给STAR以后不分享了..zip"似乎包含了一个精心封装的解决方案,能用简洁的代码实现分段控制器的切换效果。下面我们将详细探讨如何在...

    最搞笑百度恶搞图片软件很搞笑有木有

    "最搞笑百度恶搞图片软件很搞笑有木有" 这个标题提到了一个软件,它基于百度图片资源,具有恶搞功能,能够生成有趣的、带有自定义文字的图片,给人带来欢笑。关键词是“搞笑”、“百度”、“恶搞图片”,暗示这是一...

    UC浏览器 UCBrowser

    3、长按输入框选择【长文本输入】,可以边输入边预览多行文本,一个字都逃不出你的法眼!功能简易实用,已经覆盖所有页面的输入框啦~!输入大段大段文字也不怕看不到前文啦! 4、【速度优化】新体验,此版本对启动、...

    获取TrustedInstaller的权限

    其实TrustedInstaller是系统的一个隐藏用户。当然这时候你也可以改文件的权限,但是如果你需要删除的文件太多的话。呵呵是不是觉得有点手软呢。哈哈,这时候你就可以用到以上这个文件了运行一下,然后右键获取...

    好用的GHOST 版本目前最新11.5 win7下点 ghost32 可运行

    为求分义务上传啊!!!有木有???DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD

    管理后台系统界面(4套)

    本资源包包含了四套不同的管理后台系统界面设计,这对于那些在美工方面不太熟练的程序员来说,无疑是一份宝贵的参考资料。 1. **界面设计原则**: - 用户友好:好的后台界面应简洁明了,使用户能快速理解功能布局...

    域名助理-域名批量查询的软件

    更有域名自动保存功能,每次查询的结果自动按照日期格式保存为文本文件,查询结果里显示每个域名的注册时间,到期时间以及续费时间。而且准确率提高到99%以上,还有生成查询速度提到了10倍。 更更更无毒无马,不会...

    WIFI ADB调试工具

    在eclipse中通过wifi调试android程序,解放数据线,数据线丢了?不想花钱去买?android手机居然可以通过wifi进行程序的调试,太好了! 整个应用只有一个按钮,够简单!够直接!!有木有!!

    ASP.NET MVC Bootstrap极速开发框架

    有木有一个通用的快速开发框架?并且得是ASP.NET MVC And Bootstrap?数据库不要手工创建?框架对未来业务支持的扩展性好?这么简单的功能还需要一天搭建基础环境?能不能只关心我所需要的业务? 有这样的一个项目...

    虚拟机中VM tools的安装步骤

    接下来,我们需要将ISO文件复制到一个临时目录(如`/tmp`)以便进一步处理。执行命令: ``` cp -rf /media/VM/VMwareTools-<version>.iso /tmp/ ``` 其中`VMwareTools-<version>.iso`是ISO文件的实际名称,可能包含...

    基于Tiny6410下QT的人脸识别程序

    将这两者结合,我们可以创建一个功能强大的实时人脸识别系统。 首先,我们要理解 Tiny6410 的硬件特性。Tiny6410 是基于Samsung公司的S3C6410 ARM11内核的微处理器,它具有高性能、低功耗的优势,适合于嵌入式系统...

    微处理器系统结构与嵌入式系统设计(第二版)答案全

    本文档是教材的习题答案,针对电子科技大学学弟学妹倾情奉献,只要3分就可以得到习题全部答案,一秒变学霸有木有!

    打字时触摸板临时屏蔽工具 - Touchpad-Blocker

    大家在使用使用笔记本时,有没有遇到过打字时不小心碰到了 触摸板,会让你的鼠标不知道移动到哪里,并且输入的文字完全错乱,恼火,有木有!!! 有些笔记本有屏蔽触摸板功能,但每次都需要按键也很麻烦,Touchpad-...

    Srv感染型病毒专杀工具.rar

    曾经, 对于这个病毒有一个处理手段,就是全盘格式化,然后重装系统。这代价太大了有木有。 全盘格式化之后再恢复硬盘数据太浪费时间了有木有。 一不小心又被感染了有木有。。。。 现在你可以无视Srv了,这个工具...

    photoshop转CSS3插件

    CSS3PS photoshop插件,该插件直接把你做的PS图生成CSS3代码。设计与码农的福音,有木有! 插件安装即使用方法: ... 2、教程盒子翻译教程 CSS3PS插件在Photoshop CS5 ...该脚本将运行并带有转换结果打开一个浏览器窗口

Global site tag (gtag.js) - Google Analytics