Commit Graph

37 Commits (cc70274079a0de78497011e2c665838522b6e1f5)

Author SHA1 Message Date
Alejandro Sirgo Rica cc70274079 src: check if the system is hibernated before /image/create
Mount the system partition in readonly mode and check for the
hiberfil.sys file if the target system is a Windows.

Fail the image creation process if the target system is hibernated.
2024-10-02 14:25:04 +02:00
Alejandro Sirgo Rica 8754c21694 src: report used and free partition data in bytes
Add "used_size" and "free_size" to the partition data and the
cache data.

Old response from ogClient for /cache/delete, /cache/fetch
and /image/restore:
{
  'cache': [
    {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
    {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'}
  ]
}

New response:
{
  'cache': {
    'used_size': 4520232322423,
    'free_size': 48273465287452945,
    'images': [
      {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
      {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'}
    ]
  }
}
2024-09-25 14:35:41 +02:00
Alejandro Sirgo Rica e20cda122b utils: rename ogExtendFs to extend_filesystem
Use a more consistent name format for the ogExtendFs function.
2024-07-29 15:07:22 +02:00
OpenGnSys Support Team 2c10b4c92a utils: fs: error out if mkfs fails
If formatting fails, log shows:

	mkfs.ntfs reports return code 1 for /dev/sda2

but ogclient reports success to the ogserver.

Raise an exception so front-end gets an indication that formatting has failed.
2024-07-29 14:22:48 +02:00
OpenGnSys Support Team 8ac8dc306e utils: missing f-string with filesystem resize error
... failed to resize {partdev} with ext4
2024-07-15 09:50:38 +02:00
Alejandro Sirgo Rica abea6583d3 utils: add mkdir error report in mount_mkdir
Add exception checks to the os.mkdir operation and log the error
found. The previous implementation was too optimistic and only
handled mount related errors.
2024-05-07 17:24:41 +02:00
Alejandro Sirgo Rica b0d03ce588 utils:fs: fix logging statement in get_filesystem_type
Call the error() logging function from the logging object instead
of the non existent log variable previously referenced.
2024-05-07 12:20:26 +02:00
Alejandro Sirgo Rica de17bb6812 utils:fs: add mkfs logs when return code is not 0
Report mkfs failure for every partition. This does not raise an
exception as that would skip partprobe operations and the mkfs
operations in the next potentially well formated partitions.
2024-05-07 12:20:26 +02:00
OpenGnSys Support Team 8661a99a36 utils: fs: incorrect indentation 2024-04-22 13:56:39 +02:00
OpenGnSys Support Team bddbea942e utils: fs: skip shrink/extend operation for fat
fat does not support this operation, skip it
2024-04-19 12:19:54 +02:00
OpenGnSys Support Team 481ce5794f utils: fs: remove unused variable in _reduce_resize2fs
ret is set but never used in this method
2024-04-19 11:32:53 +02:00
OpenGnSys Support Team 85bbdcfcb8 utils: fs: report error when failing to grow
not really a warning, report an error instead
2024-04-19 11:32:17 +02:00
OpenGnSys Support Team db23ea76f9 utils: fs: set return value in ogReduceFs
otherwise error path uses uninitialized variable

 File "/opt/opengnsys/ogClient/src/utils/fs.py", line 112, in ogReduceFs
 return ret
 UnboundLocalError: local variable 'ret' referenced before assignment
2024-04-19 11:29:24 +02:00
Alejandro Sirgo Rica dfde363aa6 src: log backtrace in unhandled error cases
Log an error message in known error cases and log a backtrace
otherwise.

Define a new error type OgError to be used in all the 'raise'
blocks to define the error message to log. The exception
propagates until it reaches send_internal_server_error() where
the exception type is checked. If the type is OgError we log
the exception message. Logs the backtrace for other types.

The initial error implementation printed a backtrace everytime
an error ocurred. The next iteration changed it to only print
a backtrace in a very particular case but ended up omiting too
much information such as syntax errors or unknown error context.
The actual implementation only logs the cases we already cover in
the codebase and logs a bracktrace in the others, enabling a
better debugging experience.
2024-04-03 13:31:10 +02:00
Alejandro Sirgo Rica 3aa76e4039 utils: remove redundant return statements from mount_mkdir
Remove return statement the program won't reach and simplify the
return logic with a return from an if and a fallback return
statement.
2024-03-26 13:32:58 +01:00
Alejandro Sirgo Rica 2a4ce65a20 src: centralize error logging into send_internal_server_error
Use only the exception messages as the main resource for error
messages.
The previous error code had string duplication in the form of:
	logging.error('msg here')
	raise Exception('msg here')

That approach also has the downside of having log duplication as
it had the local logging.err() and a global logging.exception()
inside send_internal_server_error capturing the exception message.
The actual code only requires raising an exception with a proper
error message.
Improve exception messages to give more error context.
Log every AssertionError as a backtrace.
Use the 'raise Exception from e' syntax to modify the a previously
raised exception 'e' into an exception with aditional context or
different type. This also prevents the message that warns about
newer exceptions being launch after an initial exception.
2024-03-21 10:29:57 +01:00
OpenGnSys Support Team 26ca4c7950 fs: add swap support and improve mkfs logging 2024-02-22 10:52:03 +01:00
Alejandro Sirgo Rica bcbbe26573 fs: improve value parsing from command output
value extraction did not have error checking and was handled in
a one-liner. The actual implementation expands the parsing logic
and moves it into a function.
2024-02-19 10:06:02 +01:00
OpenGnSys Support Team 8fb8a0a66d fs: return unknown if blkid fails
instead of rising an exception
2024-02-15 16:58:51 +01:00
OpenGnSys Support Team bf7eb1542d fs: logging ext4 resize error
log error in case resize2fs fails.
2024-02-15 16:58:51 +01:00
OpenGnSys Support Team d6c32bba5d fs: check if ntfsresize actually succeded to shrink filesystem
According to ntfsresize.c, this retuns 0 in case nothing needs to be done.
It should be safe to check for non-zero error and bail out in that case.
2024-02-15 16:58:46 +01:00
OpenGnSys Support Team 0f30b1349d fs: disentagle dry-run ntfsresize loop to probe for best shrink size
Revisit 5056b8f0d5 ("fs: validate ntfsresize dry-run output") that has
introduced a possible infinity loop.

Disentangle this loop while at it: iterate until best smallest size is
found by probing.
2024-02-15 16:36:28 +01:00
Alejandro Sirgo Rica 478c4447be src: improve error check in image_create and image_restore
cover more error cases where exceptions need to be raised.
check return code in the invoked subprocess.

restoreImageCustom has been intentionally left behind, it
is unclear what this custom script returns on success and
error.
2024-02-14 12:28:28 +01:00
OpenGnSys Support Team 5056b8f0d5 fs: validate ntfsresize dry-run output
validate 'Needed relocations: ' is in place before stepping on the split chunks

(2024-01-11 10:28:16) ogClient: [ERROR] - Exception when running "image create" subprocess
Traceback (most recent call last):
  File "/opt/opengnsys/ogClient/src/live/ogOperations.py", line 454, in image_create
    ogReduceFs(disk, partition)
  File "/opt/opengnsys/ogClient/src/utils/fs.py", line 105, in ogReduceFs
    _reduce_ntfsresize(partdev)
  File "/opt/opengnsys/ogClient/src/utils/fs.py", line 235, in _reduce_ntfsresize
    extra_size = int(out_resize_dryrun.split('Needed relocations : ')[1].split(' ')[0])*1.1+1024
IndexError: list index out of range

if not present, no need to adjust size
2024-01-11 12:55:37 +01:00
Jose M. Guisado 926a73cf33 fs: fix subprocess input inside _extend_resize2fs
The subprocess module expects bytes-like object for "input" parameter by
default. Passing a string object result in the following error:

(2023-06-13 14:44:43) ogClient: [ERROR] - Exception when running "image create" subprocess
(2023-06-13 14:44:43) ogClient: [ERROR] - Unexpected error
Traceback (most recent call last):
  File "/opt/opengnsys/ogClient/src/live/ogOperations.py", line 465, in image_create
    ogExtendFs(disk, partition)
  File "/opt/opengnsys/ogClient/src/utils/fs.py", line 124, in ogExtendFs
    _extend_ntfsresize(partdev)
  File "/opt/opengnsys/ogClient/src/utils/fs.py", line 250, in _extend_ntfsresize
    proc = subprocess.run(cmd, input='y')
  File "/usr/lib/python3.8/subprocess.py", line 495, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/usr/lib/python3.8/subprocess.py", line 1013, in communicate
    self._stdin_write(input)
  File "/usr/lib/python3.8/subprocess.py", line 962, in _stdin_write
    self.stdin.write(input)
TypeError: a bytes-like object is required, not 'str'

Fixes: dd999bfe34 ("utils: rewrite ogReduceFs")
2023-06-13 17:34:20 +02:00
Jose M. Guisado 88668cb195 fs: fix bug when ntfsresize reports nothing to do
There is a corner case in which a target NTFS filesystem is already
shrunken. When this happens ntfsresize text output parsing breaks.

Check when ntfsresize reports nothing to do, warn the user about this
and stop the dry-run ntfsresize loop.
2023-06-13 17:34:04 +02:00
Jose M. Guisado 666d2fb50f fs: fix typo inside _extend_resize2fs
_extend_ntfsresize contains an incorrect variable name inside
subprocess.run referring the resize command value.

Simplify this variable name inside each specific _extend_* function:

	s/cmd_resize2fs/cmd
	s/cmd_ntfsresize/cmd
2023-06-13 09:06:13 +02:00
Jose M. Guisado cf6f50e528 utils: add umount_all function
Add utility function to unmount any mountpoint present in the /mnt
folder.

This function is a simplified version of the legacy bash function
ogUnmountAll used in several operations.
2023-05-09 13:09:07 +02:00
Jose M. Guisado 803ba74510 utils: rewrite ogExtendFs
Drop subprocess call to bash function ogExtendFs. Use a native python
solution with subprocess calls to the required underlying tools.

Use get_filesystem_type to get the present filesystem from a partition
and call the corresponding filesystem grow function.

Filesystem specific functions are declared "_extend_{filesystem}" and
should not be imported elsewhere.

Each filesystem specific function wraps a subprocess call to the
required underlying program:

- NTFS filesystems: "ntfsresize -f [partition]"
- ext4 filesystems: "resize2fs -f [partition]"

Set NTFS related subprocess stdin to 'y' because human input cannot be
unset with other ntfsresize parameters.
2023-05-09 13:09:02 +02:00
Jose M. Guisado dd999bfe34 utils: rewrite ogReduceFs
Drop subprocess call to bash function ogReduceFs. Use a native python
solution with subprocess calls to the required underlying tools.

Use get_filesystem_type to get the filesystem from a partition and call
the corresponding supported filesystem shrink function.

Filesystem specific functions are declared "_reduce_{filesystem}" and
should not be imported elsewhere.

In case of NTFS filesystems, the output of 'ntfsresize' is processed
directly. This is dirty, but we can expect no changes to the output
strings if we read the following comment in the nftsresize.c source
code:
https://github.com/tuxera/ntfs-3g/blob/edge/ntfsprogs/ntfsresize.c#L12

ntfsresize requires to do previous dry-run executions to confirm
that the resizing is possible.
If a dry-run fails but a 10% increase in size is still smaller than
original filesystem then retry the operation until dry-run reports
sucess or the size increase is bigger than original.

If resizing to a smaller ntfs filesystem is not possible then ogReduceFs
will do nothing.
2023-05-02 17:31:08 +02:00
Jose M. Guisado 22dce48d3e utils: add get_filesystem_type function
Retrieve filesystem type from a partition using get_filesystem_type.
Encapsulates a subprocess call to blkid.
2023-04-25 13:33:45 +02:00
Jose M. Guisado eac942660f utils: add fs.py
Adds utility module which wraps several mkfs.* calls as a subprocess.

The main utility function is mkfs(fs, disk, partition, label), which
subsequently calls the corresponding mkfs_*(partition_device) function.

mkfs() supports specifying a drive label where supported.

Other modules using fs.py should call mkfs() only.
2023-02-08 17:11:44 +01:00
Jose M. Guisado 91077da273 utils: minor fix for ogReduceFs and ogExtendFs
Do not return the subprocess result for ogReduceFs/ogExtendFs.

ogReduceFs works with or without the target filesystem mounted.
ogExtendFs requires the target filesystem to be mounted.

'ogMount' legacy script invocation should be replaced by a better
mount/umount wrapper.
2022-09-14 17:12:14 +02:00
Jose M. Guisado 3550da73e6 image_create: partial integration into python
Integrates some parts of this operation into native code, eg: the md5
checksum computation.

Wraps non native processes and commands using the subprocess module.
For example, legacy.py stores bash commands pending integration.

Supports python >=3.6, expected until more modern ogLives are put into
production environments.
2022-08-24 09:48:35 +02:00
Jose M. Guisado 621d1b9147 utils: mount_mkdir success if target is a mountpoint
Returns true if target is already a mountpoint. Does not call mount.

It's possible that another device might be mounted in the target
mountpoint. A future check between the source and target for
equal device major:minor must be added.
2022-06-01 11:48:30 +02:00
Jose M. Guisado 4e5b17ce6d probe: detect 64 bit operating systems
OpenGnsys partition images store OS information, including
the OS name appended with "64 bits" when the OS is meant for 64 bit
machines.

The detected OS name when probing (refresh) is important, if it differs
from what's stored in the DB OpenGnsys will wipe last image restored
information when running a refresh.

See actualizaConfiguracion from legacy ogserver (ogAdmServer.c) code:

	dato = dbi_result_get_uint(result, "idnombreso");
	if (idsoi == dato) {
		swu = false;
	}
	...
	if (swu) {
		result_update = dbi_conn_queryf(dbi->conn,
		"UPDATE ordenadores_particiones SET "
			" codpar=0x%s,"
			" tamano=%s,"
			" uso=%s,"
			" idsistemafichero=%d,"
			" idnombreso=%d,"
			" idimagen=0,"
			" idperfilsoft=0,"
			" fechadespliegue=NULL"
			" WHERE idordenador=%d AND numdisk=%s AND numpar=%s",
2022-05-09 12:57:36 +02:00
Jose M. Guisado 902e019505 Add utils modules
* disk.py

	Disk discovery

* fs.py

	Uses psutil to fetch fs usage information

* menu.py

	ogBrowser menu generation

* net.py: gets nic status information

	IP address, MAC address and ethernet speed.

* probe.py: probes mountpoints for operating systems

	Uses hivexget command to try fetching Windows installation
	information.
	Looks for /etc/os-release for probing linux systems.
2022-04-21 09:47:04 +02:00