Linux中的shm和tmpfs介绍以及ORA-00845 MEMORY_TARGET not supported on this system

0    429    3

Tags:

👉 本文共约6742个字,系统预计阅读时间或需26分钟。

简介

linux中/dev目录下一般都是一些设备文件,例如磁盘、内存、摄像头等。

使用redhat系列的操作系统,可以发现系统默认挂载了/dev/shm,挂载类型为tmpfs。tmpfs(temporary filesystem)是Linux特有的文件系统,标准挂载点是/dev/shm.

/dev/shm共享内存

/dev/shm这个目录是linux下一个利用内存虚拟出来的一个目录,这个目录中的文件都是保存在内存中,而不是磁盘上。其大小是非固定的,即不是预先分配好的内存来存储的。(shm =shared memory)

默认的Linux发行版中的内核配置都会开启tmpfs,映射到了/dev/下的shm目录。可以通过df 命令查看结果.

其他的几个tmpfs的挂载目录,其实质上于/dev/shm是一致的。

/dev/shm/是linux下一个非常有用的目录,因为这个目录不在硬盘上,而是在内存里。因此在linux下,就不需要大费周折去建ramdisk,直接使用/dev/shm/就可达到很好的优化效果。

tmpfs临时文件系统

tmpfs(temporary filesystem)是Linux/Unix系统上的一种基于内存的虚拟文件系统。tmpfs可以使用您的内存或swap分区来存储文件(即它的存储空间在virtual memory 中, VM由real memory和swap组成)。tmpfs有些像虚拟磁盘(ramdisk),但不一样。像虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储。而且传统的虚拟磁盘是个块设备,并需要一个 mkfs 之类的命令才能真正地使用它,tmpfs 是一个文件系统,而不是块设备;您只是安装它,它就可以使用了。

tmpfs有以下优势:
1、动态文件系统的大小,/dev/shm/需要注意的一个是容量问题,在linux下,它默认最大为内存的一半大小,在Docker容器中,/dev/shm/目录默认情况下是64MB,使用df -h命令可以看到。但它并不会真正的占用这块内存,如果/dev/shm/下没有任何文件,它占用的内存实际上就是0字节;如果它最大为1G,里头放有 100M文件,那剩余的900M仍然可为其它应用程序所使用,但它所占用的100M内存,是绝不会被系统回收重新划分的
2、tmpfs 的另一个主要的好处是它闪电般的速度。因为典型的 tmpfs 文件系统会完全驻留在 RAM 中,读写几乎可以是瞬间的。
3、tmpfs 数据在重新启动之后不会保留,因为虚拟内存本质上就是易失的。所以有必要做一些脚本做诸如加载,绑定的操作。

修改/dev/shm大小

默认为最大一半内存大小在某些场合可能不够用,并且默认的inode数量很低一般都要调高些,这时可以用mount命令来管理它。

在2G的机器上,将最大容量调到1.5G,并且inode数量调到1000000,这意味着大致可存入最多一百万个小文件。
如果需要永久修改/dev/shm的值,需要修改/etc/fstab

/dev/shm应用

首先在/dev/shm建个tmp文件夹,然后与实际/tmp绑定:

  在使用mount –bind olderdir newerdir命令来挂载一个目录到另一个目录后,newerdir的权限和所有者等所有信息会发生变化。挂载后的目录继承了被挂载目录的所有属性,除了名称。

ORA-00845: MEMORY_TARGET not supported on this system

Oracle 11g的AMM(Automatic Memory Management)内存管理模式就是使用/dev/shm;若使用ASMM(Automatic Shared Memory Management),则不会使用/dev/shm,也就不会报错。

所以有时候修改MEMORY_MAX_TARGET会出现ORA-00845的错误。

简单来说就是 MEMORY_MAX_TARGET 的设置不能超过 /dev/shm 的大小

永久修改,需要修改/etc/fstab:

临时修改,可以使用命令:

由于docker容器中没有/etc/fstab文件,所以我们只能每次重启容器后都修改一下/dev/shm大小。  

ORA-00845错误模拟

在Oracle 11g中如果采用AMM内存管理,那么当MEMORY_TARGET的值大于/dev/shm的时候,就会报ORA-00845: MEMORY_TARGET not supported on this system错误,解决办法增加/dev/shm大小,在redhat系列系统中,/dev/shm的默认值是系统总内存的一半

1、错误重现

2、修改/dev/shm大小

3、启动数据库验证

4、官方解释
Starting with Oracle Database 11g, the Automatic Memory Management feature requires more shared memory (/dev/shm)and file descriptors. The size of the shared memory should be at least the greater of MEMORY_MAX_TARGET and MEMORY_TARGET for each Oracle instance on the computer. If MEMORY_MAX_TARGET or MEMORY_TARGET is set to a non zero value, and an incorrect size is assigned to the shared memory, it will result in an ORA-00845 error at startup.

