vagrant压缩打包镜像容量

这里主要参考https://gist.github.com/adrienbrault/3775253python

这里是脚本内容 :linux

#!/bin/sh

# Credits to:
#  - http://vstone.eu/reducing-vagrant-box-size/
#  - https://github.com/mitchellh/vagrant/issues/343

aptitude -y purge ri
aptitude -y purge installation-report landscape-common wireless-tools wpasupplicant ubuntu-serverguide
aptitude -y purge python-dbus libnl1 python-smartpm python-twisted-core libiw30
aptitude -y purge python-twisted-bin libdbus-glib-1-2 python-pexpect python-pycurl python-serial python-gobject python-pam python-openssl libffi5
apt-get purge -y linux-image-3.0.0-12-generic-pae

# Remove APT cache
apt-get clean -y
apt-get autoclean -y

# Zero free space to aid VM compression
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY

# Remove bash history
unset HISTFILE
rm -f /root/.bash_history
rm -f /home/vagrant/.bash_history

# Cleanup log files
find /var/log -type f | while read f; do echo -ne '' > $f; done;

# Whiteout root
count=`df --sync -kP / | tail -n1  | awk -F ' ' '{print $4}'`; 
let count--
dd if=/dev/zero of=/tmp/whitespace bs=1024 count=$count;
rm /tmp/whitespace;
 
# Whiteout /boot
count=`df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}'`;
let count--
dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count;
rm /boot/whitespace;
 
swappart=$(cat /proc/swaps | grep -v Filename | tail -n1 | awk -F ' ' '{print $1}')
if [ "$swappart" != "" ]; then
  swapoff $swappart;
  dd if=/dev/zero of=$swappart;
  mkswap $swappart;
  swapon $swappart;
fi

上面最后会输出swap分区的新的uuid,把这个新的uuid填到/etc/fstab就能够了。git

debian下使用#!/bin/bash替换#!/bin/shgithub

打包的容量从5.1G降低到2.3G,效果仍是很明显的。ubuntu

上面打包获得的镜像文件,这个在编译文件的时候遇到问题:bash

解决: g++: internal compiler error: Killed (program cc1plus)app

g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
 
主要缘由大致上是由于内存不足,有点坑 临时使用交换分区来解决吧
 
sudo dd if=/dev/zero of=/swapfile bs=64M count=16
sudo mkswap /swapfile
sudo swapon /swapfile

After compiling, you may wish to

Code:
sudo swapoff /swapfile
sudo rm /swapfile

最后发现是vagrant镜像从新初始化后,swap分区没有挂载上致使上面的状况出现,咱们要从新挂载这个swap分区。less

1. 肯定swap分区没有挂载:curl

cat /proc/swaps 
Filename                                Type            Size    Used    Priority

上面没有显示挂载的分区,那就是没有挂载上。ide

2.肯定swap分区所在:

#fdisk -l
Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 40136703 40134656 19.1G 83 Linux
/dev/sda2       40138750 41940991  1802242  880M  5 Extended
/dev/sda5       40138752 41940991  1802240  880M 82 Linux swap / Solaris

能够看到/dev/sda5 的分区类型是linux swap / Solaris,这个就是咱们要挂载的swap分区。

3. 肯定swap分区的uuid,

# blkid /dev/sda5
/dev/sda5: UUID="c41a274b-d2b4-4f33-8188-e75da63adade" TYPE="swap" PARTUUID="1abd93b2-05"

/dev/sda5 的uuid就是c41a274b-d2b4-4f33-8188-e75da63adade

4.修改/etc/fstab,使系统自动挂载上。添加下面的选项:

UUID=c41a274b-d2b4-4f33-8188-e75da63adade none            swap    sw              0       0

5.挂载全部分区,

mount -a

或者重启就能够了。

相关文章
相关标签/搜索