{"id":8410,"date":"2024-11-08T01:00:00","date_gmt":"2024-11-08T06:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=8410"},"modified":"2024-11-08T08:39:19","modified_gmt":"2024-11-08T13:39:19","slug":"using-fdisk-in-a-script-to-partition-a-drive","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=8410","title":{"rendered":"How I used fdisk in a script to partition a drive"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"8410\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p class=\"has-small-font-size\">Image by Gerd Altmann on Pixabay.<\/p>\n\n\n\n<p>As you probably know by now, I believe in <a href=\"https:\/\/www.both.org\/?p=7228\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=7228\" target=\"_blank\" rel=\"noreferrer noopener\">automating everything<\/a>. So when one of my scripts failed to properly partition and create an EXT4 filesystem on a new 4GB external USB hard drive, I was surprised &#8212; but I shouldn&#8217;t have been. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The problem<\/h2>\n\n\n\n<p>As usual, this problem was one of my own making. I use these external USB hard drives for my backups. The script performs some preparation steps on the drives. It deletes existing partitions, creates a new one that fills the entire disk, and installs an EXT4 filesystem on the partition. Except that I had previously only used the script on drives of 2TB or less. I prepared a couple 4TB drives before writing my script so I didn&#8217;t catch the problem until now, when I tried to prepare a new 4TB drive. <\/p>\n\n\n\n<p>I&#8217;d used sfdisk in the script because it is allegedly designed to be used in scripts. But I hadn&#8217;t specified use of <a href=\"https:\/\/www.both.org\/?p=4597\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=4597\" target=\"_blank\" rel=\"noreferrer noopener\">GPT<\/a> in the command, and that&#8217;s required for drives over 2TB. I tried using sfdisk as a regular command with options and arguments, but that failed. The sfdisk command can also use a bit of magic with a &#8220;<a href=\"https:\/\/tldp.org\/LDP\/abs\/html\/here-docs.html\" data-type=\"link\" data-id=\"https:\/\/tldp.org\/LDP\/abs\/html\/here-docs.html\" target=\"_blank\" rel=\"noreferrer noopener\">here document<\/a>&#8221; to hold the commands that need to be used in the command. But that also failed to work. <\/p>\n\n\n\n<p>It did work &#8212; sort of &#8212; by creating a 2TB partition. Therefore, I&#8217;m not so sure sfdisk really designed to be used in scripts &#8212; at least not easily.<\/p>\n\n\n\n<p>The bottom line here is that I was never able to determine the correct wizardly incantation to create the proper partition. No amount of Internet searching provided a working answer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The solution<\/h2>\n\n\n\n<p>My searches did result in a potential solution using the ancient and venerated <strong>fdisk<\/strong> command. It also requires a here document, but it&#8217;s much easier to create and understand. <\/p>\n\n\n\n<p>The fdisk command requires a here document to contain the required commands. This was easy because I frequently use those commands frequently to prepare many types of storage devices. I use the wipefs command to wipe all existing partitions from the storage device. I could do this with fdisk in the here document, but I never know how many partitions already exist. <\/p>\n\n\n\n<p>Here&#8217;s an annotated version of my code. The first EOF specifies the text code to delimit the end of the file. The second instance <em>is<\/em> the end of the file. Any text will do, but EOF seems to be the most common delimiter. Note the use of &lt;&lt; to redirect input from STDIO to the command. $Device contains the name of the <a href=\"https:\/\/www.both.org\/?p=5446\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=5446\" target=\"_blank\" rel=\"noreferrer noopener\">device special file<\/a> for the device you&#8217;re preparing. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   # Deleting all partitions\n   wipefs -a \/dev\/$Device\n\n   sync ; sync # Always sync storage devices twice to ensure that everything is truly written\n               # to the device.\n\n   # Create new Linux GPT partition\n   echo \"Creating new partition on $Device\"\n   fdisk \/dev\/$Device &lt;&lt; EOF\ng  # Create a GPT signature and partition table.\np  # Print the current state.\nn  # Create a new partition.\n   # The default partition is 1 so just hit Enter. Blank lines are \"Enter.\"\n   # Enter to use the default first sector number.\n   # Enter to use the default last sector number. This should fill the entire storage device.\np  # Print the new partition information\nw  # Write the new partitioning scheme to the device.\nEOF\n\n   sync ; sync<\/code><\/pre>\n\n\n\n<p>Be sure to use the -a option in the <strong>wipefs<\/strong> command. Also, the trailing EOF delimiter must be on the left margin and start in the first column. I got errors until I figured that out. <\/p>\n\n\n\n<p>To use this here document, remove all comments between the two EOF delimiters. It should look like this on the command line and in your script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fdisk \/dev\/$Device &lt;&lt; EOF\ng\np\nn\n\n\n\np\nw\nEOF<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>This works well for me and does exactly what I need it to. It&#8217;s also a good illustration of using redirection to provide input to a command that&#8217;s designed to be interactive like <strong>fdisk<\/strong>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Image by Gerd Altmann on Pixabay. As you probably know by now, I believe in automating everything. So<\/p>\n","protected":false},"author":2,"featured_media":5090,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[475,149,5],"tags":[637,638,639,640],"class_list":["post-8410","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation","category-bash","category-linux","tag-bash-script","tag-fdisk","tag-here-document","tag-stdio"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8410"}],"version-history":[{"count":17,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8410\/revisions"}],"predecessor-version":[{"id":8488,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8410\/revisions\/8488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/5090"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}