Archive

SED – change something inline

If you coming from linux side, you probably always used to:

sed -i 's/foo/bar/' filename

but with OSX sed it doesn’t work. what you need to add is file extension that you want to have your backup:

sed -i.bak 's/foo/bar/' filename

[FREEBSD] How to clean up swap from old rubbish

You want to clear your swap – all articles are saying – just do swapoff/swapon but you getting error:
swapoff: /dev/xbd25: Cannot allocate memory
What to do? Easy, make some file with size of your swap, add it as swap, turn off old one, turn on old one and remove the file! Easy:

dd if=/dev/zero of=/usr/jails/swap0 bs=1m count=10240
#10gb of swap
mdconfig -a -t vnode -f /usr/jails/swap0 -u 0
mdconfig -l -v md0    vnode       10G    /usr/jails/swap0
swapon /dev/md0
swapinfo 
    Device          1K-blocks     Used    Avail Capacity
    /dev/xbd25       15728640  7773260  7955380    49%
    /dev/md0         10485760        0 10485760     0%
    Total            26214400  7773260 18441140    30%
swapoff /dev/xbd25
swapon /dev/xbd2
swapoff /dev/md0

How do I deal with NET:ERR_CERT_AUTHORITY_INVALID in Chrome? (OSX)

Lately chrome updated their HSTS and cert security – that made pain in the ass for any web developer who has their own virtual machine with https that they are developing on.
The simply solution is to add your self-signed cert to mac keychains:

export HOST="your_ip:443"
openssl s_client -connect $HOST -showcerts </dev/null 2>/dev/null|openssl x509 -outform PEM > /tmp/mycert.crt
sudo security add-trusted-cert -d -r trustRoot -k "/Library/Keychains/System.keychain" "/tmp/mycert.crt"

Enjoy! 🙂

Python tricks & tips

Generate requirements.txt file from your project needs:

pip install pipreqs
pipreqs .

Check all unneeded imports in your python file:

pip install --upgrade pyflakes
pip install --upgrade autoflake
autoflake file.py

Delete inline all unneded imports in your python file:

pip install --upgrade pyflakes
pip install --upgrade autoflake
autoflake -i file.py

Resize disk / detect new disk cheatsheet

When you resized virtual disk on your hypervisor, you would love to have space visible in your linux, for that you need to resize block device by command

# for i in `ls /sys/block/`; do echo 1 > /sys/block/$i/device/rescan ; done
-bash: /sys/block/dm-0/device/rescan: No such file or directory
-bash: /sys/block/dm-1/device/rescan: No such file or directory
-bash: /sys/block/dm-2/device/rescan: No such file or directory
-bash: /sys/block/fd0/device/rescan: No such file or directory

(it’s actually going thru all devices that you have), if you know device name you just need to do:
echo 1 > /sys/block/DEVICE_NAME/device/rescan

And of course, you need to resize your pv. If you know device name just run (let’s say it’s sda):

#pvresize /dev/sda

If not you have oneliner:
#for i in `pvs --noheadings | awk '{print $1}'`; do pvresize $i ; done 

This will resize all that needs resizing 🙂 Now you can see correct space

How to add swapfile to your linux system

We are creating one swapfile of size 1M * 1024 = 1024M = 1GiB 🙂

dd if=/dev/zero of=/swapfile bs=1M count=1024
chown root:root /swapfile
chmod 0600 /swapfile
mkswap /swapfile
swapon /swapfile

if you want to have this swap file peramanent you have two options:
worse – add swapon /swapfile to /etcc/rc.d/rc.localbetter – add it to /etc/fstab
/swapfile1 none swap sw 0 0