Search This Blog

Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Tuesday, September 6, 2016

linux cheat sheet


# resources
ps -eo size,rss,pid,user,comm --sort -rss | head -n 30
ps -eo size,rss,pcpu,pid,user,comm --sort -pcpu | head -n 30

# memstat
atop -L160 1 1 | grep MEM

# caches

# chmod directories

# awk epoch to timestamp in logs
awk '{a = strftime("%F %T", $1); $1 = "" ; print a" "$0}'

# safe file delete

# some magic in resolv.conf

# default browser ( set default apps )
xdg-mime default firefox.desktop x-scheme-handler/http
xdg-mime default firefox.desktop x-scheme-handler/https

# keyboard
setxkbmap -layout us,ru -option grp:toggle

# qt crutch
export QT_GRAPHICSSYSTEM=native | raster | opengl

# swap usage by pid

# sqlite

# get url
wget -qO- http://foo/response
wget -qO- http://bar/api/json/response | python -m json.tool
wget -qO- http://bar/api/json/response | jq
wget -qO- http://bar/api/json/response | jq '.[] | select((.k_1 == 1) or (.k_2 == 2)) | "k1:\(.k_1) k2:\(.k_2)"'

# ssh/sshfs
sshfs -o reconnect -o auto_unmount 192.168.5.5:/mnt/data/usorted/ ~/sshfs/unsorted/

Monday, October 5, 2015

How to chmod all directories except files (recursively)


find /path/to/base/dir -type d -print0 | xargs -0 chmod 755
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

Wednesday, March 11, 2015

Delete file safe


shred -vuzn 5 /path/to/file

Monday, January 26, 2015

resolv.conf


nameserver 8.8.8.8
nameserver 4.2.2.2
# some magical options
options timeout:1 attempts:1

Friday, August 8, 2014

Swap usage by PID


#> for i in `find /proc -maxdepth 1 -type d|xargs -i basename {}`; do echo -n "PID: $i SWAP: "; cat /proc/$i/smaps|fgrep Swap|awk '{s=s+$2}END{print s}'; done|sort -k4n

WINE + winetricks + WoT + Steam

Small note about installation and configuration process

#> WINEARCH=win32 winecfg

#> wget http://winetricks.org/winetricks
#> chmod +x winetricks
#> sudo cp winetricks /usr/local/bin

#> cd .wine/drive_c/
#> winetricks  d3dx9_36 vcrun2008 corefonts wininet msls31
#> winetricks  msxml3
#> winetricks  ie8

#> wine  WoT_internet_install_ru.exe
#> winecfg
Libraries:  Add: msvcp110 (native, buildin), msvcr110 (native, buildin)

#> wine msiexec /i SteamInstall.msi
#> wine Steam/Steam.exe -no-dwrite

Friday, August 1, 2014

Xorg - adding undetected resolutions


Due to buggy hardware or drivers, your monitor's correct resolutions may not always be detected. For example, the EDID data block queried from your monitor may be incorrect.
If the mode already exists, but just isn't associated for the particular output, you can add it like this:
  • $ xrandr --addmode S-video 800x600
If the mode doesn't yet exist, you'll need to create it first by specifying a modeline:
  • $ xrandr --newmode <Mode``Line>
You may create a modeline using the gtf or cvt utility. For example, if you want to add a mode with resolution 800x600 at 60 Hz, you can enter the following command: (The output is shown following.)
  • $ cvt 800 600 60
    # 800x600 59.86 Hz (CVT 0.48M3) hsync: 37.35 kHz; pclk: 38.25 MHz
    Modeline "800x600_60.00"   38.25  800 832 912 1024  600 603 607 624 -hsync +vsync
Then copy the information after the word "Modeline" into the xrandr command:

$ xrandr --newmode "800x600_60.00"   38.25  800 832 912 1024  600 603 607 624 -hsync +vsync

Tuesday, July 29, 2014

X tuning

Some useful software for low level tuning X

xprop - WM classes and etc
xwininfo - window id*, position, style, owner and etc

 * xkill -id window_id_from_xwininfo

Tuesday, January 21, 2014

A little note about ALSA and x220i thinkpad


First of all you need to find out sound cards indexes.
#> cat /proc/asound/cards
0 [PCH]:         HDA-Intel - HDA Intel PCH
                 HDA Intel PCH at 0xf2520000 irq 42
29 [ThinkPadEC]: ThinkPad EC - ThinkPad Console Audio Control
                 ThinkPad Console Audio Control at EC reg 0x30, fw unknown
