{"id":1681,"date":"2019-04-26T13:33:22","date_gmt":"2019-04-26T05:33:22","guid":{"rendered":"https:\/\/www.xiaobo.li\/?p=1681"},"modified":"2019-08-06T18:34:50","modified_gmt":"2019-08-06T10:34:50","slug":"ec2-ecs-resize2fs","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/1681","title":{"rendered":"ec2\/ecs resize2fs"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you have used a snapshot to create the EBS or with certain AMIs \nyou will need to extend the disk (xvda), extend the partition (xvda1), \nthen extend the filesystem (\/).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If I'm reading this correctly, your disk looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/dev\/xvda\n|__\/dev\/xvda1__|______________ Free space______________|\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It needs to look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/dev\/xvda\n|______________________\/dev\/xvda1______________________|\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After that, running <code>resize2fs<\/code> will grow into that new space within <code>xvda1<\/code>, using <code>fdisk<\/code>\n we can increase the block size by deleting and creating it again and \nmaking the partition bootable. all it requires is a reboot. In most \ncases it shouldn't effect your data if you use same start cylinder but <strong>Note that any mistake in recreating the partion will result in losing all data and\/or the server will not reboot.<\/strong>\n I would recommend doing this as the first step on a newly created \ninstance. Otherwise take a snapshot of your EC2 EBS Storage\/etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have marked the steps with &lt;&lt;#&gt;&gt; in the block below, so  the are not a part of the command. You need root permissions, so do a  \"sudo sh\" if you are not root.<\/p>\n\n\n\n<pre>\n<<1>> Look at the filesystem, it is 6G\n<<2>> Look at the disk and the partition, the disk is 21.5 GB but the partition is 6 GB (6291456 blocks)\n<<3>> Start fdisk for that disk (xvda, so not the partition xvda1)\n<<4>> Switch to sector display.\n<<5>> Print the partition(s), and remember the start sector (2048 in the example).\n<<6>> Delete the partition.\n<<7>> Create a new partition.\n<<8>> Make it primary.\n<<9>> First partition.\n<<10>> Enter the old start sector, do NOT make any typo here!!! (2048 in the example) \n<<11>> Hit enter to accept the default (this is the remainder of the disk)\n<<12>> Print the changes and make sure the start sector is ok, if not restart at <<6>>\n<<13>> Make the partition bootable. do NOT forget this!!!\n<<14>> Enter your partition number (1 in the example)\n<<15>> Write the partition info back, this will end the fdisk session.\n<<16>> Reboot the server, and wait for it to come up (this may take longer than usual).\n<<17>> Verify the filesystem size.\n<<18>> If the filesystem is not around 20Gb as expected, you can use this command.\n\n\n# df -h  <<1>>\n\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/xvda1      6.0G  2.0G  3.7G  35% \/ \ntmpfs            15G     0   15G   0% \/dev\/shm\n\n# fdisk -l  <<2>>\n\nDisk \/dev\/xvda: 21.5 GB, 21474836480 bytes\n97 heads, 17 sectors\/track, 25435 cylinders\nUnits = cylinders of 1649 * 512 = 844288 bytes\nSector size (logical\/physical): 512 bytes \/ 512 bytes\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\nDisk identifier: 0x0003b587\n\n    Device Boot      Start         End      Blocks   Id  System\n\/dev\/xvda1   *           2        7632     6291456   83  Linux\n\n# fdisk \/dev\/xvda  <<3>>\n\nWARNING: DOS-compatible mode is deprecated. It's strongly recommended to\n         switch off the mode (command 'c') and change display units to\n         sectors (command 'u').\n\nCommand (m for help): u  <<4>>\nChanging display\/entry units to sectors\n\nCommand (m for help): p  <<5>>\n\nDisk \/dev\/xvda: 21.5 GB, 21474836480 bytes\n97 heads, 17 sectors\/track, 25435 cylinders, total 41943040 sectors\nUnits = sectors of 1 * 512 = 512 bytes\nSector size (logical\/physical): 512 bytes \/ 512 bytes\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\nDisk identifier: 0x0003b587\n\n    Device Boot      Start         End      Blocks   Id  System\n\/dev\/xvda1   *        2048    12584959     6291456   83  Linux\n\nCommand (m for help): d  <<6>>\nSelected partition 1\n\nCommand (m for help): n  <<7>>\nCommand action\n   e   extended\n   p   primary partition (1-4)\np  <<8>>\nPartition number (1-4): 1  <<9>>\nFirst sector (17-41943039, default 17): 2048  <<10>>\nLast sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): <<11>>\nUsing default value 41943039\n\nCommand (m for help): p <<12>>\n\nDisk \/dev\/xvda: 21.5 GB, 21474836480 bytes\n97 heads, 17 sectors\/track, 25435 cylinders, total 41943040 sectors\nUnits = sectors of 1 * 512 = 512 bytes\nSector size (logical\/physical): 512 bytes \/ 512 bytes\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\nDisk identifier: 0x0003b587\n\n    Device Boot      Start         End      Blocks   Id  System\n\/dev\/xvda1            2048    41943039    20970496   83  Linux\n\nCommand (m for help): a  <<13>>\nPartition number (1-4): 1  <<14>>\n\n\nCommand (m for help): w  <<15>>\nThe partition table has been altered!\n\nCalling ioctl() to re-read partition table.\n\nWARNING: Re-reading the partition table failed with error 16: ...\nThe kernel still uses the old table. The new table will be used at\nthe next reboot or after you run partprobe(8) or kpartx(8)\nSyncing disks.\n\n# reboot  <<16>>\n\n<wait>\n\n# df -h  <<17>>\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/xvda1       20G  2.0G   17G  11% \/ \ntmpfs            15G     0   15G   0% \/dev\/shm\n\n# resize2fs \/dev\/xvda1  <<18>>\nresize2fs 1.41.12 (17-May-2010)\nFilesystem at \/dev\/xvda1 is mounted on \/; on-line resizing required\nold desc_blocks = 1, new_desc_blocks = 2\nPerforming an on-line resize of \/dev\/xvda1 to 5242624 (4k) blocks.\nThe filesystem on \/dev\/xvda1 is now 5242624 blocks long.\n\nroot@vs120 [~]#  df -h\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/xvda1       20G  7.8G   11G  42% \/\ntmpfs           498M     0  498M   0% \/dev\/shm\n\/usr\/tmpDSK     399M   11M  368M   3% \/tmp\nroot@vs120 [~]#\n# xfs\nxfs_growfs \/dev\/xvda1\n<\/pre>\n\n\n\n\n\n<p class=\"wp-block-paragraph\">Use the <strong>lsblk<\/strong> command to list the block devices attached to your instance. The example below shows three volumes: <code>\/dev\/xvda<\/code>, <code>\/dev\/xvdb<\/code>, and <code>\/dev\/xvdf<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[ec2-user ~]$ lsblk\nNAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT\nxvda    202:0    0  30G  0 disk\n\u2514\u2500xvda1 202:1    0  30G  0 part \/\nxvdb    202:16   0  30G  0 disk \/mnt\nxvdf    202:80   0  35G  0 disk\n\u2514\u2500xvdf1 202:81   0   8G  0 part<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>links:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/serverfault.com\/questions\/414983\/ec2-drive-not-ebs-volume-size\">https:\/\/serverfault.com\/questions\/414983\/ec2-drive-not-ebs-volume-size<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.aws.amazon.com\/zh_cn\/AWSEC2\/latest\/UserGuide\/recognize-expanded-volume-linux.html\">https:\/\/docs.aws.amazon.com\/zh_cn\/AWSEC2\/latest\/UserGuide\/recognize-expanded-volume-linux.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have used a snapshot to create th &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/1681\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[287],"tags":[],"class_list":["post-1681","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=1681"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1681\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=1681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=1681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=1681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}