{"id":2836,"date":"2022-08-31T05:50:54","date_gmt":"2022-08-31T05:50:54","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2836"},"modified":"2023-08-04T16:18:08","modified_gmt":"2023-08-04T16:18:08","slug":"python-expand-filesystem","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/python-expand-filesystem","title":{"rendered":"Python Script to expand filesystem in an EC2 instance"},"content":{"rendered":"<p>In this blog, we will write a script to\u00a0<strong>expand the AWS Ec2 instance filesystem after modifying the EBS volume size<\/strong>. In this script, we will use the<strong>\u00a0Boto3 and Paramiko modules<\/strong>. Boto3 is an <a href=\"https:\/\/aws.amazon.com\/modern-apps\/services\/\">AWS Software Development<\/a> Kit for Python using which we use various AWS services like EC2 and S3. Paramiko module helps us to establish Python SSH connections with servers.<\/p>\n<p>We can divide the below script into the following steps:<\/p>\n<ol>\n<li>Listing the Instance and volume IDs present in a region.<\/li>\n<li>Modifying the volume size.<\/li>\n<li>Connecting to the EC2 instance.<\/li>\n<li>List the partitions and filesystems.<\/li>\n<li>Expand the partition and filesystem.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre><code>\r\nimport boto3\r\nimport time\r\nimport paramiko\r\nimport colorama\r\nfrom colorama import Fore\r\ntry:  \r\n  region= input(Fore.BLUE+\"Enter region name:\"+Fore.RESET)\r\n  ec2_client = boto3.client('ec2', region_name=region)\r\n  response = ec2_client.describe_instances()\r\n\r\n  print(Fore.BLUE+f\"\\nList of instances and volumes in {region} region:\"+Fore.RESET)  \r\n  for reservation in response['Reservations']:\r\n        for instance in reservation['Instances']:\r\n                    volumes = ec2_client.describe_volumes(\r\n                                        Filters=[{'Name':'attachment.instance-id','Values':[instance['InstanceId']]}]\r\n                                                )\r\n                    for disk in volumes['Volumes']:\r\n                               \r\n                                          print(instance['InstanceId'], disk['VolumeId'], disk['VolumeType'], disk['Size'])\r\n\r\n  VOLUME_ID = input(Fore.BLUE+\"\\nEnter volume id:\"+Fore.RESET) \r\n  Modified_volume_size = int(input(Fore.BLUE+\"\\nEnter new ebs volume size in GB:\"+Fore.RESET))\r\n  def get_modification_state(volume_id):\r\n        response = ec2_client.describe_volumes_modifications(\r\n                        VolumeIds=[\r\n                                        VOLUME_ID\r\n                                                ]\r\n                        )\r\n        return response['VolumesModifications'][0]['ModificationState']\r\n\r\n  modify_volume_response = ec2_client.modify_volume(\r\n                    VolumeId=VOLUME_ID,\r\n                    Size=Modified_volume_size\r\n                        )\r\n\r\n  while True:\r\n                state = get_modification_state(VOLUME_ID)\r\n                if state == 'completed' or state == None:\r\n                             break\r\n                elif state == 'failed':\r\n                            raise Exception('Failed to modify volume size')\r\n                else:\r\n                   time.sleep(60)\r\n\r\n  print(Fore.BLUE+f'\\nVolume {VOLUME_ID} successfully resized'+Fore.RESET)\r\n  print(\"\\nFor expanding filesystem, we need to SSH to EC2 server\")\r\n  instanceip= input(Fore.BLUE+\"\\nEnter instance IP:\"+Fore.RESET)\r\n  instance_ip=instanceip\r\n  key = paramiko.RSAKey.from_private_key_file(\"\/path\/to\/keypairfile\")\r\n  client = paramiko.SSHClient()\r\n  client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\r\n  client.connect(hostname= instance_ip, username='ubuntu', pkey=key)\r\n\r\n  print(Fore.RED+\"\\nList of partition:\\n\"+Fore.RESET)\r\n  cmd = \"lsblk\"\r\n  stdin, stdout, stderr = client.exec_command(cmd)\r\n  stdout = stdout.readlines()\r\n  for line in stdout:\r\n        print(line)\r\n\r\n  print(Fore.RED+\"List of filesystem in instance:\\n\"+Fore.RESET)\r\n  cmd = \"df -T\"\r\n  stdin, stdout, stderr = client.exec_command(cmd)\r\n  stdout = stdout.readlines()\r\n  for line in stdout:\r\n    print(line)\r\n                                 \r\n  while True:  \r\n   partition= input(Fore.RED+\"\\nEnter partition to expand:\\n\"+Fore.RESET)\r\n   cmd = f\"sudo growpart {partition}\"\r\n   stdin, stdout, stderr = client.exec_command(cmd)\r\n   \r\n   exitcodeone=stdout.channel.recv_exit_status()\r\n   print(exitcodeone)\r\n   if exitcodeone==0:\r\n    stdout = stdout.readlines()\r\n    for line in stdout:\r\n      print(line)\r\n    break\r\n   elif exitcodeone==1:\r\n       print(\"Partition can not be extended\")\r\n   else:    \r\n       print(\"Invalid partition name\")\r\n    \r\n  while True:\r\n   fs=input(Fore.RED+\"\\nEnter filesystem type:\"+Fore.RESET)\r\n   if fs==\"ext4\":\r\n       while True:  \r\n         filesys= input(Fore.RED+\"\\nEnter filesystem to expand:\"+Fore.RESET)\r\n         cmd = f\"sudo resize2fs {filesys} \"\r\n         stdin, stdout, stderr = client.exec_command(cmd)\r\n         exitcodetwo=stdout.channel.recv_exit_status()\r\n         if exitcodetwo==0:\r\n           stdout = stdout.readlines()\r\n           for line in stdout:\r\n             print(line)\r\n           break \r\n         else:\r\n             print(\"Invalid fliesystem\")\r\n       break\r\n   elif fs==\"xfs\":\r\n       while True:  \r\n          filesys= input(Fore.RED+\"\\nEnter filesystem to expand:\"+Fore.RESET)\r\n          cmd = f\"sudo xfs_growfs {filesys}\"\r\n          stdin, stdout, stderr = client.exec_command(cmd)\r\n          exitcodethree=stdout.channel.recv_exit_status()\r\n          if exitcodethree==0:\r\n             stdout = stdout.readlines()\r\n             for line in stdout:\r\n                 print(line)\r\n             break\r\n          else:\r\n             print(\"Invalid filesystem\")\r\n       break\r\n   else:\r\n     print(Fore.RED+\"Invalid filesystem type\"+Fore.RESET)\r\n   \r\n  client.close()\r\nexcept Exception as e:\r\n    print (e)\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2793\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-aws-services-1-300x164.png\" alt=\"Python\" width=\"745\" height=\"407\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-aws-services-1-300x164.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-aws-services-1-1024x560.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-aws-services-1-768x420.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-aws-services-1.png 1066w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2794\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Kubernetes-Services-2-300x255.png\" alt=\"Python\" width=\"749\" height=\"637\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Kubernetes-Services-2-300x255.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Kubernetes-Services-2-768x652.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Kubernetes-Services-2.png 856w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2795\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-frontend-development-company-300x225.png\" alt=\"Python\" width=\"747\" height=\"560\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-frontend-development-company-300x225.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-frontend-development-company-1024x769.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-frontend-development-company-768x577.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-frontend-development-company-1200x901.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-frontend-development-company.png 1236w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2796\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-full-stack-development-company-300x84.png\" alt=\"Python\" width=\"746\" height=\"209\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-full-stack-development-company-300x84.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-full-stack-development-company-1024x287.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-full-stack-development-company-768x215.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-full-stack-development-company-1200x336.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-full-stack-development-company.png 1408w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>For more information about product engineering, MVP development, Cloud management, Data Engineering. Please <a href=\"https:\/\/www.checkmateq.com\/contact-us\">contact<\/a> with our technical team.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will write a script to\u00a0expand the AWS Ec2 instance filesystem after modifying the EBS volume size. In this script, we will use the\u00a0Boto3 and Paramiko modules. Boto3 is an AWS Software Development Kit for Python using which we use various AWS services like EC2 and S3. Paramiko module helps us to &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/python-expand-filesystem\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python Script to expand filesystem in an EC2 instance&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":2271,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,71,70,7,11,14,6],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2836"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2836"}],"version-history":[{"count":15,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2836\/revisions"}],"predecessor-version":[{"id":4251,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2836\/revisions\/4251"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2271"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}