blob: ecfe1f279f3f8a454590a3fcc1ef4e1a9558f12a [file] [log] [blame]
# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
import os
import re
import traceback
from autotest_lib.client.common_lib import error
from autotest_lib.server import test
from autotest_lib.client.bin import utils
class platform_CorruptRootfs(test.test):
"""Tests how the system recovers when the root file system is corrupted
1. Copies kernel A and rootfs A to kernel B and rootfs B.
2. Sets the modes on the partitions.
3. Corrupts rootfs A by writing some random data to a the first few sectors.
4. If enabled, corrupts the bootcache area.
5. Reboots the system.
6. Runs the test again in the reverse direction, leaving kernel A and
rootfs A in the same state.
version = 1
def _get_bootcache_offset(self):
"""Gets the offset of the bootcache from the command line.
if bootcache is found, returns offset as a string
otherwise returns '0'
# Get the linux cmd line
result ='cat /proc/cmdline')
m ='dm="(.*)"', result.stdout)
dm =
i = dm.find('bootcache')
if i > 0:
s = dm[i:].split()
return s[2] # 2nd field after bootcache has sector offset
return '0'
def _get_partition_layout(self):
"""Get the partition layout
dev - name of the device hosting the partition
kernelA - partition used to boot kernel
rootfsA - partition of current root file system
kernelB - backup copy of kernel
rootfsB - backup copy of root file system
# What is our root partition?
result ='rootdev -s')'Root partition %s', result.stdout)
rootdev = result.stdout.strip()
if os.path.basename(rootdev).startswith('mmc'):
dev = rootdev[:-2]
dev = rootdev[:-1]
kernelA = utils.get_kernel_partition(rootdev)
rootfsA = rootdev
kernelB = utils.get_free_kernel_partition(rootdev)
rootfsB = utils.get_free_root_partition(rootdev)
return dev, kernelA, rootfsA, kernelB, rootfsB
def _corrupt_rootfs(self, host):
"""Corrupt the root file system
self.client = host
self.client_test = 'platform_CorruptRootfs'
dev, kernelA, rootfsA, kernelB, rootfsB = self._get_partition_layout()
bootcache_offset = self._get_bootcache_offset()
# Copy kernel and rootfs paritions from A to B'CorruptRootfs: copy partitions A to B')'dd if=%s of=%s bs=64K' % (kernelA, kernelB))'dd if=%s of=%s bs=64K' % (rootfsA, rootfsB))
# Set attribrutes on kernel A and B'CorruptRootfs: set attributes on kernal A and B')'cgpt add -i 2 -T 5 -P 9 -S 1 %s' % dev)'cgpt add -i 4 -T 5 -P 9 -S 1 %s' % dev)
# Corrupt rootfs A and bootcache'CorruptRootfs: corrupt rootfs A ' + rootfsA)'dd if=/dev/urandom of=%s count=16' % rootfsA)
if bootcache_offset != '0':'dd if=/dev/zero of=%s seek=%s count=4096' %
(rootfsA, bootcache_offset))'CorruptRootfs: reboot ' + self.client.hostname)
except error.AutoservRebootError as e:
raise error.TestFail('%s.\nTest failed with error %s' % (
traceback.format_exc(), str(e)))
# Find what partition we are now running on
result ='rootdev -s')'Root partition %s', result.stdout)
def run_once(self, host=None):
run_once actually runs the test twice. The second run "undoes"
what was done in the first run.
@param host - the host machine running the test