so HDA Intel PCH has index 0

#> /etc/asound.conf
pcm.!default {
   type hw
   card 0
}
ctl.!default {
   type hw
   card 0
}


cat /etc/modprobe.d/alsa-base
options snd-hda-intel model=laptop index=0
options snd-hda-intel model=thinkpad

Wednesday, July 17, 2013

Linux CentOS 6 KVM hot add device

First of all, you should avoid using virsh attach-disk with its limited amount of options. Instead, I suggest to specify the exact disk format you prefer in a separate, temporary XML file, like this: 

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2' cache='writeback'/>
  <source file='/home/gert/kvm/testdomain-vdb.img'/>
  <target dev='vdb' bus='virtio'/>
</disk>


Before adding it, make sure the hotplug kernel modules are loaded in the guest: 

modprobe acpiphp
modprobe pci_hotplug


Some distributions, including recent CentOS/RHEL/Fedora have this built-in in the kernel. In this case, check for CONFIG_HOTPLUG_PCI_ACPI. If it's y, then you're all set.

Finally, add it to the running VM using 

virsh # attach-device [domain] /path/to/disk.xml
(optionally, add the --persistent option to let Libvirt update the domain XML definition 'persistent'.)

In the guest, the kernel should now be triggered, as can be checked with dmesg:
[  321.946440] virtio-pci 0000:00:06.0: using default PCI settings
[...]
[  321.952782]  vdb: vdb1 vdb2

This also works perfectly using the GUI-enabled virt-manager application.

source

How to clear memory (caches) on Linux

Since 2.6.16 you need only echo to clear cached memory on Linux box.

This will clear all cached memory.


echo 3 > /proc/sys/vm/drop_caches
You can clear only part of it by using 1 or 2. All options:
  • 1 to clear cached pages.
  • 2 to clear dentries and inodes. 
  • 3 to clear all cached memory.

Thursday, February 14, 2013

Copy LVM volume over network to another LVM pool

We need to copy huge LVM volume from HOST_A to LVM on HOST_B. It can be virtual machine disk for example.
On HOST_B create LVM_HOST_B volume and after that dd over ssh with gzip. And do not use /dev/mapper

#> dd bs=1M if=LVM_HOST_A | gzip --fast -c | ssh HOST_B "gzip -d -c | dd bs=1M of=LVM_HOST_B"

Awesome!

pv utility can show progress bar for dd. pv - monitor the progress of data through a pipe.
pv -ptrb

Wednesday, January 16, 2013

My Debian and USB memory stick

 Few days ago i bought Silicon Power USB memory stick 16Gb size.
Memory was formated to ntfs file system (mkfs.ntfs) for MS Windows compatability.
 I was very surprised when i try copy some big files (3.3 GB ) to it and my PC totaly freezed on 70-80%.

After some search in google i found solution: ehci_hcd module

#> modprobe ehci_hcd

