LoginSignup
2
2

More than 5 years have passed since last update.

[備忘録] 自作のVagrant Boxを小さくする(Debian系)

Posted at

目的

自作のVagrant Boxをpackageするため、package後のファイルサイズが小さくなるようにする

コマンド

bash
# 共有する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
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2