随笔-145  评论-173  文章-70  trackbacks-0
前段时间重装了系统,伴随而来的是硬盘分区的变化和配置问题。具体来说就是,我使用了Acronis Disk Director Suite软件,将原来安装在ext4下面的Ubuntu给格式化掉了,因为原来的该Ubuntu因为更新内核挂掉了,图形界面不可用,对我来说,目前使用图形界面还是很有必要的。在经历过多次尝试之后,我还是放弃了找回这个Ubuntu,而直接选择了重装,虽然现在有点后悔为何当初使用弱爆的wubi安装,导致现在每次进入启动界面都要先进入windows引导界面选择Ubuntu,再启动grub来选择Ubuntu(导致其实可以在第一个界面中选择Ubuntu,后面一个又选回去Win7)。早知道如此,当初或许会不给自己留退路,直接搞个Ubuntu的硬盘安装算了,少了很多麻烦,而且更省事。(这个在文章http://www.cppblog.com/deercoder/archive/2011/09/11/155572.html中也有所描述,权限的问题还是不好解决,毕竟不是在Linux下面的文件系统下直接进行操作)。

废话说了很多,问题出来了。每次启动后,都要手动选择挂载各个盘,比如fun盘,learn盘,来听听音乐,看看电子书啥的。如果不挂载的话,我的banshee就不能打开列表中的音乐,几回下来很麻烦,为此尝试每次启动后手动挂载变为自动化的过程。

在Google,baidu之后,觉得效果不大理想,主要是说的东西太多,不是我想要的,或者说内容太杂了。于是乎看看说明文档,主要有两个部分,一个是命令mount,另外一个是fstab,分别看了一点之后,开始尝试。
(以下操作最好都在sudo下面进行,否则可能会有权限问题)
现备份文件/etc/fstab,然后编辑该文件。观察样例形式,找到了
 <file system> <mount point>   <type>  <options>       <dump>  <pass>
上面这种说明形式,以及实例中关于proc的例子。
proc            /proc           proc    nodev,noexec,nosuid 0       0

于是乎试着使用自己的方式来改写。sudo fdisk -l查看硬盘信息.
  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        7572    60820168+   7  HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2            7572       11494    31498240    7  HPFS/NTFS
Partition 2 does not end on cylinder boundary.
/dev/sda3           11494       26506   120588288+   5  Extended
Partition 3 does not end on cylinder boundary.
/dev/sda4           26507       30401    31283280    7  HPFS/NTFS
Partition 4 does not end on cylinder boundary.
/dev/sda5           11494       16715    41943040    7  HPFS/NTFS
/dev/sda6           16716       21937    41943040    7  HPFS/NTFS
/dev/sda7           21937       26506    36700160    7  HPFS/NTFS
然后在逐个挂载分区,看看哪个是对应的learn盘,fun盘,首先在/media下面新建一个文件,比如test,然后执行命令:
sudo mount /dev/sda1 /media/test
通过对挂载之后内容的判断,知道上面的分区分别是哪些盘,然后就好说了。

编辑/etc/fstab文件,在最后面加入我要开机自动挂载的盘的配置信息,如下:
/dev/sda4    /media/Code    ntfs    nodev,noexec,nosuid 0      0    
/dev/sda5     /media/Learn   ntfs    nodev,noexec,nosuid 0      0
/dev/sda6    /media/Fun     ntfs    nodev,noexec,nosuid 0     0
唯一一点需要注意的就是文件类型,这里我的盘都是ntfs类型的,所以通过查看文档知道有该类型(不知道为何开始使用hpfs也是OK的,后面就不行了),在经过几次测试之后,发现出现了类型错误。

信息错误可以有dmesg | tail来查看。

在找到类型错误后,修改位ntfs,即可正确的挂载。

然后挂载的话就直接使用sudo mount /media/Fun ......即可。
当前,注意由于配置文件中有/media/Code......文件夹,所以需要在/media文件夹下面新建相应的文件夹,否则会失败,注意还要是管理员权限。

最后,注销后再次进入,就能够看到自动挂载的盘符了,而且,此时还不能umount掉哦,提示不是root用户。至此,达到了第一个目的了,能够开机自动挂载。
如果想要使用更加个性化的选项和操作,可以查看参考资料,另外,Man文档中有很多有用的信息,看文档是最快,最直接的方式。

如果想要取消挂载的话,直接使用umount命令即可。比如取下挂载fun,则使用命令:sudo umount /media/fun即可。

P.S: 如何挂载ISO文档呢?之前下载了无损音乐,格式是ISO镜像,不想解压,那么如何mount上去呢?
Key:
 sudo mount *.iso /mnt -o loop(将*.iso换成你的那个iso所在的路径即可,另外,也不一定要挂载在mnt目录下面,可以另外新建一个文件夹,类似于前面在media下面新建Fun,Learn等文件夹一样。)

P.S.S: 在终端输入命令之后,如果想在前面加入sudo怎么办?还要移动方向键来选择插入点,多麻烦啊?
Key:
使用Ctrl+A(不管大小写)键即可跳转到第一个字符,从而输入sudo后回车即可。

修改:上面的描述在实际运行的时候,能够开机的时候mount,但是今天发现,直接用Banshee来打开音乐的话,还是会出现问题,就像没有mount一样,不能打开文件,所以怀疑是权限问题。再次阅读了man mount和man fstab的资料(主要资料在man mount中),修改了相应的权限。能够实现mount的时候打开文件了,而且是开机自动mount。

修改文件如下:
proc            /proc           proc    nodev,noexec,nosuid 0       0
/host/ubuntu/disks/root.disk /               ext4    loop,errors=remount-ro 0       1
/host/ubuntu/disks/swap.disk none            swap    loop,sw         0       0
/dev/sda4       /media/Code    ntfs     users,auto,rw,dev,exec,umask=002     0   0
/dev/sda5       /media/Learn   ntfs     users,auto,rw,dev,exec,umask=002     0   0 
/dev/sda6       /media/Fun     ntfs     users,auto,rw,dev,exec,umask=002     0   0
修改后的option字段增加了很多设置,具体来说就是:
users:表示任何用户都能够umount,之前的设置不能umount,只能在命令行下面使用sudo权限才能使用。
rw:表示拥有读写权限。
auto:Can be mounted with the -a option.(使用mount -a的时候能够吧fstab中的所有自动mount上去)
dev: Interpret character or block special devices on the filesystem.
exec:可执行权限
umask以及fmask:对于文件和文件目录的权限设置。


下面是一个资料中关于fstab的详细解释,在其中有更详细的解释和说明(How to fstab)(http://ubuntuforums.org/showthread.php?t=283131)
摘录部分资料:

defaults = rw, suid, dev, exec, auto, nouser, and async.

Options for a separate /home : nodev,nosuid,relatime

My recommended options for removable (USB) drives are in green.

auto= mounted at boot
noauto= not mounted at boot

user= when mounted the mount point is owned by the user who mounted the partition
users= when mounted the mount point is owned by the user who mounted the partition and the group users

ro= read only
rw= read/write

说明:option字段中,即上面的 users,auto,rw,dev,exec,umask=002部分,是使用逗号间隔的一个设置,其中auto是在启动的时候自动mount,而noauto则是在启动的时候不mount(可见之前noauto实际上还是没有mount上去吧,这也解释了为何不能打开,虽然可以看见盘符,能够进去,但是以前保存的列表还是不能打开文件,不过进入盘符倒是可以的。) 

users能够umount,只要是使用者即可,不一定要是root。

挂载点的问题:
挂载在/mnt下面的话,不会出现在Places和Desktop中,而挂载在/media则会出现在桌面和位置上面。(默认的是在media下面,这也是为何在菜单中点击某个盘的时候实现挂载,然后在/media下面会出现相应的盘符,而不是在/mnt下面,其实,可以把ISO文件挂载在/mnt下面,硬盘分区挂载在/media下面)
  1. /mnt Typically used for fixed hard drives HD/SCSI. If you mount your hard drive in /mnt it will NOT show in "Places" and your Desktop.
  2. /media Typically used for removable media (CD/DVD/USB/Zip). If you mount your hard drive in /media it WILL show in "Places" and your Desktop.

修改2:


今天在VBox中下载的视频无法拷贝到挂载的盘,后来发现,是因为上次修改了fstab的原因,经过多次测试,终于知道问题处在umask上面,再次修改,将umask修改位0000即可。注意:umask会夺去默认的权限,比如UNIX上面创建一个文件默认的是644,这个字段的作用就是:
设定这个盘符(或者文件)的存取权限。之前的umask位004的情况,只能够读取,而不能够写入。原因就在这里!



最后,附上man文档中关于这些option的一些设置:

   The following options apply to any filesystem  that  is being  mounted
       (but  not every filesystem actually honors them - e.g., the sync option
       today has effect only for ext2, ext3, fat, vfat and ufs):
       async  All I/O to the filesystem should be  done  asynchronously.  (See
     also the sync option.)
       atime  Update  inode access time for each access. See also the stricta‐
     time mount option.
       noatime
     Do not update inode access times on this filesystem  (e.g,  for
     faster access on the news spool to speed up news servers).
       auto   Can be mounted with the -a option.(开机自动mount)
       noauto Can  only  be  mounted  explicitly (i.e., the -a option will not
     cause the filesystem to be mounted).
       context=context,  fscontext=context,  defcontext=context  and  rootcon‐
       text=context
     The  context= option is useful when mounting filesystems that do
     not support extended attributes, such as a floppy or  hard  disk
     formatted  with  VFAT,  or systems that are not normally running
     under SELinux, such as an ext3 formatted disk from a non-SELinux
     workstation. You can also use context= on filesystems you do not
     trust, such as a floppy. It also helps  in  compatibility  with
     xattr-supporting filesystems on earlier 2.4.<x> kernel versions.
     Even where xattrs are supported, you can save time not having to
     label  every file by assigning the entire disk one security con‐
     text.
     A commonly used  option  for  removable  media  is  context=sys‐
     tem_u:object_r:removable_t.
     Two  other options are fscontext= and defcontext=, both of which
     are mutually exclusive of the context option. This means you can
     use fscontext and defcontext with each other, but neither can be
     used with context.
     The fscontext= option works for all filesystems, regardless  of
     their  xattr  support. The fscontext option sets the overarching
     filesystem label to a specific security context. This filesystem
     label  is  separate  from the individual labels on the files. It
     represents the entire filesystem for certain kinds of permission
     checks,  such as during mount or file creation.  Individual file
     labels are still obtained from the xattrs  on  the  files  them‐
     selves.  The  context option actually sets the aggregate context
     that fscontext provides, in addition to supplying the same label
     for individual files.
     You  can set  the  default security context for unlabeled files
     using defcontext= option. This overrides the value set for unla‐
     beled  files  in the policy and requires a filesystem that sup‐
     ports xattr labeling.
     The rootcontext= option allows you to explicitly label the  root
     inode of a FS being mounted before that FS or inode because vis‐
     able to userspace. This was found to be useful for  things  like
     stateless linux.
     For more details, see selinux(8)
       defaults
     Use  default  options:  rw,  suid,  dev, exec, auto, nouser, and
     async.
       dev    Interpret character or block special devices on the filesystem.
       nodev  Do not interpret character or block special devices on the  file
     system.
       diratime
     Update  directory inode access times on this filesystem. This is
     the default.
       nodiratime
     Do not update directory inode access times on this filesystem.
       dirsync
     All directory updates within the filesystem should be done  syn‐
     chronously.   This  affects  the following system calls: creat,
     link, unlink, symlink, mkdir, rmdir, mknod and rename.
       exec   Permit execution of binaries.
       noexec Do not allow direct execution of any  binaries  on  the  mounted
     filesystem.   (Until  recently  it  was possible to run binaries
     anyway using a command like /lib/ld*.so /mnt/binary. This  trick
     fails since Linux 2.4.25 / 2.6.0.)
       group  Allow  an ordinary (i.e., non-root) user to mount the filesystem
     if one of his groups matches the group  of  the device.   This
     option  implies  the options nosuid and nodev (unless overridden
     by subsequent options, as in the option line group,dev,suid).
       encryption
     Specifies an encryption algorithm to use.  Used  in  conjunction
     with the loop option.
       keybits
     Specifies  the key size to use for an encryption algorithm. Used
     in conjunction with the loop and encryption options.  nofail  Do
     not  report  errors for this device if it does not exist.  iver‐
     sion Every time the inode is modified, the i_version field  will
     be incremented.
       noiversion
     Do not increment the i_version inode field.
       mand   Allow mandatory locks on this filesystem. See fcntl(2).
       nomand Do not allow mandatory locks on this filesystem.
       _netdev
     The  filesystem resides on a device that requires network access
     (used to prevent the  system  from  attempting  to  mount  these
     filesystems until the network has been enabled on the system).
       nofail Do not report errors for this device if it does not exist.
       relatime
     Update  inode  access  times  relative to modify or change time.
     Access time is only updated if the previous access time was ear‐
     lier  than  the current modify or change time. (Similar to noat‐
     ime, but doesn't break mutt or other applications that  need  to
     know  if a  file has been read since the last time it was modi‐
     fied.)
       norelatime
     Do not use relatime feature.  See  also  the  strictatime  mount
     option.
       strictatime
     Allows  to  explicitly requesting full atime updates. This makes
     it possible for kernel to defaults to relatime  or  noatime  but
     still allow userspace to override it. For more details about the
     default system mount options see /proc/mounts.
       nostrictatime
     Use  the kernel's  default  behaviour  for  inode  access  time
     updates.
       suid   Allow  set-user-identifier  or set-group-identifier bits to take
     effect.
       nosuid Do not allow set-user-identifier or set-group-identifier bits to
     take  effect.  (This seems safe, but is in fact rather unsafe if
     you have suidperl(1) installed.)
       owner  Allow an ordinary (i.e., non-root) user to mount the  filesystem
     if  he  is  the  owner  of  the device.  This option implies the
     options  nosuid  and  nodev  (unless  overridden by  subsequent
     options, as in the option line owner,dev,suid).
       remount
     Attempt  to remount an already-mounted filesystem.  This is com‐
     monly used to change the mount flags  for  a  filesystem,  espe‐
     cially  to  make a  readonly  filesystem writeable. It does not
     change device or mount point.
     The remount functionality follows the standard way how the mount
     command  works  with options from fstab. It means the mount com‐
     mand doesn't read fstab (or mtab) only when a device and dir are
     fully specified.
     mount -o remount,rw /dev/foo /dir
     After this call all old mount options are replaced and arbitrary
     stuff from fstab is ignored, except the loop=  option  which  is
     internally generated and maintained by the mount command.
     mount -o remount,rw  /dir
     After  this  call  mount reads fstab (or mtab) and merges these
     options with options from command line ( -o ).
       ro     Mount the filesystem read-only.
       rw     Mount the filesystem read-write.
       sync   All I/O to the filesystem should be done synchronously. In  case
     of  media  with  limited number of write cycles (e.g. some flash
     drives) "sync" may cause life-cycle shortening.
       user   Allow an ordinary user to mount the filesystem.  The name of the
     mounting user  is  written  to  mtab so that he can unmount the
     filesystem again.   This option implies  the  options  noexec,
     nosuid,  and  nodev (unless overridden by subsequent options, as
     in the option line user,exec,dev,suid).
       nouser Forbid an ordinary (i.e., non-root) user to mount  the  filesys‐
     tem.  This is the default.
       users  Allow  every  user  to  mount  and unmount the filesystem.  This
     option implies the options noexec,  nosuid,  and nodev  (unless
     overridden   by  subsequent  options,  as  in  the  option  line
     users,exec,dev,suid).


附参考资料:
我们在linux中常常用mount命令把硬盘分区或者光盘挂载到文件系统中。/etc/fstab就是在开机引导的时候自动挂载到linux的文件系统。

在linux中/etc/fstab的数据项如下所示:

/dev/device   mountpoint   type   rules   dump   order

设备名称        挂载点          分区类型   挂载选项     dump选项    fsck选项

例如这是一个普通的/etc/fstab:

/dev/hda2     /                    ext3        defaults   0 1

/dev/hda3     swap             swap      defaults   0 0

/dev/hda5     /usr               ext3        defaults   0 0

/dev/fdo        /mnt/flopy     ext3        noauto     0 0

/dev/cdrom    /mnt/cdrom   iso9660  noauto,ro 0 0

(1)设备名称
/dev/device就是需要挂载的设备,/hda2就是第一个IDE插槽上的主硬盘的第二个分区。如果是第二个IDE插槽主硬盘的第三个分区,那就是/dev/hdc3,具体可以在linux下使用fdisk -l  查看。

(2)挂载点
mountpoint 就是挂载点。/、 /usr、 swap 都是系统安装时分区的默认挂载点。
如果你要挂载一个新设备,你就要好好想想了,因为这个新设备将作为文件系统永久的一部分,需要根据FSSTND(文件系统标准),以及它的作用,用户需求来决定。比如你想把它做为一个共享资源,放在/home下面就是一个不错选择。

(3)分区类型
type 是指文件系统类型,下面列举几个常用的:

Linux file systems: ext2, ext3, jfs, reiserfs, reiser4, xfs, swap.
Windows:
vfat = FAT 32, FAT 16
ntfs= NTFS
Note: For NTFS rw ntfs-3g
CD/DVD/iso: iso9660
Network file systems:
nfs: server:/shared_directory /mnt/nfs nfs <options> 0 0
smb: //win_box/shared_folder /mnt/samba smbfs rw,credentials=/home/user_name/winbox-credentials.txt 0 0
auto: The file system type (ext3, iso9660, etc) it detected automatically. Usually works. Used for removable devices (CD/DVD, Floppy drives, or USB/Flash drives) as the file system may vary on thesedevices.

(4)挂载选项
rules 是指挂载时的规则。下面列举几个常用的:
auto 开机自动挂载
default 按照大多数永久文件系统的缺省值设置挂载定义
noauto 开机不自动挂载
nouser 只有超级用户可以挂载
ro 按只读权限挂载
rw 按可读可写权限挂载
user 任何用户都可以挂载
请注意光驱和软驱只有在装有介质时才可以进行挂载,因此它是noauto

(5)dump选项
这一项为0,就表示从不备份。如果上次用dump备份,将显示备份至今的天数。

(6)fsck选项
order 指fsck(启动时fsck检查的顺序)。为0就表示不检查,(/)分区永远都是1,其它的分区只能从2开始,当数字相同就同时检查(但不能有两1)。

如果我要把第二个IDE插槽主硬盘上的windows C 区挂到文件系统中,那么数据项是:

/dev/hdc1 /c vfat defaults 0 0

(/c 是事先建立的文件夹,作为c盘的挂载点。)

当你修改了/etc/fstab后,一定要重新引导系统才会有效。

fstab中存放了与分区有关的重要信息,其中每一行为一个分区记录,每一行又可分为六个部份,下面以/dev/hda7 / ext2 defaults 1 1为例逐个说明:

1. 第一项是您想要mount的储存装置的实体位置,如hdb或如上例的/dev/hda7。

2. 第二项就是您想要将其加入至哪个目录位置,如/home或如上例的/,这其实就是在安装时提示的挂入点。

3. 第三项就是所谓的local filesystem,其包含了以下格式:如ext、ext2、msdos、iso9660、nfs、swap等,或如上例的ext2,可以参见/prco/filesystems说明。

4. 第四项就是您mount时,所要设定的状态,如ro(只读)或如上例的defaults(包括了其它参数如rw、suid、exec、auto、nouser、async),可以参见「mount nfs」。

5. 第五项是提供DUMP功能,在系统DUMP时是否需要BACKUP的标志位,其内定值是0。

6. 第六项是设定此filesystem是否要在开机时做check的动作,除了root的filesystem其必要的check为1之外,其它皆可视需要设定,内定值是0。



参考链接:

http://diamonder.blog.51cto.com/159220/282542

http://blogold.chinaunix.net/u1/55527/showart_449692.html

(设置fstab需要参考的资料)
http://ubuntuforums.org/showthread.php?t=283131

http://www.tuxfiles.org/linuxhelp/fstab.html
注意主要的问题就在于第四列中的option设置,这里引用上面一文中的设置内容:

4th column: Mount options >

The fourth column in fstab lists all the mount options for the device or partition. This is also the most confusing column in the fstab file, but knowing what some of the most common options mean, saves you from a big headache. Yes, there are many options available, but I'll take a look at the most widely used ones only. For more information, check out the man page of mount.

auto and noauto With the auto option, the device will be mounted automatically (at bootup, just like I told you a bit earlier, or when you issue the mount -a command).auto is the default option. If you don't want the device to be mounted automatically, use the noauto option in /etc/fstab. With noauto, the device can be mounted only explicitly.

user and nouser These are very useful options. The user option allows normal users to mount the device, whereas nouser lets only the root to mount the device. nouseris the default, which is a major cause of headache for new Linux users. If you're not able to mount your cdrom, floppy, Windows partition, or something else as a normal user, add the user option into /etc/fstab.

exec and noexec exec lets you execute binaries that are on that partition, whereas noexec doesn't let you do that. noexec might be useful for a partition that contains binaries you don't want to execute on your system, or that can't even be executed on your system. This might be the case of a Windows partition.

exec is the default option, which is a good thing. Imagine what would happen if you accidentally used the noexec option with your Linux root partition...

ro Mount the filesystem read-only.

rw Mount the filesystem read-write. Again, using this option might cure the headache of many new Linux users who are tearing their hair off because they can't write to their floppies, Windows partitions, or something else.

sync and async How the input and output to the filesystem should be done. sync means it's done synchronously. If you look at the example fstab, you'll notice that this is the option used with the floppy. In plain English, this means that when you, for example, copy a file to the floppy, the changes are physically written to the floppy at the same time you issue the copy command.

However, if you have the async option in /etc/fstab, input and output is done asynchronously. Now when you copy a file to the floppy, the changes may be physically written to it long time after issuing the command. This isn't bad, and may sometimes be favorable, but can cause some nasty accidents: if you just remove the floppy without unmounting it first, the copied file may not physically exist on the floppy yet!

async is the default. However, it may be wise to use sync with the floppy, especially if you're used to the way it's done in Windows and have a tendency to remove floppies before unmounting them first.

defaults Uses the default options that are rw, suid, dev, exec, auto, nouser, and async.


另外,附上一点小技巧,如何保存man文档?
man command | col -b > output.txt
如,要把ls命令的man页输出到ls.txt文件,只需要:
  man ls | col -b > ls.txt


posted on 2011-10-04 00:20 deercoder 阅读(23603) 评论(4)  编辑 收藏 引用 所属分类: Unix/Linux

评论:
# re: Ubuntu下硬盘的自动挂载 2011-10-04 11:57 | 陈梓瀚(vczh)
真麻烦啊,还要自己挂载。  回复  更多评论
  
# re: Ubuntu下硬盘的自动挂载 2011-10-04 12:07 | 刘畅
@陈梓瀚(vczh)
还好吧,写好配置文件就直接OK了,每次启动就会自动挂载滴~  回复  更多评论
  
# re: Ubuntu下硬盘的自动挂载 2011-10-04 13:06 | 博洋家纺
还要自己挂载  回复  更多评论
  
# re: Ubuntu下硬盘的自动挂载 2011-10-04 18:24 | 钕铁硼磁铁工厂
很多年没有玩这个了,学习了。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理