Skip to content

Instantly share code, notes, and snippets.

@poad
Created August 18, 2017 08:36
Show Gist options
  • Save poad/4397f6781c2eaf19e35bd61ced0ed79b to your computer and use it in GitHub Desktop.
Save poad/4397f6781c2eaf19e35bd61ced0ed79b to your computer and use it in GitHub Desktop.
[備忘録] 自作のVagrant Boxを小さくする(Debian系) ref: http://qiita.com/poad1010/items/061523e2d1911cb0d07d
# 共有するBoxの場合、Vagrant Boxの鍵をinsecureなmaster keyに置き換えておく
curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub > ~/.ssh/authorized_keys && \
chmod 740 .ssh && \
chmod 640 .ssh/authorized_keys
# sudoして作業するのが面倒なので、
sudo -s
# 最新化
apt -qq update && apt -qq -y upgrade && \
apt -y --purge autoremove && apt autoclean && ¥
rm -rf /var/lib/apt/lists/* && \
apt-get clean
# 共有フォルダーをアンマウントしておく
umount /vagrant
rm -rf /vagrant/*
# /var/log の中を簡単にお掃除
cd /var/log
rm *.gz *.1
for dir in $(find /var/log -maxdepth 1 -type d); do cd $dir; rm *.gz *.1; done
# デフラグする
cd
e4defrag /
# 0埋めする
dd if=/dev/zero of=/EMPTY bs=1M || :; rm /EMPTY
# swapも0埋めする
swap_device_uuid=`sudo /sbin/blkid -t TYPE=swap -o value -s UUID | uniq`
swap_device_label=`sudo /sbin/blkid -t TYPE=swap -o value -s LABEL | uniq`
if [ -n "$swap_device_uuid" ]; then
swap_device=`readlink -f /dev/disk/by-uuid/"$swap_device_uuid"`
elif [ -n "$swap_device_label" ]; then
swap_device=`readlink -f /dev/disk/by-label/"$swap_device_label"`
fi
/sbin/swapoff "$swap_device"
dd if=/dev/zero of="$swap_device" bs=1M || :
/sbin/mkswap ${swap_device_label:+-L "$swap_device_label"} ${swap_device_uuid:+-U "$swap_device_uuid"} "$swap_device"
# もっかい /var/log をお掃除
find /var/log -type f | while read f; do echo -ne '' > $f; done
# historyを削除
echo "" > ~/.bash_history ; history -c
exit
echo "" > ~/.bash_history ; history -c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment