{"id":2562,"date":"2020-10-17T23:24:57","date_gmt":"2020-10-17T23:24:57","guid":{"rendered":"http:\/\/hauweele.net\/~gawen\/blog\/?p=2562"},"modified":"2020-10-17T23:24:57","modified_gmt":"2020-10-17T23:24:57","slug":"replace-usb-boot-key-on-freenas","status":"publish","type":"post","link":"https:\/\/hauweele.net\/~gawen\/blog\/?p=2562","title":{"rendered":"Replace USB boot key on FreeNAS"},"content":{"rendered":"<p>You don&#8217;t really install FreeNAS as you do with some other OS. Instead you have a device (often an USB key) that boots FreeNAS which itself manages your data storage. Indeed why wasting some precious SATA port for a boot device when all you need is a little USB key? Especially when thits boot device is less than 2GB in size.<\/p>\n<p>The problem with USB keys is that they wear out over time. So it inevitably comes a time when it must be replaced. This is what happened to me a few days ago.<\/p>\n<p>The <i>freenas-boot<\/i> pool switched to a DEGRADED state because checksum error happened too frequently. Fortunately a <i>zpool scrub freenas-boot<\/i> detected no error, and it seems that zfs was still able to correct those. So instead of reflashing a new USB key and restoring my config backup, I could from FreeNAS itself create a new bootable USB key and replace replace the faulty device in the freenas-boot pool.<\/p>\n<p>Let&#8217;s login on FreeNAS and plug in your new USB key. We first find and verify what is the device name for the USB key, in my case it&#8217;s <i>da2<\/i>:<\/p>\n<pre>\r\n$ gpart part show da2\r\n=>       1  60088319  da2  MBR  (29G)\r\n         1        31       - free -  (16K)\r\n        32  60088288    1  fat32lba  (29G)\r\n<\/pre>\n<p>You see that MBR table and a FAT32 partition, in my case it can only be the new USB key. We will replace that with GPT partitions. This will contain a boot partition (MBR boot or EFI boot), and a ZFS partition. As root:<\/p>\n<pre>\r\n# Clean that MBR and create GPT partition table\r\ngpart destroy -F da2\r\ngpart create -s GPT da2\r\n\r\n# If you use MBR boot\r\ngpart add -s 64K -t freebsd-boot da2\r\ngpart bootcode -b \/boot\/pmbr -p \/boot\/gptzfsboot -i 1 da2\r\n\r\n# If you use EFI boot\r\ngpart add -s 128M -t efi da2\r\ngpart bootcode -p \/boot\/boot1.efifat -i 1 da2\r\n\r\n# Create the ZFS partition\r\ngpart add -t freebsd-zfs da2\r\n<\/pre>\n<p>Now it&#8217;s time to replace the device:<\/p>\n<pre>\r\n$ zpool replace -f freenas-boot da1p2 da2p2\r\ncannot replace da1p2 with da2p2: device is too small\r\n# OOPS! :(\r\n<\/pre>\n<p>If you got the message <i>device is too small<\/i>, that is because you&#8217;ve been trying to replace a device with another one that is smaller than the original (in term of ZFS partition size I suppose). ZFS requires that you replace a device with another with a size greater or equal to the old one. This can be a problem especially if you have a ZFS partition along with others in the partition table like here, or if the vendor announces a certain disk size but only within a certain approximation. You could result in a disk announced to be the exact same size ending up to be very slighty shorter than your original disk. And as a result you won&#8217;t be able to use it to replace any disk in your ZFS pool\/vdev. That&#8217;s why some recommend to create ZFS filesystems several percents less than the available disk size.<\/p>\n<p>But since we messed up, instead we will create a pool on the other USB key and use ZFS send\/recv to migrate the files.<\/p>\n<pre>\r\n# Create the pool on the new USB key.\r\n# Note that we have to call it freenas-boot2\r\n# because freenas-boot already exists.\r\nzpool create freenas-boot2 da2p2 \r\n\r\n# Create a recursive snapshot of the original pool\r\n# that we will use to restore the backup. \r\nzfs snapshot -r freenas-boot@restore\r\n\r\n# Send the snapshot from the old to the new USB key.\r\nzfs send -R freenas-boot@restore | zfs recv -F freenas-boot2\r\n<\/pre>\n<p>We also need to save the original setting for bootfs because FreeNAS has its own way of managing the boot pool. We will set this value later on the new pool.<\/p>\n<pre>\r\n$ zpool get bootfs\r\nNAME          PROPERTY  VALUE                        SOURCE\r\nfreenas-boot  bootfs    freenas-boot\/ROOT\/11.3-U4.1  local\r\n<\/pre>\n<p>We still have to change that name from <i>freenas-boot2<\/i> to <i>freenas-boot<\/i>. However we cannot do so on the running instance of FreeNAS because it already has a pool named <i>freenas-boot<\/i>. So, we will import it on another ZFS capable machine and fix its name. <\/p>\n<p>(but if anyone knows how to do this live, I&#8217;m interested)<\/p>\n<pre>\r\n# On some other ZFS capable host (FreeBSD for instance ;) )\r\n# import the pool and change its name back to freenas-boot.\r\n# The -f is required because it comes from another host.\r\nzpool import -f freenas-boot2 freenas-boot\r\n\r\n# Configure default boot filesystem.\r\n# Remember the value of bootfs we found before on the original pool.\r\nzpool set bootfs=freenas-boot\/ROOT\/11.3-U4.1 freenas-boot\r\n\r\n# Export the pool again.\r\nzpool export freenas-boot\r\n<\/pre>\n<p>Finally you can clear the old snapshot:<\/p>\n<pre>\r\nzfs destroy -R freebsd-boot@restore\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You don&#8217;t really install FreeNAS as you do with some other OS. Instead you have a device (often an USB key) that boots FreeNAS which itself manages your data storage. Indeed why wasting some precious SATA port for a boot &hellip; <a href=\"https:\/\/hauweele.net\/~gawen\/blog\/?p=2562\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[1096,389,881,243,1094,11,1095,768,1093],"class_list":["post-2562","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-boot","tag-freebsd","tag-freenas","tag-root","tag-snapshot","tag-usb","tag-usb-key","tag-zfs","tag-zpool"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2562"}],"version-history":[{"count":0,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2562\/revisions"}],"wp:attachment":[{"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hauweele.net\/~gawen\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}