#> rsync -av --progress /home/***/Downloads/xxx-720p.mkv /mnt/usb/
sending incremental file list
xxx-720p.mkv
  3519709784 100%   29.22MB/s    0:01:54 (xfer#1, to-check=0/1)

sent 3520139543 bytes  received 31 bytes  30477398.91 bytes/sec
total size is 3519709784  speedup is 1.00


awesome!

Monday, January 7, 2013

Powersaving for Lenovo x220i

Tunes for power saving on my Lenovo x220i

You can find something usefull here: thinkwiki

Tunes from lesswatts.org

nmi_watchdog=0
pcie_aspm=force

echo 1 > /sys/devices/system/cpu/sched_mc_power_savings
echo 1 > /sys/devices/system/cpu/sched_smt_power_savings

echo 5 > /proc/sys/vm/laptop_mode
# default 0

echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
# default 500

hdparm -B 1 -S 12 /dev/sda
# default -B 128

echo 10 > /sys/module/snd_hda_intel/parameters/power_save
# default 0

if your requirements for syslog are less strict than described above, you can
edit the /etc/syslog.conf file and add a "-" in front of this line:
   *.info;mail.none;authpriv.none;cron.none        /var/log/messages
like this:
   *.info;mail.none;authpriv.none;cron.none        -/var/log/messages

/sys/class/scsi_host/

echo auto > /sys/bus/pci/devices/0000:00:00.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:02.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:16.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:19.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:1a.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:1b.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:1c.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:1c.1/power/control
echo auto > /sys/bus/pci/devices/0000:00:1c.3/power/control
echo auto > /sys/bus/pci/devices/0000:00:1c.4/power/control
echo auto > /sys/bus/pci/devices/0000:00:1d.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:1f.0/power/control
echo auto > /sys/bus/pci/devices/0000:00:1f.2/power/control
echo auto > /sys/bus/pci/devices/0000:00:1f.3/power/control
echo auto > /sys/bus/pci/devices/0000:00:1f.5/power/control
echo auto > /sys/bus/pci/devices/0000:03:00.0/power/control
echo auto > /sys/bus/pci/devices/0000:0d:00.0/power/control
# default on

echo 'min_power' > /sys/class/scsi_host/host0/link_power_management_policy
# default max_performance

#echo SATA_ALPM_ENABLE=true >> /etc/pm/config.d/sata_alpm


And from amarao-san livejournal
most important:

  pcie_aspm=force
  # in grub config
  # GRUB_CMDLINE_LINUX_DEFAULT add 'pcie_aspm=force' (delimiter space)
  # update-grub
  #
  # in sysfs
  # echo powersave > /sys/module/pcie_aspm/parameters/policy
  # default performance powersave

  i915_enable_rc6=true
  # to grub config same as pcie_aspm
  # check 'i915.lvds_downclock=1 i915.semaphores=1'

  SATA link power management
  # echo SATA_ALPM_ENABLE=true >> /etc/pm/config.d/sata_alpm


  xset +dpms
  xset dpms 0 0 300

  # dpms options to .xinitrc for screen powersave


source
= Tricks and Tips to Reduce Power Consumption =

Enable ALPM:
 echo SATA_ALPM_ENABLE=true | sudo tee /etc/pm/config.d/sata_alpm

 can save ~1-2W, may cause data corruption on some hardware

Change background to a lighter colour on laptop LCD
 can save ~1%

Enable i915 rc6:
 kernel parameter: i915.i915_enable_rc6=1
 can save 25-40% on Sandybridge
 known to lockup on some machines

Enable i915 Framebuffer Compression:
 kernel parameter: i915.i915_enable_fbc=1
 LessWatts believes it can save 0.6 Watts

DRM vblank off delay:
 kernel parameter: drm.vblankoffdelay=1
 Reduces wakeup events, possibly saves power

Disable any wireless you don't need (e.g. from desktop or blacklist bluetooth)
 E.g. bluetooth, saves ~1-2W

Disable webcam
 add "blacklist uvcvideo" to /etc/modprobe.d/blacklist.conf
 can save ~1-2W
 
Use PowerTop to put the following devices into a "Good" power saving state:
 * Webcam
 * Audio
 * DRAM
 * Ethernet
 * Wifi
 * Bluetooth
 * SATA link
 * MMC/SD

PCIe ASPM
 Use Ubuntu Precise kernel - has PCIe ASPM fix in.
 Or force to powersave using kernel parameter: pcie_aspm=powersave

Laptop Backlight
 Don't use at full brightness.
 Drop to 2/3 brightness level - will save ~1W

Disable flashing cursor on gnome-terminal:
 gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_blink_mode off
 can save a few wakeups/second

Wednesday, September 26, 2012

sudo tune

visudo

Defaults    timestamp_timeout = 0

Wednesday, September 12, 2012

MySQL Swapping to Disk

source

Over the past couple of months we've been finding some of our MySQL servers swapping to disk. This behaviour is very unusual because we allocate a lot of memory to every MySQL server and run large caches to ensure the database performs as well as it can.
We were finding that MySQL consumes memory up to about 90-95% and then chunks of memory start swapping. The majority of memory usage is cache, as the swap became active, memory was made available and MySQL would cache more, unfortunately retrieving paged caches then became slow as they were sitting in a page file on disk and not in memory as we expected.
We'll detail in this article how we have resolved this after a couple of months of investigating and testing alternative options.

The Environment

As of writing this our environment was as follows:
  • MySQL 5
  • RedHat Enterprise Linux 5.4
  • Kernel 2.6.18
  • Dell 2970 with Quad-Dual Core 2.0Ghz
  • 32Gb RAM
  • Mostly INNODB with some MyISAM tables

The Disclaimer

The issue we have experienced may not be the issue you are experiencing.  What we describe here may or may not work for you, if you don't know what you are doing - seek professional advice. Don't make changes blindly without understanding the consequences, especially to production environments.

The Problem

MySQL server swapping large chunks of data into swap and then using reclaimed memory to cache more data.
As MySQL is the only service running on these servers, we naturally assumed it to be an issue with MySQL consuming memory incorrectly or swapping way before it needed to because of an incorrect setting - how wrong we were!
With some investigation we soon discovered the issue wasn't MySQL related but was an issue with the OS itself, the system was monitoring memory usage and after reaching a certain threshold, was paging out some memory to make more available - ironically it was only making more available to MySQL. The end result was that over time, as the caches grew bigger, swap gradually increased until their was no more data to cache, unfortunately at this point much of the cache was on disk instead of in memory.
The result of this was a system running with quite a bit of free memory but an almost full swap partition and of course disk-based retrievals are much slower than memory based ones so we could see when the swap was being used for query work.
We read up extensively on this and tested out a few ideas with very little success, the only feasible one appeared to be to drop the swap partition and run without one - this is good in theory but in practice, if you miscalculate a setting or need to burst for some reason beyond your physical memory - you've got nowhere to go, we didn't want to do it.
We use our swap as a key indicator to determine if things need configuration or upgrade, when swap is used we report on this and determine whether a server requires fine-tuning, upgrade or additional servers bringing online to support load, in essence, we aim never to use swap and if we ever do we tune/upgrade.
We were stumped by this issue for a few weeks and despite finding blog post after blog post discussing it, there were never any solutions.

The Solution

Our attention had been very much on this problem within the MySQL arena however and we switched tack focussing our attentions on how swap operates.  It was this that led us to discover swappiness.
Swappiness defines how much a system can swap, the higher the number the more swapping happens, the number can be between 0 and 100.  0 is no swapping (we don't want that) and 100 is swap whenever possible thereby keeping memory clear.
You can check on the swappiness of your system by running:
/sbin/sysctl -a|grep swappiness
That will echo out the current swappiness of the system.
In our case the swappiness of the solution was 60, that seemed very high so we set about trying lower thresholds.  We only wanted to swap whenever we really, really needed to, so that is, when the system is in dire need of memory and won't be able to get it without swapping.
To change the swappiness settings we firstly edit the file: /etc/sysctl.conf
In here add or modify the line that says something like:
vm.swappiness = 60
to the value you want, in our case we ended up with 1:
vm.swappiness = 1
Ideally you would be able to reboot in this scenario to refresh your system settings but that's not always possible, if you want to apply this update immediately without a reboot then run:
/sbin/sysctl -p
Once applied you can check your current swappiness:
/sbin/sysctl -a | grep swappiness

Conclusion

Since we applied this change to our production MySQL servers we've not seen any sign of swapping.  If the system needs to swap for any reason it can, but it's a last resort rather than a regular thing, this is much better for us as RAM is cheap and we have a lot of it so we want to get the most from it.
I highly recommend reviewing your swappiness for solutions that may consume memory for the right purposes (such as memcache servers, MySQL servers, etc) and stop them chewing at your disks, you'll experience performance gains and your users will be much happier.

Fixing MySQL

If you have a MySQL issue that you need help with - from configuration through to optimisation, we can help.  Our team are experts in configuring complex hosting environments, if you have a problem you think we could help with, fill in our project request form and we'll send back some more details or help point you in the right direction.
 For Debian it's a little different-

Check swappiness:
cat /proc/sys/vm/swappiness

Temporarily change swappiness:
sudo sysctl vm.swappiness=10

Permanently change swappiness:
edit /etc/sysctl.conf with your text editor. Search for vm.swappiness and change its value as desired. If vm.swappiness doesn't exist, add it to the end of the file:
vm.swappiness=10
Save and reboot

Monday, September 3, 2012

Wi-Fi WPA manualy

vim /etc/wpa_supplicant.conf
#ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
network={
   ssid="SSID"
   psk="SECRET"
}

#> ifconfig wlan0 up
#> iwconfig wlan0 essid SSID
#> wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant.conf
...
#> killall -q wpa_suplicant



#>  ip link set wlan0 up
#> wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant.conf
...
#> killall -q wpa_suplicant

nice doc by arch

Thursday, June 7, 2012

bash multiline write to file


cat > ./outfile << DELIM
hello proble
h2 fadernaly
DELIM

Sunday, April 8, 2012

How to disable CTRL+ALT+Del keys

Open /etc/inittab file, enter:
# vi /etc/inittab

Search for line that read as follows:
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

And remove the line or comment out the above line by putting a hash mark (#) in front of it:
# ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

Save the file and exit to shell promot. Reboot system to take effect or type command:
# init q

source