5、解决问题建议
5.1. If you are installing Oracle 11g on a Linux system, note that Memory Size (SGA and PGA), which sets the initialization parameter MEMORY_TARGET or MEMORY_MAX_TARGET, cannot be greater than the shared memory filesystem (/dev/shm) on your operating system. To resolve the current error, increase the /dev/shm file size.

5.2. If configuring AMM is not possible due to lack of space on /dev/shm mount point, you can configure ASMM instead of AMM, i.e. set SGA_TARGET, SGA_MAX_SIZE and PGA_AGGREGATE_TARGET instead of MEMORY_TARGET.

在redhat6中,修改了fstab中修改后,不能生效,需要重新挂载

ORA-00845: MEMORY_TARGET示例2

Linux操作系统,oracle 11.2.0.4 启动实例时出现如下错误:

查看错误帮助信息

错误原因是这个操作系统不支持MEMORY_TARGET参数或/dev/shm在Linux上的大小不正确造成的,这是该操作系统上的第二个实例,第一个实例设置了MEMORY_TARGET参数,所以并不是不支持这个参数,原因就只有/dev/shm大小不正确了,解决方法是要将/dev/shm的最小值设置为操作系统上运行实例SGA_MAX_SIZE所设置的大小。/dev/shm/是linux下一个目录,/dev/shm目录不在磁盘上,而是在内存里,因此使用linux /dev/shm/的效率非常高,直接写进内存。
tmpfs有以下特点:
1.tmpfs 是一个文件系统,而不是块设备;您只是安装它,它就可以使用了。
2.动态文件系统的大小。
3.tmpfs 的另一个主要的好处是它闪电般的速度。因为典型的 tmpfs 文件系统会完全驻留在 RAM 中,读写几乎可以是瞬间的。
4.tmpfs 数据在重新启动之后不会保留,因为虚拟内存本质上就是易失的。所以有必要做一些脚本做诸如加载、绑定的操作。

linux下/dev/shm的容量默认最大为内存的一半大小,使用df -h命令可以看到。但它并不会真正的占用这块内存,如果/dev/shm/下没有任何文件,它占用的内存实际上就是0字节;如果它最大为1G,里头放有100M文件,那剩余的900M仍然可为其它应用程序所使用,但它所占用的100M内存,是绝不会被系统回收重新划分的。

linux /dev/shm容量(大小)是可以调整,在有些情况下(如oracle数据库)默认的最大一半内存不够用,并且默认的inode数量很低一般都要调高些,这时可以用mount命令来管理它。
mount -o size=1500M -o nr_inodes=1000000 -o noatime,nodiratime -o remount /dev/shm
在2G的机器上,将最大容量调到1.5G,并且inode数量调到1000000,这意味着大致可存入最多一百万个小文件通过/etc/fstab文件来修改/dev/shm的容量(增加size选项即可),修改后,重新挂载即可。

这里该实例的SGA_MAX_SIZE为1G,下面的命令查看/dev/shm的大小。

从上面结果可以看到/dev/shm可用大小只有0.7G,执行下面的命令来进行修改。

卸载/dev/shm,但/dev/shm正被访问

用fuser处理,fuser命令,-k:kill processes accessing the named file(杀死所有正在访问指定文件的进程),-m 表示指定文件所在的文件系统或者块设备(处于 mount 状态)。所有访问该文件系统的进程都被列出。

再重新启动实例

小结:Oracle 11g的AMM内存管理模式就是使用/dev/shm,所以有时候修改MEMORY_TARGET或者MEMORY_MAX_TARGET会出现ORA-00845的错误,在安装配置实例内存时为了避免出现这个故障可以对Linux系统中的/dev/shm进行调整,让其可用大小至少等于实例的sga_max_size。

其它虚拟内核文件系统

虚拟内核文件系统(VirtualKernel File Systems),是指那些是由内核产生但不存在于硬盘上(存在于内存中)的文件系统。例如

1、proc

proc文件系统为操作系统本身和应用程序之间的通信提供了一个安全的接口。通过它里面的一些文件,可以获取系统状态信息并修改某些系统的配置信息。当我们在内核中添加了新功能或设备驱动时,经常需要得到一些系统状态的信息,一般这样的功能需要经过一些像ioctl()这样的系统调用来完成。

2、devfs

我们知道,/dev目录下的每一个文件都对应的是一个设备,devfs也是挂载于/dev目录下。在2.6内核以前使用devfs来提供一种类似于文件的方法来管理位于/dev目录下的所有设备。但是devfs文件系统有一些缺点,有时一个设备映射的设备文件可能不同。例如,我的U盘可能对应sda,也可能对应sdb,没有足够的主/辅设备号,当设备过多的时候,显然这会成为一个问题。

3、sysfs

