-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recreating the swap file if the current size is sufficiently larger #31
Conversation
…han the needed size
@@ -41,6 +41,7 @@ HIB_ENABLED_FILE = "hibernation-enabled" | |||
IMDS_BASEURL = 'http://169.254.169.254' | |||
IMDS_API_TOKEN_PATH = 'latest/api/token' | |||
IMDS_SPOT_ACTION_PATH = 'latest/meta-data/hibernation/configured' | |||
MAX_SWAP_SIZE_OFFSET_ALLOWED = 100 * 1024 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 100K is max offset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The offset is kept just to make sure that on every fresh start we are not recreating the swap. It is a limit that I am proposing to avoid unnecessary deletion of swap. But let me know if you feel otherwise
@@ -467,7 +468,11 @@ def main(): | |||
cur_swap = os.path.getsize(SWAP_FILE) | |||
|
|||
bi = None | |||
if cur_swap >= target_swap_size - SWAP_RESERVED_SIZE: | |||
if cur_swap > target_swap_size - SWAP_RESERVED_SIZE + MAX_SWAP_SIZE_OFFSET_ALLOWED: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How was this scenario tested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How was this scenario tested?
- Created a c5.xlarge instance.
- Made the changes in the hibinit file ( inside /usr/bin)
- Stopped the instance
- Modified to small instance( c5.large)
journalctl -b | grep hibinit
to verify swap is recreated
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Effective config: {'grub_update': True, 'swap_percentage': 100, 'log_to_syslog': True, 'touch_swap': False, 'state_dir': '/var/lib/hibinit-agent', 'swapoff': 'swapoff {swapfile}', 'mkswap': 'mkswap {swapfile}', 'swapon': 'swapon {swapfile}', 'swap_mb': 4000}
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Found '/var/lib/hibinit-agent/hibernation-enabled', configuring hibernation
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Will check if swap is at least: 4000 megabytes
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Swap already exists! (have 8011616256, need 4194304000), deleting existing swap file /swap since current swap is sufficiently large and wasting memory
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Create swap and initialize it
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: kicking child process to initiate the setup
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Allocating 4194304000 bytes in /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Swap pre-heating is skipped, the swap blocks won't be touched during to ensure they are ready
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Running: mkswap /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: Setting up swapspace version 1, size = 3.9 GiB (4194299904 bytes)
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2457]: no label, UUID=f5142536-a989-42bc-95d0-cd971f491f57
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Running: swapon /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Updating the kernel offset for the swapfile: /swap
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Updating GRUB to use the device /dev/nvme0n1p1 with offset 265 for resume
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: GRUB configuration is updated
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Setting swap device to 66305 with offset 265
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Done updating the swap offset. Turning swapoff
Aug 04 18:19:03 ip-172-31-22-174.ec2.internal hibinit-agent[2462]: Running: swapoff /swap
- Verified it is successfully hibernated and resumed from hibernate
- Stop and start the instance again
journalctl -b | grep hibinit
to verified no recreation. of swap occured
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Effective config: {'grub_update': True, 'swap_percentage': 100, 'log_to_syslog': True, 'touch_swap': False, 'state_dir': '/var/lib/hibinit-agent', 'swapoff': 'swapoff {swapfile}', 'mkswap': 'mkswap {swapfile}', 'swapon': 'swapon {swapfile}', 'swap_mb': 4000}
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Found '/var/lib/hibinit-agent/hibernation-enabled', configuring hibernation
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Will check if swap is at least: 4000 megabytes
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: There's sufficient swap available (have 4194304000, need 4194304000)
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Running: swapon /swap
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Updating the kernel offset for the swapfile: /swap
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Updating GRUB to use the device /dev/nvme0n1p1 with offset 265 for resume
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: GRUB configuration is updated
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Setting swap device to 66305 with offset 265
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Done updating the swap offset. Turning swapoff
Aug 04 18:23:17 ip-172-31-22-174.ec2.internal hibinit-agent[2452]: Running: swapoff /swap
- stop the instance
- Modified back to c5.xlarge
- Start the instance
journalctl -b | grep hibinit
to verify swap is created due to larger swap required.
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Effective config: {'grub_update': True, 'swap_percentage': 100, 'log_to_syslog': True, 'touch_swap': False, 'state_dir': '/var/lib/hibinit-agent', 'swapoff': 'swapoff {swapfile}', 'mkswap': 'mkswap {swapfile}', 'swapon': 'swapon {swapfile}', 'swap_mb': 4000}
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Found '/var/lib/hibinit-agent/hibernation-enabled', configuring hibernation
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Will check if swap is at least: 7640 megabytes
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Swap already exists! (have 4194304000, need 8011620352), deleting existing swap file /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Create swap and initialize it
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: kicking child process to initiate the setup
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Allocating 8011620352 bytes in /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Swap pre-heating is skipped, the swap blocks won't be touched during to ensure they are ready
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Running: mkswap /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: Setting up swapspace version 1, size = 7.5 GiB (8011616256 bytes)
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2600]: no label, UUID=757d742b-b666-4917-87fa-0b7a5418a983
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Running: swapon /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Updating the kernel offset for the swapfile: /swap
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Updating GRUB to use the device /dev/nvme0n1p1 with offset 57612 for resume
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: GRUB configuration is updated
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Setting swap device to 66305 with offset 57612
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Done updating the swap offset. Turning swapoff
Aug 04 18:25:03 ip-172-31-22-174.ec2.internal hibinit-agent[2605]: Running: swapoff /swap
- Verify hibernation and resume from hibernation is successful post that.
Got 2 approvals. Tested the same in instances |
Description of changes:
This Pull request adds a logic of deleting and recreating the swap file, if the current swap is larger than the needed size.
This will help wasting extra memory in storage and make it available to the user when modifying instances from larger to smaller type.
Testing: Tested the change internally inside an instance by modifying the file.