summaryrefslogtreecommitdiffstats
path: root/config/chroot_local-includes/usr/share/initramfs-tools/scripts/init-top/memory_wipe
blob: 09671712d08c315a00a7435490e770132e842448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh

PREREQ=""

# Duplicated in features/step_definitions/erase_memory.rb
KERNEL_MEM_RESERVED_K=$((64*1024))
ADMIN_MEM_RESERVED_K=$((128*1024))

prereqs() {
   echo "${PREREQ}"
}

tweak_sysctl() {
   echo 3   > /proc/sys/kernel/printk

   echo 3   > /proc/sys/vm/drop_caches

   echo 0   > /proc/sys/vm/overcommit_memory
   echo 0   > /proc/sys/vm/oom_kill_allocating_task
   echo 0   > /proc/sys/vm/oom_dump_tasks

   if [ "${sdmemdebug}" = 1 ] ; then
    # Make sure the kernel doesn't starve, for better reliability when
    # running the test suite.
    echo "$KERNEL_MEM_RESERVED_K" > /proc/sys/vm/min_free_kbytes
    echo "$ADMIN_MEM_RESERVED_K"  > /proc/sys/vm/admin_reserve_kbytes
   fi
}

case ${1} in
   prereqs)
      prereqs
      exit 0
      ;;
esac

if [ -n "${sdmem}" ] ; then
   tweak_sysctl
   if [ -z "${sdmemopts}" ] ; then
      sdmemopts="v"
   fi
   # We run one instance of sdmem per 2 GiB of RAM to deal with the
   # x86 per-process memory limitation.
   mem=$(free | awk '/Mem:/ { print $2 }')
   instances=$((${mem}/(2*1024*1024)+1))
   for i in $(seq 1 "${instances}") ; do
      ( /usr/bin/sdmem "-${sdmemopts}" ; killall sdmem >/dev/null 2>&1 ) &
   done
   # Wait for sdmem jobs to finish.
   wait
fi

if [ "${sdmemdebug}" = 1 ] ; then
   clear
   echo "Going to sleep 10 minutes. Happy dumping!"
   sleep 600
fi

case "${sdmem}" in
   halt)
      /sbin/poweroff -fd
      ;;
   reboot)
      /sbin/reboot -fd
      ;;
   *)
      ;;
esac