为了克服devfs的上述问题,2.6内核引入了一个新的文件系统sysfs,它挂载于/sys目录下。sysfs文件系统把连接在系统上的设备和总线组织成为一个分级的文件,用户空间的程序同样可以利用这些信息,以实现和内核的交互。sysfs文件系统是当前系统上实际设备树的一个直观反映,它是通过kobject子系统来建立这个信息的,当一个kobject被创建的时候,对应的文件和目录也就被创建了。

4、tmpfs

tmpfs(temporary filesystem)是Linux特有的文件系统,标准挂载点是/dev/shm.

Tmpfsis a file system which keeps all files in virtual memory.

Everythingin tmpfs is temporary in the sense that no files will be
created on your hard drive. If you unmount a tmpfs instance,
everything stored therein is lost.

tmpfsputs everything into the kernel internal caches and grows and
shrinks to accommodate the files it contains and is able to swap
unneeded pages out to swap space. It has maximum size limits which can
be adjusted on the fly via ‘mount -o remount …’

Ifyou compare it to ramfs (which was the template to create tmpfs)
you gain swapping and limit checking. Another similar thing is the RAM
disk (/dev/ram*), which simulates a fixed size hard disk in physical
RAM, where you have to create an ordinary filesystem on top. Ramdisks
cannot swap and you do not have the possibility to resize them.

Sincetmpfs lives completely in the page cache and on swap, all tmpfs
pages currently in memory will show up as cached. It will not show up
as shared or something like that. Further on you can check the actual
RAM+swap use of a tmpfs instance with df(1) and du(1).

tmpfshas the following uses:

1)There is always a kernel internal mount which you will not see at
all. This is used for shared anonymous mappings and SYSV shared
memory.

Thismount does not depend on CONFIG_TMPFS. If CONFIG_TMPFS is not
set, the user visible part of tmpfs is not build. But the internal
mechanisms are always present.

2)glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
POSIX shared memory (shm_open, shm_unlink). Adding the following
line to /etc/fstab should take care of this:

tmpfs/dev/shm tmpfs defaults 0 0

Rememberto create the directory that you intend to mount tmpfs on
if necessary (/dev/shm is automagically created if you use devfs).

Thismount is not needed for SYSV shared memory. The internal
mount is used for that. (In the 2.3 kernel versions it was
necessary to mount the predecessor of tmpfs (shm fs) to use SYSV
shared memory)

3)Some people (including me) find it very convenient to mount it
e.g. on /tmp and /var/tmp and have a big swap partition. But be
aware: loop mounts of tmpfs files do not work due to the internal
design. So mkinitrd shipped by most distributions will fail with a
tmpfs /tmp.

4)And probably a lot more I do not know about

tmpfshas a couple of mount options:

size:The limit of allocated bytes for this tmpfs instance. The
default is half of your physical RAM without swap. If you
oversize your tmpfs instances the machine will deadlock
since the OOM handler will not be able to free that memory.
nr_blocks: The same as size, but in blocks of PAGECACHE_SIZE.
nr_inodes: The maximum number of inodes for this instance. The default
is half of the number of your physical RAM pages.

Theseparameters accept a suffix k, m or g for kilo, mega and giga and
can be changed on remount.

Tospecify the initial root directory you can use the following mount
options:

mode:The permissions as an octal number
uid: The user id
gid: The group id

Theseoptions do not have any effect on remount. You can change these
parameters with chmod(1), chown(1) and chgrp(1) on a mounted filesystem.

So‘mount -t tmpfs -o size=10G,nr_inodes=10k,mode=700 tmpfs /mytmpfs’
will give you tmpfs instance on /mytmpfs which can allocate 10GB
RAM/SWAP in 10240 inodes and it is only accessible by root.

TODOs:

1)give the size option a percent semantic: If you give a mount option
size=50% the tmpfs instance should be able to grow to 50 percent of
RAM + swap. So the instance should adapt automatically if you add
or remove swap space.
\2) loop mounts: This is difficult since loop.c relies on the readpage
operation. This operation gets a page from the caller to be filled
with the content of the file at that position. But tmpfs always has
the page and thus cannot copy the content to the given page. So it
cannot provide this operation. The VM had to be changed seriously
to achieve this.
\3) Show the number of tmpfs RAM pages. (As shared?)

Author:
Christoph Rohland , 1.12.01

Doc ID 1399209.1

Starting with Oracle Database 11g, the Automatic MemoryManagement feature requires more shared memory (/dev/shm) and file descriptors.The size of the shared memory must be at least the greater of theMEMORY_MAX_TARGET and MEMORY_TARGET parameters for each Oracle instance on thecomputer. If the MEMORY_MAX_TARGET parameter or the MEMORY_TARGET parameter isset to a nonzero value, and an incorrect size is assigned to the shared memory,it results in an ORA-00845 error at startup.
On Linux systems, if the operating system /dev/shm mount size is too small forthe Oracle system global area (SGA) and program global area (PGA), then youencounter the following error:

