File: inspect_fs.ml

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (393 lines) | stat: -rw-r--r-- 11,440 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
(* guestfs-inspection
 * Copyright (C) 2009-2020 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *)

open Printf

open Std_utils

open Mountable
open Inspect_types
open Inspect_utils

let rec check_for_filesystem_on mountable vfs_type =
  if verbose () then
    eprintf "check_for_filesystem_on: %s (%s)\n%!"
            (Mountable.to_string mountable) vfs_type;

  let role =
    let is_swap = vfs_type = "swap" in
    if is_swap then
      Some RoleSwap
    else (
      (* Try mounting the device.  Ignore errors if we can't do this. *)
      let mounted =
        if vfs_type = "ufs" then ( (* Hack for the *BSDs. *)
          (* FreeBSD fs is a variant of ufs called ufs2 ... *)
          try
            Mount.mount_vfs "ro,ufstype=ufs2" "ufs" mountable "/";
            true
          with _ ->
            (* while NetBSD and OpenBSD use another variant labeled 44bsd *)
            try
              Mount.mount_vfs "ro,ufstype=44bsd" "ufs" mountable "/";
              true
            with _ -> false
        ) else (
          try Mount.mount_ro mountable "/";
              true
          with _ -> false
        ) in
      if not mounted then None
      else (
        let role = check_filesystem mountable in
        Mount_utils.umount_all ();
        role
      )
    ) in

  match role with
  | None -> None
  | Some role ->
     Some { fs_location = { mountable ; vfs_type }; role }

(* When this function is called, the filesystem is mounted on sysroot (). *)
and check_filesystem mountable =
  let role = ref `Other in
  (* The following struct is mutated in place by callees.  However we
   * need to make a copy of the object here so we don't mutate the
   * null_inspection_data struct!
   *)
  let data = null_inspection_data () in

  let debug_matching what =
    if verbose () then
      eprintf "check_filesystem: %s matched %s\n%!"
              (Mountable.to_string mountable) what
  in

  (* Grub /boot? *)
  if Is.is_file "/grub/menu.lst" ||
     Is.is_file "/grub/grub.conf" ||
     Is.is_file "/grub2/grub.cfg" then (
    debug_matching "Grub /boot";
    ()
  )
  (* FreeBSD root? *)
  else if Is.is_dir "/etc" &&
          Is.is_dir "/bin" &&
          Is.is_file "/etc/freebsd-update.conf" &&
          Is.is_file "/etc/fstab" then (
    debug_matching "FreeBSD root";
    role := `Root;
    Inspect_fs_unix.check_freebsd_root mountable data
  )
  (* NetBSD root? *)
  else if Is.is_dir "/etc" &&
          Is.is_dir "/bin" &&
          Is.is_file "/netbsd" &&
          Is.is_file "/etc/fstab" &&
          Is.is_file "/etc/release" then (
    debug_matching "NetBSD root";
    role := `Root;
    Inspect_fs_unix.check_netbsd_root mountable data;
  )
  (* OpenBSD root? *)
  else if Is.is_dir "/etc" &&
          Is.is_dir "/bin" &&
          Is.is_file "/bsd" &&
          Is.is_file "/etc/fstab" &&
          Is.is_file "/etc/motd" then (
    debug_matching "OpenBSD root";
    role := `Root;
    Inspect_fs_unix.check_openbsd_root mountable data;
  )
  (* Hurd root? *)
  else if Is.is_file "/hurd/console" &&
          Is.is_file "/hurd/hello" &&
          Is.is_file "/hurd/null" then (
    debug_matching "Hurd root";
    role := `Root;
    Inspect_fs_unix.check_hurd_root mountable data;
  )
  (* Minix root? *)
  else if Is.is_dir "/etc" &&
          Is.is_dir "/bin" &&
          Is.is_file "/service/vm" &&
          Is.is_file "/etc/fstab" &&
          Is.is_file "/etc/version" then (
    debug_matching "Minix root";
    role := `Root;
    Inspect_fs_unix.check_minix_root data;
  )
  (* Linux root? *)
  else if Is.is_dir "/etc" &&
          (Is.is_dir "/bin" ||
           is_symlink_to "/bin" "usr/bin") &&
          (Is.is_file "/etc/fstab" ||
           Is.is_file "/etc/hosts") then (
    debug_matching "Linux root";
    role := `Root;
    Inspect_fs_unix.check_linux_root mountable data;
  )
  (* CoreOS root? *)
  else if Is.is_dir "/etc" &&
          Is.is_dir "/root" &&
          Is.is_dir "/home" &&
          Is.is_dir "/usr" &&
          Is.is_file "/etc/coreos/update.conf" then (
    debug_matching "CoreOS root";
    role := `Root;
    Inspect_fs_unix.check_coreos_root mountable data;
  )
  (* Linux /usr/local? *)
  else if Is.is_dir "/etc" &&
          Is.is_dir "/bin" &&
          Is.is_dir "/share" &&
          not (Is.is_dir "/local") &&
          not (Is.is_file "/etc/fstab") then (
    debug_matching "Linux /usr/local";
    ()
  )
  (* Linux /usr? *)
  else if Is.is_dir "/etc" &&
          Is.is_dir "/bin" &&
          Is.is_dir "/share" &&
          Is.is_dir "/local" &&
          not (Is.is_file "/etc/fstab") then (
    debug_matching "Linux /usr";
    role := `Usr;
    Inspect_fs_unix.check_linux_usr data;
  )
  (* CoreOS /usr? *)
  else if Is.is_dir "/bin" &&
          Is.is_dir "/share" &&
          Is.is_dir "/local" &&
          Is.is_dir "/share/coreos" then (
    debug_matching "CoreOS /usr";
    role := `Usr;
    Inspect_fs_unix.check_coreos_usr mountable data;
  )
  (* Linux /var? *)
  else if Is.is_dir "/log" &&
          Is.is_dir "/run" &&
          Is.is_dir "/spool" then (
    debug_matching "Linux /var";
    ()
  )
  (* Windows root? *)
  else if Inspect_fs_windows.is_windows_systemroot () then (
    debug_matching "Windows root";
    role := `Root;
    Inspect_fs_windows.check_windows_root data;
  )
  (* Windows volume with installed applications (but not root)? *)
  else if is_dir_nocase "/System Volume Information" &&
          is_dir_nocase "/Program Files" then (
    debug_matching "Windows volume with installed applications";
    ()
  )
  (* Windows volume (but not root)? *)
  else if is_dir_nocase "/System Volume Information" then (
    debug_matching "Windows volume without installed applications";
    ()
  )
  (* FreeDOS? *)
  else if is_dir_nocase "/FDOS" &&
          is_file_nocase "/FDOS/FREEDOS.BSS" then (
    debug_matching "FreeDOS";
    role := `Root;
    data.os_type <- Some OS_TYPE_DOS;
    data.distro <- Some DISTRO_FREEDOS;
    (* FreeDOS is a mix of 16 and 32 bit, but
     * assume it requires a 32 bit i386 processor.
     *)
    data.arch <- Some "i386"
  )
  (* MS-DOS? *)
  else if is_dir_nocase "/DOS" &&
          is_file_nocase "/DOS/COMMAND.COM" then (
    debug_matching "MS-DOS";
    role := `Root;
    data.os_type <- Some OS_TYPE_DOS;
    data.distro <- Some DISTRO_MSDOS;
    (* MS-DOS is a mix of 16 and 32 bit, but
     * assume it requires a 32 bit i386 processor.
     *)
    data.arch <- Some "i386"
  )
  (* None of the above. *)
  else (
    debug_matching "no known OS partition"
  );

  (* The above code should have set [data.os_type] and [data.distro]
   * fields, so we can now guess the package management system.
   *)
  data.package_format <- check_package_format data;
  data.package_management <- check_package_management data;

  match !role with
  | `Root -> Some (RoleRoot data)
  | `Usr -> Some (RoleUsr data)
  | `Other -> Some RoleOther

and is_symlink_to file wanted_target =
  if not (Is.is_symlink file) then false
  else Link.readlink file = wanted_target

(* At the moment, package format and package management are just a
 * simple function of the [distro] and [version[0]] fields, so these
 * can never return an error.  We might be cleverer in future.
 *)
and check_package_format { distro } =
  match distro with
  | None -> None
  | Some DISTRO_ALTLINUX
  | Some DISTRO_CENTOS
  | Some DISTRO_FEDORA
  | Some DISTRO_MAGEIA
  | Some DISTRO_MANDRIVA
  | Some DISTRO_MEEGO
  | Some DISTRO_NEOKYLIN
  | Some DISTRO_OPENMANDRIVA
  | Some DISTRO_OPENSUSE
  | Some DISTRO_ORACLE_LINUX
  | Some DISTRO_REDHAT_BASED
  | Some DISTRO_RHEL
  | Some DISTRO_SCIENTIFIC_LINUX
  | Some DISTRO_SLES
  | Some DISTRO_SUSE_BASED ->
     Some PACKAGE_FORMAT_RPM
  | Some DISTRO_DEBIAN
  | Some DISTRO_KALI_LINUX
  | Some DISTRO_LINUX_MINT
  | Some DISTRO_UBUNTU ->
     Some PACKAGE_FORMAT_DEB
  | Some DISTRO_ARCHLINUX ->
     Some PACKAGE_FORMAT_PACMAN
  | Some DISTRO_GENTOO ->
     Some PACKAGE_FORMAT_EBUILD
  | Some DISTRO_PARDUS ->
     Some PACKAGE_FORMAT_PISI
  | Some DISTRO_ALPINE_LINUX ->
     Some PACKAGE_FORMAT_APK
  | Some DISTRO_VOID_LINUX ->
     Some PACKAGE_FORMAT_XBPS
  | Some DISTRO_BUILDROOT
  | Some DISTRO_CIRROS
  | Some DISTRO_COREOS
  | Some DISTRO_FREEBSD
  | Some DISTRO_FREEDOS
  | Some DISTRO_FRUGALWARE
  | Some DISTRO_MSDOS
  | Some DISTRO_NETBSD
  | Some DISTRO_OPENBSD
  | Some DISTRO_PLD_LINUX
  | Some DISTRO_SLACKWARE
  | Some DISTRO_TTYLINUX
  | Some DISTRO_WINDOWS ->
     None

and check_package_management { distro; version } =
  let major = match version with None -> 0 | Some (major, _) -> major in
  match distro with
  | None -> None

  | Some DISTRO_MEEGO ->
     Some PACKAGE_MANAGEMENT_YUM

  | Some DISTRO_FEDORA ->
    (* If Fedora >= 22 and dnf is installed, say "dnf". *)
     if major >= 22 && Is.is_file ~followsymlinks:true "/usr/bin/dnf" then
       Some PACKAGE_MANAGEMENT_DNF
     else if major >= 1 then
       Some PACKAGE_MANAGEMENT_YUM
     else
       (* Probably parsing the release file failed, see RHBZ#1332025. *)
       None

  | Some DISTRO_NEOKYLIN ->
     (* We don't have access to NeoKylin for testing, but it is
      * supposed to be a Fedora derivative.
      *)
     Some PACKAGE_MANAGEMENT_DNF

  | Some DISTRO_CENTOS
  | Some DISTRO_ORACLE_LINUX
  | Some DISTRO_REDHAT_BASED
  | Some DISTRO_RHEL
  | Some DISTRO_SCIENTIFIC_LINUX ->
     if major >= 8 then
       Some PACKAGE_MANAGEMENT_DNF
     else if major >= 5 then
       Some PACKAGE_MANAGEMENT_YUM
     else if major >= 2 then
       Some PACKAGE_MANAGEMENT_UP2DATE
     else
       (* Probably parsing the release file failed, see RHBZ#1332025. *)
       None

  | Some DISTRO_ALTLINUX
  | Some DISTRO_DEBIAN
  | Some DISTRO_KALI_LINUX
  | Some DISTRO_LINUX_MINT
  | Some DISTRO_UBUNTU ->
     Some PACKAGE_MANAGEMENT_APT

  | Some DISTRO_ARCHLINUX ->
     Some PACKAGE_MANAGEMENT_PACMAN

  | Some DISTRO_GENTOO ->
     Some PACKAGE_MANAGEMENT_PORTAGE

  | Some DISTRO_PARDUS ->
     Some PACKAGE_MANAGEMENT_PISI

  | Some DISTRO_MAGEIA
  | Some DISTRO_MANDRIVA ->
     Some PACKAGE_MANAGEMENT_URPMI

  | Some DISTRO_OPENSUSE
  | Some DISTRO_SLES
  | Some DISTRO_SUSE_BASED ->
     Some PACKAGE_MANAGEMENT_ZYPPER

  | Some DISTRO_ALPINE_LINUX ->
     Some PACKAGE_MANAGEMENT_APK

  | Some DISTRO_VOID_LINUX ->
     Some PACKAGE_MANAGEMENT_XBPS

  | Some DISTRO_OPENMANDRIVA ->
     Some PACKAGE_MANAGEMENT_DNF

  | Some DISTRO_BUILDROOT
  | Some DISTRO_CIRROS
  | Some DISTRO_COREOS
  | Some DISTRO_FREEBSD
  | Some DISTRO_FREEDOS
  | Some DISTRO_FRUGALWARE
  | Some DISTRO_MSDOS
  | Some DISTRO_NETBSD
  | Some DISTRO_OPENBSD
  | Some DISTRO_PLD_LINUX
  | Some DISTRO_SLACKWARE
  | Some DISTRO_TTYLINUX
  | Some DISTRO_WINDOWS ->
    None