外挂磁盘作swap

外挂磁盘作swap

在开发中遇到

git fatal: Out of memory, malloc failed

我遇到的这个问题是因为内存不足。解决办法增加swap。由于OS安装盘磁盘空间有限,需要外挂磁盘作swap。

1.外挂一个磁盘到OS

1.1查看硬盘信息

[root@localhost workspace]# fdisk -l

Disk /dev/xvda: 2000 MB, 2000683008 bytes
255 heads, 63 sectors/track, 243 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1         243     1951866   83  Linux

Disk /dev/xvdd: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/xvdd doesn't contain a valid partition table

Disk /dev/xvdc: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/xvdc doesn't contain a valid partition table

可以看到刚刚分配的4G磁盘/dev/xvdc。

1.2创建新硬盘分区

fdisk可以用m命令来看fdisk命令的内部命令;

a:命令指定启动分区;

d:命令删除一个存在的分区;

l:命令显示分区ID号的列表;

m:查看fdisk命令帮助;

n:命令创建一个新分区;

p:命令显示分区列表;

t:命令修改分区的类型ID号;

w:命令是将对分区表的修改存盘让它发生作用

[root@localhost workspace]# fdisk /dev/xvdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help):  n //创建一个新的分区
Command action
   e   extended//输入e为创建扩展分区  
   p   primary partition (1-4)//输入p为创建逻辑分区
p
Partition number (1-4): 1//划分逻辑分区
First cylinder (1-522, default 1)://我这里直接回车,是不想把该磁盘分成多个分区,把整个磁盘作为1个分区
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-522, default 522):
Using default value 522

Command (m for help): write//保存分区
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

再次使用”fdisk -l”命令查看磁盘信息

[root@localhost workspace]# fdisk -l

Disk /dev/xvda: 2000 MB, 2000683008 bytes
255 heads, 63 sectors/track, 243 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1         243     1951866   83  Linux

Disk /dev/xvdd: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/xvdd doesn't contain a valid partition table

Disk /dev/xvdc: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdc1               1         522     4192933+  83  Linux

磁盘/dev/xvdc已经分区好了

1.3格式化分区

[root@localhost workspace]# mkfs.ext3 /dev/xvdc
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
524288 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost workspace]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1            1.9G  1.6G  240M  87% /
none                  246M     0  246M   0% /dev/shm
/dev/xvdd              30G  4.9G   24G  18% /root/workspace

1.4挂载分区以及开机自动挂载

挂载用mount命令即可,例如将/dev/xvdc磁盘挂载到/fanswap目录下

# mount /dev/xvdc /fanswap/

开机自动挂载,编辑/etc/fstab文件,再最后面添加

磁盘分区 挂载目录  ext3(文件格式)defaults 0 0

例如

[root@localhost vmagentless]# vi /etc/fstab
/dev/xvda1      /               ext3    defaults        1  1
none            /dev/pts        devpts  defaults        0  0
none            /dev/shm        tmpfs   defaults        0  0
none            /proc           proc    defaults        0  0
none            /sys            sysfs   defaults        0  0
/dev/xvdd       /root/workspace ext3    defaults        0  0
/dev/xvdc       /fanswap        ext3    defaults        0  0

重启就可以看到自动挂载生效了。

2.使用磁盘作swap

比如在之前挂载的磁盘fanswap中创建swap,首先切换到fanswap目录

cd /fanswap

使用free -m可以查看磁盘分区,是否有swap

2.1增加一个myswap文件

# dd if=/dev/zero of=myswap bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 13.5865 seconds, 158 MB/s

bs指的是Block Size,就是每一块的大小。这里的例子是1M,意思就是count的数字,是以1M为单位的。

count是告诉程序,新的swapfile要多少个block。这里是2048,就是说,新的swap文件是2G大小。

2.2创建交换空间

mkswap myswap

mkswap myswap
Setting up swapspace version 1, size = 2147479 kB

2.3启动新增加的交换空间

# swapon myswap

2.4修改/etc/fstab,使新加的交换空间在系统重新启动后自动生效

/fanswap/myswap   swap            swap    defaults        0  0

留下评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据