The cause of this error is an insufficient /dev/shm allocation.The total memory size of the SGA and PGA, which sets the initializationparameter MEMORY_TARGET or MEMORY_MAX_TARGET, cannot be greater than the sharedmemory file system (/dev/shm) on your operating system.

Linux umount 报 device is busy 的处理方法

今天在IDC 辐射了半天,又弄了套DG。 在Linux 挂盘这块也小学了两招。

一. umout 移动硬盘

开始用sftp 将安装文件copy到服务器的时候,速度太慢了,500k/s。几个G的东西,copy 这些就要半个多小时,扛不住,拿移动硬盘来copy了。结果移动硬盘的格式不对。 是NTFS 格式,Linux 识别不了。 只能格式化成FAT32的。 而GG 的win7 系统又不具备格式化成FAT32的功能。 有点小变态。让同事在XP 下帮我格式化了。

安装文件copy到服务器后,同事直接将移动硬盘从服务器上拔下来了。 导致的结果是,用df 命令查看,挂载的移动硬盘还存在。

[root@qs-wg-db1 ~]# df -lh

Filesystem Size Used Avail Use% Mounted on

/dev/sdb3 125G 3.3G 115G 3% /

/dev/sdb1 99M 12M 82M 13% /boot

tmpfs 3.9G 0 3.9G 0% /dev/shm

/dev/sda1 275G 72G 189G 28% /u01

/dev/sdc1 10G 2.0G 8.1G 20% /datatmp

就是这个/dev/sdc1。

这时使用umount 命令,会提示设备忙,无法挂载。

处理方法:

[root@qs-wg-db1 ~]# fuser -km /datatmp

本人提供Oracle(OCP、OCM)、MySQL(OCP)、PostgreSQL(PGCA、PGCE、PGCM)等数据库的培训和考证业务,私聊QQ646634621或微信db_bao,谢谢!

[root@qs-wg-db1 ~]# df -lh

Filesystem Size Used Avail Use% Mounted on

/dev/sdb3 125G 3.3G 115G 3% /

/dev/sdb1 99M 12M 82M 13% /boot

tmpfs 3.9G 0 3.9G 0% /dev/shm

/dev/sda1 275G 72G 189G 28% /u01

/dev/sdc1 10G 2.0G 8.1G 20% /datatmp

[root@qs-wg-db1 ~]# umount /datatmp

[root@qs-wg-db1 ~]# df -lh

Filesystem Size Used Avail Use% Mounted on

/dev/sdb3 125G 3.3G 115G 3% /

/dev/sdb1 99M 12M 82M 13% /boot

tmpfs 3.9G 0 3.9G 0% /dev/shm

/dev/sda1 275G 72G 189G 28% /u01

成功umount了。

二. umount 光驱

安装DB 之前,检查了一下相关包,少了3个。 从系统安装盘上找了包,安装了一下。 当时是直接将/dev/cdrom mount 到了/mnt目录。 也是图个方便。 结果收工时去拿盘,光驱弹不出来。 同事让我把cdrom umout掉。 同样的提示,设备忙。

处理方法:

[root@qs-wg-db1 ~]#fuser –km /dev/cdrom

[root@qs-wg-db1 ~]#eject -- 弹出光驱

在网上搜了一下,正确挂载CD-ROM的方法应该如下:

# mkdir cdrom
# mount /dev/cdrom /mnt/cdrom
或者

# mount /dev/cdrom /media/cdrom

直接挂载在/mnt,/media等系统目录下,在umount时会出现出错信息“umount: /mnt/cdrom: device is busy”的情况。

如果一个文件系统处于“busy”状态的时候,不能卸载该文件系统。如下情况将导致文件系统处于“busy”状态:

三. fuser 命令

[root@qs-wg-db1 ~]# man fuser

FUSER(1) User Commands FUSER(1)

NAME

SYNOPSIS

DESCRIPTION

fuser outputs only the PIDs to stdout, everything else is sent to stderr.

OPTIONS

FILES

fuser 命令显示访问某个文件的进程的PID. 其中-k 和 -m 参数上面红色部分有说明。-k 是kill 访问这个文件的进程。 没有进程访问,就可以成功umount了.

参考

https://www.cnblogs.com/haoxiaoyu/p/f53e68d2d09ba861dbdc1fa521f1a54d.html

标签:

头像

小麦苗

学习或考证,均可联系麦老师,请加微信db_bao或QQ646634621

您可能还喜欢...

发表回复

嘿,我是小麦,需要帮助随时找我哦
  • 18509239930
  • 个人微信

  • 麦老师QQ聊天
  • 个人邮箱
  • 点击加入QQ群
  • 个人微店

  • 回到顶部
返回顶部