这里主要参考https://gist.github.com/adrienbrault/3775253
这里是脚本内容 :
#!/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就可以了。
debian下使用#!/bin/bash替换#!/bin/sh
打包的容量从5.1G下降到2.3G,效果还是很明显的。
上面打包得到的镜像文件,这个在编译文件的时候遇到问题:
解决: g++: internal compiler error: Killed (program cc1plus)
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分区。
1. 确定swap分区没有挂载:
cat /proc/swaps
Filename Type Size Used Priority
上面没有显示挂载的分区,那就是没有挂载上。
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
或者重启就可以了。