Skip to contentSkip to conversation

rudimentary kill script

linux-lvm Xen 1 participant 1 message Aug 27, 2016

lore
Share & tools
lore

Xen

author
#0 lore raw

Here is a quick way to kill all processes that might have an open file on some volume you want to umount and close.

kill_all_dm() {
    hexdev=$(dmsetup info -c -o major,minor $1 --noheadings | tr ':' ' ' 
| { read a b; printf "D0x%02x%02x\n" $a $b; })
    process_list=$(lsof -F cDn0 | tr '\0' '\t' | awk -F '\t' '{ if 
(substr($1,1,1) == "p") P=$1; else print P "\t" $0 }' | grep $hexdev | awk '{print $1}' | sed "s/^p//")
    [ -n "$process_list" ] && echo "$process_list" | xargs kill -9
}

It works on logical volumes and device mapper (paths) equally, but you must specify the path:

kill_all_dm /dev/linux/root ;-).

Alternatively this will just list all open processes:

find_processes() {
    hexdev=$(dmsetup info -c -o major,minor $1 --noheadings | tr ':' ' ' 
| { read a b; printf "D0x%02x%02x\n" $a $b; })
    process_list=$(lsof -F cDn0 | tr '\0' '\t' | awk -F '\t' '{ if 
(substr($1,1,1) == "p") P=$1; else print P "\t" $0 }' | grep $hexdev | awk '{print $1}' | sed "s/^p//")
    [ -n "$process_list" ] && echo "$process_list" | xargs ps
}

find_processes /dev/linux/root