- How to move a partition in GNU/Linux?
- 3 Answers 3
- Moving A Partition
- MovingLinuxPartition
- Step 1: Booting through Rescue CD
- Step 2: Create new partition
- Step 3: Clone Ubuntu partition to new location
- Step 4: Generate and update UUID
- Step 5: Update grub and fstab
- Step 6: Update MBR to point the new grub
- See Also
- External Links
How to move a partition in GNU/Linux?
How to move a partition to the beginning of the disk a bit? Parted wants a filesystem for some reason (I don’t know why), I want just to shift all sectors left.
r@l:15:32:45:~# parted /dev/sdb GNU Parted 2.3 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Model: HGST HTS 541010A9E680 (scsi) Disk /dev/sdb: 1000GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 32.2GB 32.2GB primary fat32 2 32.2GB 37.6GB 5360MB primary 3 37.6GB 1000GB 963GB primary (parted) move 3 WARNING: you are attempting to use parted to operate on (move) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Error: Could not detect file system.
Are you using the partitions without FS? You can just set the FS to anyone with fdisk and then move them.
Thus is actually a LUKS container. But I don’t think a partition mover should ever look inside the filesystem.
3 Answers 3
sfdisk , which intents to be a scriptable fdisk, has since some version the —move-data option. Example from their man page:
echo '+100M,' | sfdisk --move-data /dev/sdc -N 1
Manual way with dd and fdisk:
# fdisk -l /dev/sdb | grep sdb3 /dev/sdb3 73402368 1953525167 940061400 83 Linux # fdisk /dev/sdb Command (m for help): d Partition number (1-4): 3 Command (m for help): n Partition number (1-4, default 3): 3 First sector (73385984-1953525167, default 73385984): Using default value 73385984 Last sector, +sectors or +size (73385984-1953525167, default 1953525167): Using default value 1953525167 Command (m for help): w The partition table has been altered! # fdisk -l /dev/sdb | grep sdb3 /dev/sdb3 73385984 1953525167 940069592 83 Linux # dd conv=notrunc bs=512 iflag=fullblock if=/dev/sdb3 count=100 skip=$((73402368-73385984)) seek=0 2> /dev/null | file -s - /dev/stdin: LUKS encrypted file, ver 1 [aes, cbc-essiv:sha256, sha1] UUID: af1c47f0-4ca5-4ea7-a091-065bd263653f # dd conv=notrunc bs=512 iflag=fullblock if=/dev/sdb3 skip=$((73402368-73385984)) seek=0 of=/dev/sdb3 # file -s /dev/sdb3 /dev/sdb3: sticky LUKS encrypted file, ver 1 [aes, cbc-essiv:sha256, sha1] UUID: af1c47f0-4ca5-4ea7-a091-065bd263653f
Now waiting for about 2h. (more looks more like 18h. )
Note: this only moves data back, not forward.
# pidof dd 907 # kill -STOP 907 # cat /proc/907/fdinfo/1 pos: 586921398272 flags: 0100001 # kill -9 907 remember 586921398272/512 = 1146330856
dd conv=notrunc bs=512 iflag=fullblock if=/dev/sdb3 skip=$((1146330856+73402368-73385984)) seek=1146330856 of=/dev/sdb3
Moving A Partition
There are times when you wish to increase the size of a partition, and there is free space on the disk, but it is not next to the partition you need to expand. In this case, you will need to move one or more partitions to «move» that free space next to the target partition.
For this demonstration I have the hard disk set up with a typical Windows/Ubuntu installation. We have a root (/) partition, a larger home (/home) partition, and then a swap partition, with free space at the end of the disk. We have run out of room in the home partition and need to give it more room, but we have no free space next to it in which to expand. We must move the swap partition.
- » width=»16″ height=»16″/> Note that moving a partition is particularly dangerous and is more liable to data loss or corruption. When a partition is moved, the files on that partition must be moved with it. As was said at the start, ALWAYS back up your data before performing a partitioning operation. It is a good idea to back up the entire drive to a remote location. Modern partition editors are pretty safe, but there is always the chance that the operations will be interrupted before finished; like say, the fuse in the computer’s power supply blows. If something like this were to happen, you will potentially lose all the partitions and files on the entire hard drive and have to start over. » width=»16″ height=»16″/> Beware! I have had the above happen to me! I have hosed and had to reformat an entire hard drive and reconstruct it! The vast majority of the time, partitioning is easy and safe, but if you don’t back up your hard drive before performing these operations you’re setting yourself up to get bit!
After assuring that the swap partition is unmounted, first select the partition by right-clicking on it and selecting «Resize/Move» from the menu:
In the above screenshot, the representation of the partition starts off at the left side of the graphical bar and the «Free Space Preceding» is at zero. To move the partition, left click and hold on the partition itself (rather than the arrows at the side, as with resizing) then drag the partition to the desired location. Notice the progress:
Notice that the «Free Space Preceeding/Following» values are now both non-zero.
Alternatively, you can vary either the «Free Space Preceeding» or the «Free Space Following» values, as long as neither value is or reaches zero. Adjust either one and the other will automatically self-adjust to reflect the movement. If you adjust both the «New Size» and one of the «Free Space» values, the partition will be resized as well as moved. This is particularly useful if you need to resize and move the partition, since this can be done in one operation.
When we have completed moving the Swap partition to the end of the drive, the «Free Space Following» value is now zero and the «Free Space Preceeding» value is the same as «Free Space Following» was at the beginning of the operation. Then we can expand the /home partition into the free space beside it:
Once the partitions are moved and/or resized to your satisfaction, click the «Apply» button at the top of the window to apply the changes.
HowtoPartition/MovingPartition (последним исправлял пользователь cpe-172-116-79-89 2018-01-15 10:45:38)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
MovingLinuxPartition
The purpose of this how-to is to describe how to move your Ubuntu Linux partition to a different partition either in same hard-disk or different hard-disk.
Hence, the main purpose of this article to fill in the gap as well as one stop reference for the entire steps.
- Create new target partition
- Clone current Linux partition to target partition
- Generate and update UUID for target partition
- Update grub and fstab
- Update MBR to point the new grub.
- Your Ubuntu is 9.10 or later
- Your Ubuntu is on ext4 partition (gparted should be to copy/paste any other file-system as well, however these steps were tested on ext4)
- Your Ubuntu configuration is of standard (not too customized)
- Grub2 is your boot loader
- SystemRescueCd — I prefer this rescue CD as it comes with gparted utility
- You are not changing swap partition
If any of above assumptions do not apply to your system, then you will need to execute following steps with cautions.
Step 1: Booting through Rescue CD
- A copy of SystemRescueCd can be obtain from here.
- Burn this to a CD
- And reboot the machine to boot from the rescue CD
- You may need to change configuration in your bios to enable boot up from CD
Step 2: Create new partition
- Launch gparted from start menu
- Using gparted create a new partition on your target hard-disk.
- Make sure the size new partition is same or bigger than your current Ubuntu partition used space.
- You may also mount current Ubuntu partition to clean up unnecessary files in the partition to reduce the size.
shell> mkdir ~/ubuntu shell> mount /dev/sdZY ~/ubuntu or shell> mount /dev/hdZY ~/ubuntu # where ZY is your ubuntu partition's block device name (you can find this from gparted window) shell> cd ~/ubuntu
Step 3: Clone Ubuntu partition to new location
- On gparted window, right-click on Ubuntu partition and select «Copy»
- Then right-click on the new partition you have just created and select «Paste»
- gparted will start step-by-step copy process. This may take a while.
- You may also opt to use dd for the same purpose, but I prefer gparted because
- It does calculate optimal way to copy over the data (block size)
- It able to verbose current progress status
Step 4: Generate and update UUID
- Since step 3 copy and paste produce the new partition with exact same UUID, this will conflict and grub not able to tell the partitions apart. Hence we need to update UUID information on new partition.
- Following is to check current UUID (You should notice duplicate UUID)
shell> tune2fs -U random /dev/sdZY or shell> tune2fs -U random /dev/hdZY # where ZY is your new partition's block device name (you can obtain this from gparted window)
Step 5: Update grub and fstab
- Reboot your system from Rescue system back to normal Ubuntu
- Once you have booted to your original Ubuntu, mount the partition via gnome -> Places option
- Edit grub.cfg file from new partition.
shell> gksu gedit /media//boot/grub/grub.cfg &
- Note that you should edit grub.cfg from new partition and not from «/boot/grub/grub.cfg»
- Using gedit, find and replace every «(hdX,Y)» appropriately
- X and Y should be replaced based on your new partition’s /dev/sdZY or /dev/hdZY, if Z is ‘a’ then X should be ‘0’, if Z is ‘b’ then X should be ‘1’ and so on.
shell> gksu gedit /media//etc/fstab &
- Note that you should edit grub.cfg from new partition and not from «/etc/fstab»
- Using gedit, find and replace root mount point’s (line with «/») based on new generated UUID.
Step 6: Update MBR to point the new grub
- By now, you have 2 proper bootable Ubuntu in your system, and the idea now is to make the new Ubuntu partition self bootable without any dependencies to old Ubuntu partition. And to get ride the old Ubuntu partition eventually.
- Before updating MBR, backup current MBR data
shell> sudo dd if=/dev/sdZ of=~/sdZ.img bs=1 count=512 or shell> sudo dd if=/dev/hdZ of=~/sdZ.img bs=1 count=512 # where Z is your old Ubuntu partition's block device name # note that device name is without 'Y'; without partition id.
- Mount the partition via gnome -> Places option (if is not mounted)
- Run grub setup for new partition
shell> sudo grub-install -d /media//usr/lib/grub/[instance] /dev/sda
gnome> Run the "Disks" application
- Find and select your old Ubuntu partition to highlight the partition
- Click the «Settings» gear button and select «Edit Mount Options»
- Uncheck «Mount at Startup» and click «OK»
- Find and select your new Ubuntu partition to highlight the partition
- Click the «Settings» gear button and select «Edit Mount Options»
- Set «Automatic Mount Options» to OFF
- Check «Mount at startup»
- «Show in user interface» and «Require additional authorization to mount» should NOT be checked
- Mount options should be «errors=remount-ro»
- Mount point should be «/»
- Click «OK»
- Reboot you machine again
- On booting, make sure new partition’s grub menu is displayed as.
- Make sure Ubuntu from new partition is perfectly fine before deleting the old Ubuntu partition.
- In case to revert back to old Ubuntu partition, all you need to do is restore the backed-up MBR image.
shell> sudo dd if=~/sdZ.img of=/dev/sdZ bs=1 count=512 or shell> sudo dd if=~/sdZ.img of=/dev/hdZ bs=1 count=512 # where Z is your old Ubuntu partition's block device name # note that device name is without 'Y'; without partition id.
See Also
External Links
MovingLinuxPartition (последним исправлял пользователь rs2009 2020-03-23 10:04:38)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details