這裏主要參考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
或者重啓就能夠了。