1#!/usr/bin/env python3

2#

3# This file is part of Checkbox.

4#

5# Copyright (C) 2010-2013 by Cloud Computing Center for Mobile Applications

6# Industrial Technology Research Institute

7# Copyright 2016 Canonical Ltd.

8#

9# Authors:

10# Nelson Chu <Nelson.Chu@itri.org.tw>

11# Jeff Lane <jeff@ubuntu.com>

12# Sylvain Pineau <sylvain.pineau@canonical.com>

13#

14# Checkbox is free software: you can redistribute it and/or modify

15# it under the terms of the GNU General Public License version 3,

16# as published by the Free Software Foundation.

17#

18# Checkbox is distributed in the hope that it will be useful,

19# but WITHOUT ANY WARRANTY; without even the implied warranty of

20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

21# GNU General Public License for more details.

22#

23# You should have received a copy of the GNU General Public License

24# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

25"""disk_info utility."""

26

27 import re

28 import sys

29 from subprocess import check_output, CalledProcessError

30

31 from checkbox_support.parsers.udevadm import find_pkname_is_root_mountpoint

32

33

34 def main():

35 """

36 disk_info.

37

38 Uses lsblk to gather information about disks seen by the OS.

39 Outputs kernel name, model and size data

40 """

41 pattern = re.compile(

42 r'KNAME="(?P<KNAME>.*)" '

43 r'TYPE="(?P<TYPE>.*)" '

44 r'SIZE="(?P<SIZE>.*)" '

45 r'MODEL="(?P<MODEL>.*)" '

46 r'MOUNTPOINT="(?P<MOUNTPOINT>.*)"'

47 )

48 try:

49 lsblk = check_output(

50 [

51 "lsblk",

52 "-i",

53 "-n",

54 "-P",

55 "-o",

56 "KNAME,TYPE,SIZE,MODEL,MOUNTPOINT",

57 ],

58 universal_newlines=True,

59 )

60 except CalledProcessError as e:

61 sys.exit(e)

62

63 disks = 0

64 for line in lsblk.splitlines():

65 m = pattern.match(line)

66 if not m or m.group("TYPE") not in ("disk", "crypt"):

67 continue

68 # Only consider MMC block devices if one of their mounted partitions is

69 # root (/)

70 if m.group("KNAME").startswith(

71 "mmcblk"

72 ) and not find_pkname_is_root_mountpoint(m.group("KNAME"), lsblk):

73 continue

74 # Don't consider any block dev mounted as snapd save partition

75 if "snapd/save" in m.group("MOUNTPOINT"):

76 continue

77 disks += 1

78 model = m.group("MODEL")

79 if not model:

80 model = "Unknown"

81 print("Name: /dev/{}".format(m.group("KNAME")))

82 print("\t{:7}\t{}".format("Model:", model))

83 print("\t{:7}\t{}".format("Size:", m.group("SIZE")))

84

85 if not disks:

86 print("No disk information discovered.")

87 return 10

88

89 return 0

90

91

92 if __name__ == "__main__":

93 sys.exit(main())